用C语言写一个贪吃蛇游戏
时间: 2023-12-29 19:23:31 浏览: 33
好的,下面是一个简单的贪吃蛇游戏的C语言代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <Windows.h>
#define MAX_LEN 100
int width = 20;
int height = 20;
int score = 0;
int len = 3;
int x[MAX_LEN], y[MAX_LEN];
int food_x, food_y;
int direction = 2; // 1-up, 2-right, 3-down, 4-left
int gameOver = 0;
void init()
{
x[0] = 1;
y[0] = 3;
x[1] = 1;
y[1] = 2;
x[2] = 1;
y[2] = 1;
food_x = rand() % (width - 2) + 1;
food_y = rand() % (height - 2) + 1;
}
void draw()
{
system("cls");
for (int i = 0; i < width + 2; i++)
printf("#");
printf("\n");
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
if (j == 0)
printf("#");
if (i == y[0] && j == x[0])
printf("O");
else if (i == food_y && j == food_x)
printf("F");
else
{
int flag = 0;
for (int k = 1; k < len; k++)
{
if (i == y[k] && j == x[k])
{
printf("o");
flag = 1;
}
}
if (!flag)
printf(" ");
}
if (j == width - 1)
printf("#");
}
printf("\n");
}
for (int i = 0; i < width + 2; i++)
printf("#");
printf("\n");
printf("Score: %d\n", score);
}
void input()
{
if (_kbhit())
{
switch (_getch())
{
case 'w':
if (direction != 3)
direction = 1;
break;
case 'd':
if (direction != 4)
direction = 2;
break;
case 's':
if (direction != 1)
direction = 3;
break;
case 'a':
if (direction != 2)
direction = 4;
break;
case 'x':
gameOver = 1;
break;
}
}
}
void logic()
{
int prev_x = x[0];
int prev_y = y[0];
int prev2_x, prev2_y;
x[0] = prev_x + (direction == 2 ? 1 : direction == 4 ? -1 : 0);
y[0] = prev_y + (direction == 1 ? -1 : direction == 3 ? 1 : 0);
for (int i = 1; i < len; i++)
{
prev2_x = x[i];
prev2_y = y[i];
x[i] = prev_x;
y[i] = prev_y;
prev_x = prev2_x;
prev_y = prev2_y;
}
if (x[0] == food_x && y[0] == food_y)
{
score += 10;
len++;
food_x = rand() % (width - 2) + 1;
food_y = rand() % (height - 2) + 1;
}
if (x[0] == 0 || x[0] == width - 1 || y[0] == 0 || y[0] == height - 1)
gameOver = 1;
for (int i = 1; i < len; i++)
{
if (x[0] == x[i] && y[0] == y[i])
gameOver = 1;
}
}
int main()
{
init();
while (!gameOver)
{
draw();
input();
logic();
Sleep(100);
}
printf("Game Over!\n");
system("pause");
return 0;
}
```
这个代码使用了Windows API的`Sleep`函数来实现游戏的暂停,同时使用了`conio.h`库中的`_kbhit`和`_getch`函数来实现键盘输入的检测和获取。在`draw`函数中,使用了循环来遍历整个游戏区域,并根据蛇头和食物的位置以及蛇身的长度来绘制游戏画面。在`input`函数中,使用了`switch`语句来根据不同的键盘输入来改变蛇的移动方向。在`logic`函数中,实现了蛇的移动和碰撞检测以及食物的生成和得分计算。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)