AllenYuan

Untitled

May 26th, 2020
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <time.h>
  3. using namespace sf;
  4.  
  5. int N = 30, M = 20;
  6. int size = 16;
  7. int w = size * N;
  8. int h = size * M;
  9.  
  10. int dir, num = 4; // direction, size of snake
  11.  
  12. struct Snake
  13. { int x, y; } s[100]; // snake = array of points
  14.  
  15. struct Fruit
  16. { int x,y; } f;
  17.  
  18. // move the snake
  19. void Tick()
  20. {
  21. for (int i = num; i > 0; --i) // move snake body
  22. {
  23. s[i].x = s[i-1].x;
  24. s[i].y = s[i-1].y;
  25. }
  26.  
  27. // move snake head
  28. if (dir == 0) s[0].y += 1;
  29. if (dir == 1) s[0].x -= 1;
  30. if (dir == 2) s[0].x += 1;
  31. if (dir == 3) s[0].y -= 1;
  32.  
  33. // eat fruit => grow snake and regenerate fruit
  34. if ((s[0].x == f.x) && (s[0].y == f.y))
  35. {
  36. num++;
  37. f.x = rand() % N;
  38. f.y = rand() % M;
  39. }
  40.  
  41. // reflect the snake when it goes through sides
  42. if (s[0].x > N) s[0].x = 0; if (s[0].x < 0) s[0].x = N;
  43. if (s[0].y > M) s[0].y = 0; if (s[0].y < 0) s[0].y = M;
  44.  
  45. // chop the snake when it collides with itself
  46. for (int i = 1; i < num; i++)
  47. if (s[0].x == s[i].x && s[0].y == s[i].y) num = i;
  48. }
  49.  
  50. int main()
  51. {
  52. srand(time(0)); // seed rng
  53. RenderWindow window(VideoMode(w, h), "Snake Game!");
  54.  
  55. Texture t1,t2,t3;
  56. t1.loadFromFile("/Users/alleyuan/code-snippets/16_games/04 Snake/images/white.png");
  57. t2.loadFromFile("/Users/alleyuan/code-snippets/16_games/04 Snake/images/red.png");
  58. t3.loadFromFile("/Users/alleyuan/code-snippets/16_games/04 Snake/images/green.png");
  59.  
  60. Sprite sprite1(t1);
  61. Sprite sprite2(t2);
  62. Sprite sprite3(t3);
  63.  
  64. Clock clock; // timer
  65. float timer = 0, delay = 0.1;
  66.  
  67. // coords of first fruit
  68. f.x = 10;
  69. f.y = 10;
  70.  
  71. // game loop
  72. while(window.isOpen())
  73. {
  74. float time = clock.getElapsedTime().asSeconds(); // set timer
  75. clock.restart();
  76. timer += time;
  77.  
  78. // event handlers
  79. Event e;
  80. while (window.pollEvent(e))
  81. {
  82. // window close
  83. if (e.type == Event::Closed)
  84. window.close();
  85. }
  86.  
  87. // movement event handlers
  88. if (Keyboard::isKeyPressed(Keyboard::Left)) dir = 1;
  89. if (Keyboard::isKeyPressed(Keyboard::Right)) dir = 2;
  90. if (Keyboard::isKeyPressed(Keyboard::Up)) dir = 3;
  91. if (Keyboard::isKeyPressed(Keyboard::Down)) dir = 0;
  92.  
  93. // move the snake every <delay> seconds
  94. if (timer > delay) {
  95. timer = 0;
  96. Tick();
  97. }
  98.  
  99. window.clear();
  100.  
  101. // DRAW
  102. for (int i = 0; i < N; i++) // draw game grid
  103. for (int j = 0; j < M; j++)
  104. { sprite1.setPosition(i*size,j*size); window.draw(sprite1); }
  105.  
  106. for (int i = 0; i < num; i++) // draw snake
  107. {
  108. sprite2.setPosition(s[i].x*size, s[i].y*size);
  109. window.draw(sprite2);
  110. }
  111.  
  112. sprite3.setPosition(f.x * size, f.y*size);
  113. window.draw(sprite3);
  114.  
  115. window.display();
  116. }
  117.  
  118. return 0;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment