将 pygame 碰撞点与类一起使用

问题描述

是否可以创建一个用于绘制按钮的类,然后在该类中使用碰撞点?我创建了类的实例,然后将它们用于 collidepoint 函数,但它似乎不起作用。感谢您的帮助,谢谢!

class Button:
     def __init__(self,x,y,width,height,text = ''):
         self.x = x
         self.y = y
         self.width = width
         self.height = height
         self.text = text
     def draw(self,window):
         pygame.draw.rect(window,BLACK,pygame.Rect(self.x,self.y + 150,self.width,self.height))
         if self.text != '':
             font = pygame.font.SysFont('comicsans,50')
             menu_text = font.render(self.text,1,(0,0))
             window.blit(self.x,self.y)
def draw_window():
    button = Button.draw()

buttons = Button()
def main():
FPS = 60
run = True
while run:
    clock = pygame.time.Clock()
    clock.tick(FPS)
    app_Box = pygame.draw.rect(window,pygame.Rect(WIDTH/2 - 60,50,50))
    entree_Box = pygame.draw.rect(window,pygame.Rect(WIDTH/2 + 25,50))
    pos = pygame.mouse.get_pos()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
            pygame.quit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            if app_Box.collidepoint(pos):
                for i in range(3):
                    buttons = Button(WIDTH/2 - 150,HEIGHT/5 * i,300,75,"hello")
                if buttons.collidepoint(pos):
                    print("hello")
            if entree_Box.collidepoint(pos):
                for i in range(3):
                    buttons = Button(WIDTH / 2 - 150,HEIGHT / 5 * i,"hello")
                if buttons.collidepoint(pos):
                    print("hello")
    pygame.display.update()

main()

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)