亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我是新手,我一般對 PyGame、OOP 和 Python 有疑問

我是新手,我一般對 PyGame、OOP 和 Python 有疑問

aluckdog 2022-11-01 16:04:26
我是編程新手,尤其是 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:                passplay_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()    pygame.display.update()
查看完整描述

1 回答

?
皈依舞

TA貢獻1851條經驗 獲得超3個贊

對此有不同的解決方案,例如多態性、動作或事件。

一個明顯而簡單的解決方案是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 pygame

from 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 = True


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


    pygame.display.update()


查看完整回答
反對 回復 2022-11-01
  • 1 回答
  • 0 關注
  • 107 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號