-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnakeGame.hpp
141 lines (124 loc) · 2.57 KB
/
SnakeGame.hpp
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
#ifndef SNAKEGAME_HPP
# define SNAKEGAME_HPP
# include "Board.hpp"
# include "Drawable.hpp"
# include "Apple.hpp"
# include "Empty.hpp"
# include "Snake.hpp"
# include "ScoreBoard.hpp"
# include <SDL2/SDL.h>
class SnakeGame
{
private:
Board board;
bool game_over;
Apple *apple;
Snake snake;
ScoreBoard score_board;
int score;
public:
SnakeGame(int height, int width);
~SnakeGame();
void initialize();
void process_input();
void update_state();
void redraw();
bool is_game_over();
};
SnakeGame::SnakeGame(int height, int width) {
this->board = Board(height, width);
int sb_x, sb_y;
sb_y = board.get_start_y() + height;
sb_x = board.get_start_x();
this->score_board = ScoreBoard(width, sb_y, sb_x);
initialize();
}
SnakeGame::~SnakeGame() {
delete apple;
}
void SnakeGame::initialize() {
this->score = 0;
score_board.initialize(this->score);
this->game_over = false;
this->apple = NULL;
srand(time(NULL));
SnakePiece next = SnakePiece(1, 1);
board.add(next);
snake.add_piece(next);
next = snake.next_head();
board.add(next);
snake.add_piece(next);
next = snake.next_head();
board.add(next);
snake.add_piece(next);
}
void SnakeGame::process_input() {
chtype input = board.get_input();
int old_timeout = board.get_timeout();
switch(input)
{
case KEY_UP:
case 'w':
snake.set_direction(UP);
break;
case KEY_DOWN:
case 's':
snake.set_direction(DOWN);
break;
case KEY_LEFT:
case 'a':
snake.set_direction(LEFT);
break;
case KEY_RIGHT:
case 'd':
snake.set_direction(RIGHT);
break;
case 'p':
board.pause(old_timeout);
break;
default:
break;
}
}
void SnakeGame::update_state() {
if (!apple)
{
int x, y;
board.get_empty_coordinates(y, x);
apple = new Apple(y, x);
board.add(*apple);
}
SnakePiece next = snake.next_head();
if (board.get_char_at(next.get_y(), next.get_x()) == '#' ||
next.get_x() < 1 || next.get_x() >= board.get_width() ||
next.get_y() < 1 || next.get_y() >= board.get_height() - 1)
{
this->game_over = true;
}
else if ((next.get_y() != apple->get_y() || next.get_x() != apple->get_x()))
{
int empty_y = snake.tail().get_y();
int empty_x = snake.tail().get_x();
board.add(Empty(empty_y, empty_x));
snake.remove_piece();
}
else {
delete apple;
apple = NULL;
this->score++;
score_board.update_score(this->score);
if (this->score % 5 == 0) {
board.increase_speed();
}
}
board.add(next);
snake.add_piece(next);
}
void SnakeGame::redraw() {
board.refresh();
score_board.refresh();
}
bool SnakeGame::is_game_over() {
return this->game_over;
}
#endif