#include "stdafx.h"

#include <GL/glut.h> #include <stdlib.h>

#pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"")

#include<vector>
#include<cmath>
using namespace std;
vector<int> x;
vector<int> y;
int point_x, point_y;
int direction = GLUT_KEY_RIGHT;
int level = 150;

void restart()
{
 x.clear();
 y.clear();
 int i;
 for (i = 0; i < 30; i++)
 {
  x.push_back(30 - i);
  y.push_back(75);
 }
 direction = GLUT_KEY_RIGHT;
}

void menu(int input)
{
 level = input;
 glutPostRedisplay();
}

double random(double start, double end)
{
 return start + (end - start)*rand() / (RAND_MAX + 1.0);
}

void createpoint()
{
 int i;
 bool state = true;
 while (1)
 {
  point_x = random(0, 200);
  point_y = random(0, 150);
  for (i = 0; i < x.size(); i++)
  {
   if ((x[i] == point_x) && (y[i] == point_y))
   {
    state = false;
    break;
   }
  }
  if (state == true)
   break;
 }
}

void move()
{
 int i;
 for (i = x.size() - 1; i > 0; i--)
 {
  x[i] = x[i - 1];
  y[i] = y[i - 1];
 }
}

void special(int key, int x, int y)
{
 if ((key != direction) && (abs(key - direction) != 2))
  direction = key;
}

void init(void)
{
 glClearColor(0.0, 0.0, 0.0, 0.0);
 glMatrixMode(GL_PROJECTION);
 gluOrtho2D(0.0, 200.0, 0.0, 150.0);
}

void display(void)
{
 int i;
 if ((x[0] < 0) || (x[0] > 200) || (y[0] < 0) || (y[0] > 150))
  restart();
 for (i = 1; i < x.size(); i++)
 {
  if ((x[0] == x[i]) && (y[0] == y[i]))
  {
   restart();
   break;
  }
 }
 glClear(GL_COLOR_BUFFER_BIT);
 glColor3f(1.0, 1.0, 0.0);
 glPointSize(5);
 for (i = 0; i < x.size(); i++)
 {
  glBegin(GL_POINTS);
  glVertex2i(x[i], y[i]);
  glEnd();
 }
 if ((x[0] == point_x) && (y[0] == point_y))
 {
  x.push_back(x[0]);
  y.push_back(y[0]);
  createpoint();
 }
 glBegin(GL_POINTS);
 glVertex2i(point_x, point_y);
 glEnd();
 glFlush();
}

void timer(int i) {

move();  switch (direction)

{

case GLUT_KEY_RIGHT: ::x[0]++; break;

case GLUT_KEY_UP: ::y[0]++; break;

case GLUT_KEY_LEFT: ::x[0]--; break;

case GLUT_KEY_DOWN: ::y[0]--; break;

}

glutTimerFunc(level, timer, 1);

glutPostRedisplay();

}

int main(int argc, char** argv)
{
 int i;
 for (i = 0; i < 30; i++)
 {
  x.push_back(30 - i);
  y.push_back(75);
 }
 glutInit(&argc, argv);
 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
 glutInitWindowPosition(50, 100);
 glutInitWindowSize(400, 300);
 glutCreateWindow("贪吃蛇");
 init();
 glutCreateMenu(menu);
 glutAddMenuEntry("低级", 150);
 glutAddMenuEntry("中级", 100);
 glutAddMenuEntry("高级", 50);
 glutAttachMenu(GLUT_RIGHT_BUTTON);
 createpoint();
 glutDisplayFunc(display);
 glutSpecialFunc(special);
 glutTimerFunc(level, timer, 1);
 glutMainLoop();
}

