请用python编写一个贪吃蛇小游戏,并给出完整代码
时间: 2023-02-26 16:46:09 浏览: 101
我可以尝试给出一些提示。首先,可以使用Python的pygame模块来创建游戏窗口,并在其中绘制贪吃蛇和食物。然后,使用Python的list数据结构来存储贪吃蛇的每一部分的坐标。然后,您可以使用random模块来在随机位置绘制新的食物。最后,可以使用if-else语句来实现贪吃蛇移动和吃掉食物的逻辑。
相关问题
帮我用Python编写一个贪吃蛇小游戏
非常感谢您的提问。我可以回答这个问题。以下是一个简单的 Python 贪吃蛇小游戏的代码示例:
```python
import pygame
import random
# 初始化 Pygame
pygame.init()
# 游戏窗口的大小
window_width = 500
window_height = 500
# 创建游戏窗口
game_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('分数: ' + str(score), True, black)
game_window.blit(score_text, [0, 0])
# 定义画蛇的函数
def draw_snake(snake_block_size, snake_list):
for x in snake_list:
pygame.draw.rect(game_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
# 处理按键事件
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
snake_x -= snake_block_size
elif keys[pygame.K_RIGHT]:
snake_x += snake_block_size
elif keys[pygame.K_UP]:
snake_y -= snake_block_size
elif keys[pygame.K_DOWN]:
snake_y += snake_block_size
# 判断是否吃到食物
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
# 更新蛇的位置
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
if snake_x < 0 or snake_x >= window_width or snake_y < 0 or snake_y >= window_height:
game_over = True
# 清空游戏窗口
game_window.fill(white)
# 画食物和蛇
pygame.draw.rect(game_window, red, [food_x, food_y, food_block_size, food_block_size])
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()
```
使用python3 编写一个贪吃蛇游戏,给我完整的代码
好的,以下是使用Python3编写的贪吃蛇游戏的完整代码。代码注释中有详细的说明。
```python
import pygame
import random
# 游戏界面大小和每个方块的大小
WIDTH, HEIGHT = 640, 480
GRID_SIZE = 20
# 定义颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
class Snake:
def __init__(self):
# 初始化蛇的位置
self.body = [(WIDTH/2, HEIGHT/2), (WIDTH/2-GRID_SIZE, HEIGHT/2), (WIDTH/2-2*GRID_SIZE, HEIGHT/2)]
# 初始化蛇的移动方向
self.direction = "right"
# 初始化蛇是否死亡
self.is_dead = False
def move(self):
# 蛇移动
head = self.body[0]
if self.direction == "right":
new_head = (head[0]+GRID_SIZE, head[1])
elif self.direction == "left":
new_head = (head[0]-GRID_SIZE, head[1])
elif self.direction == "up":
new_head = (head[0], head[1]-GRID_SIZE)
elif self.direction == "down":
new_head = (head[0], head[1]+GRID_SIZE)
# 判断蛇移动后是否死亡
if new_head[0] < 0 or new_head[0] >= WIDTH or new_head[1] < 0 or new_head[1] >= HEIGHT:
self.is_dead = True
elif new_head in self.body:
self.is_dead = True
# 移动蛇
if not self.is_dead:
self.body.insert(0, new_head)
if new_head != self.food:
self.body.pop()
else:
self.add_food()
def add_food(self):
# 随机生成食物的位置
while True:
x = random.randint(0, WIDTH/GRID_SIZE-1) * GRID_SIZE
y = random.randint(0, HEIGHT/GRID_SIZE-1) * GRID_SIZE
if (x, y) not in self.body:
break
self.food = (x, y)
def draw(self, surface):
# 画出蛇和食物
for block in self.body:
pygame.draw.rect(surface, GREEN, (block[0], block[1], GRID_SIZE, GRID_SIZE))
pygame.draw.rect(surface, RED, (self.food[0], self.food[1], GRID_SIZE, GRID_SIZE))
class Game:
def __init__(self):
# 初始化游戏界面
self.screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Snake Game")
# 初始化蛇和食物
self.snake = Snake()
self.snake.add_food()
# 初始化计时器
self.clock = pygame.time.Clock()
def run(self):
# 游戏循环
while True:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT and self.snake.direction != "left":
self.snake.direction = "right"
elif event.key == pygame.K_LEFT and self.snake.direction != "right":
self.snake.direction = "left"
elif event.key == pygame.K_UP and self.snake.direction != "down":
self.snake.direction = "up"
elif event.key == pygame.K_DOWN and self.snake.direction != "up":
self.snake.direction = "down"
# 移动蛇
self.snake.move()
# 画出蛇和食物
self.screen.fill(BLACK)
self.snake.draw(self.screen)
# 判断游戏是否结束
if self.snake.is_dead:
font = pygame.font.Font(None, 36)
text = font.render("Game Over", True, WHITE)
self.screen.blit(text, (WIDTH/2-70, HEIGHT/2-18))
pygame.display.update()
pygame.time.wait(2000)
pygame.quit()
quit()
# 刷新屏幕
pygame.display.update()
# 控制游戏帧率
self.clock.tick(10)
if __name__ == "__main__":
pygame.init()
game = Game()
game.run()
```
阅读全文