通过单击面向对象的 PyGame 按钮调用 PyGame 函数 [英] Calling a PyGame Function, from clicking a Object-Orientated PyGame Button

查看:63
本文介绍了通过单击面向对象的 PyGame 按钮调用 PyGame 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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()

上面是我的 FrontPage 的剥离代码,上面有一个按钮,单击该按钮时,应将用户带到菜单屏幕,其中显示了另外 6 个按钮以移至用户选择的加密方法.

Above is the stripped back code of my FrontPage, that has a button on it that, when clicked, should take the user to the menu screen where 6 more buttons are displayed to move onto the users chosen encryption method.

但是,当我按下继续按钮时,什么也没有发生.

However, when I press the Continue button, nothing happens.

是不是因为Button类有问题?还是有其他原因使按钮停止工作?

Is it because there is something wrong with the Button Class? Or is there something else that makes the button stop working?

提前致谢

推荐答案

你必须在Menu函数中调用pg.display.flip().

我对代码结构也有一点建议.我会使用另一个函数或类(在本例中为 main)来管理不同的场景.所以我首先将当前场景函数分配给一个变量,并在主 while 循环中调用它.场景完成后,我返回下一个场景并将其分配给 scene 变量以交换场景.如果您直接从另一个场景中调用 next 函数,这将避免潜在的递归错误(尽管在简单的游戏或应用程序中您不太可能超过 1000 的递归限制).

I also have a little recommendation about the code structure. I'd use another function or class (main in this case) to manage the different scenes. So I first assign the current scene function to a variable and call it in the main while loop. When the scene is done, I return the next scene and assign it to the scene variable to swap the scenes. That will avoid potential recursion errors which you get if you just call the next function directly from within another scene (although it's unlikely that you'll exceed the recursion limit of 1000 in a simple game or app).

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()

这篇关于通过单击面向对象的 PyGame 按钮调用 PyGame 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