Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <Button.h>
- #include <process.h>
- #include <ctime>
- //Include OpenGL header files, so that we can use OpenGL
- #ifdef __APPLE__
- #include <OpenGL/OpenGL.h>
- #include <GLUT/glut.h>
- #else
- #include <GL/glut.h>
- #endif
- #define Max_Snake_Length 50
- #define Up 1
- #define Left 2
- #define Right 3
- #define Down 4
- #define Easy 180
- #define Medium 100
- #define Hard 0
- using namespace std;
- void EASY();
- void MEDIUM();
- void HARD();
- void mouse(int x, int y);
- void Movement(int button, int state, int x, int y);
- int apples = 0;
- int Snake_Length = 4;
- int direction = Right;
- int score = 0;
- float snakesize = 0.05f;
- float movement = 0.125f;
- float Difficulty = Medium;
- char Display_Score[5];
- struct Snake
- {
- float X;
- float Y;
- };
- struct Apple
- {
- float XCoord;
- float YCoord;
- };
- Apple Food;
- Snake Head = {0,0};
- Snake Tail[Max_Snake_Length - 1];
- XY Screen = {800,600};
- XY Screen2 = {800,600};
- XY Screen3 = {800,600};
- // X Y Length Height Color inuseColor Text TextColor inuseTextColor scale function use Inuse
- 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};
- 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};
- 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};
- //Called when a key is pressed
- void handleKeypress(unsigned char key, //The key that was pressed
- int x, int y) { //The current mouse coordinates
- switch (key) {
- case 27: //Escape key
- exit(0); //Exit the program
- break;
- case 'w':
- if (direction != Down){direction = Up;}
- break;
- case 'a':
- if (direction != Right){direction = Left;}
- break;
- case 's':
- if (direction != Up){direction = Down;}
- break;
- case 'd':
- if (direction != Left){direction = Right;}
- break;
- }
- }
- //Initializes 3D rendering
- void initRendering() {
- //Makes 3D drawing work when something is in front of something else
- glDisable(GL_DEPTH_TEST);
- glEnable(GL_COLOR_MATERIAL); //enables colors
- glClearColor(0.0f, 0.0f, 0.0f, 1.0f); //sets background color. First three are RBG and last should ALWAYS be 1
- }
- //Called when the window is resized
- void handleResize(int w, int h) {
- if (Diff_Easy.use == false){
- //Tell OpenGL how to convert from coordinates to pixel values
- glViewport(0, 0, w, h);
- glMatrixMode(GL_PROJECTION); //Switch to setting the camera perspective
- //Set the camera perspective
- glLoadIdentity(); //Reset the camera
- gluPerspective(45.0, //The camera angle
- (double)w / (double)h, //The width-to-height ratio
- 1.0, //The near z clipping coordinate
- 200.0); //The far z clipping coordinate
- }
- else
- {
- ButtonRendering(w,h,&Diff_Easy,&Screen );
- ButtonRendering(w,h,&Diff_Med, &Screen2);
- ButtonRendering(w,h,&Diff_Hard,&Screen3);
- }
- }
- //Draws the 3D scene
- void drawScene() {
- if (Diff_Easy.use == false){ //draw Game
- int x;
- float a,b;
- float HI,LO;
- //Clear information from last draw
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
- glLoadIdentity(); //Reset the drawing perspective
- glTranslatef(0.0f,0.0f,-5.0f);
- for (a = -1.8f ; a <= 2 ; a += 0.01f)
- { //Draws the boarder
- for (b = -2.75f ; b <= 2.75f ; b += 0.01f)
- {
- if (a == -1.8f or fabs(a - 2) <= 0.001f || b == -2.75f or fabs(b - 2.75f) <= 0.001f)
- {
- glPushMatrix();
- glColor3f(0.7f,0.0f,0.0f);
- glTranslatef(b,a,0.0f);
- glutSolidCube(snakesize+0.01f);
- glPopMatrix();
- }
- }
- }
- if (apples == 0)
- {
- HI = 1.9f; LO = -1.7f;
- Food.YCoord = LO + (float)rand()/((float)RAND_MAX/(HI-LO));
- HI = 2.6f; LO = -2.6f;
- Food.XCoord = LO + (float)rand()/((float)RAND_MAX/(HI-LO));
- apples++;
- }
- glPushMatrix(); //Apples
- glColor3f(1.0f,0.0f,0.0f);
- glTranslatef(Food.XCoord,Food.YCoord,0.0f);
- glutWireSphere(snakesize+0.01f,15,15);
- glPopMatrix();
- for (x=Snake_Length;x-2>=0;x--){ //Draws Snake Tail
- if (x == 2)
- {
- Tail[x-2] = {Head.X,Head.Y};
- }
- else {
- Tail[x-2] = { Tail[x-1-2].X, Tail[x-1-2].Y };
- }
- glPushMatrix();
- glBegin(GL_TRIANGLES);
- glColor3f(0.0f,1.0f,0.0f);
- glVertex3f(Tail[x-2].X+snakesize ,Tail[x-2].Y+snakesize , 0.0f);
- glVertex3f(Tail[x-2].X-snakesize ,Tail[x-2].Y+snakesize , 0.0f);
- glVertex3f(Tail[x-2].X-snakesize ,Tail[x-2].Y-snakesize , 0.0f);
- glVertex3f(Tail[x-2].X+snakesize ,Tail[x-2].Y-snakesize , 0.0f);
- glVertex3f(Tail[x-2].X+snakesize ,Tail[x-2].Y+snakesize , 0.0f);
- glVertex3f(Tail[x-2].X-snakesize ,Tail[x-2].Y-snakesize , 0.0f);
- glEnd();
- glPopMatrix();
- }
- if(direction == Right){Head.X += movement;}
- if(direction == Left){Head.X -= movement;}
- if(direction == Up){Head.Y += movement;}
- if(direction == Down){Head.Y -= movement;}
- glPushMatrix(); //Snake Head
- glColor3f(0.0f,0.5f,0.0f);
- glTranslatef(Head.X,Head.Y,0.0f);
- glutSolidSphere(snakesize + 0.025f,10000,10000);
- glPopMatrix();
- glPushMatrix(); //Score
- glColor3f(1,1,1);
- glTranslatef(-2.75f,-2.0f,0.0f);
- drawStrokeText("Score: ",Normal_Text_3D);
- glPopMatrix();
- glPushMatrix(); //Score cont.
- glTranslatef(-2.3f,-2.0f,0.0f);
- StoreIntInChar(score,Display_Score);
- drawStrokeText(Display_Score,Normal_Text_3D);
- //drawStrokeText(IntToChar(score),Normal_Text);
- glPopMatrix();
- }
- else //draw buttons
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
- glLoadIdentity(); //Reset the drawing perspective
- DrawButton(&Diff_Easy);
- DrawButton(&Diff_Med);
- DrawButton(&Diff_Hard);
- }
- glutSwapBuffers(); //Send the 3D scene to the screen
- }
- void Check(int value)
- {
- if (Diff_Easy.use == false){
- 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 )
- { //snake hits boarder
- Sleep(2000);
- exit(0);
- }
- else if (fabs(Head.X - Food.XCoord) <= 0.2f and fabs(Head.Y - Food.YCoord) <= 0.2f)
- { //snake hits an apple
- score += 5;
- apples = 0;
- if (Snake_Length != Max_Snake_Length){Snake_Length++;}
- }
- for (int x = 0; x < Snake_Length - 1; x++)
- {
- 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) )
- { //snake hits itself
- /*cout<<"("<<Head.X<<","<<Head.Y<<")"<<endl;
- cout<<"("<<Tail[x].X<<","<<Tail[x].X<<")"<<endl;*/
- Sleep(2000);
- exit(0);
- }
- }
- glutPostRedisplay();
- glutTimerFunc(Difficulty, Check, 0);
- system("ClS");
- }
- }
- int main(int argc, char** argv) {
- srand(time(NULL));
- //Initialize GLUT
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
- glutInitWindowSize(Screen.X, Screen.Y);
- //Create the window
- glutCreateWindow("Snake Game v2 - GL");
- initRendering();
- //Set handler functions
- glutKeyboardFunc(handleKeypress);
- glutDisplayFunc(drawScene);
- glutReshapeFunc(handleResize);
- glutTimerFunc(0, Check, 0);
- glutPassiveMotionFunc(mouse);
- glutMouseFunc(Movement);
- glutMainLoop();
- return 0;
- }
- void EASY()
- {
- Difficulty = Easy;
- }
- void MEDIUM()
- {
- Difficulty = Medium;
- }
- void HARD()
- {
- Difficulty = Hard;
- }
- void mouse(int x, int y)
- {
- MouseMotion(x, y, &Diff_Easy, Screen.Y);
- MouseMotion(x, y, &Diff_Med, Screen.Y);
- MouseMotion(x, y, &Diff_Hard, Screen.Y);
- }
- void Movement(int button, int state, int x, int y)
- {
- if (
- ButtonPressed(button,state,x,y, &Diff_Easy, Screen.Y) == true or
- ButtonPressed(button,state,x,y, &Diff_Med , Screen.Y) == true or
- ButtonPressed(button,state,x,y, &Diff_Hard, Screen.Y) == true
- )
- {
- Diff_Easy.use = false;
- Diff_Med.use = false;
- Diff_Hard.use = false;
- glutHideWindow();
- main(0,0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement