forked from jcalvarezj/snake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
219 lines (188 loc) · 4.7 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
* Snake game program using the SDL library
*
* @author J. Alvarez
*/
#include <iostream>
#include <cstring>
#include <vector>
#include <time.h>
#include <stdlib.h>
#include "Snake.hpp"
#include "Food.hpp"
#include "Wall.hpp"
#include "Screen.hpp"
#include "SDL2/SDL.h"
#include "main.h"
using namespace SnakeGame;
bool holdGame(Screen & screen, int millis) {
int startTime = SDL_GetTicks();
bool quit = false;
while (SDL_GetTicks() - startTime < millis && !quit) {
if(screen.processEvents() == Screen::Action::QUIT)
quit = true;
}
return quit;
}
bool pauseGame(Screen & screen, bool & pause) {
int startTime = SDL_GetTicks();
bool quit = false;
while (!quit && pause) {
int action = screen.processEvents();
switch (action) {
case Screen::Action::QUIT:
quit = true;
break;
case Screen::Action::PAUSE:
pause = false;
break;
}
}
return quit;
}
void resetLevel(Snake & snake, Food & food, Food & food2, bool & starting) {
snake.die();
snake.reset();
food = Food();
food2 = Food();
starting = true;
}
void createWalls(std::vector<Wall *> & walls) {
const int N_HORIZONTAL = Screen::S_WIDTH / Wall::S_WALL_WIDTH;
const int N_VERTICAL = Screen::S_HEIGHT / Wall::S_WALL_WIDTH - 2;
for (int i = 0; i < N_HORIZONTAL; i++) {
Wall * upperWall = new Wall(i * Wall::S_WALL_WIDTH, 0, 0);
Wall * lowerWall = new Wall(i * Wall::S_WALL_WIDTH,
Screen::S_HEIGHT - 3 * Wall::S_WALL_WIDTH, 0);
walls.push_back(upperWall);
walls.push_back(lowerWall);
}
for (int i = 1; i < N_VERTICAL - 1; i++) {
Wall * leftmostWall = new Wall(0, i * Wall::S_WALL_WIDTH, 0);
Wall * rightmostWall = new Wall(Screen::S_WIDTH - Wall::S_WALL_WIDTH,
i * Wall::S_WALL_WIDTH, 0);
walls.push_back(leftmostWall);
walls.push_back(rightmostWall);
}
}
void drawWalls(std::vector<Wall *> & walls, Screen & screen) {
for (auto wall: walls)
wall->draw(screen);
}
void freeWalls(std::vector<Wall *> & walls) {
for (auto wall: walls)
delete wall;
walls.clear();
}
int main(int argc, char ** argv) {
srand(time(NULL));
Screen screen;
Snake snake;
Food food;
Food food2;
std::vector<Wall *> walls;
createWalls(walls);
int score = 0;
if (!screen.init()) {
SDL_Log("Error initializing screen");
return -1;
}
bool quit = false;
bool starting = true;
bool pause = false;
while (!quit && snake.m_lives > 0) {
screen.clear();
snake.draw(screen);
food.draw(screen);
food2.draw(screen);
drawWalls(walls, screen);
screen.update(score, snake.m_lives, false);
if (starting) {
quit = holdGame(screen, 1500);
starting = false;
}
switch (screen.processEvents()) {
case Screen::Action::QUIT:
quit = true;
break;
case Screen::Action::PAUSE:
pause = true;
break;
case Screen::Action::MOVE_UP:
if(!snake.m_hasUpdated)
snake.updateDirection(Snake::Direction::UP);
break;
case Screen::Action::MOVE_DOWN:
if(!snake.m_hasUpdated)
snake.updateDirection(Snake::Direction::DOWN);
break;
case Screen::Action::MOVE_LEFT:
if(!snake.m_hasUpdated)
snake.updateDirection(Snake::Direction::LEFT);
break;
case Screen::Action::MOVE_RIGHT:
if(!snake.m_hasUpdated)
snake.updateDirection(Snake::Direction::RIGHT);
break;
}
if (pause)
quit = pauseGame(screen, pause);
int elapsed = SDL_GetTicks();
if (elapsed/20 % 12 == 0) {
if (!snake.move())
resetLevel(snake, food, food2, starting);
else {
if (snake.collidesWith(food)) {
consumeFood(food, score, snake);
}
if (snake.collidesWith(food2)) {
consumeFood(food2, score, snake);
}
for (auto wall: walls)
if (snake.collidesWith(* wall))
resetLevel(snake, food, food2, starting);
for (int i = 1; i < snake.m_sections.size(); i++)
if (snake.collidesWith(* snake.m_sections[i]))
resetLevel(snake, food, food2, starting);
}
}
if (score >= 1000) {
snake.m_lives = 0;
screen.clear();
screen.drawGameWon();
screen.update(score, snake.m_lives, true);
holdGame(screen, 3000);
} else if (snake.m_lives == 0) {
screen.clear();
screen.drawGameOver();
screen.update(score, snake.m_lives, true);
holdGame(screen, 3000);
}
}
freeWalls(walls);
screen.close();
return 0;
}
void consumeFood(SnakeGame::Food &food, int &score, SnakeGame::Snake &snake)
{
int t = food.type;
food = Food();
switch (t)
{
case 1:
score += Food::S_VALUE_1;
snake.addSection();
break;
case 2:
score += Food::S_VALUE_2;
snake.addSection();
snake.addSection();
break;
case 3:
score += Food::S_VALUE_3;
snake.addSection();
snake.addSection();
snake.addSection();
break;
}
}