本文主要是介绍Let's make 16 games in C++(十):Xonix,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1、准备游戏需要的背景
2、先渲染一个25x40乘比例(18)的屏幕
#include<SFML/Graphics.hpp>
#include<time.h>using namespace sf;const int M = 25;
const int N = 40;int grid[M][N] = { 0 };
int ts = 18;int main()
{srand(time(0));RenderWindow window(VideoMode(N*ts, M*ts), "Xonix!");window.setFramerateLimit(60);Texture t1;t1.loadFromFile("./Resources/images/tiles.png");Sprite sTile(t1);int x = 0;int y = 0;int dx = 0;int dy = 0;float timer = 0;float delay = 0.07;Clock clock;while (window.isOpen()){float time = clock.getElapsedTime().asSeconds();clock.restart();timer += time;Event e;while (window.pollEvent(e)){if (e.type == Event::Closed){window.close();}}if (Keyboard::isKeyPressed(Keyboard::Left)){dx = -1;dy = 0;}if (Keyboard::isKeyPressed(Keyboard::Right)){dx = 1;dy = 0;}if (Keyboard::isKeyPressed(Keyboard::Up)){dx = 0;dy = -1;}if (Keyboard::isKeyPressed(Keyboard::Down)){dx = 0;dy = 1;}if (timer > delay){x += dx;y += dy;timer = 0;}window.clear();sTile.setTextureRect(IntRect(36, 0, ts, ts));sTile.setPosition(x*ts, y*ts);window.draw(sTile);window.display();}return 0;
}
3、实现游戏基本逻辑
#include<SFML/Graphics.hpp>
#include<time.h>using namespace sf;const int M = 25;
const int N = 40;int grid[M][N] = { 0 };
int ts = 18;int main()
{srand(time(0));RenderWindow window(VideoMode(N*ts, M*ts), "Xonix!");window.setFramerateLimit(60);Texture t1;Texture t2;t1.loadFromFile("./Resources/images/tiles.png");t2.loadFromFile("./Resources/images/gameover.png");Sprite sTile(t1);Sprite sGameover(t2);sGameover.setPosition(100, 100);bool Game = true;int x = 0;int y = 0;int dx = 0;int dy = 0;float timer = 0;float delay = 0.07;Clock clock;for (int i = 0; i < M; i++){for (int j = 0; j < N; j++){if (i == 0 || j == 0 || i == M-1 || j == N-1){grid[i][j] = 1;}}}while (window.isOpen()){float time = clock.getElapsedTime().asSeconds();clock.restart();timer += time;Event e;while (window.pollEvent(e)){if (e.type == Event::Closed){window.close();}}if (Keyboard::isKeyPressed(Keyboard::Left)){dx = -1;dy = 0;}if (Keyboard::isKeyPressed(Keyboard::Right)){dx = 1;dy = 0;}if (Keyboard::isKeyPressed(Keyboard::Up)){dx = 0;dy = -1;}if (Keyboard::isKeyPressed(Keyboard::Down)){dx = 0;dy = 1;}if (!Game){continue;}if (timer > delay){x += dx;y += dy;if (x<0){x = 0;}if (x>N-1){x = N - 1;}if (y<0){y = 0;}if (y>M-1){y = M - 1;}if (grid[y][x] == 2){Game = false;}if (grid[y][x] == 0){grid[y][x] = 2;}timer = 0;}window.clear();for (int i = 0; i < M; i++){for (int j = 0; j < N; j++){if (grid[i][j] == 0){continue;}if (grid[i][j] == 1){sTile.setTextureRect(IntRect(0, 0, ts, ts));}if (grid[i][j] == 2){sTile.setTextureRect(IntRect(54, 0, ts, ts));}sTile.setPosition(j*ts,i*ts);window.draw(sTile);}}sTile.setTextureRect(IntRect(36, 0, ts, ts));sTile.setPosition(x*ts, y*ts);window.draw(sTile);if (!Game){window.draw(sGameover);}window.display();}return 0;
}
效果图:
4、添加敌人缩小游戏区域完善游戏逻辑(在敌人撞击之前变成蓝色)
#include<SFML/Graphics.hpp>
#include<time.h>using namespace sf;const int M = 25;
const int N = 40;int grid[M][N] = { 0 };
int ts = 18;struct Enemy
{int x;int y;int dx;int dy;Enemy(){x = y = 300;dx = 4 - rand() % 8;dy = 4 - rand() % 8;}void move(){x += dx;if (grid[y/ts][x/ts] == 1){dx = -dx;x += dx;}y += dy;if (grid[y/ts][x/ts] == 1){dy = -dy;y += dy;}}};void drop(int y, int x)
{if (grid[y][x] == 0){grid[y][x] = -1;}if (grid[y-1][x] == 0){drop(y - 1, x);}if (grid[y+1][x] == 0){drop(y + 1, x);}if (grid[y][x-1] == 0){drop(y, x - 1);}if (grid[y][x+1] == 0){drop(y, x + 1);}
}int main()
{srand(time(0));RenderWindow window(VideoMode(N*ts, M*ts), "Xonix!");window.setFramerateLimit(60);Texture t1;Texture t2;Texture t3;t1.loadFromFile("./Resources/images/tiles.png");t2.loadFromFile("./Resources/images/gameover.png");t3.loadFromFile("./Resources/images/enemy.png");Sprite sTile(t1);Sprite sGameover(t2);Sprite sEnemy(t3);sGameover.setPosition(100, 100);sEnemy.setOrigin(20, 20);int enemyCount = 4;Enemy a[10];bool Game = true;int x = 0;int y = 0;int dx = 0;int dy = 0;float timer = 0;float delay = 0.07;Clock clock;for (int i = 0; i < M; i++){for (int j = 0; j < N; j++){if (i == 0 || j == 0 || i == M-1 || j == N-1){grid[i][j] = 1;}}}while (window.isOpen()){float time = clock.getElapsedTime().asSeconds();clock.restart();timer += time;Event e;while (window.pollEvent(e)){if (e.type == Event::Closed){window.close();}if (e.key.code == Keyboard::Escape){for (int i = 0; i < M-1; i++){for (int j = 0; j < N-1; j++){grid[i][j] = 0;}}x = 10;y = 0;Game = true;}}if (Keyboard::isKeyPressed(Keyboard::Left)){dx = -1;dy = 0;}if (Keyboard::isKeyPressed(Keyboard::Right)){dx = 1;dy = 0;}if (Keyboard::isKeyPressed(Keyboard::Up)){dx = 0;dy = -1;}if (Keyboard::isKeyPressed(Keyboard::Down)){dx = 0;dy = 1;}if (!Game){continue;}if (timer > delay){x += dx;y += dy;if (x<0){x = 0;}if (x>N-1){x = N - 1;}if (y<0){y = 0;}if (y>M-1){y = M - 1;}if (grid[y][x] == 2){Game = false;}if (grid[y][x] == 0){grid[y][x] = 2;}timer = 0;}for (int i = 0; i < enemyCount; i++){a[i].move();}if (grid[y][x] == 1){dx = dy = 0;for (int i = 0; i < enemyCount; i++){drop(a[i].y / ts, a[i].x / ts);}for (int i = 0; i < M; i++){for (int j = 0; j < N; j++){if (grid[i][j] == -1){grid[i][j] = 0;}else{grid[i][j] = 1;}}}}for (int i = 0; i < enemyCount; i++){if (grid[a[i].y/ts][a[i].x/ts] == 2){Game = false;}}window.clear();for (int i = 0; i < M; i++){for (int j = 0; j < N; j++){if (grid[i][j] == 0){continue;}if (grid[i][j] == 1){sTile.setTextureRect(IntRect(0, 0, ts, ts));}if (grid[i][j] == 2){sTile.setTextureRect(IntRect(54, 0, ts, ts));}sTile.setPosition(j*ts,i*ts);window.draw(sTile);}}sTile.setTextureRect(IntRect(36, 0, ts, ts));sTile.setPosition(x*ts, y*ts);window.draw(sTile);sEnemy.rotate(10);for (int i = 0; i < enemyCount; i++){sEnemy.setPosition(a[i].x, a[i].y);window.draw(sEnemy);}if (!Game){window.draw(sGameover);}window.display();}return 0;
}
结果图:
这篇关于Let's make 16 games in C++(十):Xonix的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!