Для этого есть различные решения, например, полиморфизм, действие или событие.
Очевидное и простое решение - добавить аргумент action
к методу clicked
. Аргумент - это функция действия, которая вызывается при нажатии кнопки:
class Button:
# [...]
def clicked(self, action = None):
mouse = pygame.mouse.get_pos()
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
if self.x + self.width > mouse[0] > self.x and self.y + self.height > mouse[1] > self.y:
if action:
action()
Создать функцию, которая изменяет состояния. Рекомендуется использовать оператор global
для переменных в глобальном пространстве имен main_menu
и main_game
:
def action_exit():
global main_menu, main_game
main_menu = False
main_game = True
Передать действие exit_button.clicked
:
while main_menu:
# [...]
exit_button.clicked(action_exit)
Кроме того, изменено Button.display(self, color)
на self.display(color)
в методе display()
.
Полный пример:
import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((1000, 700))
font = pygame.font.Font("freesansbold.ttf", 42)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
black = (0, 0, 0)
class Button:
main_menu = True
def __init__(self, color, x, y, width, height, text):
self.color = color
self.x = x
self.y = y
self.width = width
self.height = height
self.text = text
def display(self, color):
self.color = color
pygame.draw.rect(screen, self.color, (self.x, self.y, self.width, self.height))
text = font.render(self.text, True, red)
screen.blit(text, (self.x, self.y))
def hover(self, color):
mouse = pygame.mouse.get_pos()
if self.x + self.width > mouse[0] > self.x and self.y + self.height > mouse[1] > self.y:
self.display(color)
def clicked(self, action = None):
mouse = pygame.mouse.get_pos()
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
if self.x + self.width > mouse[0] > self.x and self.y + self.height > mouse[1] > self.y:
if action:
action()
def action_exit():
global main_menu, main_game
main_menu = False
main_game = True
play_button = Button(blue, 200, 300, 95, 46, "Play")
exit_button = Button(blue, 700, 300, 95, 46, "Exit")
tutorial_button = Button(blue, 410, 550, 165, 46, "Tutorial")
main_menu = True
main_game = False
while main_menu:
screen.fill(black)
play_button.display(blue)
exit_button.display(blue)
tutorial_button.display(blue)
play_button.hover(black)
exit_button.hover(black)
tutorial_button.hover(black)
for event in pygame.event.get():
if event.type == pygame.QUIT:
main_menu = False
exit_button.clicked(action_exit)
pygame.display.update()