-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.cpp
123 lines (105 loc) · 2.41 KB
/
game.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
#include "game.h"
int direction;
int game_state= BEFORE_START;
int score;
int lives;
bool snake_displayed = false;
void start_game()
{
init_snake();
init_food();
score = 0;
lives = INITIAL_LIVES;
direction = UP;
game_state = STARTED;
}
void end_game(){
game_state = ENDED;
}
void paint_status()
{
move(0,5);
attron(COLOR_PAIR(STATUS_COLOR_PAIR));
printw("[Score %d]", score);
move(0, COLS-20);
addstr("[Lives: ");
for(int i=0; i<lives; i++){
addstr("\u2665");
}
addstr("]");
if(game_state == BEFORE_START){
move(LINES-1,50);
addstr("Press space to start");
} else if(game_state == STARTED) {
//
}
else if(game_state == SNAKE_RESET) {
move(LINES-1,50);
addstr("YOU JUST LOST A LIFE! Press Space to continue...");
} else {
move(LINES-1,50);
addstr("GAME OVER! [Press SPACE to restart] [Q to quit] ");
}
attroff(COLOR_PAIR(STATUS_COLOR_PAIR));
attron(A_BOLD);
}
bool execute_frame()
{
paint_border();
paint_status();
char key=getch();
if(game_state == BEFORE_START){
if(key==32)
{
start_game();
}
} else if(game_state == STARTED){
if(key==UP && direction != DOWN) {
direction=UP;
}
else if(key==DOWN && direction != UP) {
direction=DOWN;
}
else if(key==RIGHT && direction != LEFT) {
direction=RIGHT;
}
else if(key==LEFT && direction != RIGHT) {
direction=LEFT;
}
pair<int, int>head = move_snake(direction);
if(try_consume_food(head)){
grow_snake();
score++;
}
paint_snake();
paint_food();
if(has_collision()){
lives--;
reset_snake();
game_state = SNAKE_RESET;
if(lives == -1){
end_game();
}
}
} else if(game_state == SNAKE_RESET) {
if(key == 32){
// direction = RIGHT;
game_state = STARTED;
}
if(snake_displayed){
paint_snake();
}
snake_displayed = !snake_displayed;
paint_food();
} else {
paint_snake();
paint_food();
if(key == 32){
start_game();
}
if(key == QUIT){
return true;
}
}
return false;
}