@cntfrgthr

Не работает метод button_click_check. Почему?

Метод необходим для того, чтобы обрабатывать клики по кнопке и получать определенный callback. Вызывается в методе update, который в свою очередь вызывается в mainloop(while True).

Сейчас в качестве колбека печатается в консоль 1(так было легче проверить работоспособность)

ПРОБЛЕМА ТОЧНО ДОЛЖНА БЫТЬ В ООП, В ИСПОЛЬЗОВАНИИ БИБЛИОТЕЧНЫХ ФУНКЦИЙ ТОЧНО ВСЕ ВЕРНО

from settings import *
import pygame

pygame.init()



display = pygame.display.set_mode((width, height))
clock = pygame.time.Clock()
pygame.display.update()






class Interface:
    def button_click_check(self, button):
        for event in pygame.event.get():
            if event.type == pygame.MOUSEBUTTONDOWN:
                self.coords = pygame.mouse.get_pos()
                if (self.button[0] < self.coords[0] <= (self.button[0] + self.button[2])) & (self.button[1] < self.coords[1] <= (self.button[1] + self.button[3])):
                    print('1')
                else:
                    pygame.draw.rect(display, (0,0,255), self.button)

    def is_cursor_on_button(coords):
        if event.type == pygame.MOUSEMOTION:
                coords = pygame.mouse.get_pos()
                if (coords[0] < coords[0] <= (coords[0] + coords[2])) & (coords[1] < coords[1] <= (coords[1] + coords[3])):
                    pygame.draw.rect(display, (255,0,0), coords)
                else:
                    pygame.draw.rect(display, (0,0,255), coords)



class Button(Interface):
    def __init__(self, x, y, W, H):
        objects.append(self)
        self.x = x
        self.y = y 
        self.W = W
        self.H = H
        
    def get_coords(self):
        return (self.x, self.y, self.W, self.H)    
        #Interface.is_cursor_on_button(self.coords)

    def update(self):
        self.button_click_check(self.get_coords())

    def draw(self):
        pygame.draw.rect(display, (0,0,255), self.get_coords())       
        
objects = []
Button1 = Button(0,0,1920,1080) 


while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()

    for object in objects:
        object.update()    
    
    display.fill('black')    
    
    for object in objects:
        object.draw()

        
    pygame.display.update()    
    clock.tick(60)
  • Вопрос задан
  • 69 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы