【发布时间】:2019-08-27 03:15:51
【问题描述】:
(英语不是我的母语,如有错误请见谅。) (谢谢大家!!)
当我点击按钮“1”时,会出现BE01,但如果我不点击,scene01Img会返回。
我尝试在 def BE01(): 中使用 gameExit = true,但它不起作用。
pygame.init()
clock = pygame.time.Clock()
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("comicsansms",20)
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ( (x+(w/2)), (y+(h/2)) )
gameDisplay.blit(textSurf, textRect)
def BE01():
gameDisplay.fill(white)
gameDisplay.blit(BE01Img,(0,0))
button("BACK",350,450,100,50,black,gray,game_intro)
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(introImg,(0,0))
button("START",350,450,100,50,black,gray,game_loop)
pygame.display.update()
clock.tick(15)
def quitgame():
pygame.quit()
quit()
def game_loop():
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
gameDisplay.fill(white)
gameDisplay.blit(scene01Img,(0,0))
button("1",200,450,100,50,black,gray,BE01)
pygame.display.update()
clock.tick(60)
game_intro()
game_loop()
pygame.quit()
quit()
点击“1”按钮后,BE01会出现,运行另一个程序,scene01应该不会出现。
【问题讨论】:
-
BE01可以将introImg分配给scene01Img,然后game_loop将显示新图像。但是BE01中还有新按钮,所以BE01应该使用新背景和新按钮运行新循环。 -
gameExit是仅存在于game_loop内部的局部变量 - 要在BE01中更改它,它必须是全局变量。您可以在使用变量gameExit的每个函数中使用global gameExit -
每个场景都应该有自己的循环或者每个场景应该有自己的功能
draw和fill, blit, button并在按下按钮时将draw替换为game_loop。在 Python 中,您可以将函数赋值给变量,即。show = print和稍后使用它show("Hello World"). You can use global variable to keep functiondraw` 你将在game_loop中运行并更改draw = draw_intro或draw = draw_game等。
标签: python loops button pygame