请优化贪吃蛇代码,不需要太花里胡哨,简单易懂就好,注:以下代码有错
小编提示:作者用的编译器是VS,若编译器不同,可以将代码中的#define _CRT_SECURE_NO_WARNINGS 1
和 system("pause");
注释掉,不然直接运行代码的话可能会报错哦~
要不直接找个现有的代码吧,这个是我写的,有问题可以问我
https://gitee.com/churuxu/codesnip/tree/master/C/Games/snakes
#include <easyx.h>
#include <stdio.h>
#include <conio.h>
#include <time.h>
#define NODE_WIDTH 40
// 节点
typedef struct {
int x;
int y;
}node;
enum direction
{
eUp,
eDown,
eLeft,
eRight
};
void paintGrid()
{
setlinecolor(WHITE);
for (int x = 0; x < 800; x += NOOD_WIDTH)
{
line(x, 0, x, 600);//画竖直线段
}
for (int y = 0; y < 600; y += NOOD_WIDTH)
{
line(0, y, 800, y);//画竖直线段
}
}
void paintSnake(node* snake, int n)
{
int left, top, right, bottom;
for (int i = 0; i < n; i++)
{
left = snake[i].x * NOOD_WIDTH;//*(snake + i).x
top = snake[i].y * NOOD_WIDTH;
right = (snake[i].x + 1) * NOOD_WIDTH;
bottom = (snake[i].y + 1) * NOOD_WIDTH;
setlinecolor(BLACK);
fillrectangle(left, top, right, bottom);
}
}
node snakeMove(node* snake, int length, int direction)
{
node tail = snake[length - 1];
//用一个循环将数组中的元素从后往前依次进行覆盖
for (int i = length - 1; i > 0; i--)
{
snake[i] = snake[i - 1];//将前一个节点覆盖后一个节点
}
node newhead = snake[0];
if (direction == eUp)
{
newhead.y--;//在原蛇头的基础上进行修改
}
if (direction == eDown)
{
newhead.y++;
}
if (direction == eLeft)
{
newhead.x--;
}
if (direction == eRight)
{
newhead.x++;
}
snake[0] = newhead;//最后在把修改后的蛇头数据赋值给原数组的首元素
return tail;
}
void changDirection(enum direction* p)
{
if (_kbhit() != 0)
{
char c = _getch();
switch (c)
{
case 'a':
if(*p != eRight)
*p = eLeft;
break;
case 'd':
if (*p != eLeft)
*p = eRight;
break;
case 'w':
if (*p != eDown)
*p = eUp;
break;
case's':
if (*p != eUp)
*p = eDown;
break;
}
}
}
node creatFood(node* snake, int length)
{
//先在画布圈定的范围之内随机生成一个食物点
node food;
//然后用一个死循环来对食物点进行检验(不能与蛇的身体进行重合)
while (1)
{
food.x = rand() % (800 / NOOD_WIDTH);
food.y = rand() % (600 / NOOD_WIDTH);
int i = 0;
for (i = 0; i < length; i++)
{
if (food.x == snake[i].x && food.y == snake[i].y)
break;
}
if (i < length)
continue;
else
break;
}
return food;
}
void paintFood(node* food)
{
int left, top, right, bottom;
left = food->x * NOOD_WIDTH;//*(snake + i).x
top = food->y* NOOD_WIDTH;
right = (food->x + 1) * NOOD_WIDTH;
bottom = (food->y + 1) * NOOD_WIDTH;
setfillcolor(YELLOW);
solidrectangle(left, top, right, bottom);
setfillcolor(WHITE);
}
bool isGameover(node* snake, int length)
{
if (snake[0].x < 0 || snake[0].x>800 / NOOD_WIDTH)
{
return true;
}
if (snake[0].y < 0 || snake[0].y>600 / NOOD_WIDTH)
{
return true;
}
for (int i = 1; i < length; i++)
{
if (snake[0].x == snake[i].x && snake[0].y == snake[i].y)
return true;
}
return false;
}
void reset(node* snake, int* plength, enum direction* pd)
{
for (int i = 0; i < 5; i++)
{
for (int j = 5; j > 0; j--)
{
snake[i].x = j;
}
snake[i].y = 7;
}
*plength = 5;
*pd = eRight;
}
int main()
{
initgraph(800, 600);
setbkcolor(RGB(164, 225, 202));
cleardevice();
node snake[100] = { {5,7},{4,7},{3,7},{2,7},{1,7} };
int length = 5;
enum direction d = eRight;
srand(unsigned int(time(NULL)));//用当前时间作为随机数种子
node food = creatFood(snake,length);
while (1)
{
cleardevice();
paintGrid();
paintSnake(snake, length);
paintFood(&food);
Sleep(500);
changDirection(&d);
node last_tail = snakeMove(snake, length, d);
if (snake[0].x == food.x && snake[0].y == food.y)
{
if (length < 100)
{
snake[length] = last_tail;
length++;
}
food = creatFood(snake, length);
}
if (isGameover(snake, length) == true)
{
reset(snake, &length, &d);
food = creatFood(snake, length);
}
}
getchar();
closegraph();
return 0;
}
根据您提供的图片,您需要优化一个贪吃蛇游戏的代码,并且要求代码简单易懂。以下是一个简化版的贪吃蛇游戏代码示例:
import pygame
import random
# 游戏初始化
pygame.init()
# 游戏界面尺寸
width = 800
height = 600
# 定义颜色
black = pygame.Color(0, 0, 0)
white = pygame.Color(255, 255, 255)
red = pygame.Color(255, 0, 0)
green = pygame.Color(0, 255, 0)
blue = pygame.Color(0, 0, 255)
# 创建游戏界面
game_surface = pygame.display.set_mode((width, height))
pygame.display.set_caption('贪吃蛇游戏')
# 蛇的初始位置
snake_position = [100, 50]
snake_body = [[100, 50], [90, 50], [80, 50]]
snake_direction = 'RIGHT'
# 食物的初始位置
food_position = [random.randrange(1, width // 10) * 10, random.randrange(1, height // 10) * 10]
food_spawn = True
# 控制游戏速度的时钟
clock = pygame.time.Clock()
# 游戏结束标志位
game_over = False
# 游戏主循环
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
snake_direction = 'RIGHT'
elif event.key == pygame.K_LEFT:
snake_direction = 'LEFT'
elif event.key == pygame.K_UP:
snake_direction = 'UP'
elif event.key == pygame.K_DOWN:
snake_direction = 'DOWN'
# 根据蛇的移动方向更新蛇头的位置
if snake_direction == 'RIGHT':
snake_position[0] += 10
elif snake_direction == 'LEFT':
snake_position[0] -= 10
elif snake_direction == 'UP':
snake_position[1] -= 10
elif snake_direction == 'DOWN':
snake_position[1] += 10
# 更新蛇的身体
snake_body.insert(0, list(snake_position))
if snake_position[0] == food_position[0] and snake_position[1] == food_position[1]:
food_spawn = False
else:
snake_body.pop()
# 生成新的食物
if not food_spawn:
food_position = [random.randrange(1, width // 10) * 10, random.randrange(1, height // 10) * 10]
food_spawn = True
# 绘制游戏界面
game_surface.fill(black)
for pos in snake_body:
pygame.draw.rect(game_surface, green, pygame.Rect(pos[0], pos[1], 10, 10))
pygame.draw.rect(game_surface, white, pygame.Rect(food_position[0], food_position[1], 10, 10))
# 游戏结束条件
if snake_position[0] < 0 or snake_position[0] >= width or snake_position[1] < 0 or snake_position[1] >= height:
game_over = True
break
for block in snake_body[1:]:
if snake_position[0] == block[0] and snake_position[1] == block[1]:
game_over = True
break
# 刷新游戏界面
pygame.display.flip()
# 控制游戏速度
clock.tick(20)
# 退出游戏
pygame.quit()
以上是一个简单的贪吃蛇游戏代码示例,使用了Pygame库来创建游戏界面和处理游戏逻辑。代码中包括了贪吃蛇的移动、食物的生成、碰撞检测等基本功能。您可以根据需要进行进一步的扩展和优化。