用C语言写一个贪吃蛇代码,包含所有代码
时间: 2023-09-24 16:05:31 浏览: 116
贪吃蛇c语言代码
### 回答1:
我很乐意为您编写一个贪吃蛇代码,包含所有代码。以下是代码:#include <stdio.h>
#include <stdlib.h>
#include <windows.h>int main()
{
// 初始化游戏
int i, j;
int score = 0;
int level = 1;
int speed = 200;
int food_x = 0;
int food_y = 0;
int size = 3;
int snake_x[60] = {0};
int snake_y[60] = {0};
int dir = 0; // 初始化屏幕
system("cls");
for (i = 0; i < 20; i++) {
for (j = 0; j < 30; j++) {
printf(" ");
}
printf("\n");
} // 初始化蛇的位置
snake_x[0] = 15;
snake_y[0] = 10;
for (i = 1; i < size; i++) {
snake_x[i] = snake_x[i - 1] - 1;
snake_y[i] = snake_y[i - 1];
} // 生成食物
food_x = rand() % 20;
food_y = rand() % 30; // 开始游戏
while (1) {
// 画出屏幕
system("cls");
for (i = 0; i < 20; i++) {
for (j = 0; j < 30; j++) {
if (i == 0 || i == 19 || j == 0 || j == 29) {
printf("*");
}
else {
int flag = 0;
for (int k = 0; k < size; k++) {
if (i == snake_x[k] && j == snake_y[k]) {
printf("@");
flag = 1;
break;
}
}
if (flag == 0) {
if (i == food_x && j == food_y) {
printf("$");
}
else {
printf(" ");
}
}
}
}
printf("\n");
} // 获取用户输入
if (kbhit()) {
char ch = getch();
if (ch == 'a' && dir != 2) {
dir = 1;
}
else if (ch == 'd' && dir != 1) {
dir = 2;
}
else if (ch == 'w' && dir != 4) {
dir = 3;
}
else if (ch == 's' && dir != 3) {
dir = 4;
}
} // 游戏逻辑
for (i = size - 1; i > 0; i--) {
snake_x[i] = snake_x[i - 1];
snake_y[i] = snake_y[i - 1];
}
switch (dir) {
case 1:
snake_y[0]--;
break;
case 2:
snake_y[0]++;
break;
case 3:
snake_x[0]--;
break;
case 4:
snake_x[0]++;
break;
} // 判断游戏是否结束
if (snake_x[0] == 0 || snake_x[0] == 19 || snake_y[0] == 0 || snake_y[0] == 29) {
printf("游戏结束,您的得分为:%d\n", score);
break;
}
for (i = 1; i < size; i++) {
if (snake_x[0] == snake_x[i] && snake_y[0] == snake_y[i]) {
printf("游戏结束,您的得分为:%d\n", score);
break;
}
} // 判断蛇是否吃到食物
if (snake_x[0] == food_x && snake_y[0] == food_y) {
size++;
food_x = rand() % 20;
food_y = rand() % 30;
score += 10;
if (score % 50 == 0) {
level++;
speed -= 50;
}
} Sleep(speed);
} return 0;
}
### 回答2:
下面是一个简单的用C语言编写的贪吃蛇代码:
```c
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#define WIDTH 20
#define HEIGHT 20
#define INIT_LENGTH 3
#define UP 72
#define DOWN 80
#define LEFT 75
#define RIGHT 77
int score;
int gameover;
int x, y;
int fruitX, fruitY;
int tailX[WIDTH * HEIGHT], tailY[WIDTH * HEIGHT];
int tailLength;
int direction;
void Setup()
{
gameover = 0;
score = 0;
x = WIDTH / 2;
y = HEIGHT / 2;
fruitX = rand() % WIDTH;
fruitY = rand() % HEIGHT;
}
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 && j == x)
printf("O");
else if (i == fruitY && j == fruitX)
printf("F");
else
{
int isTail = 0;
for (int k = 0; k < tailLength; k++)
{
if (tailX[k] == j && tailY[k] == i)
{
printf("o");
isTail = 1;
}
}
if (!isTail)
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':
direction = UP;
break;
case 's':
direction = DOWN;
break;
case 'a':
direction = LEFT;
break;
case 'd':
direction = RIGHT;
break;
case 'x':
gameover = 1;
break;
}
}
}
void Logic()
{
int prevX = tailX[0];
int prevY = tailY[0];
int prev2X, prev2Y;
tailX[0] = x;
tailY[0] = y;
for (int i = 1; i < tailLength; i++)
{
prev2X = tailX[i];
prev2Y = tailY[i];
tailX[i] = prevX;
tailY[i] = prevY;
prevX = prev2X;
prevY = prev2Y;
}
switch (direction)
{
case UP:
y--;
break;
case DOWN:
y++;
break;
case LEFT:
x--;
break;
case RIGHT:
x++;
break;
}
if (x < 0 || x >= WIDTH || y < 0 || y >= HEIGHT)
gameover = 1;
for (int i = 0; i < tailLength; i++)
{
if (tailX[i] == x && tailY[i] == y)
gameover = 1;
}
if (x == fruitX && y == fruitY)
{
score += 10;
tailLength++;
fruitX = rand() % WIDTH;
fruitY = rand() % HEIGHT;
}
}
int main()
{
Setup();
while (!gameover)
{
Draw();
Input();
Logic();
Sleep(10); // 控制移动速度
}
return 0;
}
```
这是一个基于控制台的简单贪吃蛇游戏,使用WASD或方向键控制蛇的移动方向。每吃到一个食物,蛇的长度增加,得分加10分。游戏结束条件包括蛇碰到边界或自身身体。使用`conio.h`和`windows.h`库实现控制台窗口的绘制和用户输入的功能。游戏中使用了基本的逻辑判断和循环来模拟蛇的运动和碰撞检测。
### 回答3:
下面是一个使用C语言编写的简单贪吃蛇游戏代码:
```
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <windows.h>
#define Width 20
#define Height 20
int gameOver;
int score;
int snakeX, snakeY;
int fruitX, fruitY;
int tailX[100], tailY[100];
int tailLength;
enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN };
enum eDirection dir;
void Setup()
{
gameOver = 0;
score = 0;
dir = STOP;
snakeX = Width / 2;
snakeY = Height / 2;
fruitX = rand() % Width;
fruitY = rand() % Height;
}
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 == snakeY && j == snakeX)
printf("O"); // 蛇头
else if (i == fruitY && j == fruitX)
printf("F"); // 水果
else
{
int printed = 0;
for (int k = 0; k < tailLength; k++)
{
if (tailX[k] == j && tailY[k] == i)
{
printf("o"); // 蛇身
printed = 1;
}
}
if (!printed)
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 '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 prevX = tailX[0];
int prevY = tailY[0];
int prev2X, prev2Y;
tailX[0] = snakeX;
tailY[0] = snakeY;
for (int i = 1; i < tailLength; i++)
{
prev2X = tailX[i];
prev2Y = tailY[i];
tailX[i] = prevX;
tailY[i] = prevY;
prevX = prev2X;
prevY = prev2Y;
}
switch (dir)
{
case LEFT:
snakeX--;
break;
case RIGHT:
snakeX++;
break;
case UP:
snakeY--;
break;
case DOWN:
snakeY++;
break;
}
if (snakeX < 0 || snakeX >= Width || snakeY < 0 || snakeY >= Height)
gameOver = 1;
for (int i = 0; i < tailLength; i++)
{
if (tailX[i] == snakeX && tailY[i] == snakeY)
gameOver = 1;
}
if (snakeX == fruitX && snakeY == fruitY)
{
score += 10;
fruitX = rand() % Width;
fruitY = rand() % Height;
tailLength++;
}
}
int main()
{
Setup();
while (!gameOver)
{
Draw();
Input();
Logic();
Sleep(10); // 控制游戏速度
}
return 0;
}
```
这个代码使用了控制台窗口来显示游戏界面,并使用了方向键以及字母键a、d、w、s来控制蛇的移动方向。玩家需要通过控制蛇吃水果来增加得分,如果蛇撞到自己的身体或者撞到边界,则游戏结束。请注意,基于实际环境的限制,如Windows操作系统可能需要适当调整代码。
阅读全文