-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnake.cpp
More file actions
235 lines (205 loc) · 6.97 KB
/
Copy pathSnake.cpp
File metadata and controls
235 lines (205 loc) · 6.97 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include "Snake.h"
#include "Circle.h"
#include <algorithm>
#include <sstream>
#include <string>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#define min(x, y) (x < y ? x : y)
bool Snake::Initialize()
{
if (!Game::Initialize()) {
return false;
}
if (TTF_Init() != 0) {
SDL_Log("Unable to initialize TTF: %s", TTF_GetError());
return false;
}
font = TTF_OpenFont(FONT, FONT_SIZE);
if (!font) {
SDL_Log("Failed to load font: %s", TTF_GetError());
return false;
}
Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048);
music = Mix_LoadMUS(MUSIC);
turnSound = Mix_LoadWAV(TURN_SOUND);
eatSound = Mix_LoadWAV(EAT_SOUND);
bangSound = Mix_LoadWAV(BANG_SOUND);
highScore = 0;
srand(time(NULL));
ResetGame();
return true;
}
void Snake::Shutdown()
{
TTF_CloseFont(font);
TTF_Quit();
Mix_Quit();
Game::Shutdown();
}
void Snake::ResetGame()
{
gameOver = false;
snakeLength = INITIAL_LENGTH;
snake.clear();
for (int i = 0; i <= snakeLength; ++i) {
snake.push_back(Cell(3 + i, 5));
}
progress = 0;
direction = Cell(1, 0);
prevDirection = direction;
fruitPos = Cell(12, 5);
if (!Mix_PlayingMusic()) {
Mix_PlayMusic(music, -1);
} else {
Mix_ResumeMusic();
}
}
void Snake::ProcessKeydown(SDL_Keycode sym)
{
if (gameOver) {
if (sym == SDLK_SPACE) {
ResetGame();
}
return;
}
if (sym == SDLK_UP) {
direction = Cell(0, -1);
} else if (sym == SDLK_DOWN) {
direction = Cell(0, 1);
} else if (sym == SDLK_LEFT) {
direction = Cell(-1, 0);
} else if (sym == SDLK_RIGHT) {
direction = Cell(1, 0);
}
if (!(direction == prevDirection)) {
prevDirection = direction;
Mix_PlayChannel(-1, turnSound, 0);
}
}
void Snake::UpdateGame(float deltaTime)
{
if (gameOver) {
return;
}
progress += deltaTime * 4;
if (progress >= 0.3f) {
Cell head = snake.back();
if ((head.x < 0 || head.x >= SCREEN_WIDTH / CELL_SIZE) ||
(head.y < 0 || head.y >= SCREEN_HEIGHT / CELL_SIZE) ||
std::find(snake.begin() + 1, snake.end() - 1, head) != snake.end() - 1) {
gameOver = true;
Mix_PauseMusic();
Mix_PlayChannel(-1, bangSound, 0);
}
}
if (progress >= 1.0f) {
progress -= 1.0f;
if (snake.size() > snakeLength) {
snake.pop_front();
}
Cell head = snake.back();
snake.push_back(head + direction);
if (head == fruitPos) {
++snakeLength;
do {
fruitPos.x = rand() % (SCREEN_WIDTH / CELL_SIZE);
fruitPos.y = rand() % (SCREEN_HEIGHT / CELL_SIZE);
} while (std::find(snake.begin(), snake.end(), fruitPos) != snake.end());
Mix_PlayChannel(-1, eatSound, 0);
}
}
}
void Snake::GenerateOutput()
{
for (int y = 0; y < SCREEN_HEIGHT; y += CELL_SIZE) {
for (int x = 0; x < SCREEN_WIDTH; x += CELL_SIZE) {
if ((x + y) / CELL_SIZE % 2 == 0) {
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
} else {
SDL_SetRenderDrawColor(renderer, 0, 224, 0, 255);
}
SDL_Rect cell = { x, y, CELL_SIZE, CELL_SIZE };
SDL_RenderFillRect(renderer, &cell);
}
}
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
SDL_RenderFillCircle(renderer,
fruitPos.x * CELL_SIZE + CELL_SIZE / 2,
fruitPos.y * CELL_SIZE + CELL_SIZE / 2,
SNAKE_RADIUS);
SDL_SetRenderDrawColor(renderer, 0, 64, 255, 255);
for (int i = 0; i < snake.size() - 1; ++i) {
Cell cell = snake[i];
Cell next = snake[i + 1];
int x = cell.x * CELL_SIZE + CELL_SIZE / 2;
int y = cell.y * CELL_SIZE + CELL_SIZE / 2;
int x2 = next.x * CELL_SIZE + CELL_SIZE / 2;
int y2 = next.y * CELL_SIZE + CELL_SIZE / 2;
if (i == 0 && snake.size() == snakeLength + 1) {
x += static_cast<int>(roundf((x2 - x) * progress));
y += static_cast<int>(roundf((y2 - y) * progress));
} else if (i == snake.size() - 2) {
x2 = x + static_cast<int>(roundf((x2 - x) * progress));
y2 = y + static_cast<int>(roundf((y2 - y) * progress));
}
SDL_RenderFillCircle(renderer, x, y, SNAKE_RADIUS);
SDL_Rect rect = { min(x, x2), min(y, y2), abs(x2 - x), abs(y2 - y) };
if (cell.x == next.x) {
rect.x -= SNAKE_RADIUS;
rect.w = SNAKE_RADIUS * 2 + 1;
} else {
rect.y -= SNAKE_RADIUS;
rect.h = SNAKE_RADIUS * 2 + 1;
}
SDL_RenderFillRect(renderer, &rect);
if (i == snake.size() - 2) {
x = x2;
y = y2;
if (gameOver) {
SDL_SetRenderDrawColor(renderer, 192, 0, 255, 255);
}
SDL_RenderFillCircle(renderer, x, y, SNAKE_RADIUS);
SDL_SetRenderDrawColor(renderer, 255, 255, 0, 255);
if (cell.x == next.x) {
SDL_RenderFillCircle(renderer, x - SNAKE_RADIUS / 2, y, SNAKE_RADIUS / 4);
SDL_RenderFillCircle(renderer, x + SNAKE_RADIUS / 2, y, SNAKE_RADIUS / 4);
} else {
SDL_RenderFillCircle(renderer, x, y - SNAKE_RADIUS / 2, SNAKE_RADIUS / 4);
SDL_RenderFillCircle(renderer, x, y + SNAKE_RADIUS / 2, SNAKE_RADIUS / 4);
}
}
}
if (gameOver) {
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 128);
SDL_Rect rect = { 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT };
SDL_RenderFillRect(renderer, &rect);
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE);
int score = snakeLength - INITIAL_LENGTH;
if (score > highScore) {
highScore = score;
}
RenderText("Game Over", SCREEN_WIDTH / 2, SCREEN_HEIGHT / 5);
std::stringstream scoreStream;
scoreStream << "Score " << score;
RenderText(scoreStream.str().c_str(), SCREEN_WIDTH / 2, SCREEN_HEIGHT * 2 / 5);
std::stringstream highScoreStream;
highScoreStream << "High Score " << highScore;
RenderText(highScoreStream.str().c_str(), SCREEN_WIDTH / 2, SCREEN_HEIGHT * 3 / 5);
RenderText("Press Space", SCREEN_WIDTH / 2, SCREEN_HEIGHT * 4 / 5);
}
SDL_RenderPresent(renderer);
}
void Snake::RenderText(const char* text, int x, int y)
{
SDL_Color color = { 255, 255, 255, 255 };
SDL_Surface* surface = TTF_RenderUTF8_Blended(font, text, color);
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_Rect textRect = { 0, 0, surface->w, surface->h };
SDL_Rect rect = { x - surface->w / 2, y - surface->h / 2, surface->w, surface->h };
SDL_RenderCopy(renderer, texture, &textRect, &rect);
SDL_DestroyTexture(texture);
SDL_FreeSurface(surface);
}