You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pygame开发中如何通过菜单按钮调用其他文件的游戏函数

Pygame菜单启动加法游戏修改方案

核心思路

你只需要把Suma的游戏逻辑封装成可调用的独立函数,再给Menu的Start按钮绑定这个函数作为点击回调即可,InputBox已经是封装好的组件无需改动。

第一步:修改Suma.py文件

把原来全局的游戏逻辑封装为run_suma_game函数,同时保留直接运行Suma.py也能启动游戏的能力:

import pygame
import random
from InputBox import InputBox
from pygame import mixer

def run_suma_game():
    pygame.init()
    clock = pygame.time.Clock()
    surface = pygame.display.set_mode((600, 400))
    pygame.display.set_caption("Projecte MatZanfe")
    font = pygame.font.SysFont('comicsans', 50)
    base_font = pygame.font.Font(None, 32)
    user_text = ''
    color_active = pygame.Color('lightskyblue3')
    running = True
    points = 0

    def start_the_game():
        x = random.randint(0, 10)
        y = random.randint(0, 10)
        return x, y

    def display_the_game(x, y):
        z = x + y
        surface.fill((255, 70, 90))
        text = font.render(str(x) + "+" + str(y), True, (255, 255, 255))
        text_surface = base_font.render(user_text, True, (255, 255, 255))
        surface.blit(text, (260, 120))
        input_box.draw(surface)
        punts = font.render("Puntuació: " +  str(points),True, (255,255,255))
        surface.blit(punts, (350,30))
        titolsuma = font.render("SUMA (1)", True, (0,0,0))
        surface.blit(titolsuma,(10,20))

    x, y = start_the_game()
    input_box = InputBox(190, 250, 200, 32)

    while running:
        clock.tick(60)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            else:
                result = input_box.handle_event(event)
                if result != None:
                    if int(result) == int(x) + int(y):
                        points = points + 5
                        mixer.music.load('StarPost.wav')
                        mixer.music.play(1)
                    x, y = start_the_game()
                    input_box = InputBox(190, 250, 200, 32)
        display_the_game(x, y)
        pygame.display.update()
    pygame.quit()

if __name__ == '__main__':
    run_suma_game()

第二步:修改Menu.py文件

导入Suma模块的run_suma_game函数,给Start按钮绑定点击回调:

import pygame
import pygame_menu
# 导入加法游戏函数
from Suma import run_suma_game

pygame.init()
#Size and name of the window
surface = pygame.display.set_mode((600, 400))
pygame.display.set_caption("Projecte MatZanfe")

font = pygame_menu.font.FONT_8BIT
font1 = pygame_menu.font.FONT_NEVIS

menu = pygame_menu.Menu('Projecte MatZanfe', 600, 400,
                       theme=pygame_menu.themes.THEME_SOLARIZED)

user_input = menu.add.text_input('User: ', font_name = font1, font_color = 'blue')
age_input = menu.add.text_input('Age: ', font_name = font1,font_color = 'Black')
# 给Start按钮绑定回调函数
menu.add.button('Start', run_suma_game, font_name = font, font_color = 'green')
menu.add.button('Exit', pygame_menu.events.EXIT, font_name = font,font_color = 'red')
menu.mainloop(surface)

其他注意事项

  • 三个文件需要放在同一级目录下运行
  • 确保StarPost.wav音效文件放在项目根目录,否则会报加载错误
  • 退出加法游戏后会自动回到菜单页面

内容的提问来源于stack exchange,提问作者Torchllama

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.07 00:18:01