请写出贪吃蛇吃掉食物的代码
时间: 2023-12-11 13:04:03 浏览: 34
这是一个简单的Python代码实现贪吃蛇吃掉食物的过程:
```
import random
# 初始化贪吃蛇和食物的位置
snake_pos = [(0, 0)]
food_pos = (random.randint(0, 9), random.randint(0, 9))
# 吃掉食物的函数
def eat_food(snake_pos, food_pos):
if snake_pos[0] == food_pos:
snake_pos.insert(0, food_pos)
food_pos = (random.randint(0, 9), random.randint(0, 9))
return snake_pos, food_pos
# 循环判断是否吃到食物
while True:
snake_pos, food_pos = eat_food(snake_pos, food_pos)
if snake_pos[0] == food_pos:
print("Snake ate the food!")
break
```
这里的贪吃蛇和食物的位置都用元组表示,当贪吃蛇的头部位置与食物位置相同时,将食物位置添加到贪吃蛇的头部,同时随机生成一个新的食物位置。在循环中不断判断是否吃到食物,直到吃到为止。
相关问题
写一段贪吃蛇吃食物变长的代码
以下是一段简单的 C 语言代码,实现了贪吃蛇吃食物变长的功能:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义蛇的节点结构体
struct Snake {
int x;
int y;
struct Snake *next;
};
// 定义食物的坐标
int food_x, food_y;
// 初始化蛇
void init_snake(struct Snake *head) {
head->x = 1;
head->y = 1;
head->next = NULL;
}
// 在蛇的尾部添加一个节点
void add_tail(struct Snake *head) {
struct Snake *tail = head;
while (tail->next != NULL) {
tail = tail->next;
}
struct Snake *new_node = (struct Snake *) malloc(sizeof(struct Snake));
new_node->x = tail->x;
new_node->y = tail->y;
new_node->next = NULL;
tail->next = new_node;
}
// 生成食物的坐标
void generate_food() {
food_x = rand() % 10 + 1;
food_y = rand() % 10 + 1;
}
int main() {
struct Snake head;
init_snake(&head);
// 假设蛇吃到食物后需要变长
int eat_food = 0;
// 游戏循环
while (1) {
// 判断是否吃到食物
if (head.x == food_x && head.y == food_y) {
eat_food = 1;
generate_food(); // 重新生成食物
} else {
eat_food = 0;
}
// 如果吃到食物,就在蛇的尾部添加一个节点
if (eat_food) {
add_tail(&head);
}
// 更新蛇的位置
// ...
// 输出蛇的长度
int length = 1;
struct Snake *p = &head;
while (p->next != NULL) {
length++;
p = p->next;
}
printf("Length: %d\n", length);
}
return 0;
}
```
在上面的代码中,当蛇吃到食物时,通过调用 `add_tail` 函数在蛇的尾部添加一个新的节点,表示蛇的长度加1。同时,需要注意食物的重新生成,以及在游戏循环中判断是否吃到食物。在输出蛇的长度时,通过遍历蛇的链表,计算节点数来得到蛇的长度。
请写出Python制作贪吃蛇游戏的代码
这是一个使用Python制作贪吃蛇游戏的示例代码:import pygame
import sys
import random
import time# 初始化pygame
pygame.init()# 设置窗口
width = 500
height = 500
screen = pygame.display.set_mode((width, height))# 设置标题
pygame.display.set_caption("贪吃蛇")# 设置游戏参数
block_size = 10
size = width // block_size
FPS = 10# 设置颜色
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
green = (0, 255, 0)# 初始化蛇
snake = [[3, 2], [3, 1], [3, 0]]# 初始化食物
food = [random.randint(0, size - 1), random.randint(0, size - 1)]# 初始化方向
direction = 'up'# 画方块
def draw_block(color, position):
x, y = position
pygame.draw.rect(screen, color, (x * block_size, y * block_size, block_size, block_size))# 游戏循环
while True:
# 监听事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP and direction != 'down':
direction = 'up'
elif event.key == pygame.K_DOWN and direction != 'up':
direction = 'down'
elif event.key == pygame.K_LEFT and direction != 'right':
direction = 'left'
elif event.key == pygame.K_RIGHT and direction != 'left':
direction = 'right'
# 根据方向移动蛇
if direction == 'up':
snake[0][1] -= 1
elif direction == 'down':
snake[0][1] += 1
elif direction == 'left':
snake[0][0] -= 1
elif direction == 'right':
snake[0][0] += 1
# 移动蛇
for i in range(len(snake) - 1, 0, -1):
snake[i][0] = snake[i-1][0]
snake[i][1] = snake[i-1][1]
# 把食物放到屏幕上
if snake[0] == food:
food = [random.randint(0, size - 1), random.randint(0, size - 1)]
snake.append([snake[-1][0], snake[-1][1]])
# 填充屏幕
screen.fill(black)
# 画蛇
for block in snake:
draw_block(white, block)
# 画食物
draw_block(red, food)
# 更新画面
pygame.display.update()
# 控制游戏速度
time.sleep(1 / FPS)