Let's make 16 games in C++(十):Xonix

2024-01-03 10:50
文章标签 c++ 16 make games let xonix

本文主要是介绍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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/565453

相关文章

C++11范围for初始化列表auto decltype详解

《C++11范围for初始化列表autodecltype详解》C++11引入auto类型推导、decltype类型推断、统一列表初始化、范围for循环及智能指针,提升代码简洁性、类型安全与资源管理效... 目录C++11新特性1. 自动类型推导auto1.1 基本语法2. decltype3. 列表初始化3

C++11右值引用与Lambda表达式的使用

《C++11右值引用与Lambda表达式的使用》C++11引入右值引用,实现移动语义提升性能,支持资源转移与完美转发;同时引入Lambda表达式,简化匿名函数定义,通过捕获列表和参数列表灵活处理变量... 目录C++11新特性右值引用和移动语义左值 / 右值常见的左值和右值移动语义移动构造函数移动复制运算符

C++中detach的作用、使用场景及注意事项

《C++中detach的作用、使用场景及注意事项》关于C++中的detach,它主要涉及多线程编程中的线程管理,理解detach的作用、使用场景以及注意事项,对于写出高效、安全的多线程程序至关重要,下... 目录一、什么是join()?它的作用是什么?类比一下:二、join()的作用总结三、join()怎么

C++中全局变量和局部变量的区别

《C++中全局变量和局部变量的区别》本文主要介绍了C++中全局变量和局部变量的区别,全局变量和局部变量在作用域和生命周期上有显著的区别,下面就来介绍一下,感兴趣的可以了解一下... 目录一、全局变量定义生命周期存储位置代码示例输出二、局部变量定义生命周期存储位置代码示例输出三、全局变量和局部变量的区别作用域

C++中assign函数的使用

《C++中assign函数的使用》在C++标准模板库中,std::list等容器都提供了assign成员函数,它比操作符更灵活,支持多种初始化方式,下面就来介绍一下assign的用法,具有一定的参考价... 目录​1.assign的基本功能​​语法​2. 具体用法示例​​​(1) 填充n个相同值​​(2)

c++ 类成员变量默认初始值的实现

《c++类成员变量默认初始值的实现》本文主要介绍了c++类成员变量默认初始值,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录C++类成员变量初始化c++类的变量的初始化在C++中,如果使用类成员变量时未给定其初始值,那么它将被

C++中NULL与nullptr的区别小结

《C++中NULL与nullptr的区别小结》本文介绍了C++编程中NULL与nullptr的区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编... 目录C++98空值——NULLC++11空值——nullptr区别对比示例 C++98空值——NUL

C++ Log4cpp跨平台日志库的使用小结

《C++Log4cpp跨平台日志库的使用小结》Log4cpp是c++类库,本文详细介绍了C++日志库log4cpp的使用方法,及设置日志输出格式和优先级,具有一定的参考价值,感兴趣的可以了解一下... 目录一、介绍1. log4cpp的日志方式2.设置日志输出的格式3. 设置日志的输出优先级二、Window

Go语言中make和new的区别及说明

《Go语言中make和new的区别及说明》:本文主要介绍Go语言中make和new的区别及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1 概述2 new 函数2.1 功能2.2 语法2.3 初始化案例3 make 函数3.1 功能3.2 语法3.3 初始化

从入门到精通C++11 <chrono> 库特性

《从入门到精通C++11<chrono>库特性》chrono库是C++11中一个非常强大和实用的库,它为时间处理提供了丰富的功能和类型安全的接口,通过本文的介绍,我们了解了chrono库的基本概念... 目录一、引言1.1 为什么需要<chrono>库1.2<chrono>库的基本概念二、时间段(Durat