引入带有 Pygame 的功能以显示在屏幕上
Introducing a function with Pygame to appear on screen
我正在为我的学校项目做一个数学程序,我已经准备好游戏的菜单和游戏的过程,基本上就是无限循环求和直到失败,给你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' 后菜单产生的 window 中时,总和出现在 Python 控制台中,但不在window。我希望它出现在 window 中,但我不知道该怎么做。有什么帮助吗?
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)
(有些词是加泰罗尼亚语,但我想你能看懂,看不懂我会改的)
这是一个非常简单的示例,说明如何在 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
不允许脚本 运行 冻结 window
来源:
我正在为我的学校项目做一个数学程序,我已经准备好游戏的菜单和游戏的过程,基本上就是无限循环求和直到失败,给你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' 后菜单产生的 window 中时,总和出现在 Python 控制台中,但不在window。我希望它出现在 window 中,但我不知道该怎么做。有什么帮助吗?
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)
(有些词是加泰罗尼亚语,但我想你能看懂,看不懂我会改的)
这是一个非常简单的示例,说明如何在 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
不允许脚本 运行 冻结 window
来源: