95 lines
3.2 KiB
Python
95 lines
3.2 KiB
Python
import pygame
|
|
from pygame import Surface
|
|
from pygame.constants import QUIT
|
|
from maze import Maze
|
|
|
|
from draw import Button, Toast
|
|
import sys
|
|
import os
|
|
|
|
UI_HEIGHT = 1000
|
|
UI_WIDTH = 1500
|
|
|
|
MAZE_SIZE = 150
|
|
WALL_SIZE = 50
|
|
FPS = 120
|
|
|
|
screen: Surface = None # 窗口实例
|
|
clock = None # 时钟实例
|
|
|
|
textFont = None # 字体
|
|
|
|
|
|
def pygameInit(title: str = "pygame"):
|
|
"""初始化 pygame"""
|
|
pygame.init()
|
|
pygame.mixer.init() # 声音初始化
|
|
pygame.display.set_caption(title)
|
|
global screen, clock, textFont # 修改全局变量
|
|
screen = pygame.display.set_mode((UI_WIDTH, UI_HEIGHT))
|
|
clock = pygame.time.Clock()
|
|
# Initialize font with UTF-8 support
|
|
pygame.font.init()
|
|
textFont = pygame.font.Font("syht.otf", 18)
|
|
|
|
if __name__ == "__main__":
|
|
pygameInit("maze")
|
|
maze = Maze(wall_size=WALL_SIZE, maze_size=MAZE_SIZE, file_name="maze.csv")
|
|
image_wall = pygame.image.load("assets/wall.png").convert_alpha()
|
|
image_wall = pygame.transform.scale(image_wall, (WALL_SIZE, WALL_SIZE)) # 例如缩放到50x50像素
|
|
|
|
image_coin = pygame.image.load("assets/coin.png").convert_alpha()
|
|
image_coin = pygame.transform.scale(image_coin, (WALL_SIZE, WALL_SIZE)) # 例如缩放到50x50像素
|
|
|
|
image_trap = pygame.image.load("assets/trap.png").convert_alpha()
|
|
image_trap = pygame.transform.scale(image_trap, (WALL_SIZE, WALL_SIZE)) # 例如缩放到50x50像素
|
|
|
|
button_start_texture = pygame.image.load("assets/start_button.png").convert_alpha()
|
|
button_start_texture = pygame.transform.scale(button_start_texture, (200, 100))
|
|
button_start = Button(pygame.rect.Rect(MAZE_SIZE + ((UI_WIDTH - MAZE_SIZE) / 2 - 100), 0, 200, 100), button_start_texture)
|
|
|
|
button_save_texture = pygame.image.load("assets/save.png").convert_alpha()
|
|
button_save_texture = pygame.transform.scale(button_save_texture, (80, 80))
|
|
button_save = Button(pygame.rect.Rect(MAZE_SIZE + ((UI_WIDTH - MAZE_SIZE) / 2 - 100), 110, 80, 80), button_save_texture)
|
|
|
|
|
|
# 没有生成迷宫就保存的提示框
|
|
mes1 = Toast("没有生成迷宫,无法保存", UI_WIDTH, UI_HEIGHT, font=textFont)
|
|
mes2 = Toast("迷宫已保存", UI_WIDTH, UI_HEIGHT, font=textFont)
|
|
|
|
|
|
running = True
|
|
while running:
|
|
clock.tick(FPS) # 限制帧数
|
|
screen.fill((255, 255, 255)) # 铺底
|
|
for event in pygame.event.get():
|
|
button_start.handle_event(event=event)
|
|
button_save.handle_event(event=event)
|
|
|
|
if button_start.pressed == True:
|
|
maze.generate()
|
|
|
|
if button_save.pressed == True:
|
|
if len(maze.grid) == 0:
|
|
mes1.show()
|
|
else:
|
|
maze.export_to_csv("maze.csv")
|
|
mes2.text = "迷宫已保存至maze.csv"
|
|
mes2.show()
|
|
|
|
if event.type == QUIT:
|
|
running = False
|
|
|
|
|
|
maze.draw(screen=screen, wall_texture=image_wall, coin_texture=image_coin, trap_texture=image_trap)
|
|
button_start.draw(screen=screen)
|
|
button_save.draw(screen=screen)
|
|
|
|
mes1.draw(screen=screen)
|
|
mes2.draw(screen=screen)
|
|
pygame.display.flip()
|
|
pygame.quit()
|
|
|
|
|
|
|
|
|