【发布时间】:2018-11-04 23:46:46
【问题描述】:
当按下正确的提示按钮时,我希望 pygame 界面切换到新页面(从随机确定的页面顺序)。初始化时,页面不会转换到 for 循环中所述的下一页。或者,将所有内容放在 while true 循环中只是在 func1 和 func2 的内容之间无限循环,而不考虑按键。任何见解将不胜感激。
在 Python 3x 和 pygame 1.9.5 上运行
import pygame
from pygame.locals import *
import random
control1 = 0
control2= 0
pygame.init()
display_width = 500
display_height = 500
black = (0,0,0)
white = (255,255,255)
gd = pygame.display.set_mode((display_width,display_height))
myfont = pygame.font.SysFont("Arial", 30)
pygame.event.pump()
def func1():
global control1
control1 = 1
gd.fill(white)
letter = myfont.render("Press w",0,(black))
gd.blit(letter,(100,100))
pygame.display.flip()
def func2():
global control2
control2 = 1
gd.fill(white)
letter = myfont.render("Press d",0,(black))
gd.blit(letter,(100,100))
pygame.display.flip()
my_sample = random.sample(range(2), 2)
for i in my_sample:
if my_sample[i] == 0:
func1()
if control1 == 0:
continue
if my_sample[i] == 1:
func2()
if control2 == 0:
continue
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w and control1 == 1:
control1 = 0
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_d and control2 == 1:
control2 = 0
pygame.display.update()
【问题讨论】:
标签: python python-3.x pygame