OpenGL 小游戏 贪吃蛇1(2D)的更多相关文章

  1. 第一个windows 小游戏 贪吃蛇

    最近用dx尝试做了一个小的贪吃蛇游戏,代码放到github上面:https://github.com/nightwolf-chen/MyFreakout 说一下自己实现的过程: 首先,我把蛇这个抽象成 ...

  2. JavaScript面向对象编程小游戏---贪吃蛇

    1 面向对象编程思想在程序项目中有着非常明显的优势: 1- 1 代码可读性高.由于继承的存在,即使改变需求,那么维护也只是在局部模块 1- 2 维护非常方便并且成本较低. ​ 2 这个demo是采用了 ...

  3. 用Canvas制作小游戏——贪吃蛇

    今天呢,主要和小伙伴们分享一下一个贪吃蛇游戏从构思到实现的过程~因为我不是很喜欢直接PO代码,所以只copy代码的童鞋们请出门左转不谢. 按理说canvas与其应用是老生常谈了,可我在准备阶段却搜索不 ...

  4. 使用JavaScript实现简单的小游戏-贪吃蛇

    最近初学JavaScript,在这里分享贪吃蛇小游戏的实现过程, 希望能看到的前辈们能指出这个程序的不足之处. 大致思路 首先要解决的问题 随着蛇头的前进,尾巴也要前进. 用键盘控制蛇的运动方向. 初 ...

  5. python【控制台】小游戏--贪吃蛇

    传统贪吃蛇相信大家都玩过,也是一款很老很经典的游戏,今天我们用python控制台实现 项目有很多bug没有解决,因为本人一时兴起写的一个小游戏,所以只是实现可玩部分功能,并没有花较多的时间和精力去维护 ...

  6. 手把手教学h5小游戏 - 贪吃蛇

    简单的小游戏制作,代码量只有两三百行.游戏可自行扩展延申. 源码已发布至github,喜欢的点个小星星,源码入口:game-snake 游戏已发布,游戏入口:http://snake.game.yan ...

  7. Win32小游戏--贪吃蛇

    近日里学习了关于win32编程的相关知识,利用这些知识制作了一款贪吃蛇小游戏,具体细节还是分模块来叙述 前期准备:在网上找到一些贪吃蛇的游戏素材图片,以及具体的逻辑框图 在正式写功能之前,先把一系列环 ...

  8. Java_GUI小游戏--贪吃蛇

    贪吃蛇游戏:是一条蛇在封闭围墙里,围墙里随机出现一个食物,通过按键盘四个光标键控制蛇向上下左右四个方向移动,蛇头撞倒食物,则食物被吃掉,蛇身体长一节,接着又出现食物,等待蛇来吃,如果蛇在移动中撞到墙或 ...

  9. Java经典小游戏——贪吃蛇简单实现(附源码)

    一.使用知识 Jframe GUI 双向链表 线程 二.使用工具 IntelliJ IDEA jdk 1.8 三.开发过程 3.1素材准备 首先在开发之前应该准备一些素材,已备用,我主要找了一个图片以 ...

随机推荐

  1. 【maven】 pom.xml详解

    pom.xml详解 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www ...

  2. 图解LoadAverage(负载)

    图解LoadAverage(负载) http://www.habadog.com/2015/02/27/what-is-load-average/   一.什么是Load Average 系统负载(S ...

  3. Liferay 6.2 改造系列之十三:修改用户编辑页面表单内容

    为简化用户编辑,删除无用内容: 在/portal-master/portal-impl/src/portal.properties文件中,有如下配置: # # Input a list of sect ...

  4. 使用Spring的JAVA Mail支持简化邮件发送

    http://www.cnblogs.com/codeplus/archive/2011/11/03/2232893.html

  5. Oralce 常用语句

    注:大写代表需要替换掉额 --更新字段名 alter table TABLE rename column COL_OLD to COL_NEW --添加字段名 alter table TABLE ad ...

  6. 代码审查工具StyleCop

    “代码审查”或是“代码评审”(Code Review),这是一个流程,当开发人员写好代码后,需要让别人来review一下他的代码,这是一种有效发现BUG的方法.由此,我们可以审查代码的风格.逻辑.思路 ...

  7. 【原】iOS学习46之第三方CocoaPods的安装和使用(通用方法)

    本文主要说明CocoaPods的安装步骤.使用说明和常见的报错即解决方法. 1. CocoaPods 1>  CocoaPods简介 CocoaPods是一个用来帮助我们管理第三方依赖库的工具. ...

  8. Spring Assert(方法入参检测工具类-断言)

    Web 应用在接受表单提交的数据后都需要对其进行合法性检查,如果表单数据不合法,请求将被驳回.类似的,当我们在编写类的方法时,也常常需要对方法入参进行合 法性检查,如果入参不符合要求,方法将通过抛出异 ...

  9. Codeforces Round #233 (Div. 2) B. Red and Blue Balls

    #include <iostream> #include <string> using namespace std; int main(){ int n; cin >&g ...

  10. ACM: HDU 2563 统计问题-DFS+打表

    HDU 2563 统计问题 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u HDU 2 ...