更新:添加完整脚本 - 启动脚本时,转到播放然后启动。它将运行调查问卷,但不会像你看到的那样更新(很快,然后重置;可能是由于它所在的循环)。
所以,首先:嗨伙计们。我尝试在pygame中运行一个简单的问卷。到目前为止,事情看起来还不错。但是,我遇到了一个问题,到目前为止我无法解决。我尝试在pygame窗口中运行一个函数(def_quiz),但是当我把这个函数放在'而真正的'在play_function中它继续循环(逻辑上)。我尝试将其粘贴到其他地方,例如在真实的'之上声明,但然后我的按钮不再工作了。我错过了什么?我怎样才能解决我的问题?我只是想在测验函数中播放所有问题一次。
# Import pygame and libraries
from pygame.locals import *
from random import randrange
import os
import pygame
# Import pygameMenu
import pygameMenu
from pygameMenu.locals import *
ABOUT = ['Game version: 2.0',
'Author: Mandy Koopman',
PYGAMEMENU_TEXT_NEWLINE,
'Email: mn.koopman@st.hanze.nl']
COLOR_BACKGROUND = (200, 200, 200)
COLOR_BLACK = (0, 0, 0)
COLOR_WHITE = (255, 255, 255)
FPS = 60.0
MENU_BACKGROUND_COLOR = (228, 55, 36)
WINDOW_SIZE = (1280, 720)
display_width = 1280
display_height = 720
red = (200,0,0)
green = (0,200,0)
black = (0,0,0)
bright_red = (255,0,0)
bright_green = (0,255,0)
question_prompts = [
"What color are apples?",
"What color are bananas?"
"How old are you",
]
answer_prompts = [("Antwoord A", "Antwoord B", "Antwoord C"), ("Antwoord A", "Antwoord B", "Antwoord C"),["a", "b", "c"]]
score_prompts = [(1,2,3),(1,2,3),[1,2,3]]
lijst = (0,1,2)
# -----------------------------------------------------------------------------
# Init pygame
pygame.init()
os.environ['SDL_VIDEO_CENTERED'] = '1'
# Create pygame screen and objects
surface = pygame.display.set_mode(WINDOW_SIZE)
style = pygame.display.set_mode(WINDOW_SIZE)
pygame.display.set_caption('Health Aware Game V2.0')
clock = pygame.time.Clock()
dt = 1 / FPS
# Global variables
game_topic = ['SPORTS']
# -----------------------------------------------------------------------------
def change_difficulty(d):
"""
Change difficulty of the game.
:return:
"""
print ('Selected difficulty: {0}'.format(d))
game_topic[0] = d
def random_color():
"""
Return random color.
:return: Color tuple
"""
return randrange(0, 255), randrange(0, 255), randrange(0, 255)
def text_objects(text, font):
textSurface = font.render(text, True, black)
return textSurface, textSurface.get_rect()
def button(msg,x,y,w,h,ic,ac):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(surface, ac,(x,y,w,h))
if click[0] == 1:
return True
else:
pygame.draw.rect(surface, ic,(x,y,w,h))
smallText = pygame.font.SysFont("arial",30)
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ( (x+(w/2)), (y+(h/2)) )
surface.blit(textSurf, textRect)
def update():
for i in range(len(question_prompts)):
count = i
if i <= len(question_prompts):
quiz(count)
if i == len(question_prompts):
print ("end")
return count
def quiz(count):
events = pygame.event.get()
style.fill(pygame.Color("red"))
largeText = pygame.font.Font('freesansbold.ttf',40)
TextSurf, TextRect = text_objects(question_prompts[count], largeText)
TextRect.center = ((display_width/2),(display_height/3))
style.blit(TextSurf, TextRect)
if button("Antwoord A",150,450,200,100,green,bright_green) == True:
print ("A")
count +=1
elif button("Antwoord B",550,450,200,100,red,bright_red) == True:
print ("B")
count +=1
elif button("Antwoord C",950,450,200,100,red,bright_red) == True:
print ("C")
count +=1
def play_function(game_topic, font):
"""
Main game function
:param difficulty: Difficulty of the game
:param font: Pygame font
:return: None
"""
#game_topic = game_topic[0]
#assert isinstance(game_topic, str)
#if game_topic == 'SPORTS':
# f = font.render('Playing as normie', 1, COLOR_WHITE)
#elif game_topic == 'FOOD':
# f = font.render('Playing as normie', 1, COLOR_WHITE)
#elif game_topic == 'SLEEP':
# f = font.render('Playing as god', 1, COLOR_WHITE)
#else:
# raise Exception('Unknown difficulty {0}'.format(game_topic))
# Draw random color and text
#bg_color = random_color()
#f_width = f.get_size()[0]
# Reset main menu and disable
# You also can set another menu, like a 'pause menu', or just use the same
# main_menu as the menu that will check all your input.
main_menu.disable()
main_menu.reset(1)
while True:
# Clock tick
clock.tick(60)
# Application events
update()
playevents = pygame.event.get()
for e in playevents:
if e.type == QUIT:
exit()
elif e.type == KEYDOWN:
if e.key == K_ESCAPE:
if main_menu.is_disabled():
main_menu.enable()
# Quit this function, then skip to loop of main-menu on line 197
return
# Pass events to main_menu
main_menu.mainloop(playevents)
# Continue playing
#surface.fill(bg_color)
#surface.blit(f, ((WINDOW_SIZE[0] - f_width) / 2, WINDOW_SIZE[1] / 2))
clock.tick(15)
pygame.display.flip()
def main_background():
"""
Function used by menus, draw on background while menu is active.
:return: None
"""
surface.fill(COLOR_BACKGROUND)
# -----------------------------------------------------------------------------
# PLAY MENU
play_menu = pygameMenu.Menu(surface,
bgfun=main_background,
color_selected=COLOR_WHITE,
font=pygameMenu.fonts.FONT_BEBAS,
font_color=COLOR_BLACK,
font_size=30,
menu_alpha=100,
menu_color=MENU_BACKGROUND_COLOR,
menu_height=int(WINDOW_SIZE[1] * 0.8),
menu_width=int(WINDOW_SIZE[0] * 0.8),
onclose=PYGAME_MENU_DISABLE_CLOSE,
option_shadow=False,
title='Play menu',
window_height=WINDOW_SIZE[1],
window_width=WINDOW_SIZE[0]
)
# When pressing return -> play(DIFFICULTY[0], font)
play_menu.add_option('Start', play_function, game_topic,
pygame.font.Font(pygameMenu.fonts.FONT_FRANCHISE, 30))
play_menu.add_selector('Select game topic', [('Movement & Sports', 'SPORTS'),
('Food & Health', 'FOOD'),
('Sleep & Rest', 'SLEEP'),
('Other games', 'GAMES')],
onreturn=None,
onchange=change_difficulty)
play_menu.add_option('Return to main menu', PYGAME_MENU_BACK)
#highscores
high_menu = pygameMenu.Menu(surface,
bgfun=main_background,
color_selected=COLOR_WHITE,
font=pygameMenu.fonts.FONT_BEBAS,
font_color=COLOR_BLACK,
font_size=30,
menu_alpha=100,
menu_color=MENU_BACKGROUND_COLOR,
menu_height=int(WINDOW_SIZE[1] * 0.8),
menu_width=int(WINDOW_SIZE[0] * 0.8),
onclose=PYGAME_MENU_DISABLE_CLOSE,
option_shadow=False,
title='Highscores',
window_height=WINDOW_SIZE[1],
window_width=WINDOW_SIZE[0]
)
# When pressing return -> play(DIFFICULTY[0], font)
high_menu.add_selector('Select highscores', [('Movement & Sports', 'SPORTS'),
('Food & Health', 'FOOD'),
('Sleep & Rest', 'SLEEP'),
('Other games', 'GAMES')],
onreturn=None,
onchange=change_difficulty)
high_menu.add_option('Start', play_function, game_topic,
pygame.font.Font(pygameMenu.fonts.FONT_FRANCHISE, 30))
high_menu.add_option('Return to main menu', PYGAME_MENU_BACK)
# ABOUT MENU
about_menu = pygameMenu.TextMenu(surface,
bgfun=main_background,
color_selected=COLOR_WHITE,
font=pygameMenu.fonts.FONT_BEBAS,
font_color=COLOR_BLACK,
font_size_title=30,
font_title=pygameMenu.fonts.FONT_8BIT,
menu_color=MENU_BACKGROUND_COLOR,
menu_color_title=COLOR_WHITE,
menu_height=int(WINDOW_SIZE[1] * 0.8),
menu_width=int(WINDOW_SIZE[0] * 0.8),
onclose=PYGAME_MENU_DISABLE_CLOSE,
option_shadow=False,
text_color=COLOR_BLACK,
text_fontsize=20,
title='About',
window_height=WINDOW_SIZE[1],
window_width=WINDOW_SIZE[0]
)
for m in ABOUT:
about_menu.add_line(m)
about_menu.add_line(PYGAMEMENU_TEXT_NEWLINE)
about_menu.add_option('Return to menu', PYGAME_MENU_BACK)
# MAIN MENU
main_menu = pygameMenu.Menu(surface,
bgfun=main_background,
color_selected=COLOR_WHITE,
font=pygameMenu.fonts.FONT_BEBAS,
font_color=COLOR_BLACK,
font_size=30,
menu_alpha=100,
menu_color=MENU_BACKGROUND_COLOR,
menu_height=int(WINDOW_SIZE[1] * 0.8),
menu_width=int(WINDOW_SIZE[0] * 0.8),
onclose=PYGAME_MENU_DISABLE_CLOSE,
option_shadow=False,
title='Main menu',
window_height=WINDOW_SIZE[1],
window_width=WINDOW_SIZE[0]
)
main_menu.add_option('Play', play_menu)
main_menu.add_option('Highscores', high_menu)
main_menu.add_option('About', about_menu)
main_menu.add_option('Quit', PYGAME_MENU_EXIT)
# -----------------------------------------------------------------------------
# Main loop
while True:
# Tick
clock.tick(60)
# Application events
events = pygame.event.get()
for event in events:
if event.type == QUIT:
exit()
# Main menu
main_menu.mainloop(events)
# Flip surface
pygame.display.flip()
答案 0 :(得分:0)
您可能希望将按钮创建/更新分离到将在while循环中调用的其他函数,因为除非它们在那里,否则它们将无法正确更新。或者,您可以尝试将测验()转换为状态机。
答案 1 :(得分:0)
这是轮询事件队列并获取按键/按钮按下事件的函数:
events = pygame.event.get()
在while循环中,您提到此行永远不会被运行,因此事件不会被捕获。
此外,您不会致电clock.tick()
因此你必须在play_function中避免一段时间(在那里调用两个函数是反模式......它是正确的,它们只在主循环中被调用,所以将它们留在现在的位置)。分配一个状态变量并将play_function
转换为仅执行循环的一次迭代的函数,返回主循环,您可以再次调用pygame.event.get()
并让主循环执行所有循环。对于一个状态(你定义),主循环将再次调用play_function
并检查这是否是第一次执行play_function,所以要执行或不执行&#34;当前的行#&# 34 ;.或者更好的是,除play_function
,例如play_init
和play_main
,在2中处理单个州,如果要调用play_init
或跳至play_main
。
示例:
# Probably globals or better in a Class.
initialized = False
game_on = False
def play_function(playevents, game_topic, font):
game_on = True;
def play_init():
main_menu.disable()
main_menu.reset(1)
initialized = True
def play_action(playevents, game_topic, font):
# skipping comments
# add your stuff here
update()
for e in playevents:
if e.type == KEYDOWN:
if e.key == K_ESCAPE:
if main_menu.is_disabled():
main_menu.enable()
game_on = False
initialized = False
return
# Continue playing
#surface.fill(bg_color)
#surface.blit(f, ((WINDOW_SIZE[0] - f_width) / 2, WINDOW_SIZE[1] / 2))
# Main loop
while True:
# Tick
clock.tick(60 if (game_on == False) else 15)
# Application events
events = pygame.event.get()
for event in events:
if event.type == QUIT:
exit()
if game_on == True:
if initialized == False:
play_init()
# this will be called every loop. this is what I am saying.
play_action(events, game_topic, font) # expose args here
else:
# Main menu
main_menu.mainloop(events)
# Flip surface
pygame.display.flip()