-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.cpp
86 lines (72 loc) · 2.17 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
// Created by Srijita Mallick on 18/10/20.
#include "game.h"
#include "ui.h"
#include "ncurses.h"
#include "constants.h"
#include "food.h"
#include "snake.h"
// Initialising game status
int current_game_state = kStateGameBeforeStart;
int current_score = 0;
int current_direction = KEY_UP;
// Prints game status
void PrintStatus() {
move(0, 5);
if (current_game_state == kStateGameBeforeStart) {
printw("Press any key to start.");
} else {
printw("Current Score: %d", current_score);
}
}
// Starts the game and snake moves
void StartGame() {
current_score = 0;
current_game_state = kStateGameStarted;
current_direction = KEY_UP;
erase();
InitFood();
InitSnake();
PrintFood();
}
// Moves the snake ahead
void tick(int key) {
// move the game ahead by 1 tick
if (current_game_state == kStateGameBeforeStart) {
erase();
PrintBorder();
PrintStatus();
if (key != ERR) {
StartGame();
}
} else {
erase();
std::pair< int, int > snake_head_coords;
if (key == KEY_UP && current_direction != KEY_DOWN) {
// snake_head_coords = MoveSnake(KEY_UP);
current_direction = KEY_UP;
} else if (key == KEY_DOWN && current_direction != KEY_UP ) {
// snake_head_coords = MoveSnake(KEY_DOWN);
current_direction = KEY_DOWN;
} else if (key == KEY_LEFT && current_direction != KEY_RIGHT) {
// snake_head_coords = MoveSnake(KEY_LEFT);
current_direction = KEY_LEFT;
} else if (key == KEY_RIGHT && current_direction != KEY_LEFT) {
// snake_head_coords = MoveSnake(KEY_RIGHT);
current_direction = KEY_RIGHT;
}
snake_head_coords = MoveSnake(current_direction);
if (key != ERR) {
if (ConsumeFood(snake_head_coords.first, snake_head_coords.second)) {
GrowSnake();
current_score += 1;
}
}
PrintBorder();
PrintStatus();
PaintSnake();
PrintFood();
if (DetectCollision()) {
current_game_state = kStateGameBeforeStart;
}
}
}