贪吃蛇作业

贪吃蛇界面:

 代码:

import pygame  
import random  
import sys  
import tkinter as tk  
from tkinter import messagebox 
# 初始化pygame  
pygame.init()  

# 设置屏幕大小  
screen_width = 640  
screen_height = 480  
screen = pygame.display.set_mode((screen_width, screen_height))  

# 设置颜色  
black = (0, 0, 0)  
white = (255, 255, 0)  
red = (0, 255, 255)  

# 设置游戏参数  
snake_block = 20  
snake_speed = 7  

# 蛇的初始位置和长度  
snake_list = [(screen_width // 2, screen_height // 2)]  
snake_length = 1  

# 食物位置  
food_x = random.randint(0, (screen_width // snake_block) - 1) * snake_block  
food_y = random.randint(0, (screen_height // snake_block) - 1) * snake_block  

# 游戏方向  
direction = 'RIGHT'  
change_to = direction  

# 游戏循环标志  
game_over = False  

clock = pygame.time.Clock()  


# 显示弹窗并退出游戏的函数
def show_game_over_message():
    root = tk.Tk()
    root.withdraw()  # 隐藏主窗口
    messagebox.showinfo("Game Over", "你失败了!")
    root.destroy()
    pygame.quit()
    sys.exit()

    
# 游戏主循环  
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 and direction != 'RIGHT':  
                change_to = 'LEFT'  
            if event.key == pygame.K_RIGHT and direction != 'LEFT':  
                change_to = 'RIGHT'  
            if event.key == pygame.K_UP and direction != 'DOWN':  
                change_to = 'UP'  
            if event.key == pygame.K_DOWN and direction != 'UP':  
                change_to = 'DOWN'  

    if change_to == 'LEFT' and direction != 'RIGHT':  
        direction = 'LEFT'  
    if change_to == 'RIGHT' and direction != 'LEFT':  
        direction = 'RIGHT'  
    if change_to == 'UP' and direction != 'DOWN':  
        direction = 'UP'  
    if change_to == 'DOWN' and direction != 'UP':  
        direction = 'DOWN'  

    # 移动蛇  
    if direction == 'RIGHT':  
        snake_head = (snake_list[-1][0] + snake_block, snake_list[-1][1])  
    if direction == 'LEFT':  
        snake_head = (snake_list[-1][0] - snake_block, snake_list[-1][1])  
    if direction == 'UP':  
        snake_head = (snake_list[-1][0], snake_list[-1][1] - snake_block)  
    if direction == 'DOWN':  
        snake_head = (snake_list[-1][0], snake_list[-1][1] + snake_block)  

    # 检查蛇是否撞到自己或边界  
    if snake_head[0] < 0 or snake_head[0] >= screen_width or snake_head[1] < 0 or snake_head[1] >= screen_height:  
        root = tk.Tk()
        root.withdraw()  # 隐藏主窗口
        messagebox.showinfo("Game Over", "你失败了!")
        root.destroy()
        game_over = True
    # 这里我们只检测蛇是否撞到自身的“身体”部分,不包括蛇头  
    if snake_head in snake_list[:-1]:
        root = tk.Tk()
        root.withdraw()  # 隐藏主窗口
        messagebox.showinfo("Game Over", "你失败了!")
        root.destroy()
        game_over = True  

    # 吃食物  
    if snake_head == (food_x, food_y):  
        snake_length += 1  
        food_x = random.randint(0, (screen_width // snake_block) - 1) * snake_block  
        food_y = random.randint(0, (screen_height // snake_block) - 1) * snake_block  

    # 更新蛇的位置  
    snake_list.append(snake_head)  
    if len(snake_list) > snake_length:  
        del snake_list[0]  

    # 绘制屏幕  
    screen.fill(black)  

    for block in snake_list[:-1]:  
        pygame.draw.rect(screen, white, [block[0], block[1], snake_block, snake_block])  

    # 绘制蛇头为红色以区分  
    pygame.draw.rect(screen, red, [snake_list[-1][0], snake_list[-1][1], snake_block, snake_block])  
    pygame.draw.rect(screen, white, [food_x, food_y, snake_block, snake_block])  

    pygame.display.update()  

    clock.tick(snake_speed)  

pygame.quit()  
sys.exit()  

 

posted @ 2024-09-17 22:47  艾鑫4646  阅读(5)  评论(0编辑  收藏  举报