# -*-coding:utf-8 -*- # # --------------------------------------------------------------------------- # ProjectName: Game_project # FileName: page.py # Author: Zhi_yuan # Datetime: 2026/7/25 下午10:44 # Description: # --------------------------------------------------------------------------- import pygame # 初始化pygame pygame.init() # 初始化音乐 pygame.mixer.init() # 设置窗口大小 size = (800, 600) screen = pygame.display.set_mode(size) # 窗口名称 pygame.display.set_caption("New_game") # 播放并循环音乐 pygame.mixer_music.load(r'D:\python_project\Game_project\file\vespidaze-upbeat-rpg-battle-460971.mp3') pygame.mixer_music.play(-1) # 状态值 dirty_x = 400 dirty_y = 300 Left_status = False right_status = False up_status = False down_status = False # 颜色 WHITE = (255, 255, 255) RED = (255, 0, 0) BLUE = (0, 0, 255) # 游戏循环 running = True 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_LEFT or event.key == pygame.K_a: Left_status = True elif event.key == pygame.K_RIGHT or event.key == pygame.K_d: right_status = True elif event.key == pygame.K_UP or event.key == pygame.K_w: up_status = True elif event.key == pygame.K_DOWN or event.key == pygame.K_s: down_status = True # 松开按键 elif event.type == pygame.KEYUP: if event.key == pygame.K_LEFT or event.key == pygame.K_a: Left_status = False elif event.key == pygame.K_RIGHT or event.key == pygame.K_d: right_status = False elif event.key == pygame.K_UP or event.key == pygame.K_w: up_status = False elif event.key == pygame.K_DOWN or event.key == pygame.K_s: down_status = False elif event.type == pygame.MOUSEBUTTONDOWN: print("Mouse button pressed at", event.pos) if Left_status and dirty_x >= 0: dirty_x -= 1 if right_status and dirty_x <= 780: dirty_x += 1 if up_status and dirty_y >= 0: dirty_y -= 1 if down_status and dirty_y <= 570: dirty_y += 1 # 填充窗口颜色 screen.fill((255, 255, 255)) # 绘制矩形 dirty = pygame.draw.rect(screen, BLUE, [dirty_x, dirty_y, 20, 30]) pygame.display.update() pygame.quit()