在上面的贪吃蛇代码的基础上加一个蛇撞到墙会使得游戏游戏结束的要求
时间: 2024-03-21 19:44:27 浏览: 180
好的,为了实现这个要求,我们需要在每次移动蛇的时候判断蛇是否撞到了墙壁。具体实现可以在每次移动蛇的时候,判断蛇头的坐标是否超出了屏幕的边界。如果超出了边界,则游戏结束。
以下是代码的修改部分:
```python
import pygame
import random
# 初始化
pygame.init()
# 屏幕大小
screen_width = 500
screen_height = 500
# 颜色定义
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
# 绘制屏幕
game_screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('贪吃蛇')
# 游戏结束函数
def game_over():
font = pygame.font.SysFont(None, 50)
text = font.render("游戏结束", True, red)
game_screen.blit(text, [screen_width/2, screen_height/2])
pygame.display.update()
pygame.time.wait(3000)
# 蛇的类
class Snake():
def __init__(self):
self.size = 10
self.x = screen_width / 2
self.y = screen_height / 2
self.body = []
self.length = 1
self.direction = "RIGHT"
self.speed = 10
# 绘制蛇
def draw(self):
for i in self.body:
pygame.draw.rect(game_screen, green, [i[0], i[1], self.size, self.size])
# 移动蛇
def move(self):
if self.direction == "RIGHT":
self.x += self.speed
elif self.direction == "LEFT":
self.x -= self.speed
elif self.direction == "UP":
self.y -= self.speed
elif self.direction == "DOWN":
self.y += self.speed
# 判断蛇头是否超出屏幕边界
if self.x < 0 or self.x > screen_width - self.size or self.y < 0 or self.y > screen_height - self.size:
game_over()
pygame.quit()
quit()
self.body.insert(0, [self.x, self.y])
if len(self.body) > self.length:
self.body.pop()
# 改变蛇的方向
def change_direction(self, direction):
if direction == "RIGHT" and self.direction != "LEFT":
self.direction = "RIGHT"
elif direction == "LEFT" and self.direction != "RIGHT":
self.direction = "LEFT"
elif direction == "UP" and self.direction != "DOWN":
self.direction = "UP"
elif direction == "DOWN" and self.direction != "UP":
self.direction = "DOWN"
# 增加蛇的长度
def add_length(self):
self.length += 1
# 食物的类
class Food():
def __init__(self):
self.size = 10
self.x = random.randrange(0, screen_width - self.size, self.size)
self.y = random.randrange(0, screen_height - self.size, self.size)
# 绘制食物
def draw(self):
pygame.draw.rect(game_screen, red, [self.x, self.y, self.size, self.size])
# 重新生成食物
def regenerate(self):
self.x = random.randrange(0, screen_width - self.size, self.size)
self.y = random.randrange(0, screen_height - self.size, self.size)
# 初始化蛇和食物
snake = Snake()
food = Food()
# 游戏循环
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:
snake.change_direction("RIGHT")
elif event.key == pygame.K_LEFT:
snake.change_direction("LEFT")
elif event.key == pygame.K_UP:
snake.change_direction("UP")
elif event.key == pygame.K_DOWN:
snake.change_direction("DOWN")
# 绘制背景
game_screen.fill(black)
# 绘制蛇和食物
snake.move()
snake.draw()
food.draw()
# 判断是否吃到食物
if snake.body[0][0] == food.x and snake.body[0][1] == food.y:
snake.add_length()
food.regenerate()
# 更新屏幕
pygame.display.update()
# 控制游戏帧率
pygame.time.Clock().tick(30)
```
在修改后的代码中,我们在每次移动蛇的时候,都会判断蛇头是否超出了屏幕的边界,如果超出了边界,则调用 `game_over()` 函数,显示游戏结束的提示,并退出游戏。
阅读全文