从 Pygame 的 menu.add.text.input 创建一个变量(菜单)
Create a variable from the menu.add.text.input from Pygame (Menu)
哪里写着user_input = menu.add.text_input('User: ')
或 age_input = menu.add.text_input('Age: ')
,你可以写点什么。我需要将写入的单词分配给一个变量。我该怎么做?
import pygame
import pygame_menu
import random
pygame.init()
#Size - name of the window
surface = pygame.display.set_mode((600, 400))
pygame.display.set_caption("Example")
def game():
#Variables
score = 0
user_age = age_input.get_value()
user_name = user_input.get_value()
while True:
x = random.randint(0, 10)
y = random.randint(0, 10)
z = x + y
print(str(x) + "+" + str(y))
result = int(input())
if result == z:
print("Correct")
score = score + 5
else:
if result != z:
stop = input("Wrong! Wanna stop? ")
if stop == ("yes"):
print("You have " + str(score) + " points")
break
else:
continue
menu = pygame_menu.Menu('Menu', 600, 400,
theme=pygame_menu.themes.THEME_BLUE)
user_input = menu.add.text_input('User: ')
age_input = menu.add.text_input('Age: ')
menu.add.button('Start', game)
menu.add.button('Exit', pygame_menu.events.EXIT)
print (user_input)
print (age_input)
menu.mainloop(surface)
这是在开始游戏前检索用户数据的方法:
def start_the_game():
user_age = age_input.get_value()
print(user_age) # to print the value of user_age, only for debugging tho, don't use this when you have finished the game, because user is not really supposed to read the console anyways
# rest of the game code
age_input = menu.add.text_input('Age: ', font_name = font1,font_color = 'Black')
menu.add.button('Comencem', start_the_game,font_name = font, font_color = 'green')
哪里写着user_input = menu.add.text_input('User: ')
或 age_input = menu.add.text_input('Age: ')
,你可以写点什么。我需要将写入的单词分配给一个变量。我该怎么做?
import pygame
import pygame_menu
import random
pygame.init()
#Size - name of the window
surface = pygame.display.set_mode((600, 400))
pygame.display.set_caption("Example")
def game():
#Variables
score = 0
user_age = age_input.get_value()
user_name = user_input.get_value()
while True:
x = random.randint(0, 10)
y = random.randint(0, 10)
z = x + y
print(str(x) + "+" + str(y))
result = int(input())
if result == z:
print("Correct")
score = score + 5
else:
if result != z:
stop = input("Wrong! Wanna stop? ")
if stop == ("yes"):
print("You have " + str(score) + " points")
break
else:
continue
menu = pygame_menu.Menu('Menu', 600, 400,
theme=pygame_menu.themes.THEME_BLUE)
user_input = menu.add.text_input('User: ')
age_input = menu.add.text_input('Age: ')
menu.add.button('Start', game)
menu.add.button('Exit', pygame_menu.events.EXIT)
print (user_input)
print (age_input)
menu.mainloop(surface)
这是在开始游戏前检索用户数据的方法:
def start_the_game():
user_age = age_input.get_value()
print(user_age) # to print the value of user_age, only for debugging tho, don't use this when you have finished the game, because user is not really supposed to read the console anyways
# rest of the game code
age_input = menu.add.text_input('Age: ', font_name = font1,font_color = 'Black')
menu.add.button('Comencem', start_the_game,font_name = font, font_color = 'green')