我是新手,我一般对 PyGame、OOP 和 Python 有疑问

我是编程新手,尤其是 PyGame 和 OOP。我不知道如何让按钮在 PyGame 中执行特定命令。我尝试为按钮创建一个类,如果您查看此代码,我可以执行悬停方法/功能作为示例,但是当我按下退出按钮时,我正在努力关闭游戏。我似乎无法理解如何传递一个参数,使 main_menu 在执行 exit 时为假,而 main_game 在执行 play 时为真。


from ColorsAndCoordinates import *


pygame.init()


screen = pygame.display.set_mode((1000, 700))

font = pygame.font.Font("freesansbold.ttf", 42)



class Button:

    main_menu = True


    def __init__(self, color, x, y, width, height, text):

        self.color = color

        self.x = x

        self.y = y

        self.width = width

        self.height = height

        self.text = text


    def display(self, color):

        self.color = color

        pygame.draw.rect(screen, self.color, (self.x, self.y, self.width, self.height))

        text = font.render(self.text, True, red)

        screen.blit(text, (self.x, self.y))


    def hover(self, color):

        mouse = pygame.mouse.get_pos()

        if self.x + self.width > mouse[0] > self.x and self.y + self.height > mouse[1] > self.y:

            Button.display(self, color)


    def clicked(self):

        mouse = pygame.mouse.get_pos()

        if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:

            if self.x + self.width > mouse[0] > self.x and self.y + self.height > mouse[1] > self.y:

                pass



play_button = Button(blue, 200, 300, 95, 46, "Play")

exit_button = Button(blue, 700, 300, 95, 46, "Exit")

tutorial_button = Button(blue, 410, 550, 165, 46, "Tutorial")


main_menu = True

main_game = False

while main_menu:


    screen.fill(black)

    play_button.display(blue)

    exit_button.display(blue)

    tutorial_button.display(blue)

    play_button.hover(black)

    exit_button.hover(black)

    tutorial_button.hover(black)


    for event in pygame.event.get():

        if event.type == pygame.QUIT:

            main_menu = False


    exit_button.clicked()


    pygame.display.update()


aluckdog
浏览 93回答 1
1回答

皈依舞

对此有不同的解决方案,例如多态性、动作或事件。一个明显而简单的解决方案是action在方法中添加一个参数clicked。参数是一个动作函数,当按钮被点击时被调用:class Button:    # [...]    def clicked(self, action = None):        mouse = pygame.mouse.get_pos()        if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:            if self.x + self.width > mouse[0] > self.x and self.y + self.height > mouse[1] > self.y:                if action:                    action()创建一个改变状态的函数。考虑将global语句用于全局名称空间中的变量main_menu和main_game:def action_exit():    global main_menu, main_game     main_menu = False    main_game = True将动作传递给exit_button.clicked:while main_menu:    # [...]    exit_button.clicked(action_exit)再改成Button.display(self, color)方法self.display(color)中display()。完整示例:https://i.stack.imgur.com/zYkBf.gif import pygamefrom pygame.locals import *pygame.init()screen = pygame.display.set_mode((1000, 700))font = pygame.font.Font("freesansbold.ttf", 42)red = (255, 0, 0)green = (0, 255, 0)blue = (0, 0, 255)black = (0, 0, 0)class Button:    main_menu = True    def __init__(self, color, x, y, width, height, text):        self.color = color        self.x = x        self.y = y        self.width = width        self.height = height        self.text = text    def display(self, color):        self.color = color        pygame.draw.rect(screen, self.color, (self.x, self.y, self.width, self.height))        text = font.render(self.text, True, red)        screen.blit(text, (self.x, self.y))    def hover(self, color):        mouse = pygame.mouse.get_pos()        if self.x + self.width > mouse[0] > self.x and self.y + self.height > mouse[1] > self.y:            self.display(color)    def clicked(self, action = None):        mouse = pygame.mouse.get_pos()        if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:            if self.x + self.width > mouse[0] > self.x and self.y + self.height > mouse[1] > self.y:                if action:                    action()def action_exit():    global main_menu, main_game     main_menu = False    main_game = Trueplay_button = Button(blue, 200, 300, 95, 46, "Play")exit_button = Button(blue, 700, 300, 95, 46, "Exit")tutorial_button = Button(blue, 410, 550, 165, 46, "Tutorial")main_menu = Truemain_game = Falsewhile main_menu:    screen.fill(black)    play_button.display(blue)    exit_button.display(blue)    tutorial_button.display(blue)    play_button.hover(black)    exit_button.hover(black)    tutorial_button.hover(black)    for event in pygame.event.get():        if event.type == pygame.QUIT:            main_menu = False    exit_button.clicked(action_exit)    pygame.display.update()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python