/
main.cpp
100 lines (82 loc) · 2.25 KB
/
main.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
#include <iostream>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <math.h>
#include "snake.h"
#include "screen.h"
using namespace std;
bool redraw(Screen &screen, Snake &snake){
for(int i=0; i < 40; i++){
for(int j=0; j < 30; j++){
if(i == 0 || i == 39)
screen.print('|', i, j, 2);
else if(j == 0 || j == 29)
screen.print('-', i, j, 2);
else
screen.print(' ', i, j, 2);
}
}
snake.move_snake();
screen.print('@', snake.apple.x, snake.apple.y, 1);
for(unsigned int i=0; i < snake.body.size(); i++){
screen.print('#', snake.body[i].x, snake.body[i].y, 4);
}
screen.print('0', snake.head.x, snake.head.y, 3);
char msg[30];
snprintf(msg, 20, "%-10s%9d", "Score", (int)snake.body.size() - 1);
for(unsigned int i=0; i < 19; i++){
screen.print(msg[i], 5+i, 33, 5);
}
if(snake.heading == 0){
snprintf(msg, 30, "Use Arrow Keys to Start");
for(unsigned int i=0; i < 23; i++){
screen.print(msg[i], 5+i, 29, 5);
}
}
screen.redraw();
if(snake.hit_detect()){
snprintf(msg, 11, "Game Over!");
for(int i=0; i < 10; i++){
screen.print(msg[i], 12 + i, 15, 2);
}
screen.redraw();
usleep(1500000);
return false;
}
return true;
}
int main(){
srand(time(NULL));
Snake snake = Snake();
Screen screen;
clock_t last = clock();
clock_t now = clock();
int new_head;
float time_c = 1.0;
bool play = true;
redraw(screen, snake);
while(play){
now = clock();
int size = snake.body.size();
time_c = 0.5 * pow(2.71828, (-size * 0.046));
if(((float)(now - last) / CLOCKS_PER_SEC) > time_c){
last = clock();
play = redraw(screen, snake);
}
new_head = screen.get();
if(new_head != ERR){
if(new_head == 'q'){
break;
}
if(snake.change_dir(new_head)){
play = redraw(screen, snake);
last = clock();
}
}
screen.redraw();
}
screen.clear();
screen.destroy();
return 0;
}