AllenYuan

Untitled

May 26th, 2020
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 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.  
  42. int main()
  43. {
  44. srand(time(0)); // seed rng
  45. RenderWindow window(VideoMode(w, h), "Snake Game!");
  46.  
  47. Texture t1,t2,t3;
  48. t1.loadFromFile("/Users/alleyuan/code-snippets/16_games/04 Snake/images/white.png");
  49. t2.loadFromFile("/Users/alleyuan/code-snippets/16_games/04 Snake/images/red.png");
  50. t3.loadFromFile("/Users/alleyuan/code-snippets/16_games/04 Snake/images/green.png");
  51.  
  52. Sprite sprite1(t1);
  53. Sprite sprite2(t2);
  54. Sprite sprite3(t3);
  55.  
  56. Clock clock; // timer
  57. float timer = 0, delay = 0.1;
  58.  
  59. // coords of first fruit
  60. f.x = 10;
  61. f.y = 10;
  62.  
  63. // game loop
  64. while(window.isOpen())
  65. {
  66. float time = clock.getElapsedTime().asSeconds(); // set timer
  67. clock.restart();
  68. timer += time;
  69.  
  70. // event handlers
  71. Event e;
  72. while (window.pollEvent(e))
  73. {
  74. // window close
  75. if (e.type == Event::Closed)
  76. window.close();
  77. }
  78.  
  79. // movement event handlers
  80. if (Keyboard::isKeyPressed(Keyboard::Left)) dir = 1;
  81. if (Keyboard::isKeyPressed(Keyboard::Right)) dir = 2;
  82. if (Keyboard::isKeyPressed(Keyboard::Up)) dir = 3;
  83. if (Keyboard::isKeyPressed(Keyboard::Down)) dir = 0;
  84.  
  85. // move the snake every <delay> seconds
  86. if (timer > delay) {
  87. timer = 0;
  88. Tick();
  89. }
  90.  
  91. window.clear();
  92.  
  93. // DRAW
  94. for (int i = 0; i < N; i++) // draw game grid
  95. for (int j = 0; j < M; j++)
  96. { sprite1.setPosition(i*size,j*size); window.draw(sprite1); }
  97.  
  98. for (int i = 0; i < num; i++) // draw snake
  99. {
  100. sprite2.setPosition(s[i].x*size, s[i].y*size);
  101. window.draw(sprite2);
  102. }
  103.  
  104. sprite3.setPosition(f.x * size, f.y*size);
  105. window.draw(sprite3);
  106.  
  107. window.display();
  108. }
  109.  
  110. return 0;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment