-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake.cc
More file actions
160 lines (138 loc) · 2.76 KB
/
snake.cc
File metadata and controls
160 lines (138 loc) · 2.76 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <iostream>
#include <ncurses.h>
#include <cstdlib>
bool gameOver;
const int width = 20, height = 20;
int x, y, Fx, Fy, score;
enum eDirection {STOP = 0, UP, LEFT, RIGHT, DOWN};
eDirection dir;
int Tx[100], Ty[100];
int nTail = 0;
void setup()
{
initscr();
clear();
noecho();
cbreak();
curs_set(0);
gameOver = false;
dir = STOP;
x = width/2;
y = height/2;
Fx = (rand() % width) + 1;
Fy = (rand() % height) + 1;
score = 0;
}
void input()
{
keypad(stdscr, TRUE);
halfdelay(1); // the snake moves after pressing key once
int c = getch();
switch(c)
{
case KEY_LEFT: dir = LEFT; break;
case KEY_RIGHT: dir = RIGHT; break;
case KEY_UP: dir = UP; break;
case KEY_DOWN: dir = DOWN; break;
case 113: gameOver = true; break;
}
}
void draw()
{
clear();
for (int i = 0; i < width + 2; i++)
{
mvprintw(0, i, "+");
}
for (int i = 0; i < height + 2; i++)
{
for (int j = 0; j < width + 2; j++)
{
if (i == 0 || i == 21)
{
mvprintw(i, j, "+");
}
else if (j == 0 || j == 21)
{
mvprintw(i, j, "+");
}
else if (i == y && j == x)
{
mvprintw(i, j, "0");
}
else if (i == Fy && j == Fx)
{
mvprintw(i, j, "@");
}
else{
for (int k = 0; k < nTail; k++)
{
if (Tx[k] == j && Ty[k] == i)
{
mvprintw(i, j, "o");
}
}
}
}
}
mvprintw(23, 0, "score = %d", score);
refresh();
}
void logic()
{
int Px;
int Py;
int P2x, P2y;
Tx[0] = x;
Ty[0] = y;
Px = Tx[0];
Py = Ty[0];
for (int i = 0; i < nTail; i++)
{
P2x = Tx[i];
P2y = Ty[i];
Tx[i] = Px;
Ty[i] = Py;
Px = P2x;
Py = P2y;
}
switch(dir)
{
case LEFT: x--; break;
case RIGHT: x++; break;
case UP: y--; break;
case DOWN: y++; break;
default: break;
}
if (x < 1 || x > width || y < 1 || y > height)
{
gameOver = true;
}
if (x == Fx && y == Fy)
{
score++;
Fx = (rand()%width) + 1;
Fy = (rand()%height) + 1;
nTail++;
}
for (int i = 0; i < nTail; i++)
{
if (Tx[i] == x && Ty[i] == y)
{
gameOver = true;
}
}
}
int main()
{
setup();
while(!gameOver)
{
draw();
input();
logic();
}
getch();
endwin();
return 0;
}