1、准备游戏需要的背景图片

//shader.frag
uniform vec2 frag_LightOrigin;
uniform vec3 frag_LightColor;
uniform float frag_LightAttenuation;
uniform vec2 frag_ScreenResolution;
uniform sampler2D texture;
void main()
{       vec2 baseDistance =  gl_FragCoord.xy; baseDistance.y = frag_ScreenResolution.y-baseDistance.y; float d = length(frag_LightOrigin - baseDistance); float a = 1.0/(frag_LightAttenuation * d);    vec4 color = vec4(a,a,a,1.0) * vec4(frag_LightColor, 1.0);  vec4 t = texture2D(texture, gl_TexCoord[0].xy);if (t[0]>color[0]) color[0]=t[0];if (t[1]>color[1]) color[1]=t[1];if (t[2]>color[2]) color[2]=t[2];gl_FragColor=color;
}

2、绘制一个600x400的屏幕

#include<SFML/Graphics.hpp>
#include<time.h>using namespace sf;const int W = 600;
const int H = 400;
int speed = 4;
bool field[W][H] = { 0 };struct player
{int x;int y;int dir;Color color;player(Color c){x = rand() % W;y = rand() % H;color = c;dir = rand() % 4;}void tick(){if (dir == 0){y += 1;}if (dir == 1){x -= 1;}if (dir == 2){x += 1;}if (dir == 3){y -= 1;}}Vector3f getColor(){return Vector3f(color.r, color.g, color.b);}};int main()
{srand(time(0));RenderWindow window(VideoMode(W, H), "The Tron Game!");window.setFramerateLimit(60);Texture texture;texture.loadFromFile("./Resources/images/background.jpg");Sprite sBackground(texture);player p1(Color::Red);player p2(Color::Green);Sprite sprite;RenderTexture t;t.create(W, H);t.setSmooth(true);sprite.setTexture(t.getTexture());t.clear();t.draw(sBackground);bool Game = 1;while (window.isOpen()){Event e;while (window.pollEvent(e)){if (e.type == Event::Closed){window.close();}}if (Keyboard::isKeyPressed(Keyboard::Left)){if (p1.dir != 2){p1.dir = 1;}}if (Keyboard::isKeyPressed(Keyboard::Right)){if (p1.dir != 1){p1.dir = 2;}}if (Keyboard::isKeyPressed(Keyboard::Up)){if (p1.dir != 0){p1.dir = 3;}}if (Keyboard::isKeyPressed(Keyboard::Down)){if (p1.dir != 3){p1.dir = 0;}}if (Keyboard::isKeyPressed(Keyboard::A)){if (p2.dir != 2){p2.dir = 1;}}if (Keyboard::isKeyPressed(Keyboard::D)){if (p2.dir != 1){p2.dir = 2;}}if (Keyboard::isKeyPressed(Keyboard::W)){if (p2.dir != 0){p2.dir = 3;}}if (Keyboard::isKeyPressed(Keyboard::S)){if (p2.dir != 3){p2.dir = 0;}}if (!Game){continue;}for (int i = 0; i < speed; i++){p1.tick();p2.tick();if (field[p1.x][p1.y] == 1){Game = 0;}if (field[p2.x][p2.y] == 1){Game = 0;}field[p1.x][p1.y] = 1;field[p2.x][p2.y] = 1;CircleShape c(3);c.setPosition(p1.x, p2.y);c.setFillColor(p1.color);t.draw(c);c.setPosition(p2.x, p2.y);c.setFillColor(p2.color);t.draw(c);t.display();}//drawwindow.clear();window.draw(sprite);window.display();}return 0;
}

效果图:

3、添加文字:

#include<SFML/Graphics.hpp>
#include<time.h>using namespace sf;const int W = 600;
const int H = 400;
int speed = 4;
bool field[W][H] = { 0 };struct player
{int x;int y;int dir;Color color;player(Color c){x = rand() % W;y = rand() % H;color = c;dir = rand() % 4;}void tick(){if (dir == 0){y += 1;}if (dir == 1){x -= 1;}if (dir == 2){x += 1;}if (dir == 3){y -= 1;}if (x >= W){x = 0;}if (x < 0){x = W - 1;}if (y >= H){y = 0;}if (y < 0){y = H - 1;}}Vector3f getColor(){return Vector3f(color.r, color.g, color.b);}};int main()
{srand(time(0));RenderWindow window(VideoMode(W, H), "The Tron Game!");window.setFramerateLimit(60);Texture texture;texture.loadFromFile("./Resources/images/background.jpg");Sprite sBackground(texture);player p1(Color::Red);player p2(Color::Green);Sprite sprite;RenderTexture t;t.create(W, H);t.setSmooth(true);sprite.setTexture(t.getTexture());t.clear();t.draw(sBackground);Font font;font.loadFromFile("./Resources/font/arial.ttf");Text text("YOU WIN!", font, 35);text.setPosition(W / 2 - 80, 20);bool Game = 1;while (window.isOpen()){Event e;while (window.pollEvent(e)){if (e.type == Event::Closed){window.close();}}if (Keyboard::isKeyPressed(Keyboard::Left)){if (p1.dir != 2){p1.dir = 1;}}if (Keyboard::isKeyPressed(Keyboard::Right)){if (p1.dir != 1){p1.dir = 2;}}if (Keyboard::isKeyPressed(Keyboard::Up)){if (p1.dir != 0){p1.dir = 3;}}if (Keyboard::isKeyPressed(Keyboard::Down)){if (p1.dir != 3){p1.dir = 0;}}if (Keyboard::isKeyPressed(Keyboard::A)){if (p2.dir != 2){p2.dir = 1;}}if (Keyboard::isKeyPressed(Keyboard::D)){if (p2.dir != 1){p2.dir = 2;}}if (Keyboard::isKeyPressed(Keyboard::W)){if (p2.dir != 0){p2.dir = 3;}}if (Keyboard::isKeyPressed(Keyboard::S)){if (p2.dir != 3){p2.dir = 0;}}if (!Game){window.draw(text);window.display();continue;}for (int i = 0; i < speed; i++){p1.tick();p2.tick();if (field[p1.x][p1.y] == 1){Game = 0;text.setColor(p2.color);}if (field[p2.x][p2.y] == 1){Game = 0;text.setColor(p1.color);}field[p1.x][p1.y] = 1;field[p2.x][p2.y] = 1;CircleShape c(3);c.setPosition(p1.x, p2.y);c.setFillColor(p1.color);t.draw(c);c.setPosition(p2.x, p2.y);c.setFillColor(p2.color);t.draw(c);t.display();}//drawwindow.clear();window.draw(sprite);window.display();}return 0;
}

效果图:

4、添加着色器完善游戏逻辑

#include<SFML/Graphics.hpp>
#include<time.h>using namespace sf;const int W = 600;
const int H = 400;
int speed = 4;
bool field[W][H] = { 0 };struct player
{int x;int y;int dir;Color color;player(Color c){x = rand() % W;y = rand() % H;color = c;dir = rand() % 4;}void tick(){if (dir == 0){y += 1;}if (dir == 1){x -= 1;}if (dir == 2){x += 1;}if (dir == 3){y -= 1;}if (x >= W){x = 0;}if (x < 0){x = W - 1;}if (y >= H){y = 0;}if (y < 0){y = H - 1;}}Vector3f getColor(){return Vector3f(color.r, color.g, color.b);}};int main()
{srand(time(0));RenderWindow window(VideoMode(W, H), "The Tron Game!");window.setFramerateLimit(60);Texture texture;texture.loadFromFile("./Resources/images/background.jpg");Sprite sBackground(texture);player p1(Color::Red);player p2(Color::Green);Sprite sprite;RenderTexture t;t.create(W, H);t.setSmooth(true);sprite.setTexture(t.getTexture());t.clear();t.draw(sBackground);Font font;font.loadFromFile("./Resources/font/arial.ttf");Text text("YOU WIN!", font, 35);text.setPosition(W / 2 - 80, 20);Shader *shader = new Shader;shader->loadFromFile("./Resources/files/shader.frag", Shader::Fragment);shader->setParameter("frag_ScreenResolution", Vector2f(W, H));shader->setParameter("frag_LightAttenuation", 100);RenderStates states;states.shader = shader;bool Game = 1;while (window.isOpen()){Event e;while (window.pollEvent(e)){if (e.type == Event::Closed){window.close();}}if (Keyboard::isKeyPressed(Keyboard::Left)){if (p1.dir != 2){p1.dir = 1;}}if (Keyboard::isKeyPressed(Keyboard::Right)){if (p1.dir != 1){p1.dir = 2;}}if (Keyboard::isKeyPressed(Keyboard::Up)){if (p1.dir != 0){p1.dir = 3;}}if (Keyboard::isKeyPressed(Keyboard::Down)){if (p1.dir != 3){p1.dir = 0;}}if (Keyboard::isKeyPressed(Keyboard::A)){if (p2.dir != 2){p2.dir = 1;}}if (Keyboard::isKeyPressed(Keyboard::D)){if (p2.dir != 1){p2.dir = 2;}}if (Keyboard::isKeyPressed(Keyboard::W)){if (p2.dir != 0){p2.dir = 3;}}if (Keyboard::isKeyPressed(Keyboard::S)){if (p2.dir != 3){p2.dir = 0;}}if (!Game){window.draw(text);window.display();continue;}for (int i = 0; i < speed; i++){p1.tick();p2.tick();if (field[p1.x][p1.y] == 1){Game = 0;text.setColor(p2.color);}if (field[p2.x][p2.y] == 1){Game = 0;text.setColor(p1.color);}field[p1.x][p1.y] = 1;field[p2.x][p2.y] = 1;t.display();shader->setParameter("frag_LightOrigin", Vector2f(p1.x, p1.y));shader->setParameter("frag_LightColor", p1.getColor());t.draw(sprite, states);shader->setParameter("frag_LightOrigin", Vector2f(p2.x, p2.y));shader->setParameter("frag_LightColor", p2.getColor());t.draw(sprite, states);}//drawwindow.clear();window.draw(sprite);window.display();}return 0;
}

结果图:

PS:如果出现关于setColor的错误:Project Properties > Configuration Properties > C/C++ > General > SDL checks选否

Let's make 16 games in C++(十四):Tron相关推荐

  1. Let's make 16 games in C++(十六):Volleyball

    1.首先配置Box2D的VS环境 ①在官网或GitHub上下载Box2D的源码,http://www.box2d.org,https://github.com/erincatto/Box2D ②在官网 ...

  2. 飞思卡尔16位单片机(十四)—— CAN总线模块测试

    一.CAN模块介绍 这个实验我们来研究XEP100单片机内部的CAN模块. XEP100单片机的CAN的基本特性如下: • 实施CAN协议-2.0A/B版 - 标准和扩展数据帧 - 0-8字节数据长度 ...

  3. 编译《视觉SLAM十四讲》ch5里joinmap出现 ***/anaconda3/lib/libpng16.so.16:‘inflateValidate@ZLIB_1.2.9’未定义的引用

    编译<视觉SLAM十四讲>ch5里joinmap,make时出现//home/yjbing11/anaconda3/lib/libpng16.so.16:对'inflateValidate ...

  4. 视觉SLAM十四讲从理论到实践第二版源码调试笔记(理论基础1-6章)

    2019-2020-2学期机器人工程专业需要开设SLAM技术课程,使用教材为视觉SLAM十四讲从理论到实践第二版. 为方便学生学习课程知识,将Arduino.ROS1.ROS2和SLAM集成到课程定制 ...

  5. slam十四讲-ubuntu20安装opencv3.4.16

    slam十四讲-ubuntu20按照opencv3.4.16 slam十四讲-ubuntu20安装opencv3.4.16 opencv 下载 依赖库的安装 编译安装 遇到的问题 slam十四讲-ub ...

  6. 孙鑫mfc学习笔记第十四课

    第十四课 网络的相关知识,网络程序的编写,Socket是连接应用程序与网络驱动程序的桥梁,Socket在应用程序中创建,通过bind与驱动程序建立关系.此后,应用程序送给Socket的数据,由Sock ...

  7. 谭浩强《C++程序设计》书后习题 第十三章-第十四章

    2019独角兽企业重金招聘Python工程师标准>>> 最近要复习一下C和C++的基础知识,于是计划把之前学过的谭浩强的<C程序设计>和<C++程序设计>习题 ...

  8. WCF技术剖析之十四:泛型数据契约和集合数据契约(下篇)

    [爱心链接:拯救一个25岁身患急性白血病的女孩[内有苏州电视台经济频道<天天山海经>为此录制的节目视频(苏州话)]]在.NET中,所有的集合都实现了IEnumerable接口,比如Arra ...

  9. 十五天精通WCF——第十四天 一起聊聊FaultException

    原文:十五天精通WCF--第十四天 一起聊聊FaultException  我们在玩web编程的时候,可能你会不经意的见到一些http500的错误,我想你应该不会陌生的,原因你应该也知道,服务器异常嘛 ...

最新文章

  1. 3.db2性能和优化
  2. jsp mysql 注入攻击实例
  3. 隐藏tomcat页面异常显示的版本信息
  4. QPixmap、QImage、QPicture、QBitmap四者区别
  5. java一维数组排序
  6. 【JQuery】on/off 绑定事件和解绑事件
  7. ASP.NET Core 网站运行时修改设置如何自动生效
  8. php文件的作用,php入口文件的作用-PHP问题
  9. C/C++ OpenCV滑动条的创建与使用
  10. 编程猫获新一轮 3 亿融资:做好少儿编程教育基础设施!
  11. Webrtc demo system
  12. 【ArcGIS微课1000例】0033:地图点状标记符号设计教程
  13. 基于天天动听API开发在线音乐查询网站
  14. 《数据结构》实验三:单链表
  15. python不区分大小写的列表比较_关于列表:不区分大小写’in’ – Python
  16. 复旦大学与国网上海共建“电力大数据实验室”
  17. linux SIGABRT信号
  18. HANA XS Administration Tool登录参数设置
  19. 大鹅模拟器 for Mac休闲模拟游戏
  20. Unity Shader之燃烧消散效果

热门文章

  1. vcpkg安装freeglut(起因:#include <GL/glut.h>无法识别)
  2. ADS学习:LineCalc使用说明
  3. 基于Vue开发的D2-Admin框架使用方法
  4. windows的一些装B用法
  5. 世界500强高管都在用的GROW模型,到底怎么操作?
  6. 2022年技能大赛“网络安全”(中职组)D模块电子答题卡
  7. 酷派n3c有没有英文语言,联发科CPU的手机便宜的多,21克M2C、酷派锋尚N3C配置、报价对比...
  8. 考研逻辑课程学习笔记(二)
  9. 一定要讲给孩子的20个故事
  10. windows wds linux,Syslinux+WDS+MDT2010部署Windows、Linux