-
Notifications
You must be signed in to change notification settings - Fork 0
/
menuSystem.py
75 lines (58 loc) · 2.82 KB
/
menuSystem.py
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import pygame
class StartMenu:
def __init__(self, screen, font, text_colour, button_colour):
self.__screen = screen
self.__font = font
self.__text_colour = text_colour
self.__button_colour = button_colour
self.__click = False
self.__button_width = 150
self.__button_height = 75
self.__option = None
self.__buttons_xy = None
self.__button_objects = None
self.__button_command = ["Quit Game", "Start Game"]
self.__title = "Tic Tac Toe - by Frazer Mills"
@property
def Option(self):
return self.__option
def setup(self):
pygame.display.set_caption(f"{self.__title}")
self.__screen.fill((0,0,0))
def draw_text(self, text, x, y):
textobj = self.__font.render(text, 1, self.__text_colour)
textrect = textobj.get_rect()
textrect.center = (x, y)
self.__screen.blit(textobj, textrect)
def get_button_objects(self):
self.__buttons_xy = [
((self.__screen.get_width() // 2) - (self.__button_width // 2), (self.__screen.get_width() // 2) - i)
for i in range(-100, 100, 100)
]
self.__button_objects = {
f"button {i}": pygame.Rect(self.__buttons_xy[i][0], self.__buttons_xy[i][1], self.__button_width, self.__button_height)
for i, button in enumerate(self.__buttons_xy)
}
def check_collisions(self):
mousex, mousey = pygame.mouse.get_pos()
if self.__button_objects[f"button 0"].collidepoint((mousex, mousey)):
if self.__click:
self.__option = self.__button_command[0]
elif self.__button_objects[f"button 1"].collidepoint((mousex, mousey)):
if self.__click:
self.__option = self.__button_command[1]
def display_buttons(self):
for i, button_object in enumerate(self.__button_objects):
pygame.draw.rect(self.__screen, self.__button_colour, self.__button_objects[button_object])
self.draw_text(f"{self.__title}", self.__screen.get_width() // 2, self.__screen.get_height() // 4)
self.draw_text(f"{self.__button_command[0]}", self.__buttons_xy[0][0] + 75, self.__buttons_xy[0][1] + 35)
self.draw_text(f"{self.__button_command[1]}", self.__buttons_xy[1][0] + 75, self.__buttons_xy[1][1] + 35)
pygame.display.update()
def is_clicked(self):
self.__click = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == True:
self.__click = True