-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
80 lines (65 loc) · 1.6 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <SFML/Graphics.hpp>
#include <iostream>
#include "snake.h"
#include "score.h"
Snake snake;
Walls walls;
Food food;
Score score;
float counter = 0;
void levelUp() {
snake.speed(0.1);
snake.grow();
score.point();
food.move();
}
void gameOver() {
snake = Snake();
score.reset();
food.move();
}
int main()
{
snake.changeDir(down);
srand(time(0));
// create the window
sf::RenderWindow window(sf::VideoMode(600, 600), "My window");
//window.setVerticalSyncEnabled(true); // call it once, after creating the window
window.setFramerateLimit(60); // call it once, after creating the window
// run the program as long as the window is open
while (window.isOpen())
{
// check all the window's events that were triggered since the last iteration of the loop
sf::Event event;
while (window.pollEvent(event))
{
// "close requested" event: we close the window
if (event.type == sf::Event::Closed)
window.close();
}
window.clear(sf::Color::Black);
bool ateFood = snake.checkForCollision(food);
bool crashSelf = snake.checkForSelfCollision();
bool crashWall = snake.checkForCollision(walls);
if (ateFood) levelUp();
if (crashSelf || crashWall) gameOver();
snake.keyboardListener();
if (counter <= 0) {
snake.move(11);
counter = snake.speed();
} else {
counter--;
}
for ( auto & segment : snake.segments ) {
window.draw(segment.get());
}
for ( auto & wall : walls.get() ) {
window.draw(wall);
}
window.draw(food.get());
window.draw(score.get());
// end the current frame
window.display();
}
return 0;
}