-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.cpp
117 lines (110 loc) · 2.59 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
#include "game.h"
#include <locale.h>
#include <thread>
#include <chrono>
#include <stdlib.h>
#include <time.h>
Game::Game(size_t wait) {
setlocale(LC_ALL, "");
seed = time(NULL);
initscr();
cbreak();
noecho();
keypad(stdscr, true);
getmaxyx(stdscr, rows, cols);
delay = wait;
if (has_colors()) {
use_default_colors();
start_color();
init_pair(1, COLOR_GREEN, -1);
}
}
Game::~Game() {
endwin();
printf("Score: %d\n", snake.score());
}
void Game::play() {
nodelay(stdscr, true);
// placing food first
placeFood();
while (snake.isAlive()) {
int ch = getch();
switch(ch) {
case KEY_UP:
case 'k':
case 'w':
snake.turn(Direction::up);
break;
case KEY_DOWN:
case 'j':
case 's':
snake.turn(Direction::down);
break;
case KEY_LEFT:
case 'h':
case 'a':
snake.turn(Direction::left);
break;
case KEY_RIGHT:
case 'l':
case 'd':
snake.turn(Direction::right);
break;
case 'q':
case 27: // escape key
return;
}
print();
// should eat if has food since will print at 0,0 otherwise
// if eat is after step
if (foodInSnake()) {
snake.eat();
placeFood();
}
snake.step();
std::this_thread::sleep_for(std::chrono::milliseconds(delay));
}
// mvprintw(rows / 2, cols / 2, "Score: %d", snake.score());
nodelay(stdscr, false);
getch();
}
void Game::print() {
clear();
//mvprintw(foodRow, foodCol, "∙");
auto seg = snake.getHead();
if (seg->row < 0)
seg->row = rows - 1;
if (seg->row >= rows)
seg->row = 0;
if (seg->col < 0)
seg->col = cols/2 - 1;
if (seg->col >= cols/2)
seg->col = 0;
attron(COLOR_PAIR(1));
while(seg) {
mvprintw(seg->row, seg->col*2, "██");
seg = seg->getNext();
}
attroff(COLOR_PAIR(1));
move(foodRow, foodCol);
refresh();
}
void Game::placeFood() {
srand(seed++);
int row = rand() % (rows - 1);
int col = rand() % (cols - 1);
foodRow = row;
foodCol = col;
if (foodInSnake())
return placeFood();
return;
}
bool Game::foodInSnake() {
auto seg = snake.getHead();
while (seg) {
if (foodRow == seg->row && foodCol/2 == seg->col)
return true;
seg = seg->getNext();
}
return false;
}