-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.c
220 lines (194 loc) · 4.67 KB
/
game.c
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include <stdlib.h>
#include <ncurses.h>
#include <stdbool.h>
#include <time.h>
#include "include/snake.h"
snake snak;
fruit frut;
bool running;
unsigned int score;
int maxx, maxy;
int border_width;
//-----------Bounded random number function prototipe----------------//
int rand_from_to(int from, int to);
//------------------------------------------------------------------ //
void setup_game()
{
initscr();
noecho(); // dont print what i type
cbreak(); // process input as it happens
nodelay(stdscr, TRUE); // dont wait on getch()
curs_set(0); // dont show cursor
keypad(stdscr, TRUE); // allow arrow keys, Fkeys, etc.
getmaxyx(stdscr,maxy,maxx); // get terminal dimentions
box(stdscr,0,0);
border_width = 1;
score = 0;
mvprintw(0,maxx/2 - 4,"SCORE:%u",score);
snak = snake_create(maxx/2, maxy/2);
frut = fruit_create(rand_from_to(border_width, maxx-border_width), rand_from_to(border_width,maxy-border_width));
fruit_update(frut);
running = true;
}
void pause(int x, int y)
{
bool ispaused = true;
int width = 20;
int height = 5;
int px = x-(width/2);
int py = y-(height/2);
int pause_maxx,pause_maxy;
WINDOW *pause = newwin(height, width, py, px);
box(pause,0,0);
getmaxyx(pause,pause_maxy,pause_maxx);
mvwaddstr(pause,pause_maxy/2 , pause_maxx/2 - 3, "PAUSED");
wrefresh(pause);
while (ispaused)
{
switch (wgetch(pause))
{
case 'p':
case 'P':
werase(pause);
wrefresh(pause);
delwin(pause);
ispaused = false;
break;
case 'q':
case 'Q':
running = false;
ispaused = false;
break;
}
}
}
void game_over(int x, int y)
{
bool idle = true;
int width = 20;
int height= 11;
int px = x - (width/2);
int py = y - (height/2);
WINDOW *go = newwin(height,width,py,px);
int xmax = getmaxx(go);
int ymax = getmaxy(go);
mvwaddstr(go,ymax/2 -1,xmax/2 - 5, "YOU SUCK!");
mvwaddstr(go,ymax/2 +1,xmax/2 - 7, "New Game? (Y/N)");
wborder(go,'#','#','#','#','#','#','#','#');
wrefresh(go);
while (idle)
{
switch (wgetch(go))
{
case 'n':
case 'N':
running = false;
idle = false;
break;
case 'y':
case 'Y':
snak = snake_destroy(snak);
frut = fruit_destroy(frut);
delwin(go);
clear();
refresh();
endwin();
setup_game();
idle = false;
break;
}
}
}
void move_snake(snake s, int key)
{
direction snake_current_dir = snake_get_dir(s);
switch (key)
{
case KEY_UP:
if(snake_current_dir != DOWN)
snake_set_dir(s, UP);
break;
case KEY_DOWN:
if(snake_current_dir != UP)
snake_set_dir(s, DOWN);
break;
case KEY_LEFT:
if(snake_current_dir != RIGHT)
snake_set_dir(s, LEFT);
break;
case KEY_RIGHT:
if(snake_current_dir != LEFT)
snake_set_dir(s, RIGHT);
break;
default:
break;
}
}
bool is_coliding(snake s)
{
int sx,sy;
sx = snake_getx(s);
sy = snake_gety(s);
if(snake_is_suicidal(s) ||
sx <= getbegx(stdscr) ||
sy <= getbegy(stdscr) ||
sx >= maxx-border_width ||
sy >= maxy-border_width)
{
return true;
}
return false;
}
void check_input(int ch)
{
switch (ch)
{
case 'q':
case 'Q':
running = false;
break;
case 'p':
case 'P':
pause(maxx/2, maxy/2);
default:
break;
}
}
bool snake_is_eating(snake snak, fruit frut)
{
return (fruit_getx(frut) == snake_getx(snak)) && (fruit_gety(frut) == snake_gety(snak));
}
int rand_from_to(int from, int to)
{
return (rand() % (to - from)) + from;
}
void run_game()
{
srand(time(NULL));
setup_game();
int ch;
while (running)
{
ch = getch();
check_input(ch);
move_snake(snak,ch);
if(is_coliding(snak))
{
game_over(maxx/2,maxy/2);
}
if(snake_is_eating(snak,frut))
{
snake_eat(snak);
fruit_setx(frut, rand_from_to(border_width,maxx-border_width));
fruit_sety(frut, rand_from_to(border_width,maxy-border_width));
mvprintw(0,getmaxx(stdscr)/2 +2,"%u",++score);
}
snake_update(snak);
fruit_update(frut);
refresh();
delay_output(70);
}
snak = snake_destroy(snak);
frut = fruit_destroy(frut);
endwin();
}