简体   繁体   中英

I'm continuously repeating code and wondering if there's a way to simplify these if statements

The code I created is used in pygame to highlight buttons that I am hovering over and when clicked on will trigger their corresponding function

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 is a rectangle

####_button_text is text which gets blitted on top the rectangle

Read a bout Classes .

Create a Button class:

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

Create a list of Botton objects:

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, register),
    Button(exit_button, exit_button, exit_window)
]

Draw the buttons in a loop:

for button in buttons:
    button.draw(WIN)

Do the click detection in a loop:

for button in buttons:
    button.click(mx, my, click)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM