Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- author: S. M. Shahriar Nirjon
- last modified: August 8, 2008
- */
- # include "iGraphics.h"
- /*
- function iDraw() is called again and again by the system.
- */
- int wd = 800;
- int hg = 600;
- int sp = 10;
- int start = 0;
- int gameOption = 0;
- int help = 0;
- int rad = 35;
- int newGame = 0;
- int exitOption = 0;
- int optionMenu = 0;
- int easyMaze = 0;
- int dx = 30;
- int count = 0;
- int foodCount = 0;
- int foodFound = 0;
- int food[6];
- int finish = 0;
- char score[1000] = "0";
- void drawFood(int x, int y, int rad)
- {
- iSetcolor(255, 0, 0);
- iFilledCircle(x, y, rad);
- iSetcolor(0, 0, 0);
- iCircle(x, y, rad);
- }
- void drawCircle(int x, int y, int rad)
- {
- iSetcolor(0, 64, 128);
- iFilledCircle(x, y, rad);
- iSetcolor(0, 0, 0);
- iCircle(x, y, rad);
- }
- void drawEllipse(int x, int y, int a, int b)
- {
- iSetcolor(0, 64, 128);
- iFilledEllipse(x, y, a, b);
- iSetcolor(0, 0, 0);
- iEllipse(x, y, a, b);
- }
- void drawEye(int x, int y, int r)
- {
- iSetcolor(255, 255, 255);
- iFilledCircle(x, y, r);
- iSetcolor(0, 0, 0);
- iCircle(x, y, r);
- }
- void drawEyeBall(int x, int y, int r)
- {
- iSetcolor(0, 0, 0);
- iFilledCircle(x, y, r);
- }
- void clearFood(int u, int v)
- {
- iSetcolor(34, 177, 76);
- drawFood(u, v, 15);
- }
- void drawFoodPlacing()
- {
- if(food[0] == 0) drawFood(25 + (10 * 30) + 15, 30 + (5 * 30) + 15, 15);
- if(food[1] == 0) drawFood(25 + (13 * 30) + 15, 30 + (5 * 30) + 15, 15);
- if(food[2] == 0) drawFood(25 + (13 * 30) + 15, 30 + (8 * 30) + 15, 15);
- if(food[3] == 0) drawFood(25 + (7 * 30) + 15, 30 + (16 * 30) + 15, 15);
- if(food[4] == 0) drawFood(25 + (10 * 30) + 15, 30 + (13 * 30) + 15, 15);
- if(food[5] == 0) drawFood(25 + (10 * 30) + 15, 30 + (10 * 30) + 15, 15);
- }
- int drawMovingSnake()
- {
- drawCircle(25 + (5 * 30) + 15 + (count * dx), 30 + (5 * 30) + 15, 15);
- drawEllipse(25 + (4 * 30) + 15 + (count * dx), 30 + (5 * 30) + 15, 15, 10);
- drawEllipse(25 + (3 * 30) + 15 + (count * dx), 30 + (5 * 30) + 15, 15, 10);
- drawEllipse(25 + (2 * 30) + 15 + (count * dx), 30 + (5 * 30) + 15, 15, 10);
- drawEye(25 + (5 * 30) + 15 + 5 + (count * dx), 30 + (5 * 30) + 15 + 7, 5);
- drawEye(25 + (5 * 30) + 15 + 5 + (count * dx), 30 + (5 * 30) + 10, 5);
- drawEyeBall(25 + (5 * 30) + 15 + 5 + (count * dx), 30 + (5 * 30) + 15 + 7, 2);
- drawEyeBall(25 + (5 * 30) + 15 + 5 + (count * dx), 30 + (5 * 30) + 10, 2);
- return 25 + (5 * 30) + 15 + (count * dx);
- }
- void drawSnakePic()
- {
- drawCircle(60, 300, 35);
- drawEllipse(116, 272, 30, 20);
- drawEllipse(116 + 60, 272, 30, 20);
- drawEllipse(116 + 120, 272, 30, 20);
- drawEye(45, 290, 10);
- drawEye(65, 310, 10);
- drawEyeBall(42, 291, 7);
- drawEyeBall(62, 312, 7);
- }
- void drawTitle()
- {
- iSetcolor(0, 0, 51);
- //Draw S
- double sx[] = {100, 140, 200, 160};
- double sy[] = {hg - 200, hg - 200, hg - 260, hg - 260};
- iFilledRectangle(100, hg - 200, 100, 30);
- iFilledRectangle(100, hg - 200 - 60 - 30, 100, 30);
- iFilledPolygon(sx, sy, 4);
- //Draw N
- double nx[] = {200 + sp, 230 + sp, 290 + sp, 260 + sp};
- double ny[] = {hg - 170, hg - 170, hg - 290, hg - 290};
- iFilledRectangle(100 + 100 + sp, hg - 200 - 90, 30, 120);
- iFilledRectangle(100 + 100 + 60 + sp, hg - 200 - 90, 30, 120);
- iFilledPolygon(nx, ny, 4);
- //Draw A
- double a1x[] = {340 + sp, 370 + sp, 330 + sp, 300 + sp};
- double a1y[] = {hg - 170, hg - 170, hg - 290, hg - 290};
- double a2x[] = {340 + sp, 370 + sp, 410 + sp, 380 + sp};
- double a2y[] = {hg - 170, hg - 170, hg - 290, hg - 290};
- double a3x[] = {320 + sp, 390 + sp, 400 + sp, 310 + sp};
- double a3y[] = {hg - 235, hg - 235, hg - 265, hg - 265};
- iFilledPolygon(a1x, a1y, 4);
- iFilledPolygon(a2x, a2y, 4);
- iFilledPolygon(a3x, a3y, 4);
- //Draw K
- double k1x[] = {420 + sp, 420 + sp, 500 + sp, 500 + sp};
- double k1y[] = {hg - 210, hg - 250, hg - 170 - 30, hg - 170};
- double k2x[] = {420 + sp, 420 + sp, 500 + sp, 500 + sp};
- double k2y[] = {hg - 210, hg - 250, hg - 290, hg - 260};
- iFilledRectangle(420 + sp, hg - 290, 30, 120);
- iFilledPolygon(k1x, k1y, 4);
- iFilledPolygon(k2x, k2y, 4);
- //Draw E
- iFilledRectangle(510 + sp, hg - 290, 30, 120);
- iFilledRectangle(510 + sp, hg - 200, 80, 30);
- iFilledRectangle(510 + sp, hg - 245, 60, 30);
- iFilledRectangle(510 + sp, hg - 290, 80, 30);
- //Game
- iSetcolor(0,0,51);
- //iShowBMP(220, 20, "pic1.bmp");
- //start
- iSetcolor(0, 0, 51);
- iText(600, 150, "START...", GLUT_BITMAP_TIMES_ROMAN_24);
- }
- void drawPlayGround()
- {
- int i = 0 ;
- int j = 0;
- iSetcolor(233, 233, 233);
- iFilledRectangle(0, 0, wd, hg - 30);
- iSetcolor(34, 177, 76);
- iFilledRectangle(25, 30, wd - 50, hg - 90);
- }
- void drawMenuBar()
- {
- iSetcolor(200, 191, 231);
- iFilledRectangle(0, hg - 30, 800, 30);
- iSetcolor(0, 0, 0);
- //iRectangle(10, hg - 25, 50, 20);//Game
- //iRectangle(70, hg - 25, 50, 20);//Help
- iText(15, hg - 20, "Game", GLUT_BITMAP_9_BY_15);
- iText(75, hg - 20, "Help", GLUT_BITMAP_9_BY_15);
- iText(500, 10, "Score : ", GLUT_BITMAP_9_BY_15);
- iRectangle(570, 5, 60, 20);
- iText(590, 10, score, GLUT_BITMAP_9_BY_15);
- }
- void drawFirstPage()
- {
- iSetcolor(119, 119, 255);
- iFilledRectangle(0, 0, 800, 600);
- drawTitle();
- drawSnakePic();
- }
- void drawHelpMenu()
- {
- iSetcolor(200, 191, 231);
- iFilledRectangle(70, hg - 25 - 60, 150, 55);
- iSetcolor(233, 233, 233);
- iLine(70, hg - 25 - 60, 70, hg - 25 - 60 + 55);
- iLine(70 + 25, hg - 25 - 60, 70 + 25, hg - 25 - 60 + 55);
- iLine(70 + 150, hg - 25 - 60, 70 + 150, hg - 25 - 60 + 55);
- iLine(70, hg - 25 - 60 + 55, 70 + 150, hg - 25 - 60 + 55);
- iLine(70, hg - 25 - 60, 70 + 150, hg - 25 - 60);
- iLine(70 + 25, hg - 25 - 60 + 27.5, 70 + 150, hg - 25 - 60 + 27.5);
- iSetcolor(0, 0, 0);
- iText(70 + 35, hg - 25 - 60 + 55 - 15, "About Game", GLUT_BITMAP_9_BY_15);
- iText(70 + 35, hg - 25 - 60 + 12, "View Help", GLUT_BITMAP_9_BY_15);
- }
- void drawOptionMenu()
- {
- iSetcolor(200, 191, 231);
- iFilledRectangle(10, hg - 25 - 115, 150, 110);
- iSetcolor(233, 233, 233);
- iLine(10, hg - 25 - 115, 10, hg - 25 - 115 + 110);
- iLine(10 + 25, hg - 25 - 115, 10 + 25, hg - 25 - 115 + 110);
- iLine(10 + 150, hg - 25 - 115, 10 + 150, hg - 25 - 115 + 110);
- iLine(10, hg - 25 - 115 + 110, 10 + 150, hg - 25 - 115 + 110);
- iLine(10, hg - 25 - 115, 10 + 150, hg - 25 - 115);
- iLine(10 + 25, hg - 25 - 115 + 27.5, 10 + 150, hg - 25 - 115 + 27.5);
- iLine(10 + 25, hg - 25 - 115 + ( 2 * 27.5), 10 + 150, hg - 25 - 115 + ( 2 * 27.5));
- iLine(10 + 25, hg - 25 - 115 + ( 3 * 27.5), 10 + 150, hg - 25 - 115 + ( 3 * 27.5));
- iSetcolor(0, 0, 0);
- iText(10 + 35, hg - 25 - 60 + 12 + 27, "New Game", GLUT_BITMAP_9_BY_15);
- iText(10 + 35, hg - 25 - 60 + 12, "Statistics", GLUT_BITMAP_9_BY_15);
- iText(10 + 35, hg - 25 - 60 + 12 - 27.5, "Options", GLUT_BITMAP_9_BY_15);
- iText(10 + 35, hg - 25 - 60 + 12 - 54, "Exit", GLUT_BITMAP_9_BY_15);
- }
- void drawEasyMaze()
- {
- int i =0;
- int j = 0;
- //iShowBMP(100,38,"pic.bmp");
- iSetcolor(34, 177, 76);
- iFilledRectangle(25, 30, wd - 50, hg - 90);
- while(i <= 17)
- {
- iSetcolor(0, 0, 0);
- iLine(25, 30 + ((480/16)*i), wd - 55 + 27.5, 30 + ((480/16)*i));
- i++;
- }
- while(j <= 25)
- {
- iSetcolor(0, 0, 0);
- iLine(25 + ((720/24)*j), 30 , 25 + ((720/24)*j), hg - 90 + 30);
- j++;
- }
- iSetcolor(99, 62, 41);
- iFilledRectangle((25 + (3 * 30)), (2 * 30), (8 * 30), (1 * 30));
- iFilledRectangle((25 + (5 * 30)), (8 * 30), (1 * 30), (6 * 30));
- iFilledRectangle((25 + (6 * 30)), (5 * 30), (10 * 30), (1 * 30));
- iFilledRectangle((25 + (3 * 30)), (16 * 30), (5 * 30), (1 * 30));
- iFilledRectangle((25 + (20 * 30)), (4 * 30), (1 * 30), (12 * 30));
- }
- void drawDifficultyOptions()
- {
- iSetcolor(105, 114, 216);
- iFilledRectangle(200, 200, 300, 200);
- iSetcolor(17, 20, 64);
- iRectangle(200, 200, 300, 200);
- iSetcolor(200, 191, 231);
- iFilledRectangle(210, 210, 280, 160);
- iSetcolor(0, 0, 0);
- iRectangle(210, 210, 280, 160);
- iText(210, 380, "Options", GLUT_BITMAP_HELVETICA_18);
- iText(220, 350, "Difficulty lavels : ", GLUT_BITMAP_HELVETICA_18);
- iSetcolor(233, 233, 233);
- iLine(211, 340, 490, 340);
- iLine(211, 300, 490, 300);
- iLine(211, 250, 490, 250);
- iSetcolor(0, 0, 0);
- iFilledCircle(220, 320, 5);
- //iRectangle(230, 305, 80, 30);
- iText(230, 315, "Easy Maze", GLUT_BITMAP_9_BY_15);
- iFilledCircle(220, 270, 5);
- iText(230, 265, "Medium Maze", GLUT_BITMAP_9_BY_15);
- iFilledCircle(220, 230, 5);
- iText(230, 225, "Advanced Maze", GLUT_BITMAP_9_BY_15);
- }
- void iDraw()
- {
- //place your drawing codes here
- //iClear();
- int snakePlace;
- drawFirstPage();
- if(start == 1)
- {
- drawPlayGround();
- drawMenuBar();
- }
- if(gameOption == 1)
- {
- if(newGame == 1)
- {
- drawDifficultyOptions();
- }
- }
- if(easyMaze == 1)
- {
- drawEasyMaze();
- drawFoodPlacing();
- drawMovingSnake();
- snakePlace = drawMovingSnake();
- if( (snakePlace == (25 + (10 * 30) + 15)) && (food[0]!= 1) )
- {
- foodCount++;
- sprintf(score, "%d", foodCount *5);
- food[0] = 1;
- }
- else if( snakePlace == (25 + (13 * 30) + 15) && (food[1]!= 1))
- {
- foodCount++;
- sprintf(score, "%d", foodCount *5);
- food[1] = 1;
- }
- if(snakePlace >= ((25 + (20 * 30))))
- {
- easyMaze = 0;
- finish = 1;
- }
- }
- if(foodFound == 1)
- {
- drawMovingSnake();
- }
- if(help == 1)
- {
- drawHelpMenu();
- }
- if(gameOption == 1)
- {
- drawOptionMenu();
- }
- if(finish == 1)
- {
- iText(300, 300, "GAME OVER!!!", GLUT_BITMAP_TIMES_ROMAN_24);
- iText(200, 260, "EXIT and RESTART THE GAME AGAIN", GLUT_BITMAP_TIMES_ROMAN_24);
- }
- }
- /*
- function iMouseMove() is called when the user presses and drags the mouse.
- (mx, my) is the position where the mouse pointer is.
- */
- void iMouseMove(int mx, int my)
- {
- //place your codes here
- }
- /*
- function iMouse() is called when the user presses/releases the mouse.
- (mx, my) is the position where the mouse pointer is.
- */
- void iMouse(int button, int state, int mx, int my)
- {
- if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
- {
- drawMovingSnake();
- if((mx >= 600) && (mx <= (600 + 100)) && (my >= 150) && (my <= (150 + 20)))
- {
- start = 1;
- }
- if((mx >= 10) && (mx <= 60) && (my >= (hg - 25)) && (my <= (hg - 5)))
- {
- help = 0;
- gameOption = 1;
- }
- if((mx >= 70) && (mx <= 120) && (my >= (hg - 25)) && (my <= (hg - 5)))
- {
- gameOption = 0;
- help = 1;
- }
- if((mx >= 120) && (mx <= wd) && (my >= (hg - 25)) && (my <= (hg - 5)))
- {
- gameOption = 0;
- help = 0;
- }
- if((mx >= (10 + 25)) && (mx <= (10 + 150)) && (my >= (hg - 25 - 115 + ( 3 * 27.5))) && (my <= (hg - 25 - 115 + ( 4 * 27.5))))
- {
- newGame = 1;
- finish = 0;
- //easyMaze = 1;
- }
- if((mx >= 230) && (mx <= (230 + 80)) && (my >= 305) && (my <= (305 + 30)))
- {
- //finish = 0;
- easyMaze = 1;
- gameOption = 0;
- newGame = 0;
- finish = 0;
- }
- if((mx >= (10 + 25)) && (mx <= (10 + 150)) && (my >= (hg - 25 - 115 + ( 0 * 27.5))) && (my <= (hg - 25 - 115) + ( 1 * 27.5)))
- {
- printf("exit");
- exitOption = 1;
- exit(0);
- }
- }
- if(button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
- {
- //place your codes here
- }
- }
- /*
- function iKeyboard() is called whenever the user hits a key in keyboard.
- key- holds the ASCII value of the key pressed.
- */
- void iKeyboard(unsigned char key)
- {
- if(key == 'q')
- {
- //do something with 'q'
- }
- //place your codes for other keys here
- }
- /*
- function iSpecialKeyboard() is called whenver user hits special keys like-
- function keys, home, end, pg up, pg down, arraows etc. you have to use
- appropriate constants to detect them. A list is:
- GLUT_KEY_F1, GLUT_KEY_F2, GLUT_KEY_F3, GLUT_KEY_F4, GLUT_KEY_F5, GLUT_KEY_F6,
- GLUT_KEY_F7, GLUT_KEY_F8, GLUT_KEY_F9, GLUT_KEY_F10, GLUT_KEY_F11, GLUT_KEY_F12,
- GLUT_KEY_LEFT, GLUT_KEY_UP, GLUT_KEY_RIGHT, GLUT_KEY_DOWN, GLUT_KEY_PAGE UP,
- GLUT_KEY_PAGE DOWN, GLUT_KEY_HOME, GLUT_KEY_END, GLUT_KEY_INSERT
- */
- void iSpecialKeyboard(unsigned char key)
- {
- if(key == GLUT_KEY_END)
- {
- exit(0);
- }
- if(key == GLUT_KEY_RIGHT)
- {
- count++;
- }
- //place your codes for other keys here
- }
- int main()
- {
- //place your own initialization codes here.
- iInitialize(wd, hg, "Snake Game");
- return 0;
- }
Add Comment
Please, Sign In to add comment