用Python编写一个。贪吃蛇的代码
时间: 2023-08-31 07:39:59 浏览: 76
python 贪吃蛇代码
5星 · 资源好评率100%
### 回答1:
好的,以下是用Python编写的贪吃蛇代码:
```python
import pygame
import random
# 初始化pygame
pygame.init()
# 设置游戏窗口大小
window_width = 500
window_height = 500
window = pygame.display.set_mode((window_width, window_height))
# 设置游戏标题
pygame.display.set_caption("贪吃蛇")
# 定义颜色
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
green = (0, 255, 0)
# 定义蛇的初始位置和大小
snake_block_size = 10
snake_speed = 15
snake_list = []
snake_length = 1
snake_x = window_width / 2
snake_y = window_height / 2
# 定义食物的位置和大小
food_block_size = 10
food_x = round(random.randrange(0, window_width - food_block_size) / 10.0) * 10.0
food_y = round(random.randrange(0, window_height - food_block_size) / 10.0) * 10.0
# 定义字体
font_style = pygame.font.SysFont(None, 30)
# 定义显示分数的函数
def show_score(score):
score_text = font_style.render("Score: " + str(score), True, black)
window.blit(score_text, [0, 0])
# 定义画蛇的函数
def draw_snake(snake_block_size, snake_list):
for x in snake_list:
pygame.draw.rect(window, green, [x[0], x[1], snake_block_size, snake_block_size])
# 游戏循环
game_over = False
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
# 控制蛇的移动
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
snake_x_change = -snake_block_size
snake_y_change = 0
elif event.key == pygame.K_RIGHT:
snake_x_change = snake_block_size
snake_y_change = 0
elif event.key == pygame.K_UP:
snake_y_change = -snake_block_size
snake_x_change = 0
elif event.key == pygame.K_DOWN:
snake_y_change = snake_block_size
snake_x_change = 0
# 判断蛇是否撞到边界
if snake_x >= window_width or snake_x < 0 or snake_y >= window_height or snake_y < 0:
game_over = True
# 移动蛇的位置
snake_x += snake_x_change
snake_y += snake_y_change
# 判断蛇是否吃到食物
if snake_x == food_x and snake_y == food_y:
food_x = round(random.randrange(0, window_width - food_block_size) / 10.0) * 10.0
food_y = round(random.randrange(0, window_height - food_block_size) / 10.0) * 10.0
snake_length += 1
# 画出食物
pygame.draw.rect(window, red, [food_x, food_y, food_block_size, food_block_size])
# 定义蛇的位置列表
snake_head = []
snake_head.append(snake_x)
snake_head.append(snake_y)
snake_list.append(snake_head)
# 判断蛇的长度是否超过最大长度
if len(snake_list) > snake_length:
del snake_list[0]
# 判断蛇是否撞到自己
for x in snake_list[:-1]:
if x == snake_head:
game_over = True
# 画出蛇
draw_snake(snake_block_size, snake_list)
# 显示分数
show_score(snake_length - 1)
# 更新窗口
pygame.display.update()
# 控制游戏速度
clock = pygame.time.Clock()
clock.tick(snake_speed)
# 退出pygame
pygame.quit()
```
希望能够帮到你!
### 回答2:
贪吃蛇是一个经典的游戏,在Python中编写一个贪吃蛇的代码可以通过使用图形界面库Pygame来实现。
首先,需要导入pygame模块和random模块:
import pygame
import random
然后,可以定义游戏窗口的大小、颜色和速度等参数:
window_width = 640
window_height = 480
snake_size = 20
snake_speed = 10
snake_color = (0, 255, 0)
food_color = (255, 0, 0)
接下来,需要定义Snake类和Food类,分别表示蛇和食物的属性和行为:
class Snake:
def __init__(self):
self.position = [100, 50]
self.body = [[100, 50], [90, 50], [80, 50]]
self.direction = "RIGHT"
self.change_direction_to = self.direction
def move(self):
if self.change_direction_to == "RIGHT" and self.direction != "LEFT":
self.direction = "RIGHT"
if self.change_direction_to == "LEFT" and self.direction != "RIGHT":
self.direction = "LEFT"
if self.change_direction_to == "UP" and self.direction != "DOWN":
self.direction = "UP"
if self.change_direction_to == "DOWN" and self.direction != "UP":
self.direction = "DOWN"
if self.direction == "RIGHT":
self.position[0] += snake_size
if self.direction == "LEFT":
self.position[0] -= snake_size
if self.direction == "UP":
self.position[1] -= snake_size
if self.direction == "DOWN":
self.position[1] += snake_size
self.body.insert(0, list(self.position))
if self.position == food.position:
return 1
else:
self.body.pop()
return 0
def check_collision(self):
if self.position[0] > window_width-20 or self.position[0] < 0:
return 1
elif self.position[1] > window_height-20 or self.position[1] < 0:
return 1
for body_part in self.body[1:]:
if self.position == body_part:
return 1
return 0
class Food:
def __init__(self):
self.position = [random.randrange(1, (window_width//snake_size))*snake_size,
random.randrange(1, (window_height//snake_size))*snake_size]
然后,可以创建游戏窗口,并初始化蛇和食物:
window = pygame.display.set_mode((window_width, window_height))
snake = Snake()
food = Food()
接着,可以设置游戏循环,在游戏循环中不断更新蛇的位置和检测游戏是否结束:
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_RIGHT:
snake.change_direction_to = "RIGHT"
if event.key == pygame.K_LEFT:
snake.change_direction_to = "LEFT"
if event.key == pygame.K_UP:
snake.change_direction_to = "UP"
if event.key == pygame.K_DOWN:
snake.change_direction_to = "DOWN"
food_position = food.position
if snake.move() == 1:
score += 1
food = Food()
if snake.check_collision() == 1:
pygame.quit()
sys.exit()
最后,可以在游戏循环中更新窗口和绘制蛇和食物:
window.fill((0, 0, 0))
for pos in snake.body:
pygame.draw.rect(window, snake_color, pygame.Rect(
pos[0], pos[1], snake_size, snake_size))
pygame.draw.rect(window, food_color, pygame.Rect(
food.position[0], food.position[1], snake_size, snake_size))
pygame.display.flip()
这样,一个简单的贪吃蛇游戏就完成了。可以运行代码并控制蛇前进吃食物,直至游戏结束。
阅读全文