2024.9.10(周二)

import pygame
import time
import random

# 初始化pygame
pygame.init()

# 游戏窗口的尺寸
window_width = 800
window_height = 400
cell_size = 20

# 颜色定义
black = (0, 0, 0)
white = (255, 255, 255)
red = (213, 50, 80)
green = (0, 255, 0)
blue = (50, 153, 213)

# 创建游戏窗口
dis = pygame.display.set_mode((window_width, window_height))
pygame.display.set_caption('贪吃蛇小游戏')

# 时钟
clock = pygame.time.Clock()
snake_speed = 8

# 字体路径
font_path = 'SimHei.ttf'  # 确保这个文件存在于你的工作目录下

#
def our_snake(snake_block, snake_list):
    for x in snake_list:
        pygame.draw.rect(dis, black, [x[0], x[1], snake_block, snake_block])

# 显示分数
def Your_score(score):
    font_style = pygame.font.Font(font_path, 35)
    value = font_style.render("你的分数: " + str(score), True, black)
    dis.blit(value, [0, 0])

# 游戏主循环
def gameLoop():
    game_over = False
    game_close = False

    x1 = window_width / 2
    y1 = window_height / 2

    x1_change = 0
    y1_change = 0

    snake_List = []
    Length_of_snake = 1

    foodx = round(random.randrange(0, window_width - cell_size) / 20.0) * 20.0
    foody = round(random.randrange(0, window_height - cell_size) / 20.0) * 20.0

    while not game_over:

        while game_close == True:
            dis.fill(blue)
            Your_score(Length_of_snake - 1)
            font_style = pygame.font.Font(font_path, 35)
            mesg = font_style.render("你输了! 按Q退出 或 按C重新开始", True, red)
            dis.blit(mesg, [window_width / 6, window_height / 3])
            pygame.display.update()

            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_q:
                        game_over = True
                        game_close = False
                    if event.key == pygame.K_c:
                        gameLoop()

        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:
                    x1_change = -cell_size
                    y1_change = 0
                elif event.key == pygame.K_RIGHT:
                    x1_change = cell_size
                    y1_change = 0
                elif event.key == pygame.K_UP:
                    y1_change = -cell_size
                    x1_change = 0
                elif event.key == pygame.K_DOWN:
                    y1_change = cell_size
                    x1_change = 0

        if x1 >= window_width or x1 < 0 or y1 >= window_height or y1 < 0:
            game_close = True
        x1 += x1_change
        y1 += y1_change
        dis.fill(blue)
        pygame.draw.rect(dis, green, [foodx, foody, cell_size, cell_size])
        snake_Head = []
        snake_Head.append(x1)
        snake_Head.append(y1)
        snake_List.append(snake_Head)
        if len(snake_List) > Length_of_snake:
            del snake_List[0]

        for x in snake_List[:-1]:
            if x == snake_Head:
                game_close = True

        our_snake(cell_size, snake_List)
        Your_score(Length_of_snake - 1)

        pygame.display.update()

        if x1 == foodx and y1 == foody:
            foodx = round(random.randrange(0, window_width - cell_size) / 20.0) * 20.0
            foody = round(random.randrange(0, window_height - cell_size) / 20.0) * 20.0
            Length_of_snake += 1

        clock.tick(snake_speed)

    pygame.quit()
    quit()

gameLoop()

软件构造的作业:贪吃蛇小游戏(python版)

posted @ 2024-09-14 14:33  记得关月亮  阅读(2)  评论(0编辑  收藏  举报