forked from InfoProjekt/game
added the textbox for the buttons (not just any blank color) added function for exiting the game
42 lines
No EOL
1.5 KiB
Python
42 lines
No EOL
1.5 KiB
Python
import pygame
|
|
|
|
pygame.font.init()
|
|
font = pygame.font.Font('fonts/medieval.ttf', 48)
|
|
|
|
class Button():
|
|
def __init__(self, x, y, width, height, buttonText='Button', onclickFunction=None, onePress=False):
|
|
self.x = x
|
|
self.y = y
|
|
self.width = width
|
|
self.height = height
|
|
self.onclickFunction = onclickFunction
|
|
self.onePress = onePress
|
|
self.alreadyPressed = False
|
|
|
|
with open('art/textbox.png', 'r') as tb:
|
|
self.box = pygame.image.load(tb)
|
|
self.box = pygame.transform.scale(self.box, (width, height))
|
|
|
|
|
|
self.buttonRect = pygame.Rect(self.x, self.y, self.width, self.height)
|
|
|
|
self.buttonSurf = font.render(buttonText, True, '#baab80')
|
|
|
|
def process(self, screen):
|
|
mousePos = pygame.mouse.get_pos()
|
|
if self.buttonRect.collidepoint(mousePos):
|
|
#self.buttonSurface.fill(self.fillColors['hover'])
|
|
if pygame.mouse.get_pressed(num_buttons=3)[0]:
|
|
#self.buttonSurface.fill(self.fillColors['pressed'])
|
|
if self.onePress:
|
|
self.onclickFunction()
|
|
elif not self.alreadyPressed:
|
|
self.onclickFunction()
|
|
self.alreadyPressed = True
|
|
else:
|
|
self.alreadyPressed = False
|
|
self.box.blit(self.buttonSurf, [
|
|
self.buttonRect.width/2 - self.buttonSurf.get_rect().width/2,
|
|
self.buttonRect.height/2 - self.buttonSurf.get_rect().height/2
|
|
])
|
|
screen.blit(self.box, self.buttonRect) |