用pyzhon做一个有游戏开始界面的贪吃蛇游戏
时间: 2023-05-26 13:01:06 浏览: 49
A:
这里是一个简单的贪吃蛇游戏,使用Python和Pygame库实现,其中包括开始界面和游戏界面。
```python
import pygame
import random
# 初始化pygame
pygame.init()
# 定义颜色
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
# 设置游戏界面
screen_width = 640
screen_height = 480
screen = pygame.display.set_mode([screen_width, screen_height])
pygame.display.set_caption("Snake Game")
# 设置字体
font = pygame.font.Font(None, 36)
# 定义贪吃蛇类
class Snake:
def __init__(self):
self.x = screen_width / 2
self.y = screen_height / 2
self.body = [[self.x, self.y], [self.x-10, self.y], [self.x-20, self.y]]
self.direction = "right"
self.score = 0
def update(self):
if self.direction == "right":
self.x += 10
elif self.direction == "left":
self.x -= 10
elif self.direction == "up":
self.y -= 10
elif self.direction == "down":
self.y += 10
# 添加新的身体
self.body.append([self.x, self.y])
# 删除尾部
if len(self.body) > self.score + 3:
del self.body[0]
def draw(self):
for i in range(len(self.body)):
pygame.draw.rect(screen, BLACK, [self.body[i][0], self.body[i][1], 10, 10])
def check_food(self, food):
if self.x == food.x and self.y == food.y:
self.score += 1
return True
else:
return False
def check_collision(self):
if self.x < 0 or self.x > screen_width - 10 or self.y < 0 or self.y > screen_height - 10:
return True
for i in range(3, len(self.body)):
if self.x == self.body[i][0] and self.y == self.body[i][1]:
return True
return False
# 定义食物类
class Food:
def __init__(self):
self.x = random.randrange(0, screen_width - 10, 10)
self.y = random.randrange(0, screen_height - 10, 10)
def draw(self):
pygame.draw.rect(screen, RED, [self.x, self.y, 10, 10])
# 显示开始界面
def show_start_screen():
screen.fill(WHITE)
start_text = font.render("Press SPACE to Start", True, BLACK)
screen.blit(start_text, [screen_width/2 - start_text.get_width()/2, screen_height/2 - start_text.get_height()])
pygame.display.flip()
# 等待用户按下空格键
waiting = True
while waiting:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
waiting = False
# 显示结束界面
def show_game_over_screen(score):
screen.fill(WHITE)
end_text = font.render("Game Over", True, BLACK)
score_text = font.render("Score: " + str(score), True, BLACK)
screen.blit(end_text, [screen_width/2 - end_text.get_width()/2, screen_height/2 - end_text.get_height()])
screen.blit(score_text, [screen_width/2 - score_text.get_width()/2, screen_height/2])
pygame.display.flip()
# 等待3秒然后退出游戏
pygame.time.wait(3000)
pygame.quit()
# 初始化游戏
def init_game():
snake = Snake()
food = Food()
return snake, food
# 主程序
def main():
show_start_screen()
snake, food = init_game()
# 游戏循环
running = True
clock = pygame.time.Clock()
while running:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT and snake.direction != "left":
snake.direction = "right"
elif event.key == pygame.K_LEFT and snake.direction != "right":
snake.direction = "left"
elif event.key == pygame.K_UP and snake.direction != "down":
snake.direction = "up"
elif event.key == pygame.K_DOWN and snake.direction != "up":
snake.direction = "down"
# 更新贪吃蛇
snake.update()
# 检查食物碰撞
if snake.check_food(food):
food = Food()
# 检查贪吃蛇碰撞
if snake.check_collision():
show_game_over_screen(snake.score)
running = False
# 绘制游戏界面
screen.fill(WHITE)
snake.draw()
food.draw()
# 显示分数
score_text = font.render("Score: " + str(snake.score), True, BLACK)
screen.blit(score_text, [10, 10])
pygame.display.flip()
clock.tick(10)
if __name__ == "__main__":
main()
```
这个游戏使用了 Pygame 库,它是一个 Python 的游戏开发库,提供了一些简单而强大的方法,方便开发者创建游戏。在这个代码中,我们创建了一个名为“ Snake”的类,代表贪吃蛇,也创建了一个名为“ Food”的类,代表食物。然后我们处理一些游戏逻辑,如检测是否碰到墙或自身,以及更新身体长度,这些都在 Snake 类中完成的。最后,稍作修改即可添加开始界面和结束界面。