讲道理,半节课没上,在搞这个小游戏,直接上代码:

玩法:

  • 就是个简单的吃金币的游戏,成功吃到以后人(会跳的小方块)会随机变一个颜色
  • 金币每次到最左边(没吃到)以后会返回最右边,同时移动速度会随机变化
  • w键跳跃,s键让金币停止移动,m键让金币恢复运动,q退出游戏
  • 无聊的小游戏,别玩太久。
  • 原理代码注释里写挺多的,不再赘述了。
  • UI设计的比较丑,大家可以自己改一下代码里的颜色(doge
#define FREEGLUT_STATIC
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <GL/freeglut.h>
#include <iostream>
#include <cstdlib>
typedef struct { GLfloat x, y; } point; // define a 2D point
point p0 = { 400,200 }; // set initial co-ordinate values of the point  金币
point p1 = { 0,50 }; // set initial co-ordinate values of the point  小人
GLfloat finishx = 300; // declare position of vertical axis//Task 2
GLfloat jump = 0; //跳跃步长
GLfloat step = 10; //移动步长
GLint count = 0;
int time_interval = 16; // declare refresh interval in ms
void when_in_mainloop()
{ // idle callback functionglutPostRedisplay(); // force OpenGL to redraw the current window
}bool checkHug() {if ((p0.x >= 0 && p0.x <= 20 )&& (p1.y + 20 >= 200)) {return 1;}else return 0;
}void OnTimer(int value)
{   p0.x -= step; //移动步长if (p0.x >= 600)p0.x = 0;else if (p0.x <= 0) {p0.x = 599;step = rand()%10+5;//5以上的2位数}//反向则从另一侧刷新bool check = 0;check = checkHug();if (check == 1) {p0.x = 400; count++;}glutTimerFunc(time_interval, OnTimer, 1);
}bool flag = 0;void JumpOnTimer(int value) {if (flag==0 && p1.y <= 200) {p1.y += jump;if (p1.y == 200) {flag = 1;}}if (flag == 1 && p1.y>=50) {p1.y -= jump;if (p1.y ==50) {flag = 0;jump = 0;}}glutTimerFunc(time_interval, JumpOnTimer, 2);
}//Task 3
void keyboard_input(unsigned char key, int x, int y) // keyboard interactions
{if (key == 'q' || key == 'Q')exit(0);else if (key == 'w' || key == 'W') // change direction of movement{jump = 10;}else if (key == 's' || key == 'S') // stop movementstep = 0;else if (key == 'r' || key == 'R') // reset step (resume movement)p0.x=400;else if (key == 'm' || key == 'M') // reset step (resume movement)step = 2;
}//Task 4void display(void)
{glMatrixMode(GL_PROJECTION);glLoadIdentity();gluOrtho2D(0, 600, 0, 400);glClearColor(0, 0, 1, 1);glClear(GL_COLOR_BUFFER_BIT);glColor3f(1, 1, 1);glBegin(GL_LINES);glVertex2f(finishx, 50);glVertex2f(finishx, 350);glEnd();glColor3f(0, 1, 0);glBegin(GL_POLYGON);glVertex2f(0, 0);glVertex2f(0, 50);glVertex2f(600, 50);glVertex2f(600, 0);glEnd();glPolygonMode(GL_BACK, GL_FILL);           // 设置反面为填充方式// guyfor (int i = 0; i < count; i++){glColor3f(i & 0x04, i & 0x02, i & 0x01);}//glColor3f(1, 0, 0);glBegin(GL_POLYGON);glVertex2f(p1.x, p1.y);glVertex2f(p1.x, p1.y + 20);glVertex2f(p1.x + 20, p1.y + 20);glVertex2f(p1.x + 20, p1.y);glEnd();glPolygonMode(GL_FRONT, GL_LINE);            // 设置正面(顺时针)为边缘绘制方式glColor3f(0, 0, 0); glBegin(GL_POLYGON);glVertex2f(p1.x, p1.y);glVertex2f(p1.x + 20, p1.y);glVertex2f(p1.x + 20, p1.y + 20);glVertex2f(p1.x, p1.y + 20); //换为反向顺序glEnd();glPolygonMode(GL_BACK, GL_FILL);           // 设置正面为填充方式
// goldglColor3f(1, 1, 0);glBegin(GL_POLYGON);glVertex2f(p0.x, p0.y);glVertex2f(p0.x, p0.y + 10);glVertex2f(p0.x + 10, p0.y + 10);glVertex2f(p0.x + 10, p0.y);glEnd();glPolygonMode(GL_FRONT, GL_LINE);            // 设置反面为边缘绘制方式glColor3f(0, 0, 0); //黑色边框glBegin(GL_POLYGON);glVertex2f(p0.x, p0.y);glVertex2f(p0.x + 10, p0.y);glVertex2f(p0.x + 10, p0.y + 10);glVertex2f(p0.x, p0.y + 10);glEnd();glutSwapBuffers();
}
int main(int argc, char** argv)
{glutInit(&argc, argv);glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);glutInitWindowPosition(100, 100);glutInitWindowSize(600, 400);glutCreateWindow("My Interactive Window");glutDisplayFunc(display);//Task 2//Task 3glutIdleFunc(when_in_mainloop);/* glutIdleFunc sets the global idle callback to be func so a GLUT program can perform backgroundprocessing tasks or continuous animation when window system events are not being received.If enabled, the idle callback is continuously called when events are not being received. The callbackroutine has no parameters. The current window and current menu will not be changed before the idlecallback. Programs with multiple windows and/or menus should explicitly set the current window and/orcurrent menu and not rely on its current setting. The amount of computation and rendering done in an idlecallback should be minimized to avoid affecting the program's interactive response. In general, not morethan a single frame of rendering should be done in an idle callback.Assigning NULL to glutIdleFunc disables the generation of the idle callback. */glutTimerFunc(time_interval, OnTimer, 1);glutTimerFunc(time_interval, JumpOnTimer, 2);glutKeyboardFunc(keyboard_input); // keyboard callback functionglutMainLoop();
}

