├── README.md
├── 08 Outrun
├── images
│ ├── 1.png
│ ├── 2.png
│ ├── 3.png
│ ├── 4.png
│ ├── 5.png
│ ├── 6.png
│ ├── 7.png
│ └── bg.png
└── main.cpp
├── 13 Tron
├── background.jpg
├── shader.frag
└── main.cpp
├── 14 Chess
├── stockfish.exe
├── images
│ ├── board.png
│ ├── board0.png
│ └── figures.png
├── Connector.hpp
└── main.cpp
├── 04 Snake
├── images
│ ├── green.png
│ ├── red.png
│ └── white.png
└── main.cpp
├── 09 Xonix
├── images
│ ├── enemy.png
│ ├── tiles.png
│ └── gameover.png
└── main.cpp
├── 01 Tetris
├── images
│ ├── frame.png
│ ├── tiles.png
│ └── background.png
├── .idea
│ ├── encodings.xml
│ ├── misc.xml
│ ├── modules.xml
│ ├── 01 Tetris.iml
│ └── workspace.xml
└── main.cpp
├── 03 Arkanoid
├── images
│ ├── ball.png
│ ├── block01.png
│ ├── block02.png
│ ├── block03.png
│ ├── block04.png
│ ├── block05.png
│ ├── paddle.png
│ └── background.jpg
└── main.cpp
├── 10 Bejeweled
├── images
│ ├── cursor.png
│ ├── gems.png
│ └── background.png
└── main.cpp
├── 15 Volleyball
├── images
│ ├── ball.png
│ ├── blobby.png
│ └── background.PNG
└── main.cpp
├── 16 Asteroids
├── images
│ ├── rock.png
│ ├── fire_red.png
│ ├── background.jpg
│ ├── fire_blue.png
│ ├── rock_small.png
│ ├── spaceship.png
│ └── explosions
│ │ ├── type_A.gif
│ │ ├── type_A.png
│ │ ├── type_B.gif
│ │ ├── type_B.png
│ │ ├── type_C.gif
│ │ └── type_C.png
└── main.cpp
├── 02 Doodle Jump
├── images
│ ├── doodle.png
│ ├── platform.png
│ └── background.png
└── main.cpp
├── 05 Minesweeper
├── images
│ └── tiles.jpg
└── main.cpp
├── 06 Fifteen-Puzzle
├── images
│ └── 15.png
└── main.cpp
├── 07 Racing (Top Down)
├── images
│ ├── car.png
│ └── background.png
└── main.cpp
├── 12 Mahjong Solitaire
├── files
│ ├── tiles.png
│ ├── background.png
│ └── map.txt
└── main.cpp
└── 11 NetWalk (Pipe Puzzle)
├── images
├── comp.png
├── pipes.png
├── server.png
└── background.png
└── main.cpp
/README.md:
--------------------------------------------------------------------------------
1 | # Games16
2 | 代码巴士
3 |
--------------------------------------------------------------------------------
/08 Outrun/images/1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Suvanning/Games16/HEAD/08 Outrun/images/1.png
--------------------------------------------------------------------------------
/08 Outrun/images/2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Suvanning/Games16/HEAD/08 Outrun/images/2.png
--------------------------------------------------------------------------------
/08 Outrun/images/3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Suvanning/Games16/HEAD/08 Outrun/images/3.png
--------------------------------------------------------------------------------
/08 Outrun/images/4.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Suvanning/Games16/HEAD/08 Outrun/images/4.png
--------------------------------------------------------------------------------
/08 Outrun/images/5.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Suvanning/Games16/HEAD/08 Outrun/images/5.png
--------------------------------------------------------------------------------
/08 Outrun/images/6.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Suvanning/Games16/HEAD/08 Outrun/images/6.png
--------------------------------------------------------------------------------
/08 Outrun/images/7.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Suvanning/Games16/HEAD/08 Outrun/images/7.png
--------------------------------------------------------------------------------
/13 Tron/background.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Suvanning/Games16/HEAD/13 Tron/background.jpg
--------------------------------------------------------------------------------
/14 Chess/stockfish.exe:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Suvanning/Games16/HEAD/14 Chess/stockfish.exe
--------------------------------------------------------------------------------
/04 Snake/images/green.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Suvanning/Games16/HEAD/04 Snake/images/green.png
--------------------------------------------------------------------------------
/04 Snake/images/red.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Suvanning/Games16/HEAD/04 Snake/images/red.png
--------------------------------------------------------------------------------
/04 Snake/images/white.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Suvanning/Games16/HEAD/04 Snake/images/white.png
--------------------------------------------------------------------------------
/08 Outrun/images/bg.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Suvanning/Games16/HEAD/08 Outrun/images/bg.png
--------------------------------------------------------------------------------
/09 Xonix/images/enemy.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Suvanning/Games16/HEAD/09 Xonix/images/enemy.png
--------------------------------------------------------------------------------
/09 Xonix/images/tiles.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Suvanning/Games16/HEAD/09 Xonix/images/tiles.png
--------------------------------------------------------------------------------
/14 Chess/images/board.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Suvanning/Games16/HEAD/14 Chess/images/board.png
--------------------------------------------------------------------------------
/01 Tetris/images/frame.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Suvanning/Games16/HEAD/01 Tetris/images/frame.png
--------------------------------------------------------------------------------
/01 Tetris/images/tiles.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Suvanning/Games16/HEAD/01 Tetris/images/tiles.png
--------------------------------------------------------------------------------
/03 Arkanoid/images/ball.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Suvanning/Games16/HEAD/03 Arkanoid/images/ball.png
--------------------------------------------------------------------------------
/14 Chess/images/board0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Suvanning/Games16/HEAD/14 Chess/images/board0.png
--------------------------------------------------------------------------------
/14 Chess/images/figures.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Suvanning/Games16/HEAD/14 Chess/images/figures.png
--------------------------------------------------------------------------------
/03 Arkanoid/images/block01.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Suvanning/Games16/HEAD/03 Arkanoid/images/block01.png
--------------------------------------------------------------------------------
/03 Arkanoid/images/block02.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Suvanning/Games16/HEAD/03 Arkanoid/images/block02.png
--------------------------------------------------------------------------------
/03 Arkanoid/images/block03.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Suvanning/Games16/HEAD/03 Arkanoid/images/block03.png
--------------------------------------------------------------------------------
/03 Arkanoid/images/block04.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Suvanning/Games16/HEAD/03 Arkanoid/images/block04.png
--------------------------------------------------------------------------------
/03 Arkanoid/images/block05.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Suvanning/Games16/HEAD/03 Arkanoid/images/block05.png
--------------------------------------------------------------------------------
/03 Arkanoid/images/paddle.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Suvanning/Games16/HEAD/03 Arkanoid/images/paddle.png
--------------------------------------------------------------------------------
/09 Xonix/images/gameover.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Suvanning/Games16/HEAD/09 Xonix/images/gameover.png
--------------------------------------------------------------------------------
/10 Bejeweled/images/cursor.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Suvanning/Games16/HEAD/10 Bejeweled/images/cursor.png
--------------------------------------------------------------------------------
/10 Bejeweled/images/gems.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Suvanning/Games16/HEAD/10 Bejeweled/images/gems.png
--------------------------------------------------------------------------------
/15 Volleyball/images/ball.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Suvanning/Games16/HEAD/15 Volleyball/images/ball.png
--------------------------------------------------------------------------------
/16 Asteroids/images/rock.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Suvanning/Games16/HEAD/16 Asteroids/images/rock.png
--------------------------------------------------------------------------------
/01 Tetris/images/background.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Suvanning/Games16/HEAD/01 Tetris/images/background.png
--------------------------------------------------------------------------------
/02 Doodle Jump/images/doodle.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Suvanning/Games16/HEAD/02 Doodle Jump/images/doodle.png
--------------------------------------------------------------------------------
/05 Minesweeper/images/tiles.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Suvanning/Games16/HEAD/05 Minesweeper/images/tiles.jpg
--------------------------------------------------------------------------------
/06 Fifteen-Puzzle/images/15.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Suvanning/Games16/HEAD/06 Fifteen-Puzzle/images/15.png
--------------------------------------------------------------------------------
/15 Volleyball/images/blobby.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Suvanning/Games16/HEAD/15 Volleyball/images/blobby.png
--------------------------------------------------------------------------------
/16 Asteroids/images/fire_red.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Suvanning/Games16/HEAD/16 Asteroids/images/fire_red.png
--------------------------------------------------------------------------------
/02 Doodle Jump/images/platform.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Suvanning/Games16/HEAD/02 Doodle Jump/images/platform.png
--------------------------------------------------------------------------------
/03 Arkanoid/images/background.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Suvanning/Games16/HEAD/03 Arkanoid/images/background.jpg
--------------------------------------------------------------------------------
/07 Racing (Top Down)/images/car.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Suvanning/Games16/HEAD/07 Racing (Top Down)/images/car.png
--------------------------------------------------------------------------------
/10 Bejeweled/images/background.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Suvanning/Games16/HEAD/10 Bejeweled/images/background.png
--------------------------------------------------------------------------------
/15 Volleyball/images/background.PNG:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Suvanning/Games16/HEAD/15 Volleyball/images/background.PNG
--------------------------------------------------------------------------------
/16 Asteroids/images/background.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Suvanning/Games16/HEAD/16 Asteroids/images/background.jpg
--------------------------------------------------------------------------------
/16 Asteroids/images/fire_blue.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Suvanning/Games16/HEAD/16 Asteroids/images/fire_blue.png
--------------------------------------------------------------------------------
/16 Asteroids/images/rock_small.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Suvanning/Games16/HEAD/16 Asteroids/images/rock_small.png
--------------------------------------------------------------------------------
/16 Asteroids/images/spaceship.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Suvanning/Games16/HEAD/16 Asteroids/images/spaceship.png
--------------------------------------------------------------------------------
/02 Doodle Jump/images/background.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Suvanning/Games16/HEAD/02 Doodle Jump/images/background.png
--------------------------------------------------------------------------------
/12 Mahjong Solitaire/files/tiles.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Suvanning/Games16/HEAD/12 Mahjong Solitaire/files/tiles.png
--------------------------------------------------------------------------------
/11 NetWalk (Pipe Puzzle)/images/comp.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Suvanning/Games16/HEAD/11 NetWalk (Pipe Puzzle)/images/comp.png
--------------------------------------------------------------------------------
/07 Racing (Top Down)/images/background.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Suvanning/Games16/HEAD/07 Racing (Top Down)/images/background.png
--------------------------------------------------------------------------------
/11 NetWalk (Pipe Puzzle)/images/pipes.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Suvanning/Games16/HEAD/11 NetWalk (Pipe Puzzle)/images/pipes.png
--------------------------------------------------------------------------------
/11 NetWalk (Pipe Puzzle)/images/server.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Suvanning/Games16/HEAD/11 NetWalk (Pipe Puzzle)/images/server.png
--------------------------------------------------------------------------------
/12 Mahjong Solitaire/files/background.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Suvanning/Games16/HEAD/12 Mahjong Solitaire/files/background.png
--------------------------------------------------------------------------------
/16 Asteroids/images/explosions/type_A.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Suvanning/Games16/HEAD/16 Asteroids/images/explosions/type_A.gif
--------------------------------------------------------------------------------
/16 Asteroids/images/explosions/type_A.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Suvanning/Games16/HEAD/16 Asteroids/images/explosions/type_A.png
--------------------------------------------------------------------------------
/16 Asteroids/images/explosions/type_B.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Suvanning/Games16/HEAD/16 Asteroids/images/explosions/type_B.gif
--------------------------------------------------------------------------------
/16 Asteroids/images/explosions/type_B.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Suvanning/Games16/HEAD/16 Asteroids/images/explosions/type_B.png
--------------------------------------------------------------------------------
/16 Asteroids/images/explosions/type_C.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Suvanning/Games16/HEAD/16 Asteroids/images/explosions/type_C.gif
--------------------------------------------------------------------------------
/16 Asteroids/images/explosions/type_C.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Suvanning/Games16/HEAD/16 Asteroids/images/explosions/type_C.png
--------------------------------------------------------------------------------
/11 NetWalk (Pipe Puzzle)/images/background.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Suvanning/Games16/HEAD/11 NetWalk (Pipe Puzzle)/images/background.png
--------------------------------------------------------------------------------
/01 Tetris/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/01 Tetris/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/01 Tetris/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/01 Tetris/.idea/01 Tetris.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/12 Mahjong Solitaire/files/map.txt:
--------------------------------------------------------------------------------
1 | 000000000000000000000000000000
2 | 001111111111111111111111110000
3 | 001111111111111111111111110000
4 | 000000112222222222221100000000
5 | 000000112222222222221100000000
6 | 000011112233333333221111000000
7 | 000011112233333333221111000000
8 | 001111112233444433221111110000
9 | 111111112233455433221111111111
10 | 111111112233455433221111111111
11 | 001111112233444433221111110000
12 | 000011112233333333221111000000
13 | 000011112233333333221111000000
14 | 000000112222222222221100000000
15 | 000000112222222222221100000000
16 | 001111111111111111111111110000
17 | 001111111111111111111111110000
18 | 000000000000000000000000000000
19 |
--------------------------------------------------------------------------------
/13 Tron/shader.frag:
--------------------------------------------------------------------------------
1 | uniform vec2 frag_LightOrigin;
2 | uniform vec3 frag_LightColor;
3 | uniform float frag_LightAttenuation;
4 | uniform vec2 frag_ScreenResolution;
5 | uniform sampler2D texture;
6 | void main()
7 | {
8 | vec2 baseDistance = gl_FragCoord.xy;
9 | baseDistance.y = frag_ScreenResolution.y-baseDistance.y;
10 | float d = length(frag_LightOrigin - baseDistance);
11 | float a = 1.0/(frag_LightAttenuation * d);
12 | vec4 color = vec4(a,a,a,1.0) * vec4(frag_LightColor, 1.0);
13 | vec4 t = texture2D(texture, gl_TexCoord[0].xy);
14 | if (t[0]>color[0]) color[0]=t[0];
15 | if (t[1]>color[1]) color[1]=t[1];
16 | if (t[2]>color[2]) color[2]=t[2];
17 | gl_FragColor=color;
18 | }
19 |
--------------------------------------------------------------------------------
/02 Doodle Jump/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | using namespace sf;
4 |
5 | struct point
6 | { int x,y;};
7 |
8 | int main()
9 | {
10 | srand(time(0));
11 |
12 | RenderWindow app(VideoMode(400, 533), "Doodle Game!");
13 | app.setFramerateLimit(60);
14 |
15 | Texture t1,t2,t3;
16 | t1.loadFromFile("images/background.png");
17 | t2.loadFromFile("images/platform.png");
18 | t3.loadFromFile("images/doodle.png");
19 |
20 | Sprite sBackground(t1), sPlat(t2), sPers(t3);
21 |
22 | point plat[20];
23 |
24 | for (int i=0;i<10;i++)
25 | {
26 | plat[i].x=rand()%400;
27 | plat[i].y=rand()%533;
28 | }
29 |
30 | int x=100,y=100,h=200;
31 | float dx=0,dy=0;
32 |
33 | while (app.isOpen())
34 | {
35 | Event e;
36 | while (app.pollEvent(e))
37 | {
38 | if (e.type == Event::Closed)
39 | app.close();
40 | }
41 |
42 | if (Keyboard::isKeyPressed(Keyboard::Right)) x+=3;
43 | if (Keyboard::isKeyPressed(Keyboard::Left)) x-=3;
44 |
45 | dy+=0.2;
46 | y+=dy;
47 | if (y>500) dy=-10;
48 |
49 | if (y533) {plat[i].y=0; plat[i].x=rand()%400;}
55 | }
56 |
57 | for (int i=0;i<10;i++)
58 | if ((x+50>plat[i].x) && (x+20plat[i].y) && (y+700)) dy=-10;
60 |
61 | sPers.setPosition(x,y);
62 |
63 | app.draw(sBackground);
64 | app.draw(sPers);
65 | for (int i=0;i<10;i++)
66 | {
67 | sPlat.setPosition(plat[i].x,plat[i].y);
68 | app.draw(sPlat);
69 | }
70 |
71 | app.display();
72 | }
73 |
74 | return 0;
75 | }
76 |
--------------------------------------------------------------------------------
/03 Arkanoid/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | using namespace sf;
4 |
5 | int main()
6 | {
7 | srand(time(0));
8 |
9 | RenderWindow app(VideoMode(520, 450), "Arkanoid!");
10 | app.setFramerateLimit(60);
11 |
12 | Texture t1,t2,t3,t4;
13 | t1.loadFromFile("images/block01.png");
14 | t2.loadFromFile("images/background.jpg");
15 | t3.loadFromFile("images/ball.png");
16 | t4.loadFromFile("images/paddle.png");
17 |
18 | Sprite sBackground(t2), sBall(t3), sPaddle(t4);
19 | sPaddle.setPosition(300,440);
20 |
21 | Sprite block[1000];
22 |
23 | int n=0;
24 | for (int i=1;i<=10;i++)
25 | for (int j=1;j<=10;j++)
26 | {
27 | block[n].setTexture(t1);
28 | block[n].setPosition(i*43,j*20);
29 | n++;
30 | }
31 |
32 | float dx=6, dy=5;
33 | float x=300, y=300;
34 |
35 | while (app.isOpen())
36 | {
37 | Event e;
38 | while (app.pollEvent(e))
39 | {
40 | if (e.type == Event::Closed)
41 | app.close();
42 | }
43 |
44 | x+=dx;
45 | for (int i=0;i520) dx=-dx;
55 | if (y<0 || y>450) dy=-dy;
56 |
57 | if (Keyboard::isKeyPressed(Keyboard::Right)) sPaddle.move(6,0);
58 | if (Keyboard::isKeyPressed(Keyboard::Left)) sPaddle.move(-6,0);
59 |
60 | if ( FloatRect(x,y,12,12).intersects(sPaddle.getGlobalBounds()) ) dy=-(rand()%5+2);
61 |
62 | sBall.setPosition(x,y);
63 |
64 | app.clear();
65 | app.draw(sBackground);
66 | app.draw(sBall);
67 | app.draw(sPaddle);
68 |
69 | for (int i=0;i
2 | #include
3 | using namespace sf;
4 |
5 | int main()
6 | {
7 | srand(time(0));
8 |
9 | RenderWindow app(VideoMode(400, 400), "Minesweeper!");
10 |
11 | int w=32;
12 | int grid[12][12];
13 | int sgrid[12][12]; //for showing
14 |
15 | Texture t;
16 | t.loadFromFile("images/tiles.jpg");
17 | Sprite s(t);
18 |
19 | for (int i=1;i<=10;i++)
20 | for (int j=1;j<=10;j++)
21 | {
22 | sgrid[i][j]=10;
23 | if (rand()%5==0) grid[i][j]=9;
24 | else grid[i][j]=0;
25 | }
26 |
27 | for (int i=1;i<=10;i++)
28 | for (int j=1;j<=10;j++)
29 | {
30 | int n=0;
31 | if (grid[i][j]==9) continue;
32 | if (grid[i+1][j]==9) n++;
33 | if (grid[i][j+1]==9) n++;
34 | if (grid[i-1][j]==9) n++;
35 | if (grid[i][j-1]==9) n++;
36 | if (grid[i+1][j+1]==9) n++;
37 | if (grid[i-1][j-1]==9) n++;
38 | if (grid[i-1][j+1]==9) n++;
39 | if (grid[i+1][j-1]==9) n++;
40 | grid[i][j]=n;
41 | }
42 |
43 | while (app.isOpen())
44 | {
45 | Vector2i pos = Mouse::getPosition(app);
46 | int x = pos.x/w;
47 | int y = pos.y/w;
48 |
49 | Event e;
50 | while (app.pollEvent(e))
51 | {
52 | if (e.type == Event::Closed)
53 | app.close();
54 |
55 | if (e.type == Event::MouseButtonPressed)
56 | if (e.key.code == Mouse::Left) sgrid[x][y]=grid[x][y];
57 | else if (e.key.code == Mouse::Right) sgrid[x][y]=11;
58 | }
59 |
60 | app.clear(Color::White);
61 | for (int i=1;i<=10;i++)
62 | for (int j=1;j<=10;j++)
63 | {
64 | if (sgrid[x][y]==9) sgrid[i][j]=grid[i][j];
65 | s.setTextureRect(IntRect(sgrid[i][j]*w,0,w,w));
66 | s.setPosition(i*w, j*w);
67 | app.draw(s);
68 | }
69 |
70 | app.display();
71 | }
72 |
73 | return 0;
74 | }
75 |
--------------------------------------------------------------------------------
/14 Chess/Connector.hpp:
--------------------------------------------------------------------------------
1 | #ifndef CONNECTOR_H
2 | #define CONNECTOR_H
3 |
4 | #include
5 | #include
6 | #include
7 | #include
8 |
9 | STARTUPINFO sti = {0};
10 | SECURITY_ATTRIBUTES sats = {0};
11 | PROCESS_INFORMATION pi = {0};
12 | HANDLE pipin_w, pipin_r, pipout_w, pipout_r;
13 | BYTE buffer[2048];
14 | DWORD writ, excode, read, available;
15 |
16 |
17 | void ConnectToEngine(char* path)
18 | {
19 | pipin_w = pipin_r = pipout_w = pipout_r = NULL;
20 | sats.nLength = sizeof(sats);
21 | sats.bInheritHandle = TRUE;
22 | sats.lpSecurityDescriptor = NULL;
23 |
24 | CreatePipe(&pipout_r, &pipout_w, &sats, 0);
25 | CreatePipe(&pipin_r, &pipin_w, &sats, 0);
26 |
27 | sti.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
28 | sti.wShowWindow = SW_HIDE;
29 | sti.hStdInput = pipin_r;
30 | sti.hStdOutput = pipout_w;
31 | sti.hStdError = pipout_w;
32 |
33 | CreateProcess(NULL, path, NULL, NULL, TRUE,0, NULL, NULL, &sti, &pi);
34 | }
35 |
36 |
37 | std::string getNextMove(std::string position)
38 | {
39 | std::string str;
40 | position = "position startpos moves "+position+"\ngo\n";
41 |
42 | WriteFile(pipin_w, position.c_str(), position.length(),&writ, NULL);
43 | Sleep(500);
44 |
45 | PeekNamedPipe(pipout_r, buffer,sizeof(buffer), &read, &available, NULL);
46 | do
47 | {
48 | ZeroMemory(buffer, sizeof(buffer));
49 | if(!ReadFile(pipout_r, buffer, sizeof(buffer), &read, NULL) || !read) break;
50 | buffer[read] = 0;
51 | str+=(char*)buffer;
52 | }
53 | while(read >= sizeof(buffer));
54 |
55 | int n = str.find("bestmove");
56 | if (n!=-1) return str.substr(n+9,4);
57 |
58 | return "error";
59 | }
60 |
61 |
62 | void CloseConnection()
63 | {
64 | WriteFile(pipin_w, "quit\n", 5,&writ, NULL);
65 | if(pipin_w != NULL) CloseHandle(pipin_w);
66 | if(pipin_r != NULL) CloseHandle(pipin_r);
67 | if(pipout_w != NULL) CloseHandle(pipout_w);
68 | if(pipout_r != NULL) CloseHandle(pipout_r);
69 | if(pi.hProcess != NULL) CloseHandle(pi.hProcess);
70 | if(pi.hThread != NULL) CloseHandle(pi.hThread);
71 | }
72 |
73 |
74 | #endif CONNECTOR_H
--------------------------------------------------------------------------------
/06 Fifteen-Puzzle/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | using namespace sf;
5 |
6 | int main()
7 | {
8 | RenderWindow app(VideoMode(256,256), "15-Puzzle!");
9 | app.setFramerateLimit(60);
10 |
11 | Texture t;
12 | t.loadFromFile("images/15.png");
13 |
14 | int w = 64;
15 | int grid[6][6] = {0};
16 | Sprite sprite[20];
17 |
18 | int n=0;
19 | for (int i=0;i<4;i++)
20 | for (int j=0;j<4;j++)
21 | {
22 | n++;
23 | sprite[n].setTexture(t);
24 | sprite[n].setTextureRect( IntRect(i*w,j*w,w,w) );
25 | grid[i+1][j+1]=n;
26 | }
27 |
28 | while (app.isOpen())
29 | {
30 | Event e;
31 | while (app.pollEvent(e))
32 | {
33 | if (e.type == Event::Closed)
34 | app.close();
35 |
36 | if (e.type == Event::MouseButtonPressed)
37 | if (e.key.code == Mouse::Left)
38 | {
39 | Vector2i pos = Mouse::getPosition(app);
40 | int x = pos.x/w + 1;
41 | int y = pos.y/w + 1;
42 |
43 | int dx=0;
44 | int dy=0;
45 |
46 | if (grid[x+1][y]==16) {dx=1; dy=0;};
47 | if (grid[x][y+1]==16) {dx=0; dy=1;};
48 | if (grid[x][y-1]==16) {dx=0; dy=-1;};
49 | if (grid[x-1][y]==16) {dx=-1;dy=0;};
50 |
51 | int n = grid[x][y];
52 | grid[x][y] = 16;
53 | grid[x+dx][y+dy] = n;
54 |
55 | //animation
56 | sprite[16].move(-dx*w,-dy*w);
57 | float speed=3;
58 |
59 | for (int i=0;i
2 | #include
3 | using namespace sf;
4 |
5 | int N=30,M=20;
6 | int size=16;
7 | int w = size*N;
8 | int h = size*M;
9 |
10 | int dir,num=4;
11 |
12 | struct Snake
13 | { int x,y;} s[100];
14 |
15 | struct Fruct
16 | { int x,y;} f;
17 |
18 | void Tick()
19 | {
20 | for (int i=num;i>0;--i)
21 | {s[i].x=s[i-1].x;
22 | s[i].y=s[i-1].y;}
23 |
24 | if (dir==0) s[0].y+=1;
25 | if (dir==1) s[0].x-=1;
26 | if (dir==2) s[0].x+=1;
27 | if (dir==3) s[0].y-=1;
28 |
29 | if ((s[0].x==f.x) && (s[0].y==f.y))
30 | { num++;
31 | f.x=rand() % N;
32 | f.y=rand() % M; }
33 |
34 | if (s[0].x>N) s[0].x=0; if (s[0].x<0) s[0].x=N;
35 | if (s[0].y>M) s[0].y=0; if (s[0].y<0) s[0].y=M;
36 |
37 | for (int i=1;idelay) {timer=0; Tick();}
79 |
80 | ////// draw ///////
81 | window.clear();
82 |
83 | for (int i=0; i
2 | #include
3 | using namespace sf;
4 |
5 | const int W=600;
6 | const int H=480;
7 | int speed = 4;
8 | bool field[W][H]={0};
9 |
10 | struct player
11 | { int x,y,dir;
12 | Color color;
13 | player(Color c)
14 | {
15 | x=rand() % W;
16 | y=rand() % H;
17 | color=c;
18 | dir=rand() % 4;
19 | }
20 | void tick()
21 | {
22 | if (dir==0) y+=1;
23 | if (dir==1) x-=1;
24 | if (dir==2) x+=1;
25 | if (dir==3) y-=1;
26 |
27 | if (x>=W) x=0; if (x<0) x=W-1;
28 | if (y>=H) y=0; if (y<0) y=H-1;
29 | }
30 |
31 | Vector3f getColor()
32 | {return Vector3f(color.r,color.g,color.b);}
33 | };
34 |
35 | int main()
36 | {
37 | srand(time(0));
38 |
39 | RenderWindow window(VideoMode(W, H), "The Tron Game!");
40 | window.setFramerateLimit(60);
41 |
42 | Texture texture;
43 | texture.loadFromFile("background.jpg");
44 | Sprite sBackground(texture);
45 |
46 | player p1(Color::Red), p2(Color::Green);
47 |
48 | Sprite sprite;
49 | RenderTexture t;
50 | t.create(W, H);
51 | t.setSmooth(true);
52 | sprite.setTexture(t.getTexture());
53 | t.clear(); t.draw(sBackground);
54 |
55 | bool Game=1;
56 |
57 | while (window.isOpen())
58 | {
59 | Event e;
60 | while (window.pollEvent(e))
61 | {
62 | if (e.type == Event::Closed)
63 | window.close();
64 | }
65 |
66 | if (Keyboard::isKeyPressed(Keyboard::Left)) if (p1.dir!=2) p1.dir=1;
67 | if (Keyboard::isKeyPressed(Keyboard::Right)) if (p1.dir!=1) p1.dir=2;
68 | if (Keyboard::isKeyPressed(Keyboard::Up)) if (p1.dir!=0) p1.dir=3;
69 | if (Keyboard::isKeyPressed(Keyboard::Down)) if (p1.dir!=3) p1.dir=0;
70 |
71 | if (Keyboard::isKeyPressed(Keyboard::A)) if (p2.dir!=2) p2.dir=1;
72 | if (Keyboard::isKeyPressed(Keyboard::D)) if (p2.dir!=1) p2.dir=2;
73 | if (Keyboard::isKeyPressed(Keyboard::W)) if (p2.dir!=0) p2.dir=3;
74 | if (Keyboard::isKeyPressed(Keyboard::S)) if (p2.dir!=3) p2.dir=0;
75 |
76 | if (!Game) continue;
77 |
78 | for(int i=0;i
2 | #include
3 | using namespace sf;
4 |
5 | const int M = 20;
6 | const int N = 10;
7 |
8 | int field[M][N] = {0};
9 |
10 | struct Point
11 | {int x,y;} a[4], b[4];
12 |
13 | int figures[7][4] =
14 | {
15 | 1,3,5,7, // I
16 | 2,4,5,7, // Z
17 | 3,5,4,6, // S
18 | 3,5,4,7, // T
19 | 2,3,5,7, // L
20 | 3,5,7,6, // J
21 | 2,3,4,5, // O
22 | };
23 |
24 | bool check()
25 | {
26 | for (int i=0;i<4;i++)
27 | if (a[i].x<0 || a[i].x>=N || a[i].y>=M) return 0;
28 | else if (field[a[i].y][a[i].x]) return 0;
29 |
30 | return 1;
31 | };
32 |
33 |
34 | int main()
35 | {
36 | srand(time(0));
37 |
38 | RenderWindow window(VideoMode(320, 480), "The Game!");
39 |
40 | Texture t1,t2,t3;
41 | t1.loadFromFile("images/tiles.png");
42 | t2.loadFromFile("images/background.png");
43 | t3.loadFromFile("images/frame.png");
44 |
45 | Sprite s(t1), background(t2), frame(t3);
46 |
47 | int dx=0; bool rotate=0; int colorNum=1;
48 | float timer=0,delay=0.3;
49 |
50 | Clock clock;
51 |
52 | while (window.isOpen())
53 | {
54 | float time = clock.getElapsedTime().asSeconds();
55 | clock.restart();
56 | timer+=time;
57 |
58 | Event e;
59 | while (window.pollEvent(e))
60 | {
61 | if (e.type == Event::Closed)
62 | window.close();
63 |
64 | if (e.type == Event::KeyPressed)
65 | if (e.key.code==Keyboard::Up) rotate=true;
66 | else if (e.key.code==Keyboard::Left) dx=-1;
67 | else if (e.key.code==Keyboard::Right) dx=1;
68 | }
69 |
70 | if (Keyboard::isKeyPressed(Keyboard::Down)) delay=0.05;
71 |
72 | //// <- Move -> ///
73 | for (int i=0;i<4;i++) { b[i]=a[i]; a[i].x+=dx; }
74 | if (!check()) for (int i=0;i<4;i++) a[i]=b[i];
75 |
76 | //////Rotate//////
77 | if (rotate)
78 | {
79 | Point p = a[1]; //center of rotation
80 | for (int i=0;i<4;i++)
81 | {
82 | int x = a[i].y-p.y;
83 | int y = a[i].x-p.x;
84 | a[i].x = p.x - x;
85 | a[i].y = p.y + y;
86 | }
87 | if (!check()) for (int i=0;i<4;i++) a[i]=b[i];
88 | }
89 |
90 | ///////Tick//////
91 | if (timer>delay)
92 | {
93 | for (int i=0;i<4;i++) { b[i]=a[i]; a[i].y+=1; }
94 |
95 | if (!check())
96 | {
97 | for (int i=0;i<4;i++) field[b[i].y][b[i].x]=colorNum;
98 |
99 | colorNum=1+rand()%7;
100 | int n=rand()%7;
101 | for (int i=0;i<4;i++)
102 | {
103 | a[i].x = figures[n][i] % 2;
104 | a[i].y = figures[n][i] / 2;
105 | }
106 | }
107 |
108 | timer=0;
109 | }
110 |
111 | ///////check lines//////////
112 | int k=M-1;
113 | for (int i=M-1;i>0;i--)
114 | {
115 | int count=0;
116 | for (int j=0;j
2 | using namespace sf;
3 |
4 | const int num=8; //checkpoints
5 | int points[num][2] = {300, 610,
6 | 1270,430,
7 | 1380,2380,
8 | 1900,2460,
9 | 1970,1700,
10 | 2550,1680,
11 | 2560,3150,
12 | 500, 3300};
13 |
14 | struct Car
15 | {
16 | float x,y,speed,angle; int n;
17 |
18 | Car() {speed=2; angle=0; n=0;}
19 |
20 | void move()
21 | {
22 | x += sin(angle) * speed;
23 | y -= cos(angle) * speed;
24 | }
25 |
26 | void findTarget()
27 | {
28 | float tx=points[n][0];
29 | float ty=points[n][1];
30 | float beta = angle-atan2(tx-x,-ty+y);
31 | if (sin(beta)<0) angle+=0.005*speed; else angle-=0.005*speed;
32 | if ((x-tx)*(x-tx)+(y-ty)*(y-ty)<25*25) n=(n+1)%num;
33 | }
34 | };
35 |
36 |
37 | int main()
38 | {
39 | RenderWindow app(VideoMode(640, 480), "Car Racing Game!");
40 | app.setFramerateLimit(60);
41 |
42 | Texture t1,t2,t3;
43 | t1.loadFromFile("images/background.png");
44 | t2.loadFromFile("images/car.png");
45 | t1.setSmooth(true);
46 | t2.setSmooth(true);
47 |
48 | Sprite sBackground(t1), sCar(t2);
49 | sBackground.scale(2,2);
50 |
51 | sCar.setOrigin(22, 22);
52 | float R=22;
53 |
54 | const int N=5;
55 | Car car[N];
56 | for(int i=0;i-maxSpeed)
91 | if (speed > 0) speed -= dec;
92 | else speed -= acc;
93 |
94 | if (!Up && !Down)
95 | if (speed - dec > 0) speed -= dec;
96 | else if (speed + dec < 0) speed += dec;
97 | else speed = 0;
98 |
99 | if (Right && speed!=0) angle += turnSpeed * speed/maxSpeed;
100 | if (Left && speed!=0) angle -= turnSpeed * speed/maxSpeed;
101 |
102 | car[0].speed = speed;
103 | car[0].angle = angle;
104 |
105 | for(int i=0;i320) offsetX = car[0].x-320;
129 | if (car[0].y>240) offsetY = car[0].y-240;
130 |
131 | sBackground.setPosition(-offsetX,-offsetY);
132 | app.draw(sBackground);
133 |
134 | Color colors[10] = {Color::Red, Color::Green, Color::Magenta, Color::Blue, Color::White};
135 |
136 | for(int i=0;i
2 | #include
3 | using namespace sf;
4 |
5 | const int M = 25;
6 | const int N = 40;
7 |
8 | int grid[M][N] = {0};
9 | int ts = 18; //tile size
10 |
11 | struct Enemy
12 | {int x,y,dx,dy;
13 |
14 | Enemy()
15 | {
16 | x=y=300;
17 | dx=4-rand()%8;
18 | dy=4-rand()%8;
19 | }
20 |
21 | void move()
22 | {
23 | x+=dx; if (grid[y/ts][x/ts]==1) {dx=-dx; x+=dx;}
24 | y+=dy; if (grid[y/ts][x/ts]==1) {dy=-dy; y+=dy;}
25 | }
26 | };
27 |
28 | void drop(int y,int x)
29 | {
30 | if (grid[y][x]==0) grid[y][x]=-1;
31 | if (grid[y-1][x]==0) drop(y-1,x);
32 | if (grid[y+1][x]==0) drop(y+1,x);
33 | if (grid[y][x-1]==0) drop(y,x-1);
34 | if (grid[y][x+1]==0) drop(y,x+1);
35 | }
36 |
37 | int main()
38 | {
39 | srand(time(0));
40 |
41 | RenderWindow window(VideoMode(N*ts, M*ts), "Xonix Game!");
42 | window.setFramerateLimit(60);
43 |
44 | Texture t1,t2,t3;
45 | t1.loadFromFile("images/tiles.png");
46 | t2.loadFromFile("images/gameover.png");
47 | t3.loadFromFile("images/enemy.png");
48 |
49 | Sprite sTile(t1), sGameover(t2), sEnemy(t3);
50 | sGameover.setPosition(100,100);
51 | sEnemy.setOrigin(20,20);
52 |
53 | int enemyCount = 4;
54 | Enemy a[10];
55 |
56 | bool Game=true;
57 | int x=0, y=0, dx=0, dy=0;
58 | float timer=0, delay=0.07;
59 | Clock clock;
60 |
61 | for (int i=0;idelay)
97 | {
98 | x+=dx;
99 | y+=dy;
100 |
101 | if (x<0) x=0; if (x>N-1) x=N-1;
102 | if (y<0) y=0; if (y>M-1) y=M-1;
103 |
104 | if (grid[y][x]==2) Game=false;
105 | if (grid[y][x]==0) grid[y][x]=2;
106 | timer=0;
107 | }
108 |
109 | for (int i=0;i
2 | #include
3 | #include
4 | using namespace sf;
5 |
6 | int field[50][50][50] = {0};
7 |
8 | int& f(int x,int y,int z){return field[y+2][x+2][z];}
9 | int& f(Vector3i v){return f(v.x,v.y,v.z);}
10 |
11 |
12 | bool isOpen(int x,int y,int z)
13 | {
14 | for(int i=-1;i<=1;i++)
15 | for(int j=-1;j<=1;j++)
16 | if (f(x+2,y+i,z)>0 && f(x-2,y+j,z)>0) return 0;
17 |
18 | for(int i=-1;i<=1;i++)
19 | for(int j=-1;j<=1;j++)
20 | if ( f(x+i,y+j,z+1)>0 ) return 0;
21 |
22 | return 1;
23 | }
24 |
25 |
26 | int main()
27 | {
28 | srand(time(0));
29 |
30 | RenderWindow app(VideoMode(740, 570), "Mahjong Solitaire!");
31 |
32 | Texture t1,t2;
33 | t1.loadFromFile("files/tiles.png");
34 | t2.loadFromFile("files/background.png");
35 | Sprite s(t1), sBackground(t2);
36 | int w=48, h=66;
37 | int stepX=w/2-2, stepY=h/2-2;
38 | float offX=4.6, offY=7.1; // z offset
39 | Vector3i v1,v2;
40 | std::vector moves;
41 |
42 |
43 | ////load from file////
44 | std::fstream myfile("files/map.txt");
45 | for(int y=0;y<18;y++)
46 | for(int x=0;x<30;x++)
47 | {
48 | char a; myfile >> a;
49 | int n = a - '0';
50 | for(int z=0;z opens;
59 | for(int z=0;z<10;z++)
60 | for(int y=0;y<18;y++)
61 | for(int x=0;x<30;x++)
62 | if (f(x,y,z)>0 && isOpen(x,y,z)) opens.push_back(Vector3i(x,y,z));
63 |
64 | int n=opens.size();
65 | if (n<2) break;
66 | int a=0,b=0;
67 | while(a==b){a=rand()%n;b=rand()%n;}
68 | f(opens[a])=-k; if (k>34) k++;
69 | f(opens[b])=-k;
70 | k%=42;
71 | }
72 |
73 | for(int z=0;z<10;z++)
74 | for(int y=0;y<18;y++)
75 | for(int x=0;x<30;x++) f(x,y,z)*=-1;
76 |
77 |
78 | while (app.isOpen())
79 | {
80 | Event e;
81 | while (app.pollEvent(e))
82 | {
83 | if (e.type == Event::Closed)
84 | app.close();
85 |
86 | //move back
87 | if (e.type == Event::MouseButtonReleased)
88 | if (e.key.code == Mouse::Right)
89 | {
90 | int n = moves.size();
91 | if (n==0) continue;
92 | f(moves[n-1])*=-1; moves.pop_back();
93 | f(moves[n-2])*=-1; moves.pop_back();
94 | }
95 |
96 | if (e.type == Event::MouseButtonPressed)
97 | if (e.key.code == Mouse::Left)
98 | for(int z=0;z<10;z++)
99 | {
100 | Vector2i pos = Mouse::getPosition(app) - Vector2i(30,0); // 30 - desk offset
101 | int x = (pos.x-z*offX)/stepX;
102 | int y = (pos.y+z*offY)/stepY;
103 |
104 | for(int i=0;i<2;i++)
105 | for(int j=0;j<2;j++)
106 | if (f(x-i,y-j,z)>0 && isOpen(x-i,y-j,z))
107 | v1=Vector3i(x-i,y-j,z);
108 |
109 | if (v1==v2) continue;
110 |
111 | int a=f(v1),b=f(v2);
112 | if ( a==b || (a>34 && a<39 && b>34 && b<39) || (a>=39 && b>=39) )
113 | {
114 | f(v1)*=-1; moves.push_back(v1);
115 | f(v2)*=-1; moves.push_back(v2);
116 | }
117 | v2=v1;
118 | }
119 | }
120 |
121 | app.clear();
122 | app.draw(sBackground);
123 | for(int z=0;z<10;z++)
124 | for(int x=30;x>=0;x--)
125 | for(int y=0;y<18;y++)
126 | {
127 | int k = f(x,y,z)-1;
128 | if (k<0) continue;
129 | s.setTextureRect(IntRect(k*w,0,w,h));
130 | if (isOpen(x,y,z)) s.setTextureRect(IntRect(k*w,h,w,h));
131 | s.setPosition(x*stepX + z*offX, y*stepY - z*offY);
132 | s.move(30,0); //desk offset
133 | app.draw(s);
134 | }
135 |
136 | app.display();
137 | }
138 |
139 | return 0;
140 | }
141 |
--------------------------------------------------------------------------------
/10 Bejeweled/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | using namespace sf;
4 |
5 | int ts = 54; //tile size
6 | Vector2i offset(48,24);
7 |
8 | struct piece
9 | { int x,y,col,row,kind,match,alpha;
10 | piece(){match=0; alpha=255;}
11 | } grid[10][10];
12 |
13 | void swap(piece p1,piece p2)
14 | {
15 | std::swap(p1.col,p2.col);
16 | std::swap(p1.row,p2.row);
17 |
18 | grid[p1.row][p1.col]=p1;
19 | grid[p2.row][p2.col]=p2;
20 | }
21 |
22 |
23 | int main()
24 | {
25 | srand(time(0));
26 |
27 | RenderWindow app(VideoMode(740,480), "Match-3 Game!");
28 | app.setFramerateLimit(60);
29 |
30 | Texture t1,t2;
31 | t1.loadFromFile("images/background.png");
32 | t2.loadFromFile("images/gems.png");
33 |
34 | Sprite background(t1), gems(t2);
35 |
36 | for (int i=1;i<=8;i++)
37 | for (int j=1;j<=8;j++)
38 | {
39 | grid[i][j].kind=rand()%3;
40 | grid[i][j].col=j;
41 | grid[i][j].row=i;
42 | grid[i][j].x = j*ts;
43 | grid[i][j].y = i*ts;
44 | }
45 |
46 | int x0,y0,x,y; int click=0; Vector2i pos;
47 | bool isSwap=false, isMoving=false;
48 |
49 | while (app.isOpen())
50 | {
51 | Event e;
52 | while (app.pollEvent(e))
53 | {
54 | if (e.type == Event::Closed)
55 | app.close();
56 |
57 | if (e.type == Event::MouseButtonPressed)
58 | if (e.key.code == Mouse::Left)
59 | {
60 | if (!isSwap && !isMoving) click++;
61 | pos = Mouse::getPosition(app)-offset;
62 | }
63 | }
64 |
65 | // mouse click
66 | if (click==1)
67 | {
68 | x0=pos.x/ts+1;
69 | y0=pos.y/ts+1;
70 | }
71 | if (click==2)
72 | {
73 | x=pos.x/ts+1;
74 | y=pos.y/ts+1;
75 | if (abs(x-x0)+abs(y-y0)==1)
76 | {swap(grid[y0][x0],grid[y][x]); isSwap=1; click=0;}
77 | else click=1;
78 | }
79 |
80 | //Match finding
81 | for(int i=1;i<=8;i++)
82 | for(int j=1;j<=8;j++)
83 | {
84 | if (grid[i][j].kind==grid[i+1][j].kind)
85 | if (grid[i][j].kind==grid[i-1][j].kind)
86 | for(int n=-1;n<=1;n++) grid[i+n][j].match++;
87 |
88 | if (grid[i][j].kind==grid[i][j+1].kind)
89 | if (grid[i][j].kind==grid[i][j-1].kind)
90 | for(int n=-1;n<=1;n++) grid[i][j+n].match++;
91 | }
92 |
93 | //Moving animation
94 | isMoving=false;
95 | for (int i=1;i<=8;i++)
96 | for (int j=1;j<=8;j++)
97 | {
98 | piece &p = grid[i][j];
99 | int dx,dy;
100 | for(int n=0;n<4;n++) // 4 - speed
101 | {dx = p.x-p.col*ts;
102 | dy = p.y-p.row*ts;
103 | if (dx) p.x-=dx/abs(dx);
104 | if (dy) p.y-=dy/abs(dy);}
105 | if (dx||dy) isMoving=1;
106 | }
107 |
108 | //Deleting amimation
109 | if (!isMoving)
110 | for (int i=1;i<=8;i++)
111 | for (int j=1;j<=8;j++)
112 | if (grid[i][j].match) if (grid[i][j].alpha>10) {grid[i][j].alpha-=10; isMoving=true;}
113 |
114 | //Get score
115 | int score=0;
116 | for (int i=1;i<=8;i++)
117 | for (int j=1;j<=8;j++)
118 | score+=grid[i][j].match;
119 |
120 | //Second swap if no match
121 | if (isSwap && !isMoving)
122 | {if (!score) swap(grid[y0][x0],grid[y][x]); isSwap=0;}
123 |
124 | //Update grid
125 | if (!isMoving)
126 | {
127 | for(int i=8;i>0;i--)
128 | for(int j=1;j<=8;j++)
129 | if (grid[i][j].match)
130 | for(int n=i;n>0;n--)
131 | if (!grid[n][j].match) {swap(grid[n][j],grid[i][j]); break;};
132 |
133 | for(int j=1;j<=8;j++)
134 | for(int i=8,n=0;i>0;i--)
135 | if (grid[i][j].match)
136 | {
137 | grid[i][j].kind = rand()%7;
138 | grid[i][j].y = -ts*n++;
139 | grid[i][j].match=0;
140 | grid[i][j].alpha = 255;
141 | }
142 | }
143 |
144 |
145 | //////draw///////
146 | app.draw(background);
147 |
148 | for (int i=1;i<=8;i++)
149 | for (int j=1;j<=8;j++)
150 | {
151 | piece p = grid[i][j];
152 | gems.setTextureRect( IntRect(p.kind*49,0,49,49));
153 | gems.setColor(Color(255,255,255,p.alpha));
154 | gems.setPosition(p.x,p.y);
155 | gems.move(offset.x-ts,offset.y-ts);
156 | app.draw(gems);
157 | }
158 |
159 | app.display();
160 | }
161 | return 0;
162 | }
163 |
--------------------------------------------------------------------------------
/15 Volleyball/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | using namespace sf;
4 |
5 | const float SCALE = 30.f;
6 | const float DEG = 57.29577f;
7 |
8 | b2Vec2 Gravity(0.f, 9.8f);
9 | b2World World(Gravity);
10 |
11 | void setWall(int x,int y,int w,int h)
12 | {
13 | b2PolygonShape gr;
14 | gr.SetAsBox(w/SCALE,h/SCALE);
15 |
16 | b2BodyDef bdef;
17 | bdef.position.Set(x/SCALE, y/SCALE);
18 |
19 | b2Body *b_ground = World.CreateBody(&bdef);
20 | b_ground->CreateFixture(&gr,1);
21 | }
22 |
23 | int main()
24 | {
25 | RenderWindow window(VideoMode(800, 600), "Volleyball Game!");
26 | window.setFramerateLimit(60);
27 | window.setSize(Vector2u(800*0.8,600*0.8));
28 |
29 | Texture t1,t2,t3;
30 | t1.loadFromFile("images/background.png");
31 | t2.loadFromFile("images/ball.png");
32 | t3.loadFromFile("images/blobby.png");
33 | t1.setSmooth(true);
34 | t2.setSmooth(true);
35 | t3.setSmooth(true);
36 |
37 | Sprite sBackground(t1), sBall(t2), sPlayer(t3);
38 | sPlayer.setOrigin(75/2,90/2);
39 | sBall.setOrigin(32,32);
40 |
41 |
42 | /////////box2d///////////
43 | setWall(400,520,2000,10);
44 | setWall(400, 450,10,170);
45 | setWall(0,0,10,2000);
46 | setWall(800,0,10,2000);
47 |
48 |
49 | b2PolygonShape shape;
50 | shape.SetAsBox(30/SCALE,30/SCALE);
51 | b2BodyDef bdef;
52 | bdef.type=b2_dynamicBody;
53 | ///players///////////////
54 | b2Body *pBody[2];
55 | for(int i=0;i<2;i++){
56 | bdef.position.Set(20*i,2);
57 | b2CircleShape circle;
58 | circle.m_radius=32/SCALE;
59 | circle.m_p.Set(0,13/SCALE);
60 | pBody[i] = World.CreateBody(&bdef);
61 | pBody[i]->CreateFixture(&circle,5);
62 | circle.m_radius=25/SCALE;
63 | circle.m_p.Set(0,-20/SCALE);
64 | pBody[i]->CreateFixture(&circle,5);
65 | pBody[i]->SetFixedRotation(true);
66 | }
67 | pBody[0]->SetUserData("player1");
68 | pBody[1]->SetUserData("player2");
69 |
70 | /// ball /////////////
71 | bdef.position.Set(5,1);
72 | b2CircleShape circle;
73 | circle.m_radius=32/SCALE;
74 | b2Body *b = World.CreateBody(&bdef);
75 | b2FixtureDef fdef;
76 | fdef.shape=&circle;
77 | fdef.restitution=0.95;
78 | fdef.density=0.2;
79 | b->CreateFixture(&fdef);
80 | b->SetUserData("ball");
81 | /////////////////////////
82 |
83 | bool onGround=0;
84 |
85 | while (window.isOpen())
86 | {
87 | Event e;
88 | while (window.pollEvent(e))
89 | {
90 | if (e.type == Event::Closed)
91 | window.close();
92 | }
93 |
94 | for(int n=0;n<2;n++) // 2 - speed
95 | World.Step(1/60.f, 8, 3);
96 |
97 | //player1
98 | b2Vec2 pos = pBody[0]->GetPosition();
99 | b2Vec2 vel = pBody[0]->GetLinearVelocity();
100 |
101 | if (Keyboard::isKeyPressed(Keyboard::Right)) vel.x=5;
102 | if (Keyboard::isKeyPressed(Keyboard::Left)) vel.x=-5;
103 | if (Keyboard::isKeyPressed(Keyboard::Up)) if (pos.y*SCALE>=463) vel.y=-13;
104 |
105 | if (!Keyboard::isKeyPressed(Keyboard::Right))
106 | if (!Keyboard::isKeyPressed(Keyboard::Left))
107 | vel.x=0;
108 |
109 | pBody[0]->SetLinearVelocity(vel);
110 |
111 |
112 | //player2
113 | pos = pBody[1]->GetPosition();
114 | vel = pBody[1]->GetLinearVelocity();
115 |
116 | if (Keyboard::isKeyPressed(Keyboard::D)) vel.x=5;
117 | if (Keyboard::isKeyPressed(Keyboard::A)) vel.x=-5;
118 | if (Keyboard::isKeyPressed(Keyboard::W)) if (pos.y*SCALE>=463) vel.y=-13;
119 |
120 | if (!Keyboard::isKeyPressed(Keyboard::D))
121 | if (!Keyboard::isKeyPressed(Keyboard::A))
122 | vel.x=0;
123 |
124 | pBody[1]->SetLinearVelocity(vel);
125 |
126 | //ball max speed
127 | vel = b->GetLinearVelocity();
128 | if (vel.Length()>15) b->SetLinearVelocity( 15/vel.Length() * vel );
129 |
130 | //////////Draw///////////////
131 | window.draw(sBackground);
132 |
133 | for (b2Body* it = World.GetBodyList(); it != 0; it = it->GetNext())
134 | {
135 | b2Vec2 pos = it->GetPosition();
136 | float angle = it->GetAngle();
137 |
138 | if (it->GetUserData()=="player1")
139 | {
140 | sPlayer.setPosition(pos.x*SCALE,pos.y*SCALE);
141 | sPlayer.setRotation(angle*DEG);
142 | sPlayer.setColor(Color::Red);
143 | window.draw(sPlayer);
144 | }
145 |
146 | if (it->GetUserData()=="player2")
147 | {
148 | sPlayer.setPosition(pos.x*SCALE,pos.y*SCALE);
149 | sPlayer.setRotation(angle*DEG);
150 | sPlayer.setColor(Color::Green);
151 | window.draw(sPlayer);
152 | }
153 |
154 | if (it->GetUserData()=="ball")
155 | {
156 | sBall.setPosition(pos.x*SCALE,pos.y*SCALE);
157 | sBall.setRotation(angle*DEG);
158 | window.draw(sBall);
159 | }
160 | }
161 |
162 | window.display();
163 | }
164 | return 0;
165 | }
--------------------------------------------------------------------------------
/14 Chess/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "Connector.hpp"
4 | using namespace sf;
5 |
6 | int size = 56;
7 | Vector2f offset(28,28);
8 |
9 | Sprite f[32]; //figures
10 | std::string position="";
11 |
12 | int board[8][8] =
13 | {-1,-2,-3,-4,-5,-3,-2,-1,
14 | -6,-6,-6,-6,-6,-6,-6,-6,
15 | 0, 0, 0, 0, 0, 0, 0, 0,
16 | 0, 0, 0, 0, 0, 0, 0, 0,
17 | 0, 0, 0, 0, 0, 0, 0, 0,
18 | 0, 0, 0, 0, 0, 0, 0, 0,
19 | 6, 6, 6, 6, 6, 6, 6, 6,
20 | 1, 2, 3, 4, 5, 3, 2, 1};
21 |
22 | std::string toChessNote(Vector2f p)
23 | {
24 | std::string s = "";
25 | s += char(p.x/size+97);
26 | s += char(7-p.y/size+49);
27 | return s;
28 | }
29 |
30 | Vector2f toCoord(char a,char b)
31 | {
32 | int x = int(a) - 97;
33 | int y = 7-int(b)+49;
34 | return Vector2f(x*size,y*size);
35 | }
36 |
37 | void move(std::string str)
38 | {
39 | Vector2f oldPos = toCoord(str[0],str[1]);
40 | Vector2f newPos = toCoord(str[2],str[3]);
41 |
42 | for(int i=0;i<32;i++)
43 | if (f[i].getPosition()==newPos) f[i].setPosition(-100,-100);
44 |
45 | for(int i=0;i<32;i++)
46 | if (f[i].getPosition()==oldPos) f[i].setPosition(newPos);
47 |
48 | //castling //if the king didn't move
49 | if (str=="e1g1") if (position.find("e1")==-1) move("h1f1");
50 | if (str=="e8g8") if (position.find("e8")==-1) move("h8f8");
51 | if (str=="e1c1") if (position.find("e1")==-1) move("a1d1");
52 | if (str=="e8c8") if (position.find("e8")==-1) move("a8d8");
53 | }
54 |
55 | void loadPosition()
56 | {
57 | int k=0;
58 | for(int i=0;i<8;i++)
59 | for(int j=0;j<8;j++)
60 | {
61 | int n = board[i][j];
62 | if (!n) continue;
63 | int x = abs(n)-1;
64 | int y = n>0?1:0;
65 | f[k].setTextureRect( IntRect(size*x,size*y,size,size) );
66 | f[k].setPosition(size*j,size*i);
67 | k++;
68 | }
69 |
70 | for(int i=0;i6) position.erase(position.length()-6,5); loadPosition();}
110 |
111 | /////drag and drop///////
112 | if (e.type == Event::MouseButtonPressed)
113 | if (e.key.code == Mouse::Left)
114 | for(int i=0;i<32;i++)
115 | if (f[i].getGlobalBounds().contains(pos.x,pos.y))
116 | {
117 | isMove=true; n=i;
118 | dx=pos.x - f[i].getPosition().x;
119 | dy=pos.y - f[i].getPosition().y;
120 | oldPos = f[i].getPosition();
121 | }
122 |
123 | if (e.type == Event::MouseButtonReleased)
124 | if (e.key.code == Mouse::Left)
125 | {
126 | isMove=false;
127 | Vector2f p = f[n].getPosition() + Vector2f(size/2,size/2);
128 | newPos = Vector2f( size*int(p.x/size), size*int(p.y/size) );
129 | str = toChessNote(oldPos)+toChessNote(newPos);
130 | move(str);
131 | if (oldPos!=newPos) position+=str+" ";
132 | f[n].setPosition(newPos);
133 | }
134 | }
135 |
136 | //comp move
137 | if (Keyboard::isKeyPressed(Keyboard::Space))
138 | {
139 | str = getNextMove(position);
140 |
141 | oldPos = toCoord(str[0],str[1]);
142 | newPos = toCoord(str[2],str[3]);
143 |
144 | for(int i=0;i<32;i++) if (f[i].getPosition()==oldPos) n=i;
145 |
146 | /////animation///////
147 | for(int k=0;k<50;k++)
148 | {
149 | Vector2f p = newPos - oldPos;
150 | f[n].move(p.x/50, p.y/50);
151 | window.draw(sBoard);
152 | for(int i=0;i<32;i++) f[i].move(offset);
153 | for(int i=0;i<32;i++) window.draw(f[i]); window.draw(f[n]);
154 | for(int i=0;i<32;i++) f[i].move(-offset);
155 | window.display();
156 | }
157 |
158 | move(str); position+=str+" ";
159 | f[n].setPosition(newPos);
160 | }
161 |
162 | if (isMove) f[n].setPosition(pos.x-dx,pos.y-dy);
163 |
164 | ////// draw ///////
165 | window.clear();
166 | window.draw(sBoard);
167 | for(int i=0;i<32;i++) f[i].move(offset);
168 | for(int i=0;i<32;i++) window.draw(f[i]); window.draw(f[n]);
169 | for(int i=0;i<32;i++) f[i].move(-offset);
170 | window.display();
171 | }
172 |
173 | CloseConnection();
174 |
175 | return 0;
176 | }
177 |
--------------------------------------------------------------------------------
/08 Outrun/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace sf;
3 |
4 | int width = 1024;
5 | int height = 768;
6 | int roadW = 2000;
7 | int segL = 200; //segment length
8 | float camD = 0.84; //camera depth
9 |
10 | void drawQuad(RenderWindow &w, Color c, int x1,int y1,int w1,int x2,int y2,int w2)
11 | {
12 | ConvexShape shape(4);
13 | shape.setFillColor(c);
14 | shape.setPoint(0, Vector2f(x1-w1,y1));
15 | shape.setPoint(1, Vector2f(x2-w2,y2));
16 | shape.setPoint(2, Vector2f(x2+w2,y2));
17 | shape.setPoint(3, Vector2f(x1+w1,y1));
18 | w.draw(shape);
19 | }
20 |
21 | struct Line
22 | {
23 | float x,y,z; //3d center of line
24 | float X,Y,W; //screen coord
25 | float curve,spriteX,clip,scale;
26 | Sprite sprite;
27 |
28 | Line()
29 | {spriteX=curve=x=y=z=0;}
30 |
31 | void project(int camX,int camY,int camZ)
32 | {
33 | scale = camD/(z-camZ);
34 | X = (1 + scale*(x - camX)) * width/2;
35 | Y = (1 - scale*(y - camY)) * height/2;
36 | W = scale * roadW * width/2;
37 | }
38 |
39 | void drawSprite(RenderWindow &app)
40 | {
41 | Sprite s = sprite;
42 | int w = s.getTextureRect().width;
43 | int h = s.getTextureRect().height;
44 |
45 | float destX = X + scale * spriteX * width/2;
46 | float destY = Y + 4;
47 | float destW = w * W / 266;
48 | float destH = h * W / 266;
49 |
50 | destX += destW * spriteX; //offsetX
51 | destY += destH * (-1); //offsetY
52 |
53 | float clipH = destY+destH-clip;
54 | if (clipH<0) clipH=0;
55 |
56 | if (clipH>=destH) return;
57 | s.setTextureRect(IntRect(0,0,w,h-h*clipH/destH));
58 | s.setScale(destW/w,destH/h);
59 | s.setPosition(destX, destY);
60 | app.draw(s);
61 | }
62 | };
63 |
64 |
65 | int main()
66 | {
67 | RenderWindow app(VideoMode(width, height), "Outrun Racing!");
68 | app.setFramerateLimit(60);
69 |
70 | Texture t[50];
71 | Sprite object[50];
72 | for(int i=1;i<=7;i++)
73 | {
74 | t[i].loadFromFile("images/"+std::to_string(i)+".png");
75 | t[i].setSmooth(true);
76 | object[i].setTexture(t[i]);
77 | }
78 |
79 | Texture bg;
80 | bg.loadFromFile("images/bg.png");
81 | bg.setRepeated(true);
82 | Sprite sBackground(bg);
83 | sBackground.setTextureRect(IntRect(0,0,5000,411));
84 | sBackground.setPosition(-2000,0);
85 |
86 | std::vector lines;
87 |
88 | for(int i=0;i<1600;i++)
89 | {
90 | Line line;
91 | line.z = i*segL;
92 |
93 | if (i>300 && i<700) line.curve=0.5;
94 | if (i>1100) line.curve=-0.7;
95 |
96 | if (i<300 && i%20==0) {line.spriteX=-2.5; line.sprite=object[5];}
97 | if (i%17==0) {line.spriteX=2.0; line.sprite=object[6];}
98 | if (i>300 && i%20==0) {line.spriteX=-0.7; line.sprite=object[4];}
99 | if (i>800 && i%20==0) {line.spriteX=-1.2; line.sprite=object[1];}
100 | if (i==400) {line.spriteX=-1.2; line.sprite=object[7];}
101 |
102 | if (i>750) line.y = sin(i/30.0)*1500;
103 |
104 | lines.push_back(line);
105 | }
106 |
107 | int N = lines.size();
108 | float playerX = 0;
109 | int pos = 0;
110 | int H = 1500;
111 |
112 | while (app.isOpen())
113 | {
114 | Event e;
115 | while (app.pollEvent(e))
116 | {
117 | if (e.type == Event::Closed)
118 | app.close();
119 | }
120 |
121 | int speed=0;
122 |
123 | if (Keyboard::isKeyPressed(Keyboard::Right)) playerX+=0.1;
124 | if (Keyboard::isKeyPressed(Keyboard::Left)) playerX-=0.1;
125 | if (Keyboard::isKeyPressed(Keyboard::Up)) speed=200;
126 | if (Keyboard::isKeyPressed(Keyboard::Down)) speed=-200;
127 | if (Keyboard::isKeyPressed(Keyboard::Tab)) speed*=3;
128 | if (Keyboard::isKeyPressed(Keyboard::W)) H+=100;
129 | if (Keyboard::isKeyPressed(Keyboard::S)) H-=100;
130 |
131 | pos+=speed;
132 | while (pos >= N*segL) pos-=N*segL;
133 | while (pos < 0) pos += N*segL;
134 |
135 | app.clear(Color(105,205,4));
136 | app.draw(sBackground);
137 | int startPos = pos/segL;
138 | int camH = lines[startPos].y + H;
139 | if (speed>0) sBackground.move(-lines[startPos].curve*2,0);
140 | if (speed<0) sBackground.move( lines[startPos].curve*2,0);
141 |
142 | int maxy = height;
143 | float x=0,dx=0;
144 |
145 | ///////draw road////////
146 | for(int n = startPos; n=N?N*segL:0));
150 | x+=dx;
151 | dx+=l.curve;
152 |
153 | l.clip=maxy;
154 | if (l.Y>=maxy) continue;
155 | maxy = l.Y;
156 |
157 | Color grass = (n/3)%2?Color(16,200,16):Color(0,154,0);
158 | Color rumble = (n/3)%2?Color(255,255,255):Color(0,0,0);
159 | Color road = (n/3)%2?Color(107,107,107):Color(105,105,105);
160 |
161 | Line p = lines[(n-1)%N]; //previous line
162 |
163 | drawQuad(app, grass, 0, p.Y, width, 0, l.Y, width);
164 | drawQuad(app, rumble,p.X, p.Y, p.W*1.2, l.X, l.Y, l.W*1.2);
165 | drawQuad(app, road, p.X, p.Y, p.W, l.X, l.Y, l.W);
166 | }
167 |
168 | ////////draw objects////////
169 | for(int n=startPos+300; n>startPos; n--)
170 | lines[n%N].drawSprite(app);
171 |
172 | app.display();
173 | }
174 |
175 | return 0;
176 | }
177 |
--------------------------------------------------------------------------------
/11 NetWalk (Pipe Puzzle)/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | using namespace sf;
4 |
5 | const int N = 6;
6 | int ts = 54; //tile size
7 | Vector2f offset(65,55);
8 |
9 | Vector2i Up(0,-1);
10 | Vector2i Down(0,1);
11 | Vector2i Right(1,0);
12 | Vector2i Left(-1,0);
13 |
14 | Vector2i DIR[4] = {Up,Right,Down,Left};
15 |
16 | struct pipe
17 | {
18 | std::vector dirs;
19 | int orientation;
20 | float angle; bool on;
21 |
22 | pipe() {angle=0;}
23 |
24 | void rotate()
25 | {
26 | for(int i=0;i nodes;
50 | nodes.push_back(Vector2i(rand()%N,rand()%N));
51 |
52 | while(!nodes.empty())
53 | {
54 | int n = rand()%nodes.size();
55 | Vector2i v = nodes[n];
56 | Vector2i d = DIR[rand()%4];
57 |
58 | if (cell(v).dirs.size()==3) {nodes.erase(nodes.begin() + n); continue;}
59 | if (cell(v).dirs.size()==2) if (rand()%50) continue;
60 |
61 | bool complete=1;
62 | for(auto D:DIR)
63 | if (!isOut(v+D) && cell(v+D).dirs.empty()) complete=0;
64 | if (complete) {nodes.erase(nodes.begin() + n); continue; }
65 |
66 | if (isOut(v+d)) continue;
67 | if (!cell(v+d).dirs.empty()) continue;
68 | cell(v).dirs.push_back(d);
69 | cell(v+d).dirs.push_back(-d);
70 | nodes.push_back(v+d);
71 | }
72 | }
73 |
74 |
75 | void drop(Vector2i v)
76 | {
77 | if (cell(v).on) return;
78 | cell(v).on=true;
79 |
80 | for(auto d:DIR)
81 | if (!isOut(v+d))
82 | if (cell(v).isConnect(d) && cell(v+d).isConnect(-d))
83 | drop(v+d);
84 | }
85 |
86 |
87 | int main()
88 | {
89 | srand(time(0));
90 |
91 | RenderWindow app(VideoMode(390, 390), "The Pipe Puzzle!");
92 |
93 | Texture t1,t2,t3,t4;
94 | t1.loadFromFile("images/background.png");
95 | t2.loadFromFile("images/comp.png");
96 | t3.loadFromFile("images/server.png");
97 | t4.loadFromFile("images/pipes.png");
98 | t4.setSmooth(true);
99 |
100 | Sprite sBackground(t1), sComp(t2), sServer(t3), sPipe(t4);
101 | sPipe.setOrigin(27,27);
102 | sComp.setOrigin(18,18);
103 | sServer.setOrigin(20,20);
104 |
105 |
106 | generatePuzzle();
107 |
108 | for(int i=0;i0;n--) //find orientation//
114 | {
115 | std::string s="";
116 | for(auto d: DIR) s+=p.isConnect(d)? '1':'0';
117 | if (s=="0011" || s=="0111" || s=="0101" || s=="0010") p.orientation=n;
118 | p.rotate();
119 | }
120 |
121 | for(int n=0;np.orientation*90) p.angle=p.orientation*90;
168 |
169 | sPipe.setTextureRect(IntRect(ts*kind,0,ts,ts));
170 | sPipe.setRotation(p.angle);
171 | sPipe.setPosition(j*ts,i*ts);sPipe.move(offset);
172 | app.draw(sPipe);
173 |
174 | if (kind==1)
175 | { if (p.on) sComp.setTextureRect(IntRect(53,0,36,36));
176 | else sComp.setTextureRect(IntRect(0,0,36,36));
177 | sComp.setPosition(j*ts,i*ts);sComp.move(offset);
178 | app.draw(sComp);
179 | }
180 | }
181 |
182 | app.draw(sServer);
183 | app.display();
184 | }
185 |
186 | return 0;
187 | }
188 |
189 |
--------------------------------------------------------------------------------
/01 Tetris/.idea/workspace.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 | 1554457529545
82 |
83 |
84 | 1554457529545
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
--------------------------------------------------------------------------------
/16 Asteroids/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | using namespace sf;
5 |
6 | const int W = 1200;
7 | const int H = 800;
8 |
9 | float DEGTORAD = 0.017453f;
10 |
11 | class Animation
12 | {
13 | public:
14 | float Frame, speed;
15 | Sprite sprite;
16 | std::vector frames;
17 |
18 | Animation(){}
19 |
20 | Animation (Texture &t, int x, int y, int w, int h, int count, float Speed)
21 | {
22 | Frame = 0;
23 | speed = Speed;
24 |
25 | for (int i=0;i= n) Frame -= n;
39 | if (n>0) sprite.setTextureRect( frames[int(Frame)] );
40 | }
41 |
42 | bool isEnd()
43 | {
44 | return Frame+speed>=frames.size();
45 | }
46 |
47 | };
48 |
49 |
50 | class Entity
51 | {
52 | public:
53 | float x,y,dx,dy,R,angle;
54 | bool life;
55 | std::string name;
56 | Animation anim;
57 |
58 | Entity()
59 | {
60 | life=1;
61 | }
62 |
63 | void settings(Animation &a,int X,int Y,float Angle=0,int radius=1)
64 | {
65 | anim = a;
66 | x=X; y=Y;
67 | angle = Angle;
68 | R = radius;
69 | }
70 |
71 | virtual void update(){};
72 |
73 | void draw(RenderWindow &app)
74 | {
75 | anim.sprite.setPosition(x,y);
76 | anim.sprite.setRotation(angle+90);
77 | app.draw(anim.sprite);
78 |
79 | CircleShape circle(R);
80 | circle.setFillColor(Color(255,0,0,170));
81 | circle.setPosition(x,y);
82 | circle.setOrigin(R,R);
83 | //app.draw(circle);
84 | }
85 |
86 | virtual ~Entity(){};
87 | };
88 |
89 |
90 | class asteroid: public Entity
91 | {
92 | public:
93 | asteroid()
94 | {
95 | dx=rand()%8-4;
96 | dy=rand()%8-4;
97 | name="asteroid";
98 | }
99 |
100 | void update()
101 | {
102 | x+=dx;
103 | y+=dy;
104 |
105 | if (x>W) x=0; if (x<0) x=W;
106 | if (y>H) y=0; if (y<0) y=H;
107 | }
108 |
109 | };
110 |
111 |
112 |
113 | class bullet: public Entity
114 | {
115 | public:
116 | bullet()
117 | {
118 | name="bullet";
119 | }
120 |
121 | void update()
122 | {
123 | dx=cos(angle*DEGTORAD)*6;
124 | dy=sin(angle*DEGTORAD)*6;
125 | // angle+=rand()%6-3;
126 | x+=dx;
127 | y+=dy;
128 |
129 | if (x>W || x<0 || y>H || y<0) life=0;
130 | }
131 |
132 | };
133 |
134 |
135 | class player: public Entity
136 | {
137 | public:
138 | bool thrust;
139 |
140 | player()
141 | {
142 | name="player";
143 | }
144 |
145 | void update()
146 | {
147 | if (thrust)
148 | { dx+=cos(angle*DEGTORAD)*0.2;
149 | dy+=sin(angle*DEGTORAD)*0.2; }
150 | else
151 | { dx*=0.99;
152 | dy*=0.99; }
153 |
154 | int maxSpeed=15;
155 | float speed = sqrt(dx*dx+dy*dy);
156 | if (speed>maxSpeed)
157 | { dx *= maxSpeed/speed;
158 | dy *= maxSpeed/speed; }
159 |
160 | x+=dx;
161 | y+=dy;
162 |
163 | if (x>W) x=0; if (x<0) x=W;
164 | if (y>H) y=0; if (y<0) y=H;
165 | }
166 |
167 | };
168 |
169 |
170 | bool isCollide(Entity *a,Entity *b)
171 | {
172 | return (b->x - a->x)*(b->x - a->x)+
173 | (b->y - a->y)*(b->y - a->y)<
174 | (a->R + b->R)*(a->R + b->R);
175 | }
176 |
177 |
178 | int main()
179 | {
180 | srand(time(0));
181 |
182 | RenderWindow app(VideoMode(W, H), "Asteroids!");
183 | app.setFramerateLimit(60);
184 |
185 | Texture t1,t2,t3,t4,t5,t6,t7;
186 | t1.loadFromFile("images/spaceship.png");
187 | t2.loadFromFile("images/background.jpg");
188 | t3.loadFromFile("images/explosions/type_C.png");
189 | t4.loadFromFile("images/rock.png");
190 | t5.loadFromFile("images/fire_blue.png");
191 | t6.loadFromFile("images/rock_small.png");
192 | t7.loadFromFile("images/explosions/type_B.png");
193 |
194 | t1.setSmooth(true);
195 | t2.setSmooth(true);
196 |
197 | Sprite background(t2);
198 |
199 | Animation sExplosion(t3, 0,0,256,256, 48, 0.5);
200 | Animation sRock(t4, 0,0,64,64, 16, 0.2);
201 | Animation sRock_small(t6, 0,0,64,64, 16, 0.2);
202 | Animation sBullet(t5, 0,0,32,64, 16, 0.8);
203 | Animation sPlayer(t1, 40,0,40,40, 1, 0);
204 | Animation sPlayer_go(t1, 40,40,40,40, 1, 0);
205 | Animation sExplosion_ship(t7, 0,0,192,192, 64, 0.5);
206 |
207 |
208 | std::list entities;
209 |
210 | for(int i=0;i<15;i++)
211 | {
212 | asteroid *a = new asteroid();
213 | a->settings(sRock, rand()%W, rand()%H, rand()%360, 25);
214 | entities.push_back(a);
215 | }
216 |
217 | player *p = new player();
218 | p->settings(sPlayer,200,200,0,20);
219 | entities.push_back(p);
220 |
221 | /////main loop/////
222 | while (app.isOpen())
223 | {
224 | Event event;
225 | while (app.pollEvent(event))
226 | {
227 | if (event.type == Event::Closed)
228 | app.close();
229 |
230 | if (event.type == Event::KeyPressed)
231 | if (event.key.code == Keyboard::Space)
232 | {
233 | bullet *b = new bullet();
234 | b->settings(sBullet,p->x,p->y,p->angle,10);
235 | entities.push_back(b);
236 | }
237 | }
238 |
239 | if (Keyboard::isKeyPressed(Keyboard::Right)) p->angle+=3;
240 | if (Keyboard::isKeyPressed(Keyboard::Left)) p->angle-=3;
241 | if (Keyboard::isKeyPressed(Keyboard::Up)) p->thrust=true;
242 | else p->thrust=false;
243 |
244 |
245 |
246 | for(auto a:entities)
247 | for(auto b:entities)
248 | {
249 | if (a->name=="asteroid" && b->name=="bullet")
250 | if ( isCollide(a,b) )
251 | {
252 | a->life=false;
253 | b->life=false;
254 |
255 | Entity *e = new Entity();
256 | e->settings(sExplosion,a->x,a->y);
257 | e->name="explosion";
258 | entities.push_back(e);
259 |
260 |
261 | for(int i=0;i<2;i++)
262 | {
263 | if (a->R==15) continue;
264 | Entity *e = new asteroid();
265 | e->settings(sRock_small,a->x,a->y,rand()%360,15);
266 | entities.push_back(e);
267 | }
268 |
269 | }
270 |
271 | if (a->name=="player" && b->name=="asteroid")
272 | if ( isCollide(a,b) )
273 | {
274 | b->life=false;
275 |
276 | Entity *e = new Entity();
277 | e->settings(sExplosion_ship,a->x,a->y);
278 | e->name="explosion";
279 | entities.push_back(e);
280 |
281 | p->settings(sPlayer,W/2,H/2,0,20);
282 | p->dx=0; p->dy=0;
283 | }
284 | }
285 |
286 |
287 | if (p->thrust) p->anim = sPlayer_go;
288 | else p->anim = sPlayer;
289 |
290 |
291 | for(auto e:entities)
292 | if (e->name=="explosion")
293 | if (e->anim.isEnd()) e->life=0;
294 |
295 | if (rand()%150==0)
296 | {
297 | asteroid *a = new asteroid();
298 | a->settings(sRock, 0,rand()%H, rand()%360, 25);
299 | entities.push_back(a);
300 | }
301 |
302 | for(auto i=entities.begin();i!=entities.end();)
303 | {
304 | Entity *e = *i;
305 |
306 | e->update();
307 | e->anim.update();
308 |
309 | if (e->life==false) {i=entities.erase(i); delete e;}
310 | else i++;
311 | }
312 |
313 |
314 |
315 | //////draw//////
316 | app.draw(background);
317 |
318 | for(auto i:entities)
319 | i->draw(app);
320 |
321 | app.display();
322 | }
323 |
324 | return 0;
325 | }
326 |
--------------------------------------------------------------------------------