-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbutton.py
More file actions
60 lines (51 loc) · 1.99 KB
/
button.py
File metadata and controls
60 lines (51 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import pygame
from constants import *
pygame.init()
class Button:
"""
A class representing a button in pygame.
Attributes:
- _x (int): x-coordinate of the button.
- _y (int): y-coordinate of the button.
- _text (str): Text displayed on the button.
- _width (int): Width of the button.
- _height (int): Height of the button.
- _colour (tuple): RGB color of the button.
- rect (pygame.Rect): A rectangle representing the button's position and size.
"""
def __init__(self, x, y, width, height, text, colour):
self._x = x
self._y = y
self._text = text
self._height = height
self._width = width
self._colour = colour
# Create a rectangle centered at (x, y) with given width and height
self.rect = pygame.Rect(x - self._width // 2, y - self._height // 2, self._width, self._height)
def draw(self, window):
"""
Draws the button on the given pygame window.
- Draws a filled rectangle with the specified button color.
- Draws a black border around the button.
- Displays the button text in the center.
"""
pygame.draw.rect(window, self._colour, self.rect) # Draw button background
pygame.draw.rect(window, BLACK, self.rect, 2) # Draw border
textSurface = mediumFont.render(self._text, True, BLACK) # Render button text
window.blit(textSurface, (self._x - self._width // 2.5, self._y - self._height // 2.5)) # Center text on button
def isClicked(self):
"""
Simulates a button click.
- Returns the button text
"""
return self._text, True # Simulate a click event
def keyClicked(self):
"""
If button is the selected object when a key is clicked, nothing happens.
"""
pass
def getText(self):
"""
Retrieves the text displayed on the button.
"""
return self._text