zoukankan      html  css  js  c++  java
  • OpenGL 小游戏 贪吃蛇1(2D)

    #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();
    }

  • 相关阅读:
    char *详细指针
    UIScrollViewA都PI得知。
    Android动画之二:View Animation
    计算机基本知识拾遗(七)页面缓存数据的同步和恢复机制页
    得到Android系统语言设置
    Harry Potter and the Prisoner of Azkaban
    zerglurker的c语言教程006——第一功能
    ExtJs迄今datefield控制设置默认值
    quick 2.23 它们的定义c++代码lua与总结的一些细节
    uva live 6190 Beautiful Spacing (二分法+dp试 基于优化的独特性质)
  • 原文地址:https://www.cnblogs.com/cci8go/p/4342315.html
Copyright © 2011-2022 走看看