用C语言写一个贪吃蛇代码,包含所有代码
时间: 2023-09-15 16:05:27 浏览: 99
贪吃蛇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:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <windows.h>
#define WIDTH 20
#define HEIGHT 20
int score = 0;
int gameover = 0;
int speed = 100;
int x, y, fx, fy, flag;
int len = 1;
int tailX[100], tailY[100];
void setup()
{
gameover = 0;
x = WIDTH / 2;
y = HEIGHT / 2;
srand(time(NULL));
fx = rand() % WIDTH;
fy = rand() % HEIGHT;
}
void draw()
{
int i, j;
system("cls");
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 == fy && j == fx)
printf("F");
else
{
int printtail = 0;
for (int k = 0; k < len; k++)
{
if (i == tailY[k] && j == tailX[k])
{
printf("o");
printtail = 1;
break;
}
}
if (!printtail)
printf(" ");
}
if (j == WIDTH - 1)
printf("#");
}
printf("\n");
}
for (i = 0; i < WIDTH + 2; i++)
printf("#");
printf("\n");
printf("Score: %d\n", score);
printf("Speed: %d\n", speed);
printf("Use arrow keys to control the snake\n");
}
void input()
{
if (_kbhit())
{
switch (_getch())
{
case 'a':
flag = 1;
break;
case 'd':
flag = 2;
break;
case 'w':
flag = 3;
break;
case 's':
flag = 4;
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 < len; i++)
{
prev2X = tailX[i];
prev2Y = tailY[i];
tailX[i] = prevX;
tailY[i] = prevY;
prevX = prev2X;
prevY = prev2Y;
}
switch (flag)
{
case 1:
x--;
break;
case 2:
x++;
break;
case 3:
y--;
break;
case 4:
y++;
break;
default:
break;
}
if (x < 0 || x >= WIDTH || y < 0 || y >= HEIGHT)
gameover = 1;
for (int i = 0; i < len; i++)
{
if (tailX[i] == x && tailY[i] == y)
gameover = 1;
}
if (x == fx && y == fy)
{
score += 10;
fx = rand() % WIDTH;
fy = rand() % HEIGHT;
len++;
if (score % 100 == 0)
speed -= 10;
}
}
int main()
{
system("cls");
printf("Welcome to Snake Game!\n");
printf("Press any key to start...\n");
_getch();
setup();
while (!gameover)
{
draw();
input();
logic();
Sleep(speed);
}
system("cls");
printf("GAME OVER. Your score is: %d\n", score);
printf("Thank you for playing!\n");
return 0;
}
### 回答3:
#include <stdio.h>
#include <conio.h> //使用getch函数
#include <stdlib.h> //使用rand和srand函数
#include <windows.h> //使用Sleep函数
//定义地图的大小
#define MAP_WIDTH 60
#define MAP_HEIGHT 20
//定义蛇的最大长度
#define SNAKE_MAX_LENGTH 50
//定义方向常量
#define UP 1
#define DOWN 2
#define LEFT 3
#define RIGHT 4
//定义蛇身体的节点结构体
typedef struct _Node
{
int x;
int y;
} Node;
//定义蛇的结构体
typedef struct _Snake
{
Node body[SNAKE_MAX_LENGTH]; //蛇的身体
int length; //蛇的长度
int direction; //蛇的移动方向
} Snake;
//定义食物的结构体
typedef struct _Food
{
Node position; //食物的位置
} Food;
Snake snake;
Food food;
//初始化地图
void initMap()
{
system("cls"); //清屏
int i, j;
for (i = 0; i < MAP_HEIGHT; i++)
{
for (j = 0; j < MAP_WIDTH; j++)
{
if (i == 0 || i == MAP_HEIGHT-1 || j == 0 || j == MAP_WIDTH-1)
printf("#"); //边界用#表示
else
printf(" "); //其他地方为空格
}
printf("\n");
}
}
//初始化蛇
void initSnake()
{
snake.length = 4; //初始长度为4
snake.direction = RIGHT; //初始方向为向右
//初始化蛇的初始位置
snake.body[0].x = 10;
snake.body[0].y = 5;
snake.body[1].x = 9;
snake.body[1].y = 5;
snake.body[2].x = 8;
snake.body[2].y = 5;
snake.body[3].x = 7;
snake.body[3].y = 5;
//打印蛇的初始位置
gotoxy(snake.body[0].x, snake.body[0].y);
printf("*");
for (int i = 1; i < snake.length; i++)
{
gotoxy(snake.body[i].x, snake.body[i].y);
printf("#");
}
}
//在指定位置打印食物
void printFood(Node foodPosition)
{
gotoxy(foodPosition.x, foodPosition.y);
printf("@");
}
//生成随机食物
void generateFood()
{
int x, y;
do {
x = rand() % (MAP_WIDTH - 2) + 1; //随机生成x坐标
y = rand() % (MAP_HEIGHT - 2) + 1; //随机生成y坐标
} while (isSnakeNode(x, y));
food.position.x = x;
food.position.y = y;
printFood(food.position);
}
//判断指定位置是否是蛇的身体节点
int isSnakeNode(int x, int y)
{
for (int i = 0; i < snake.length; i++)
{
if (snake.body[i].x == x && snake.body[i].y == y)
return 1;
}
return 0;
}
//移动蛇
void moveSnake()
{
int x = snake.body[0].x;
int y = snake.body[0].y;
if (snake.direction == UP)
y--;
else if (snake.direction == DOWN)
y++;
else if (snake.direction == LEFT)
x--;
else if (snake.direction == RIGHT)
x++;
//判断移动后是否撞到边界或自身
if (x == 0 || x == MAP_WIDTH-1 || y == 0 || y == MAP_HEIGHT-1 || isSnakeNode(x, y))
gameOver();
//判断是否吃到食物
if (x == food.position.x && y == food.position.y)
{
snake.length++;
generateFood();
}
else
{
gotoxy(snake.body[snake.length-1].x, snake.body[snake.length-1].y);
printf(" ");
}
//更新蛇的身体
for (int i = snake.length - 1; i > 0; i--)
{
snake.body[i] = snake.body[i - 1];
}
snake.body[0].x = x;
snake.body[0].y = y;
//打印蛇的新位置
gotoxy(x, y);
printf("*");
}
//判断是否按下方向键
int kbhit()
{
if (_kbhit())
return _getch();
return 0;
}
//结束游戏
void gameOver()
{
system("cls");
printf("Game Over!\n");
exit(0);
}
int main()
{
//隐藏光标
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO CursorInfo;
GetConsoleCursorInfo(handle, &CursorInfo);
CursorInfo.bVisible = 0;
SetConsoleCursorInfo(handle, &CursorInfo);
//初始化地图和蛇
initMap();
initSnake();
generateFood();
//游戏循环
while (1)
{
if (kbhit())
{
int key = _getch();
if (key == 224) //方向键按下时返回224
{
key = _getch();
if (key == 72 && snake.direction != DOWN) //上键
snake.direction = UP;
else if (key == 80 && snake.direction != UP) //下键
snake.direction = DOWN;
else if (key == 75 && snake.direction != RIGHT) //左键
snake.direction = LEFT;
else if (key == 77 && snake.direction != LEFT) //右键
snake.direction = RIGHT;
}
}
moveSnake();
//延时控制游戏速度
Sleep(200);
}
return 0;
}
阅读全文