python - 我制作的按钮在 pygame 中跳过了一个表面
问题描述
我正在学习 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()
解决方案
推荐阅读
- r - 在 Shiny 中显示最高数字的值
- zooming - Camera2 API 缩放相机预览
- reactjs - 如果想在我的整个 React 应用程序中访问变量,我应该在哪里设置变量?
- haskell - 从字符串列表中删除特殊字符 Haskell
- javascript - Chart.js - 图表响应/调整大小
- c++ - 如何修复并使我的代码在 C++ 中工作?
- javascript - Cocos Creator - 碰撞不适用于从 JS 添加的节点
- c++ - weak_ptr 中的可能错误已传递给线程
- asp.net-mvc - ASP.NET MVC:使用 Microsoft.AspNet.Identity.Owin 会导致错误“对象引用未设置为对象的实例”。
- r - 使用自定义主题将图例添加到 ggplot 折线图的问题