【问题标题】:Introducing a function with Pygame to appear on screen [duplicate]使用 Pygame 引入一个功能以显示在屏幕上 [重复]
【发布时间】:2021-08-13 20:27:08
【问题描述】:

我正在为我的学校项目做一个数学程序,我已经准备好游戏的菜单和游戏的过程,基本上就是无限循环求和,直到你失败,每次给你 5 分正确答案(将来我计划将它连接到 SQLite 基础,因为它会询问您的姓名和年龄)。 无论如何,这里是总和和菜单的代码:

https://www.sololearn.com/Discuss/2848691/help-me-to-turn-this-into-a-function-and-make-this-infinite-school-project https://www.sololearn.com/Discuss/2856421/making-the-school-project-menu-with-pygame-menu-school-project

[更新] 问题在于,当我尝试使总和的代码出现在按“Comencem”后菜单生成的窗口中时,总和出现在 Python 控制台中,但不在窗口中。我希望它出现在窗口中,但我不知道该怎么做。有什么帮助吗?

import pygame
import pygame_menu
from pygame import mixer
import random

pygame.init()
#Mida i nom de la finestra
surface = pygame.display.set_mode((600, 400))
pygame.display.set_caption("Projecte MatZanfe")
#Logotip de la finestra
logotip = pygame.image.load("calculator.png")
pygame.display.set_icon(logotip)

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

def start_the_game():
    # Variables
    puntuacio = 0
    while True:
        x = random.randint(0, 10)
        y = random.randint(0, 10)
        z = x + y
        print(str(x) + "+" + str(y))
        resultat = int(input())
        if resultat == z:
            print("Correcte")
            puntuacio = puntuacio + 5
            print("Tens aquests punts:", puntuacio)
        else:
            if resultat != z:
                print("Malament!")
                parar = input("Vols parar? ")
                if parar == ("si"):
                    print("Has aconseguit " + str(puntuacio) + " punts")
                    break
                else:
                    continue

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

menu.add.text_input('Usuari: ', font_name = font1, font_color = 'blue')
mixer.music.load('MusicaMenu.wav')
mixer.music.play(-1)
menu.add.text_input('Edat: ', font_name = font1,font_color = 'Black')
menu.add.button('Comencem', start_the_game,font_name = font, font_color = 'green')
menu.add.button('Sortir', pygame_menu.events.EXIT, font_name = font,font_color = 'red')

menu.mainloop(surface)

(有些词是加泰罗尼亚语,但我想你可以理解。如果你不能,我会改变它。

【问题讨论】:

  • 因为您没有将任何文本呈现到屏幕上; read the docs
  • 看了一下,还是不明白怎么介绍。我找了一些教程,但没有什么能真正解释我在找什么。有人可以给我一个他们会做什么的例子吗?抱歉,我是新手,非常感谢。

标签: python python-3.x pygame menu project


【解决方案1】:

这是一个非常简单的例子,说明如何在pygame中呈现文本:

import pygame


pygame.init()
screen = pygame.display.set_mode((500, 400))
font = pygame.font.SysFont('comicsans', 50)

x = 0
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()

    screen.fill((0, 0, 0))
    text = font.render(str(x), True, (255, 255, 255))
    screen.blit(text, (0, 0))
    pygame.display.update()
    x += 1

这是你的函数的样子:

import pygame
import random

pygame.init()
surface = pygame.display.set_mode((600, 400))
pygame.display.set_caption("Projecte MatZanfe")
font = pygame.font.SysFont('comicsans', 50)


def start_the_game():
    # Variables
    points = 0
    while True:
        x = random.randint(0, 10)
        y = random.randint(0, 10)
        z = x + y
        surface.fill((0, 0, 0))
        text = font.render(str(x) + "+" + str(y), True, (255, 255, 255))
        surface.blit(text, (0, 0))
        pygame.display.update()
        result = int(input())
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit()
        if result == z:
            print("Correcte")
            points += 5
            print("Tens aquests punts:", points)
        else:
            if result != z:
                print("Malament!")
                parar = input("Vols parar? ")
                if parar == "yes":
                    print("Has aconseguit " + str(points) + " punts")
                    break
                else:
                    continue


start_the_game()

问题是input 不允许运行脚本会冻结窗口

来源:

【讨论】:

  • 不,我已尝试应用您告诉我的内容,但我一直在 python 解释器中获取总和,而不是在窗口中。除此之外,如果我单击“开始”,这将被翻译为“让我们开始”,窗口冻结,但总和出现在解释器上。我将显示代码。我希望任何人都可以解决这个烂摊子。
  • @Torchllama 它冻结,因为您要求输入input() 会停止脚本,直到您输入某事为止
  • 那我该如何解决呢?我真的需要在那里请求一个 input()。
  • 好的,但是当没有输入任何内容时它会冻结,或者你需要让用户输入其他方式,例如使用threading
  • 是否可以在开始时要求输入(在菜单中,在按下“Comencem”之前)?因为我可以在游戏开始前询问这个人的姓名和年龄,但是我应该对代码进行哪些更改?
猜你喜欢
  • 2020-11-18
  • 1970-01-01
  • 1970-01-01
  • 2017-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-24
相关资源
最近更新 更多