Python 通过单击面向对象的PyGame按钮调用PyGame函数
上面是我的FrontPage的精简代码,上面有一个按钮,单击该按钮时,应将用户带到菜单屏幕,其中显示6个以上的按钮,以转到用户选择的加密方法 但是,当我按下“继续”按钮时,什么也没有发生 是因为Button类有问题吗? 或者是其他什么东西使按钮停止工作Python 通过单击面向对象的PyGame按钮调用PyGame函数,python,pygame,Python,Pygame,上面是我的FrontPage的精简代码,上面有一个按钮,单击该按钮时,应将用户带到菜单屏幕,其中显示6个以上的按钮,以转到用户选择的加密方法 但是,当我按下“继续”按钮时,什么也没有发生 是因为Button类有问题吗? 或者是其他什么东西使按钮停止工作 提前感谢您必须在菜单功能中调用pg.display.flip() 我还对代码结构提出了一些建议。我会使用另一个函数或类(main,在本例中)来管理不同的场景。因此,我首先将当前场景函数指定给一个变量,并在主while循环中调用它。场景完成后,我返
提前感谢您必须在
菜单
功能中调用pg.display.flip()
我还对代码结构提出了一些建议。我会使用另一个函数或类(main
,在本例中)来管理不同的场景。因此,我首先将当前场景函数指定给一个变量,并在主while循环中调用它。场景完成后,我返回下一个场景,并将其指定给scene
变量以交换场景。这将避免直接从另一个场景中调用下一个函数时可能出现的递归错误(尽管在一个简单的游戏或应用程序中不太可能超过1000的递归限制)
当然很容易,我知道我错过了一些容易的事情。没有更多的问题,谢谢所有的帮助。我还添加了一个关于现场管理的建议。事实上,我会使用中的类。
import pygame as pg
import sys
pg.init()
buttonFont = pg.font.SysFont("garamond", 25)
screenGray = pg.Color('gray80')
buttonGray2 = pg.Color('gray50')
textColour = pg.Color('navy')
screen = pg.display.set_mode((800, 600))
clock = pg.time.Clock()
class Button(pg.sprite.Sprite):
def __init__(self, text, x, y, width, height, colour):
super().__init__()
self.image = pg.Surface((width, height))
self.image.fill(colour)
self.rect = self.image.get_rect()
txt = buttonFont.render(text, True, textColour)
txtRect = txt.get_rect(center = self.rect.center)
self.image.blit(txt, txtRect)
self.rect.topleft = x, y
def isPressed(self, event):
if event.type == pg.MOUSEBUTTONDOWN:
if self.rect.collidepoint(event.pos):
return True
return False
def FrontPage():
screen.fill(screenGray)
Continue = Button('Continue', 105, 455, 120, 50, buttonGray2)
buttonsGroup1 = pg.sprite.Group(Continue)
while True:
for event in pg.event.get():
if event.type == pg.QUIT:
pg.quit()
sys.exit()
elif Continue.isPressed(event):
Menu()
buttonsGroup1.draw(screen)
pg.display.flip()
clock.tick(60)
def Menu():
screen.fill(screenGray)
Scytale = Button('Scytale', 105,105,140,65, buttonGray2)
Caesar = Button('Caesar', 330,105,140,65, buttonGray2)
Vigenere = Button('Vigenere', 555,105,140,65, buttonGray2)
Enigma = Button('Enigma', 105,430,140,65,buttonGray2)
PublicKey = Button('Public Key', 330,430,140,65, buttonGray2)
Rijndael = Button('Rijndael', 555,430,140,65, buttonGray2)
buttonsGroup2 = pg.sprite.Group(Scytale,Caesar,Vigenere,Enigma,PublicKey,Rijndael)
while True:
for event in pg.event.get():
if event.type == pg.QUIT:
pg.quit()
sys.exit()
buttonsGroup2.draw(screen)
clock.tick(60)
FrontPage()
import pygame as pg
pg.init()
screen = pg.display.set_mode((600, 400))
clock = pg.time.Clock()
BLUE = pg.Color('dodgerblue3')
ORANGE = pg.Color('sienna3')
def front_page():
while True:
for event in pg.event.get():
if event.type == pg.QUIT:
return None
# Press a key to return the next scene.
elif event.type == pg.KEYDOWN:
return menu
screen.fill(BLUE)
pg.display.flip()
clock.tick(60)
def menu():
while True:
for event in pg.event.get():
if event.type == pg.QUIT:
return None
# Press a key to return the next scene.
elif event.type == pg.KEYDOWN:
return front_page
screen.fill(ORANGE)
pg.display.flip()
clock.tick(60)
def main():
scene = front_page # Set the current scene.
while scene is not None:
# Execute the current scene function. When it's done
# it returns either the next scene or None which we
# assign to the scene variable.
scene = scene()
main()
pg.quit()