【发布时间】:2020-08-10 03:39:06
【问题描述】:
我的前几个按钮工作正常,但我的 CSTWindow 一个有时只能工作。我不愿意发帖,因为我是自学成才的,代码不会完美无缺,任何关于如何让这个按钮一直工作而不是每 10 次点击的建议都将不胜感激。我的部分代码如下。我再次自学它并不完美!
def button(msg,x,y,w,h,ic,ac,action=None):
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(gameDisplay, ac, (x,y,w,h))
if click[0] == 1 and action != None:
action()
else:
pygame.draw.rect(gameDisplay, ic, (x,y,w,h))
smallText = pygame.font.SysFont("Ariel", 20)
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ( (x+(w/2)), (y+(h/2)) )
gameDisplay.blit(textSurf, textRect)
def game_intro():
intro = True
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
gameDisplay.fill(white)
gameDisplay.blit(background, (x,y))
largeText = pygame.font.SysFont("Arial",45)
TextSurf, TextRect = text_objects("Crime Scene and Evidence Examination Game", largeText)
TextRect.center = ((display_width/2),(display_height/1.2))
gameDisplay.blit(TextSurf, TextRect)
button("Scene Selection",150,540,100,50,blue,bright_blue,game_loop)
button("Quit",550,540,100,50,red,bright_red,quitgame)
pygame.display.update()
def CSTWindow():
CSTWindow = True
while CSTWindow:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
print("Crime Scene")
def TutorialWindow():
tutorial = True
while tutorial:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
screen = pygame.display.set_mode((display_width,display_height))
screen.fill(white)
screen.blit(TutorialBG, (x,y))
button("Crime Scene",150,540,100,50,blue,bright_blue,CSTWindow)
pygame.display.update()
def game_loop():
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
gameDisplay.fill(blue)
button("Tutorial",(370), (100),100, 50, lightBlue, bright_blue,TutorialWindow)
pygame.display.update()
clock.tick(60)
【问题讨论】:
标签: python events button pygame