-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmySnake.cpp
206 lines (205 loc) · 5.8 KB
/
mySnake.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
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
#include <stdio.h>
#include <stdlib.h>
#include "windowsLTP.cpp"
#include <string.h>
#include <conio.h>
#include <time.h>
//---------------------------------------------------------------------------
//map will be 100x25
#define MaxX 100
#define MaxY 25
//---------------------------------------------------------------------------
bool endgame = false, mode1 = false, mode2 = false;
int n,m,speed,mode;
//---------------------------------------------------------------------------
enum Status {len,xuong,trai,phai};
struct point { int x,y; };
struct game {
int length;
int countfood;
Status move;
point snake[50];
point food;
} player;
//---------------------------------------------------------------------------
void map(){
for(int i = 0; i <= MaxX; i++){
gotoxy(i,0); puts("+");
gotoxy(i,MaxY); puts("+");
}
for(int j = 0; j <= MaxY; j++){
gotoxy(0,j); puts("+");
gotoxy(MaxX,j); puts("+");
}
}
void show() {
int j = 0;
while(1) {
SetColor(j);
printf("\n");
printf("\t\t ___________________________________\n");
printf("\t\t| |\n");
printf("\t\t| GAME SNAKE IN C CODE |\n");
printf("\t\t| *Author: LTP* |\n");
printf("\t\t| |\n"); //ko chon duoc dau :))
printf("\t\t| |\n"); //luoi lam
printf("\t\t| - Play - |\n"); //lam mau thoi
printf("\t\t| |\n");
printf("\t\t| Chose your color |\n");
printf("\t\t|___________________________________|\n\n");
Sleep(200);
if (j == 15) j = 0;
if (kbhit()) break;
system("cls");
j++;
}
}
void start(){
show();
getch();
printf("\t\tSpace to pause\n\t\tA,W,S,D to move\n\t\tEat food(*), Careful block(X)\n\t\t------------------------\n\n");
printf("\t\tNhap n = "); scanf("%d",&n);
printf("\n\t\tNhap speed (fast = 1, little fast = 2, little slow = 3, slow = 4) : "); scanf("%d",&m);
printf("\n\t\tMode 1: Co Dien\tMode 2: Xuyen Tuong\t"); scanf("%d",&mode);
if(mode == 1){ //co dien
mode1 = true;
}
if(mode == 2){ //xuyen tuong
mode2 = true;
}
switch(m) {
case 1 : speed = 50; break;
case 2 : speed = 100; break;
case 3 : speed = 150; break;
case 4 : speed = 200; break;
}
system("cls");
map();
endgame = false;
}
void khoitao(game &player,int n) {
player.length = n+1;
//start in (15,5)
for (int i = 0; i < player.length; i++) {
player.snake[i].x = i+10;
player.snake[i].y = 3;
}
player.move = trai;
player.countfood = 0;
}
void xuyentuong(game &player){
if (player.snake[0].x <= 0) {
player.snake[0].x = MaxX - 1;
} else if (player.snake[0].y <= 0) {
player.snake[0].y = MaxY - 1;
} else if (player.snake[0].x >= MaxX) {
player.snake[0].x = 1;
} else if (player.snake[0].y >= MaxY) {
player.snake[0].y = 1;
}
}
void drawsnake(game &player) {
if(mode2 == true){
xuyentuong(player);
}
for (int i = player.length; i > 0 ; i--) {
SetColor(2);
player.snake[i].x = player.snake[i-1].x;
player.snake[i].y = player.snake[i-1].y;
//check dieu kien
if (player.snake[i].x > 0 && player.snake[i].y > 0 && player.snake[i].x < MaxX && player.snake[i].y < MaxY) {
gotoxy(player.snake[i].x,player.snake[i].y); printf("o");
gotoxy(player.snake[player.length].x,player.snake[player.length].y); printf(" ");
SetColor(9);
gotoxy(player.snake[0].x,player.snake[0].y); printf("o");
}
}
SetColor(7);
}
bool needfood() {
if (player.countfood == 1) {
return false;
}
for (int i = 1; i < player.length; i++) {
if (player.food.x == player.snake[i].x && player.food.y == player.snake[i].y) {
return true;
}
}
return true;
}
void drawfood(game &player) {
if (needfood()) {
srand(time(0));
player.food.x = 1 + rand() % 99;
player.food.y = 1 + rand() % 24;
SetColor(3);
gotoxy(player.food.x, player.food.y); printf("*");
SetColor(7);
player.countfood = 1;
}
}
void dieukhien(char key, game &player) {
if (key == 'W' || key == 'w') player.move = len;
else if (key == 'S' || key == 's') player.move = xuong;
else if (key == 'D' || key == 'd') player.move = phai;
else if (key == 'A' || key == 'a') player.move = trai;
else if (key == ' ') { getch(); } //enter to pause
}
void move(game &player) {
if (player.move == len) player.snake[0].y--;
else if (player.move == xuong) player.snake[0].y++;
else if (player.move == trai) player.snake[0].x--;
else if (player.move == phai) player.snake[0].x++;
}
void eatfood(game &player) {
if (player.snake[0].x == player.food.x && player.snake[0].y == player.food.y) {
player.length ++;
player.countfood = 0;
}
}
bool die() {
if(mode1 == true){
if (player.snake[0].x <= 0 || player.snake[0].y <= 0 || player.snake[0].x >= MaxX || player.snake[0].y >= MaxY) {
endgame = true;
}
}
for (int i = 5; i < player.length; i++) {
if (player.snake[0].x == player.snake[i].x && player.snake[0].y == player.snake[i].y) {
endgame = true;
}
}
}
void checkendgame() {
if (endgame == true) {
for(int i = 5; i > 0; i--){
gotoxy(20,25);
printf("Game Over!!! Play Again in %d s . . . Press Space to Exit",i);
if (kbhit()) {
char c = getch();
if(c = ' ') exit(0);
}
Sleep(1000);
}
start();
khoitao(player,n);
}
}
//---------------------------------------------------------------------------
int main() {
HideCur(false);
start();
khoitao(player,n);
while (1) {
drawfood(player);
if (kbhit()) {
char key = getch();
dieukhien(key,player);
}
move(player);
drawsnake(player);
eatfood(player);
die();
checkendgame();
Sleep(speed);
}
}