CPT-205 lab04 task相关推荐

  1. 轻量级自动化运维工具Fabric的安装与实践

    一.背景环境 在运维工作中,经常会遇到重复性的劳动,这个时候为了效率就必须要使用自动化运维工具. 这里我给大家介绍轻量级自动化运维工具Fabric,Fabric是基于Python语言开发的,是开发同事 ...

  2. Linux-进程调度(CFS)

    目录 调度概念 PELT CFS调度 调度概念 linux 线程调度策略 SCHED_OTHER   分时调度策略 SCHED_FF           实时调度策略,先到先服务 SCHED_RR   ...

  3. Linux集群和自动化运维

    Linux/Unix技术丛书 Linux集群和自动化运维 余洪春 著 图书在版编目(CIP)数据 Linux集群和自动化运维/余洪春著. -北京:机械工业出版社,2016.8 (Linux/Unix技 ...

  4. 【Android 逆向】Android 系统文件分析 ( /proc/pid 进程号对应进程目录 | oom_adj | maps | smaps | mem | task | environ )

    文章目录 一./proc/pid_num 进程号对应进程信息文件 1.进程查询 2.进程目录 3.进程启动命令 / 包名 4.oom_adj 进程优先级 5.maps 进程内存使用概况 6.smaps ...

  5. 【项目管理】专用中英文术语词汇 205

    1. 活动,Activity 2. 活动定义,Activity Definition 3. 活动描述/说明,AD=Activity Description 4. 活动历时估算,Activity Dur ...

  6. Hive on Tez出现exec.Task: Failed to execute tez graph. java.lang.NullPointerException

    报错复现: hive>select count(*) from student; 报错如下: 2020-06-03 22:00:36,787 ERROR [57ee4918-ac03-4f15- ...

  7. ERROR [com.netflix.discovery.TimedSupervisorTask] - task supervisor timed out

    错误信息 [plan-upms] [2020-05-11 13:32:18] 05-11 13:32:18.000 ERROR [com.netflix.discovery.TimedSupervis ...

  8. VS Code 配置调试参数、launch.json 配置文件属性、task.json 变量替换、自动保存并格式化、空格和制表符、函数调用关系、文件搜索和全局搜索、

    1. 生成配置参数 对于大多数的调试都需要在当前项目目录下创建一个 lanch.json 文件,位置是在当前项目目录下生成一个 .vscode 的隐藏文件夹,在里面放置一些配置内容,比如:settin ...

  9. 完美解决Error:Execution failed for task ':APP:transformClassesWithDexForDebug'...问题

    今天下载一个demo运行出现问题,错误如下图 我的问题是JDK 1.8 版本问题问题,我吧1.8改成1.7运行成功 这个实在app下面的build.gradle 相信大伙在Android开发过程中都避 ...

最新文章

  1. OpenCV 笔记(01)— OpenCV 概念、整体架构、各模块主要功能
  2. 500个普通人名_2020年世界500强汽车行业排名:大众公司第一,丰田汽车公司第二...
  3. PCB模拟地和数字地的处理
  4. 开发日记-20190715 关键词 读书笔记 《Perl语言入门》Day 9
  5. 一些经常会用到的vbscript检测函数
  6. 2020-10-25(动态调试SMC代码)
  7. python 批量读取文件夹的动漫美女图并显示
  8. 首届Apache Hadoop技术社区中国Meetup在京举办(附PPT)
  9. Servlet JSP系列文章总结
  10. php web 目录遍历,php的目录遍历操作
  11. Kali Linux 2019.1 发布,Metasploit 更新到 5.0 版本
  12. schedule调用相关整理
  13. CentOS 7配置Docker Storage
  14. 常见积分求导公式表--便于记忆
  15. Vim激荡30年发展史
  16. Git 码云 上传 本地项目 步骤/创建分支
  17. ORA-20001: Invalid or inconsistent input values
  18. 云锁linux宝塔安装,宝塔面板安装云锁
  19. 皮尔逊、斯皮尔曼、肯德尔等级应用场景及代码实现(附Python代码)
  20. 什么是redis数据库?

热门文章

  1. 爬虫快速入门——Request对象的使用
  2. PHP完成微信小程序在线支付功能
  3. 大咖电子元件助手界面截图
  4. 有哪些有效解决程序员中年危机的方法?
  5. Unity 实战项目 | Unity实现 双屏或多屏幕 显示效果
  6. 华语乐坛十二大伤感情歌手
  7. 第一篇-python入门
  8. java race condition_java 多线程下race condition问题
  9. 浅谈jdk-spi与dubbo-spi
  10. Qt编写视频监控系统70-OSD标签和图形信息(支持写入到文件)