-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake.c
More file actions
188 lines (170 loc) · 4.81 KB
/
Copy pathsnake.c
File metadata and controls
188 lines (170 loc) · 4.81 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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <windows.h>
#include <time.h>
//define
#define WIDTH 25
#define HEIGHT 25
#define SLEEP_TIME 100
//全局变量
int gameOver = 0;
int x, y, foodX, foodY, score;
int tailX[100], tailY[100]; // 蛇身坐标数组
int dir; // 蛇的移动方向: 1上, 2下, 3左, 4右
void Setup();
void Input();
void Logic();
void Draw();
void gotoxy(int cursorX, int cursorY);
int main() {
Setup(); // 初始化
while (!gameOver) {
Input(); // 检测玩家有没有按键
Logic(); // 根据按键和时间,更新蛇的位置和游戏状态
Draw(); // 画出当前画面
Sleep(SLEEP_TIME); // 暂停一下控制速度
}
// 游戏结束的提示
gotoxy(WIDTH / 2 - 4, HEIGHT / 2);
printf("GAME OVER!");
gotoxy(WIDTH / 2 - 8, HEIGHT / 2 + 1);
printf("Final Score: %d\n", score);
gotoxy(0, HEIGHT + 2); // 把结束后的终端提示顶到最下面,不影响画面
// 按任意键后再退出
printf("Press any key to exit...");
while(_kbhit()) _getch(); // 清空一下残留的按键
_getch(); //
return 0;
}
void gotoxy(int cursorX, int cursorY) {
COORD coord;
coord.X = cursorX;
coord.Y = cursorY;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void HideCursor() {
HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO info;
GetConsoleCursorInfo(consoleHandle, &info);
info.bVisible = FALSE; // 设置为不可见!
SetConsoleCursorInfo(consoleHandle, &info);
}
void Setup() {
HideCursor();
srand(time(NULL)); // 用当前时间作为随机数种子
x=13;
y=13;
score=0;
// 这会覆盖掉文件最上面的全局变量 tailX 和 tailY
// int tailX[100]={0};
// int tailY[100]={0};
foodX=rand()%WIDTH;
foodY=rand()%HEIGHT;
dir = 0; // 初始停止
}
void Draw(){
gotoxy(0, 0);
for(int i=0;i<HEIGHT;i++){
for(int j=0;j<WIDTH;j++){
if(i==0 || i==HEIGHT-1 || j==0 || j==WIDTH-1){
printf("#");
}
// i 是行/Y,j 是列/X
else if(j==x && i==y){
printf("O");
}
else if(j==foodX && i==foodY){
printf("@");
}
else{
int isBody = 0;
for(int k = 0;k < score;k++){
if (tailX[k] == j && tailY[k] == i){
printf("o");
isBody = 1;
break;
}
}
if (isBody == 0) {
printf(" ");
}
}
}
printf("\n");
}
printf("Score: %d\n", score);
}
void Input(){
if (_kbhit()) {
switch (_getch()) {
case 'w': dir = 1; break;
case 's': dir = 2; break;
case 'a': dir = 3; break;
case 'd': dir = 4; break;
case 'x': gameOver = 1; break;
}
}
}
void Logic(){
for (int k = score-1; k > 0; k--){
tailX[k] = tailX[k-1];
tailY[k] = tailY[k-1];
}
if (score > 0) {
tailX[0] = x;
tailY[0] = y;
}
switch (dir){
case 1: y=y-1;break;
case 2: y=y+1;break;
case 3: x=x-1;break;
case 4: x=x+1;break;
}
if(x >= WIDTH - 1 || x <= 0 || y >= HEIGHT - 1 || y <= 0){
gameOver = 1;
return; )
}
if(x == foodX && y == foodY){
score++;
int isOk = 0;
while(isOk == 0){
// 确保食物生成在墙壁内部 (1 到 WIDTH-2 之间)
foodX = 1 + rand() % (WIDTH - 2);
foodY = 1 + rand() % (HEIGHT - 2);
isOk = 1;
if(foodX == x && foodY == y){
isOk = 0;
}
if (isOk == 1) {
for(int k = 0; k < score; k++){
if (tailX[k] == foodX && tailY[k] == foodY){
isOk = 0;
break;
}
}
}
}
}
for (int k = 0; k < score; k++) {
if (tailX[k] == x && tailY[k] == y) {
gameOver = 1;
}
}
}
// 1 123456
// 7 123456
// 7 712345
//#########################
//#########################
//###################@#####
//#########################
//############+++++++++####
//############+############
//############+############
//#########################
//#########################
//#########################
//#########################
//#########################