我正在学习pygame上的按钮功能,我将主菜单上的“播放”按钮链接到第二个序列(游戏循环),但不是转到第二个序列,而是直接进入第三个序列(你赢了/输了)或暂停菜单),跳过导致你输/赢菜单的第二个序列上的按钮。请帮助!!
import pygame as pg
import time
pg.init()
display_height = 900
display_width = 600
black = (0,0,0)
white = (255,255,255)
green = (0, 255, 12)
red = (255,0,0)
blue = (0,97,255)
gd = pg.display.set_mode((display_height,display_width))
pg.display.set_caption('For-Get-Ful')
clock = pg.time.Clock()
gd.fill(white)
pg.display.flip()
def text_objects(text, font, color):
textSurface = font.render(text, True, color)
return textSurface, textSurface.get_rect()
def intro_button(msg,x,y,w,h,ic,ac,action=None):
mouse1 = pg.mouse.get_pos()
click = pg.mouse.get_pressed()
if x+w > mouse1[0] > x and y+h > mouse1[1] > y:
pg.draw.rect(gd,ac,(x,y,w,h))
smalltext = pg.font.Font("freesansbold.ttf",20)
textsurf, textrect = text_objects (msg,smalltext,black)
textrect.center = ( (x+(w/2)), (y+(h/2)) )
gd.blit(textsurf,textrect)
if click[0] == 1 and action != None:
if action == "play":
second_seq()
elif action == "quit":
third_seq()
else:
pg.draw.rect(gd,ic,(x,y,w,h))
smalltext = pg.font.Font("freesansbold.ttf",20)
textsurf, textrect = text_objects (msg,smalltext,black)
textrect.center = ( (x+(w/2)), (y+(h/2)) )
gd.blit(textsurf,textrect)
gd.blit(textsurf,textrect)
def first_seq():
fs = True
# To keep the game running longer than 1 frame
while fs:
for event in pg.event.get():
if event.type == pg.QUIT:
pg.quit()
quit()
gd.fill(black)
pg.draw.rect(gd,blue,(400,400,400,400))
intro_button("Play",100,100,100,100,green,green,"play")
pg.display.update()
clock.tick(15)
def second_seq():
ss = True
# To keep the game running longer than 1 frame
while ss:
for event in pg.event.get():
if event.type == pg.QUIT:
pg.quit()
quit()
gd.fill(black)
pg.draw.rect(gd,blue,(190,130,530,130))
intro_button("quit",100,100,100,100,red,red,"quit")
pg.display.update()
clock.tick(15)
def third_seq():
ts = True
# To keep the game running longer than 1 frame
while ts:
for event in pg.event.get():
if event.type == pg.QUIT:
pg.quit()
quit()
gd.fill(black)
intro_button("sweet",100,100,100,100,red,red,"sweet")
pg.display.update()
clock.tick(15)
first_seq()
second_seq()
third_seq()
pg.quit()
quit()