[英]Loop in pygame - it never continues but keeps looping; what am I missing
更新:添加了完整脚本-启动脚本时,先播放然后再启动。 它将运行问卷,但不会如您所见更新(很快,然后重置;可能是由于循环的影响)。
所以,首先:嗨。 我尝试在pygame中运行一个简单的问卷。 到目前为止,一切看起来还不错。 但是,我遇到了一个问题,直到现在我还是无法解决。 我尝试在pygame窗口中运行一个函数(def_quiz),但是当我将此函数放在play_function中的“ while True”下面时,它将继续循环(逻辑上)。 我试图将其粘贴到其他位置,例如,在“ while True”语句上方,但随后我的按钮不再起作用。 我想念什么? 我该如何解决我的问题? 我只想只玩一次测验功能中的所有问题。
# 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()
您可能希望将按钮的创建/更新分离到一个在while循环内调用的不同函数,因为除非它们在那里,否则它们将无法正确更新。 或者,您可以尝试将quiz()转换为状态机。
这是轮询事件队列并获取按下的键/按钮事件的函数:
events = pygame.event.get()
在while循环中,您提到此行永远不会运行,因此不会捕获事件。
此外,您不会调用clock.tick()
因此,您必须避免在play_function中使用一会儿(这是一种反模式,可以在while内调用这两个函数...正确的是,它们仅在主循环中被调用,因此将它们保留在当前位置)。 分配一个状态变量并将play_function
转换为仅执行循环一次迭代的函数,返回主循环,您可以在其中再次调用pygame.event.get()
并让主循环执行所有循环。 在状态(您定义)的情况下,主循环将再次调用play_function
,并检查这是否是play_function的首次执行,因此要执行“ current while”之前的行。 或play_function
是,将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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.