ฉัน continuously ซ้ำรหัสและสงสัยว่าถ้ามีทางที่จะ simplify พวกนี้ถ้าการให้สอบสวน

0

คำถาม

รหัสฉันสร้างคือใช้ใน pygame จะเน้นปุ่มว่าฉันป้วนเปี้ยนจบและเมื่อมีการคลิกบจะกระตุ้นพวกเขาฟังก์ชันที่สัมพันธ์กัน

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 เป็นสี่เหลี่ยม

####_button_text คือข้อความซึ่งจะ blitted บที่สี่เหลี่ยม

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

คำตอบที่ดีที่สุด

3

อ่านหล่ะ เรียน.

สร้าง Button คลาส:

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

สร้างรายการของ Botton วัตถุ:

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

วาดกระดุมไปเรื่อยๆ:

for button in buttons:
    button.draw(WIN)

ทำการคลิกการตรวจสอบไปเรื่อยๆ:

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

ขอบคุณสำหรับการแก้ถ้าฉันสามารถ upvote นี้ผมจะทำ
Minhaj Rahman

@MinhajRahman ขอบคุณ ยินดีต้อนรับ
Rabbid76

ฉันแค่อยากจะติดตามผลบนเรื่องนี้ได้. ฉันเข้าใจแล้วเกิดข้อผิดพลาดตอนที่ฉันพยายามจะหนีกฎเส้น 147 ในกล่องชนะblit(งตัวเองข้อความถึงนี่ซักพักนึงแล้วข้อความget_rect(ศูนย์=งตัวเองrect.ศูนย์กลาง))AttributeError:'pygame.Rect'วัตถุไม่มีแอททริบิวต์'get_rect'
Minhaj Rahman

@MinhajRahman ขอโทษของฉันเลวร้าย นั่นเป็น typo ในคำตอบ Button(register_button, register_button_text, register), แทนที่จะเป็น Button(register_button, register_button, register), (2 ข้อโต้แย้งคือ register_button_text).
Rabbid76

แบบเดียวกันกับ Button(exit_button, exit_button_text, exit_window).
Rabbid76

ยิ่งฉันพยายามจะจัดการเรื่องนี้มาพักนึงแล้วและไม่เคยสังเกตเห็นขอบคุณอีกครั้ง
Minhaj Rahman

ในภาษาอื่นๆ

หน้านี้อยู่ในภาษาอื่นๆ

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

ดังอยู่ในนี้หมวดหมู่

ดังคำถามอยู่ในนี้หมวดหมู่