-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnakeGame.cpp
167 lines (136 loc) · 3.27 KB
/
SnakeGame.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
#include "SnakeGame.h"
using namespace sf;
const int FIELD_WIDTH = 30;
const int FIELD_HEIGHT = 20;
const int BLOCK_SIZE = 16;
const int WIDTH = BLOCK_SIZE * FIELD_WIDTH;
const int HEIGHT = BLOCK_SIZE * FIELD_HEIGHT;
const int SNAKE_HEAD_START_POS_X = 15;
const int SNAKE_HEAD_START_POS_Y = 10;
const int INCREMENT_BODY = 2;
const int START_BODY = 4;
const int APPLE_START_POS_X = 5;
const int APPLE_START_POS_Y = 5;
const float DELAY = 0.1f;
SnakeGame::SnakeGame(const std::string& title, int width, int height) : Game(title, WIDTH, HEIGHT)
{
Reset();
}
SnakeGame::~SnakeGame()
{
}
void SnakeGame::Reset()
{
for (int i = 0; i < nSegments; ++i)
{
snakePos[i].x = SNAKE_HEAD_START_POS_X + i;
snakePos[i].y = SNAKE_HEAD_START_POS_Y;
}
direction = etDirection::eNONE;
nSegments = START_BODY;
applePos.x = APPLE_START_POS_X;
applePos.y = APPLE_START_POS_Y;
timer = 0.0f;
}
void SnakeGame::OnInit()
{
textureRed.loadFromFile("images/red.png");
textureWhite.loadFromFile("images/white.png");
textureGreen.loadFromFile("images/green.png");
spriteApple.setTexture(textureGreen);
spriteBackground.setTexture(textureWhite);
spriteSnake.setTexture(textureRed);
}
void SnakeGame::OnUpdate()
{
float time = clock.getElapsedTime().asSeconds();
clock.restart();
timer += time;
if (timer >= DELAY)
{
timer = 0.0f;
Tick();
}
}
void SnakeGame::OnDraw(::Window& window)
{
for (int i = 0; i < FIELD_WIDTH; ++i)
{
for (int j = 0; j < FIELD_HEIGHT; ++j)
{
spriteBackground.setPosition(i * BLOCK_SIZE, j * BLOCK_SIZE);
window.Draw(spriteBackground);
}
}
spriteApple.setPosition(applePos.x * BLOCK_SIZE, applePos.y * BLOCK_SIZE);
window.Draw(spriteApple);
for (int i = 0; i < nSegments; ++i)
{
spriteSnake.setPosition(snakePos[i].x * BLOCK_SIZE, snakePos[i].y * BLOCK_SIZE);
window.Draw(spriteSnake);
}
}
void SnakeGame::OnHandleInput()
{
if (Keyboard::isKeyPressed(Keyboard::Left) && direction != etDirection::eRIGHT)
direction = etDirection::eLEFT;
if (Keyboard::isKeyPressed(Keyboard::Right) && direction != etDirection::eLEFT)
direction = etDirection::eRIGHT;
if (Keyboard::isKeyPressed(Keyboard::Up) && direction != etDirection::eDOWN)
direction = etDirection::eUP;
if (Keyboard::isKeyPressed(Keyboard::Down) & direction != etDirection::eUP)
direction = etDirection::eDOWN;
}
void SnakeGame::Tick()
{
// Move snake
for (int i = nSegments - 1; i > 0; --i)
{
snakePos[i] = snakePos[i - 1];
}
switch (direction)
{
case etDirection::eDOWN:
snakePos[0].y += 1;
break;
case etDirection::eLEFT:
snakePos[0].x -= 1;
break;
case etDirection::eUP:
snakePos[0].y -= 1;
break;
case etDirection::eRIGHT:
snakePos[0].x += 1;
break;
default:
break;
}
// Check with borders
if (snakePos[0].x < 0 || snakePos[0].x > FIELD_WIDTH ||
snakePos[0].y < 0 || snakePos[0].y > FIELD_HEIGHT)
{
Reset();
}
// Check with body
for (int i = 1; i < nSegments; ++i)
{
if (snakePos[0] == snakePos[i])
{
Reset();
}
}
// Check with apple
if (applePos == snakePos[0])
{
applePos.x = rand() % FIELD_WIDTH;
applePos.y = rand() % FIELD_HEIGHT;
for (int i = nSegments; i < nSegments + INCREMENT_BODY; ++i)
{
snakePos[i] = snakePos[i - 1];
}
if (nSegments + INCREMENT_BODY < SEGMENT_MAX)
{
nSegments += INCREMENT_BODY;
}
}
}