Advertisement
Niven

Snake Game

Nov 16th, 2012
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.62 KB | None | 0 0
  1. #include <iostream>
  2. #include <Button.h>
  3. #include <process.h>
  4. #include <ctime>
  5.  
  6. //Include OpenGL header files, so that we can use OpenGL
  7. #ifdef __APPLE__
  8. #include <OpenGL/OpenGL.h>
  9. #include <GLUT/glut.h>
  10. #else
  11. #include <GL/glut.h>
  12. #endif
  13.  
  14. #define Max_Snake_Length 50
  15.  
  16. #define Up 1
  17. #define Left 2
  18. #define Right 3
  19. #define Down 4
  20.  
  21. #define Easy 180
  22. #define Medium 100
  23. #define Hard 0
  24.  
  25. using namespace std;
  26.  
  27. void EASY();
  28. void MEDIUM();
  29. void HARD();
  30. void mouse(int x, int y);
  31. void Movement(int button, int state, int x, int y);
  32.  
  33. int apples = 0;
  34. int Snake_Length = 4;
  35. int direction = Right;
  36. int score = 0;
  37.  
  38. float snakesize = 0.05f;
  39. float movement = 0.125f;
  40. float Difficulty = Medium;
  41.  
  42. char Display_Score[5];
  43.  
  44. struct Snake
  45. {
  46.     float X;
  47.     float Y;
  48. };
  49.  
  50. struct Apple
  51. {
  52.     float XCoord;
  53.     float YCoord;
  54. };
  55.  
  56. Apple Food;
  57. Snake Head = {0,0};
  58. Snake Tail[Max_Snake_Length - 1];
  59.  
  60. XY Screen  = {800,600};
  61. XY Screen2 = {800,600};
  62. XY Screen3 = {800,600};
  63. //                   X    Y   Length  Height   Color   inuseColor   Text      TextColor      inuseTextColor          scale     function   use     Inuse
  64. Button Diff_Easy = { 80, 540, 640,    120,    {0,1,0}, {0,0.8,0},   "Easy",   {.9,.9,.9},    {0.1,0.1,0.1},         {1.8,.7},   EASY,     true,   false};
  65. Button Diff_Med  = { 80, 360, 640,    120,    {1,1,0}, {.8,.8,0},   "Medium", {.9,0.9,1},    {0.1,0.1,0.1},         {1,0.7},    MEDIUM,   true,   false};
  66. Button Diff_Hard = { 80, 180, 640,    120,    {1,0,0}, {0.8,0,0},   "Hard",   {.9,.9,.9},    {0.1,0.1,0.1},         {1.8,.7},   HARD,     true,   false};
  67.  
  68. //Called when a key is pressed
  69. void handleKeypress(unsigned char key, //The key that was pressed
  70.                     int x, int y) {    //The current mouse coordinates
  71.     switch (key) {
  72.  
  73.         case 27: //Escape key
  74.             exit(0); //Exit the program
  75.             break;
  76.  
  77.         case 'w':
  78.             if (direction != Down){direction = Up;}
  79.             break;
  80.  
  81.         case 'a':
  82.             if (direction != Right){direction = Left;}
  83.             break;
  84.  
  85.         case 's':
  86.             if (direction != Up){direction = Down;}
  87.             break;
  88.  
  89.         case 'd':
  90.             if (direction != Left){direction = Right;}
  91.             break;
  92.  
  93.     }
  94.  
  95. }
  96.  
  97. //Initializes 3D rendering
  98. void initRendering() {
  99.     //Makes 3D drawing work when something is in front of something else
  100.     glDisable(GL_DEPTH_TEST);
  101.     glEnable(GL_COLOR_MATERIAL); //enables colors
  102.     glClearColor(0.0f, 0.0f, 0.0f, 1.0f); //sets background color. First three are RBG and last should ALWAYS be 1
  103. }
  104.  
  105. //Called when the window is resized
  106. void handleResize(int w, int h) {
  107.     if (Diff_Easy.use == false){
  108.     //Tell OpenGL how to convert from coordinates to pixel values
  109.     glViewport(0, 0, w, h);
  110.  
  111.     glMatrixMode(GL_PROJECTION); //Switch to setting the camera perspective
  112.  
  113.     //Set the camera perspective
  114.     glLoadIdentity(); //Reset the camera
  115.     gluPerspective(45.0,                  //The camera angle
  116.                    (double)w / (double)h, //The width-to-height ratio
  117.                    1.0,                   //The near z clipping coordinate
  118.                    200.0);                //The far z clipping coordinate
  119.     }
  120.     else
  121.     {
  122.         ButtonRendering(w,h,&Diff_Easy,&Screen );
  123.         ButtonRendering(w,h,&Diff_Med, &Screen2);
  124.         ButtonRendering(w,h,&Diff_Hard,&Screen3);
  125.     }
  126. }
  127.  
  128. //Draws the 3D scene
  129. void drawScene() {
  130.  
  131.     if (Diff_Easy.use == false){ //draw Game
  132.     int x;
  133.     float a,b;
  134.     float HI,LO;
  135.  
  136.     //Clear information from last draw
  137.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  138.  
  139.     glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
  140.     glLoadIdentity(); //Reset the drawing perspective
  141.     glTranslatef(0.0f,0.0f,-5.0f);
  142.  
  143.  
  144.     for (a = -1.8f ; a <= 2 ; a += 0.01f)
  145.     { //Draws the boarder
  146.         for (b = -2.75f ; b <= 2.75f ; b += 0.01f)
  147.         {
  148.             if (a == -1.8f or fabs(a - 2) <= 0.001f || b == -2.75f or fabs(b - 2.75f) <= 0.001f)
  149.             {
  150.                 glPushMatrix();
  151.     glColor3f(0.7f,0.0f,0.0f);
  152.     glTranslatef(b,a,0.0f);
  153.     glutSolidCube(snakesize+0.01f);
  154.                 glPopMatrix();
  155.             }
  156.         }
  157.     }
  158.  
  159.     if (apples == 0)
  160.     {
  161.         HI = 1.9f; LO = -1.7f;
  162.         Food.YCoord = LO + (float)rand()/((float)RAND_MAX/(HI-LO));
  163.         HI = 2.6f; LO = -2.6f;
  164.         Food.XCoord = LO + (float)rand()/((float)RAND_MAX/(HI-LO));
  165.  
  166.         apples++;
  167.     }
  168.  
  169.         glPushMatrix(); //Apples
  170.     glColor3f(1.0f,0.0f,0.0f);
  171.     glTranslatef(Food.XCoord,Food.YCoord,0.0f);
  172.     glutWireSphere(snakesize+0.01f,15,15);
  173.         glPopMatrix();
  174.  
  175.     for (x=Snake_Length;x-2>=0;x--){ //Draws Snake Tail
  176.  
  177.         if (x == 2)
  178.         {
  179.         Tail[x-2] = {Head.X,Head.Y};
  180.         }
  181.  
  182.         else {
  183.         Tail[x-2] = { Tail[x-1-2].X, Tail[x-1-2].Y };
  184.         }
  185.  
  186.         glPushMatrix();
  187.  
  188.     glBegin(GL_TRIANGLES);
  189.  
  190.     glColor3f(0.0f,1.0f,0.0f);
  191.  
  192.     glVertex3f(Tail[x-2].X+snakesize ,Tail[x-2].Y+snakesize , 0.0f);
  193.     glVertex3f(Tail[x-2].X-snakesize ,Tail[x-2].Y+snakesize , 0.0f);
  194.     glVertex3f(Tail[x-2].X-snakesize ,Tail[x-2].Y-snakesize , 0.0f);
  195.  
  196.     glVertex3f(Tail[x-2].X+snakesize ,Tail[x-2].Y-snakesize , 0.0f);
  197.     glVertex3f(Tail[x-2].X+snakesize ,Tail[x-2].Y+snakesize , 0.0f);
  198.     glVertex3f(Tail[x-2].X-snakesize ,Tail[x-2].Y-snakesize , 0.0f);
  199.  
  200.         glEnd();
  201.  
  202.         glPopMatrix();
  203.     }
  204.  
  205.     if(direction == Right){Head.X += movement;}
  206.     if(direction == Left){Head.X -= movement;}
  207.     if(direction == Up){Head.Y += movement;}
  208.     if(direction == Down){Head.Y -= movement;}
  209.  
  210.         glPushMatrix(); //Snake Head
  211.     glColor3f(0.0f,0.5f,0.0f);
  212.     glTranslatef(Head.X,Head.Y,0.0f);
  213.     glutSolidSphere(snakesize + 0.025f,10000,10000);
  214.         glPopMatrix();
  215.  
  216.         glPushMatrix(); //Score
  217.     glColor3f(1,1,1);
  218.     glTranslatef(-2.75f,-2.0f,0.0f);
  219.     drawStrokeText("Score: ",Normal_Text_3D);
  220.         glPopMatrix();
  221.  
  222.         glPushMatrix(); //Score cont.
  223.     glTranslatef(-2.3f,-2.0f,0.0f);
  224.     StoreIntInChar(score,Display_Score);
  225.     drawStrokeText(Display_Score,Normal_Text_3D);
  226.     //drawStrokeText(IntToChar(score),Normal_Text);
  227.         glPopMatrix();
  228.     }
  229.     else //draw buttons
  230.     {
  231.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  232.  
  233.     glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
  234.     glLoadIdentity(); //Reset the drawing perspective
  235.  
  236.     DrawButton(&Diff_Easy);
  237.     DrawButton(&Diff_Med);
  238.     DrawButton(&Diff_Hard);
  239.     }
  240.     glutSwapBuffers(); //Send the 3D scene to the screen
  241. }
  242.  
  243. void Check(int value)
  244. {
  245.     if (Diff_Easy.use == false){
  246.     if ( fabs(Head.Y + 1.8f) <= 0.075 or fabs(Head.Y - 2) <= 0.001f || Head.X == -2.75f or fabs(Head.X - 2.75f) <= 0.001f )
  247.         { //snake hits boarder
  248.             Sleep(2000);
  249.             exit(0);
  250.         }
  251.     else if (fabs(Head.X - Food.XCoord) <= 0.2f and fabs(Head.Y - Food.YCoord) <= 0.2f)
  252.     { //snake hits an apple
  253.         score += 5;
  254.         apples = 0;
  255.         if (Snake_Length != Max_Snake_Length){Snake_Length++;}
  256.     }
  257.     for (int x = 0; x < Snake_Length - 1; x++)
  258.     {
  259.         if (fabs(Head.X - Tail[x].X) <= 0.00001f and fabs(Head.Y - Tail[x].Y) <= 0.00001f and (Head.X != 0 and Head.Y != 0) )
  260.         { //snake hits itself
  261.             /*cout<<"("<<Head.X<<","<<Head.Y<<")"<<endl;
  262.             cout<<"("<<Tail[x].X<<","<<Tail[x].X<<")"<<endl;*/
  263.             Sleep(2000);
  264.             exit(0);
  265.         }
  266.     }
  267.     glutPostRedisplay();
  268.     glutTimerFunc(Difficulty, Check, 0);
  269.     system("ClS");
  270.     }
  271.  
  272. }
  273.  
  274. int main(int argc, char** argv) {
  275.  
  276.     srand(time(NULL));
  277.  
  278.     //Initialize GLUT
  279.     glutInit(&argc, argv);
  280.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  281.     glutInitWindowSize(Screen.X, Screen.Y);
  282.  
  283.     //Create the window
  284.     glutCreateWindow("Snake Game v2 - GL");
  285.     initRendering();
  286.  
  287.     //Set handler functions
  288.     glutKeyboardFunc(handleKeypress);
  289.     glutDisplayFunc(drawScene);
  290.     glutReshapeFunc(handleResize);
  291.     glutTimerFunc(0, Check, 0);
  292.     glutPassiveMotionFunc(mouse);
  293.     glutMouseFunc(Movement);
  294.  
  295.     glutMainLoop();
  296.     return 0;
  297. }
  298.  
  299. void EASY()
  300. {
  301.     Difficulty = Easy;
  302. }
  303.  
  304. void MEDIUM()
  305. {
  306.     Difficulty = Medium;
  307. }
  308.  
  309. void HARD()
  310. {
  311.     Difficulty = Hard;
  312. }
  313.  
  314. void mouse(int x, int y)
  315. {
  316.     MouseMotion(x, y, &Diff_Easy, Screen.Y);
  317.     MouseMotion(x, y, &Diff_Med,  Screen.Y);
  318.     MouseMotion(x, y, &Diff_Hard, Screen.Y);
  319. }
  320.  
  321. void Movement(int button, int state, int x, int y)
  322. {
  323.     if (
  324.     ButtonPressed(button,state,x,y, &Diff_Easy, Screen.Y)  == true or
  325.     ButtonPressed(button,state,x,y, &Diff_Med , Screen.Y)  == true or
  326.     ButtonPressed(button,state,x,y, &Diff_Hard, Screen.Y)  == true
  327.        )
  328.         {
  329.             Diff_Easy.use = false;
  330.             Diff_Med.use  = false;
  331.             Diff_Hard.use = false;
  332.             glutHideWindow();
  333.             main(0,0);
  334.         }
  335. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement