Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. //Snake game :)
  2. #include<iostream>
  3. #include<graphics.h>
  4. #include<conio.h>
  5. #include<stdlib.h>
  6. using namespace std;
  7. int main()
  8. {
  9. int gdriver=DETECT,gmode;
  10. initgraph(&gdriver,&gmode,"c:\\tc\\bgi");
  11. cleardevice();
  12. //x and y are for the circle
  13. int x=300,y=250;
  14. //x1 and y1 are fo generating foot at random place
  15. int x1=200,y1=230,food=0;
  16. // key is used to take input from KEYBOARD
  17. char key;
  18. // a boundry line
  19. rectangle(100,100,500,400);
  20. // if x or y exceeds the boundry limit game is out
  21. while(x>100 && x<500 && y>100 && y<400)
  22. {
  23. //these are for generating food at randm pleaces..
  24. // these 4 conditions are if the circle that is in your ccontrol reaches the food then
  25. // generate new foot at new place within the boundry
  26. if(x+1==x1 && y+1==y1 || x-1==x1 && y+1==y1 || x+1==x1 && y-1==y1 || x-1==x1 && y-1==y1)
  27. {
  28. // food counts number of dots accessed
  29. food++;
  30. againx:
  31. x1=rand();
  32. // these conditions are for generating food within the boundry
  33. if(x1<100 || x1>500)
  34. goto againx;
  35. againy:
  36. y1=rand();
  37. if(y1<100 || y1>400)
  38. goto againy;
  39. // this draws a dot for food
  40. circle(x1,y1,3);
  41. }
  42. // KEYS INFORMATION
  43. key=getch();
  44. if(key=='A' || key=='a')//move up
  45. y--;
  46. else if(key=='Z'||key=='z')//move right
  47. x++;
  48. else if(key=='L' || key=='l')//for left
  49. x--;
  50. else if(key=='.')//for downward
  51. y++;
  52. cleardevice();
  53. outtextxy(100,0,"SNAKE GAME BY JAVERIA");
  54.  
  55. outtextxy(100,20,"Z to move right");
  56. outtextxy(100,40,"L to move left");
  57. outtextxy(100,60,". to move down");
  58. outtextxy(100,80,"A to move up");
  59. // boundry line and the circle
  60. rectangle(100,100,500,400);
  61. circle(x1,y1,3);
  62. // you can control the circle of green color to reach to food
  63. setcolor(2);
  64.  
  65. circle(x,y,4);
  66. setcolor(WHITE);
  67.  
  68. }
  69. outtextxy(100,420,"You are out");
  70. cout<<"\n\n\n\tYOUR TOTAL SCORES ARE "<<food<<endl<<endl;
  71. //conditions so that the game exit when you press space
  72. char exitt;
  73. again:
  74. exitt=getch();
  75. if(exitt==32)
  76. closegraph();
  77. else
  78. goto again;
  79.  
  80. return 0;
  81.  
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement