-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbutton_manager.py
More file actions
150 lines (126 loc) · 8.32 KB
/
button_manager.py
File metadata and controls
150 lines (126 loc) · 8.32 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
from pygame_widgets.button import Button
from pygame_widgets.slider import Slider
import pygame as pg
import csv
class ButtonManager:
def __init__(self, parent):
self.parent = parent
self.spawn_buttons()
self.show_homescreen()
def spawn_buttons(self):
self.font = pg.font.Font('freesansbold.ttf', self.get_display_height() // 15)
self.font2 = pg.font.Font('freesansbold.ttf', self.get_display_width() // 38)
self.begin_text = self.font.render("Radiator Springs Adventure", True, (0, 0, 0, 255))
self.begin_start_button = Button(self.get_display(), self.get_display_width() * (1 / 3),
self.get_display_height() * (1 / 2),
width=self.get_display_width() * (3 / 10),
height=self.get_display_height() * (1 / 20),
text='Start Game', radius=20, colour=(255, 0, 0),
onClick=self.parent.init_race,
font=pg.font.Font('freesansbold.ttf', 20))
self.begin_end_button = Button(self.get_display(), self.get_display_width() * (1 / 3),
self.get_display_height() * (3 / 4),
width=self.get_display_width() * (1.5 / 10),
height=self.get_display_height() * (1 / 20),
text='Quit', radius=20, colour=(255, 0, 0), onClick=self.parent.quit_game,
font=pg.font.Font('freesansbold.ttf', 20))
self.rect = pg.Surface((self.get_display_width(), self.get_display_height()), pg.SRCALPHA)
self.rect.fill((128, 128, 128, 128))
self.pause_volume_slider = Slider(self.get_display(), int(self.get_display_width() * (1 / 4)),
int(self.get_display_height() * (2 / 5)), width=self.get_display_width() // 2,
height=self.get_display_height() // 20, min=0, max=1, step=0.1, curved=True,
initial=pg.mixer.music.get_volume())
self.pause_unpause_button = Button(self.get_display(), int(self.get_display_width() * (2 / 3)),
int(self.get_display_height() * (3 / 5)),
width=self.get_display_width() * (1.5 / 10),
height=self.get_display_height() * (1 / 20),
text='Resume', radius=20, colour=(0, 0, 255), onClick=self.parent.unpaused,
font=pg.font.Font('freesansbold.ttf', 20))
self.pause_quit_game_button = Button(self.get_display(), int(self.get_display_width() * (1 / 4)),
self.get_display_height() * (3 / 5),
width=self.get_display_width() * (1.5 / 10),
height=self.get_display_height() * (1 / 20),
text='Quit', radius=20, colour=(255, 0, 0),
onClick=self.parent.init_homescreen,
font=pg.font.Font('freesansbold.ttf', 20))
self.pause_restart_button = Button(self.get_display(), self.get_display_width() * (1 / 4),
int(self.get_display_height() * (4 / 5)),
width=self.get_display_width() * (1.5 / 10),
height=self.get_display_height() * (1 / 20),
text='Restart', radius=20, colour=(0, 255, 0),
onClick=self.parent.restart_race,
font=pg.font.Font('freesansbold.ttf', 20))
self.pause_new_game_button = Button(self.get_display(), self.get_display_width() * (2 / 3),
int(self.get_display_height() * (4 / 5)),
width=int(self.get_display_width() * (2 / 10)),
height=self.get_display_height() * (1 / 20),
text='New Game', radius=20, colour=(0, 255, 0),
onClick=self.parent.init_race,
font=pg.font.Font('freesansbold.ttf', 20))
self.pause_statistics_text_value = ""
self.endgame_start_button = Button(self.get_display(), self.get_display_width() * (1 / 3),
self.get_display_height() * (1 / 2),
width=self.get_display_width() * (3 / 10),
height=self.get_display_height() * (1 / 20),
text='Start Game', radius=20, colour=(255, 0, 0),
onClick=self.parent.init_race,
font=pg.font.Font('freesansbold.ttf', 20))
self.endgame_quit_game_button = Button(self.get_display(), self.get_display_width() * (1 / 3),
self.get_display_height() * (3 / 4),
width=self.get_display_width() * (1.5 / 10),
height=self.get_display_height() * (1 / 20),
text='Quit', radius=20, colour=(255, 0, 0),
onClick=self.parent.init_homescreen,
font=pg.font.Font('freesansbold.ttf', 20))
self.hide_all()
def show_homescreen(self):
self.hide_all()
self.begin_text = self.font.render("Radiator Springs Adventure", True, (0, 0, 0, 255))
self.begin_start_button.show()
self.begin_end_button.show()
def show_pause(self):
self.hide_all()
self.rect.fill((128, 128, 128, 128))
self.pause_volume_slider.show()
self.pause_unpause_button.show()
self.pause_quit_game_button.show()
self.pause_restart_button.show()
self.pause_new_game_button.show()
self.pause_statistics_text = self.font2.render(self.pause_statistics_text_value, True, (255, 140, 0, 255))
def show_endgame(self):
self.hide_all()
file = open('statistics/stats.csv', mode='a', newline='')
writer = csv.writer(file)
self.endgame_start_button.show()
self.endgame_quit_game_button.show()
if self.parent.opponent_car.finish_line():
self.endgame_text = self.font.render('You LOSE', True, (255, 0, 0, 255), (128, 128, 128, 128))
writer.writerow([0, self.parent.player_car.get_avg_velocity()])
else:
self.endgame_text = self.font.render('You WIN', True, (0, 255, 0, 255), (128, 128, 128, 128))
writer.writerow([1, self.parent.player_car.get_avg_velocity()])
def hide_all(self):
self.begin_start_button.hide()
self.begin_end_button.hide()
self.rect.fill((128, 128, 128, 0))
self.pause_volume_slider.hide()
self.pause_unpause_button.hide()
self.pause_quit_game_button.hide()
self.pause_restart_button.hide()
self.pause_new_game_button.hide()
self.endgame_start_button.hide()
self.endgame_quit_game_button.hide()
self.endgame_text = self.font.render('', True, (255, 0, 0, 0))
self.pause_statistics_text = self.font2.render(self.pause_statistics_text_value, True, (255, 140, 0, 0))
self.begin_text = self.font.render('', True, (255, 0, 0, 0))
def get_display_width(self):
return self.parent.display.width
def get_display_height(self):
return self.parent.display.height
def get_display(self):
return self.parent.display.display
def getVolumeValue(self):
return self.pause_volume_slider.getValue()
def set_pause_statistics_text(self, text):
self.pause_statistics_text_value = text
self.pause_statistics_text = self.font2.render(self.pause_statistics_text_value, True, (255, 140, 0))