帮我编写一个贪吃蛇游戏
时间: 2023-09-06 11:10:38 浏览: 36
好的,我可以为您提供一个简单的控制台版贪吃蛇游戏的代码,基于C语言实现。代码如下:
```c
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#define WIDTH 40
#define HEIGHT 20
int x, y, fruitX, fruitY, score, gameover;
int tailX[100], tailY[100];
int ntail = 0;
enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN };
enum eDirection dir;
void Setup() {
gameover = 0;
dir = STOP;
x = WIDTH / 2;
y = HEIGHT / 2;
fruitX = rand() % WIDTH;
fruitY = rand() % HEIGHT;
score = 0;
}
void Draw() {
system("cls");
int i, j;
for (i = 0; i < WIDTH + 2; i++)
printf("#");
printf("\n");
for (i = 0; i < HEIGHT; i++) {
for (j = 0; j < WIDTH; j++) {
if (j == 0)
printf("#");
if (i == y && j == x)
printf("O");
else if (i == fruitY && j == fruitX)
printf("F");
else {
int k;
int flag = 0;
for (k = 0; k < ntail; k++) {
if (tailX[k] == j && tailY[k] == i) {
printf("o");
flag = 1;
}
}
if (!flag)
printf(" ");
}
if (j == WIDTH - 1)
printf("#");
}
printf("\n");
}
for (i = 0; i < WIDTH + 2; i++)
printf("#");
printf("\n");
printf("Score: %d\n", score);
}
void Input() {
if (_kbhit()) {
switch (_getch()) {
case 'a':
dir = LEFT;
break;
case 'd':
dir = RIGHT;
break;
case 'w':
dir = UP;
break;
case 's':
dir = DOWN;
break;
case 'x':
gameover = 1;
break;
}
}
}
void Logic() {
int i;
int prevX = tailX[0];
int prevY = tailY[0];
int prev2X, prev2Y;
tailX[0] = x;
tailY[0] = y;
for (i = 1; i < ntail; i++) {
prev2X = tailX[i];
prev2Y = tailY[i];
tailX[i] = prevX;
tailY[i] = prevY;
prevX = prev2X;
prevY = prev2Y;
}
switch (dir) {
case LEFT:
x--;
break;
case RIGHT:
x++;
break;
case UP:
y--;
break;
case DOWN:
y++;
break;
default:
break;
}
if (x < 0 || x >= WIDTH || y < 0 || y >= HEIGHT)
gameover = 1;
for (i = 0; i < ntail; i++) {
if (tailX[i] == x && tailY[i] == y)
gameover = 1;
}
if (x == fruitX && y == fruitY) {
score += 10;
fruitX = rand() % WIDTH;
fruitY = rand() % HEIGHT;
ntail++;
}
}
int main() {
Setup();
while (!gameover) {
Draw();
Input();
Logic();
Sleep(50);
}
printf("Game Over!\n");
printf("Your Score is: %d\n", score);
return 0;
}
```
这个游戏使用了Windows API的`Sleep()`函数来控制每个游戏循环的频率。游戏界面是使用控制台输出来实现的。您可以直接复制代码,然后在本地编译运行,体验一下贪吃蛇游戏的乐趣。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)