-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
51 lines (41 loc) · 1.12 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include "Snake.h"
#include <stdlib.h>
#include <thread>
#include <time.h>
using namespace std;
sf::CircleShape createFruit(const Snake & s,const sf::Color & color){
srand(time(NULL));
int x;
int y;
while(true){
x = rand() % constants::windowsWidth;
y = rand() % constants::windowsHeight;
if(!s.isPointInsideSnake(x,y)){
break;
}
}
return s.createSnakeBodyElement(x,y,color);
}
int main(){
sf::RenderWindow window{{constants::windowsWidth,constants::windowsHeight},"Snake"};
window.setFramerateLimit(60);
Snake snake;
bool isEaten = true;
sf::CircleShape fruit;
while(true){
window.clear(sf::Color::Black);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Escape)) break;
if(isEaten){
fruit = createFruit(snake,sf::Color::Green);
isEaten = false;
}
window.draw(fruit);
if(!snake.update(fruit,isEaten)){
break;
}
snake.display(window);
window.display();
// std::this_thread::sleep_for(std::chrono::seconds(1));
}
return 0;
}