Почему метод класса не вызывается?
Есть класс, экземпляр которого рисует квадрат. Я же хочу что бы при нажатии на квадрат он менял, например, ширину рамки.
В условии if event.type == pygame.MOUSEBUTTONDOWN: делать это не хочу.
Помогите разобраться, почему не вызывается метод action_button?
import pygame
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
pygame.init()
size = (150, 150)
sc = pygame.display.set_mode(size)
sc.fill(WHITE)
pygame.display.set_caption("Game2")
fps = pygame.time.Clock()
class Button:
def __init__(self, surface, color, rect, border_radius):
self.surfase = surface
self.color = color
self.rect = rect
self.border_radius = border_radius
self.draw_switch = True
self.switch_buttons()
#замысел такой - экземпляр класса рисует квадрат с рамкой 2, а если нажать на квадрат, метод draw перестает работать и работает
#метод action_button
def switch_buttons(self):
if self.draw_switch:
self.draw()
else:
self.action_button()
def draw(self):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
pygame.draw.rect(self.surfase, WHITE,
(self.rect[0] - 4, self.rect[1] - 4, self.rect[2] + 15, self.rect[3] + 15))
pygame.draw.rect(self.surfase, self.color, self.rect, self.border_radius)
if self.rect[0] < mouse[0] < self.rect[0] + self.rect[2]\
and self.rect[1] < mouse[1] < self.rect[1] + self.rect[3]:
if click[0] == 1:
print('кнопка')
print(self.draw_switch)
self.draw_switch = False
print(self.draw_switch)
def action_button(self): #почему эта чертова функция не вызывается?
pygame.draw.rect(self.surfase, WHITE,
(self.rect[0] - 4, self.rect[1] - 4, self.rect[2] + 15, self.rect[3] + 15))
pygame.draw.rect(self.surfase, self.color, self.rect, 4)
print('action')
def run_game():
game = True
while game:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
game = False
quit()
if event.type == pygame.MOUSEBUTTONDOWN:
pygame.display.update()
button0 = Button(sc, BLACK, (50, 50, 40, 40), 2)
fps.tick(60)
pygame.display.update()
run_game()
Ответы (2 шт):
Автор решения: CrazyElf
→ Ссылка
Наверное, вы забыли вот в этом месте вызвать функцию self.switch_buttons():
if click[0] == 1:
print('кнопка')
print(self.draw_switch)
self.draw_switch = False
print(self.draw_switch)
Надо добавить тут её вызов. Ну или где-то ещё. В данный момент логика работы программы у вас выглядит вообще несколько странной.
Автор решения: S. Nick
→ Ссылка
я не знаю правильно ли понимаю вас, но попробуйте так:
import sys
import pygame
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
pygame.init()
size = (150, 150)
sc = pygame.display.set_mode(size)
sc.fill(WHITE)
pygame.display.set_caption("Game2")
fps = pygame.time.Clock()
class Button:
def __init__(self, surface, color, rect, border_radius):
self.surfase = surface
self.color = color
self.rect = rect
self.border_radius = border_radius
self.flag = True
pygame.draw.rect(self.surfase, WHITE,
(self.rect[0] - 4, self.rect[1] - 4, self.rect[2] + 15, self.rect[3] + 15))
pygame.draw.rect(self.surfase, self.color, self.rect, self.border_radius)
def draw(self):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if (self.rect[0] < mouse[0] < self.rect[0] + self.rect[2]\
and self.rect[1] < mouse[1] < self.rect[1] + self.rect[3]):
# if click[0] == 1:
if click[0] == 1 and self.flag: # +++
self.action_button()
self.flag = False
elif click[0] == 1 and not self.flag: # +++
pygame.draw.rect(
self.surfase,
WHITE,
(self.rect[0] - 4, self.rect[1] - 4, self.rect[2] + 15, self.rect[3] + 15)
)
pygame.draw.rect(self.surfase, self.color, self.rect, self.border_radius)
self.flag = True
def action_button(self):
pygame.draw.rect(self.surfase, WHITE,
(self.rect[0] - 4, self.rect[1] - 4, self.rect[2] + 15, self.rect[3] + 15))
pygame.draw.rect(self.surfase, (250, 0, 0), self.rect, 4)
# print('action')
def run_game():
game = True
button0 = Button(sc, BLACK, (50, 50, 40, 40), 2) # +++
while game:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
game = False
quit()
if event.type == pygame.MOUSEBUTTONDOWN:
pygame.display.update()
# button0 = Button(sc, RED, (50, 50, 40, 40), 2) # ---
button0.draw() # +++
fps.tick(60)
pygame.display.update()
run_game()

