Budu neustále opakovat, kód a přemýšlel, jestli existuje způsob, jak zjednodušit tyto příkazy if

0

Otázka

Kód jsem vytvořil se používá v pygame pro zvýraznění tlačítek, že jsem se vznášející se nad, a po kliknutí na aktivují jejich příslušné funkce

if singleplayer_button.collidepoint(mx, my):
    pygame.draw.rect(WIN, (180, 0, 0), singleplayer_button)
    WIN.blit(singleplayer_button_text, (295, 400))
    if click:
        singleplayer()
else:
    pygame.draw.rect(WIN, RED, singleplayer_button)
    WIN.blit(singleplayer_button_text, (295, 400))

if multiplayer_button.collidepoint(mx, my):
    pygame.draw.rect(WIN, (180, 0, 0), multiplayer_button)
    WIN.blit(multiplayer_button_text, (727, 400))
    if click:
        multiplayer1()
else:
    pygame.draw.rect(WIN, RED, multiplayer_button)
    WIN.blit(multiplayer_button_text, (727, 400))

if leaderboard_button.collidepoint(mx, my):
    pygame.draw.rect(WIN, (180, 0, 0), leaderboard_button)
    WIN.blit(leaderboard_button_text, (291, 550))
    if click:
        leaderboard()
else:
    pygame.draw.rect(WIN, RED, leaderboard_button)
    WIN.blit(leaderboard_button_text, (291, 550))

if credit_button.collidepoint(mx, my):
    pygame.draw.rect(WIN, (180, 0, 0), credit_button)
    WIN.blit(credits_button_text, (774, 550))
    if click:
        credit()
else:
    pygame.draw.rect(WIN, RED, credit_button)
    WIN.blit(credits_button_text, (774, 550))

if register_button.collidepoint(mx, my):
    pygame.draw.rect(WIN, (180, 0, 0), register_button)
    WIN.blit(register_button_text, (300, 700))
    if click:
        register()
else:
    pygame.draw.rect(WIN, RED, register_button)
    WIN.blit(register_button_text, (300, 700))

if exit_button.collidepoint(mx, my):
    pygame.draw.rect(WIN, (180, 0, 0), exit_button)
    WIN.blit(exit_button_text, (737, 700))
    if click:
        exit_window()
else:
    pygame.draw.rect(WIN, RED, exit_button)
    WIN.blit(exit_button_text, (737, 700))

####_button je obdélník

####_button_text je text, který dostane blitted na vrcholu obdélníku

pygame python
2021-11-23 17:15:29
1

Nejlepší odpověď

3

Přečtěte si zápas Tříd.

Vytvořit Button třída:

class Button():
    def __init__(self, rect, text, action):
        self.rect = rect
        self.text = text
        self.action = action
    def draw(self, win):
        pygame.draw.rect(win, (180, 0, 0), self.rect)
        WIN.blit(self.text, self.text.get_rect(center = self.rect.center))
    def click(self, mx, my, click):
        if self.rect.collidepoint(mx, my) and click:
            self.action()

Vytvořit seznam Botton objekty:

buttons = [
    Button(singleplayer_button, singleplayer_button_text, singleplayer),
    Button(multiplayer_button,  multiplayer_button_text,  multiplayer1),
    Button(leaderboard_button,  leaderboard_button_text,  leaderboard),
    Button(credit_button,       credits_button_text,      credit),
    Button(register_button,     register_button_text,     register),
    Button(exit_button,         exit_button_text,         exit_window)
]

Nakreslete tlačítky ve smyčce:

for button in buttons:
    button.draw(WIN)

Udělat klepněte na tlačítko detekce ve smyčce:

for button in buttons:
    button.click(mx, my, click)
2021-11-23 19:01:17

Díky za řešení, kdybych mohl upvote tohoto bych.
Minhaj Rahman

@MinhajRahman Děkuji. Nemáš zač.
Rabbid76

Jen jsem chtěl navázat na to. Chápu a chyby, když se snažím spustit kód řádku 147, v losování VYHRÁT.blit(self.textu, samostatně.text.get_rect(centrum=self.rect.centrum)) AttributeError: 'pygame.Rect' objekt nemá žádný atribut 'get_rect'
Minhaj Rahman

@MinhajRahman Omlouvám se, moje chyba. Byl tam překlep v odpověď. Button(register_button, register_button_text, register), místo Button(register_button, register_button, register), (2. argument je register_button_text).
Rabbid76

Totéž pro Button(exit_button, exit_button_text, exit_window).
Rabbid76

Ahhhh snažil jsem se přijít na to na chvíli teď a ani si nevšiml, ještě jednou díky
Minhaj Rahman

V jiných jazycích

Tato stránka je v jiných jazycích

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................