-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuttons.py
155 lines (117 loc) · 5.06 KB
/
buttons.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
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
151
152
153
154
155
import arcade.gui
from arcade.gui import UIManager
class ChangeViewButton(arcade.gui.UIFlatButton):
'''Button used to switch to another specified view'''
def __init__(self, text, center_x, center_y, view, width=100, height=100):
super().__init__(text, center_x, center_y, width, height)
self.view = view
self.window = arcade.get_window()
def on_click(self):
# Sets view to view specified in object
self.window.show_view(self.view)
class MusicButton(arcade.gui.UIFlatButton):
'''Button used to toggle music on and off'''
def __init__(self, view, center_x, center_y, width=100, height=100):
text = 'Toggle Music'
super().__init__(text, center_x, center_y, width, height)
self.view = view
def on_click(self):
# If music = True it will set it to false and vice-versa
self.view.music_enabled = not self.view.music_enabled
class FullScreenButton(arcade.gui.UIFlatButton):
'''Button used to toggle fullscreen on and off'''
def __init__(self, text, center_x, center_y, width=100, height=100):
super().__init__(text, center_x, center_y, width, height)
self.window = arcade.get_window()
def on_click(self):
# If fullscreen = True it will set it to false and vice-versa
self.window.set_fullscreen(not self.window.fullscreen)
class ToggleAttributeButton(arcade.gui.UIFlatButton):
'''Used to toggle the value of a specific attribute in a view from true to false or false to true'''
def __init__(
self,
text,
center_x,
center_y,
view,
attribute,
width=100,
height=100):
super().__init__(text, center_x, center_y, width, height)
self.view = view
self.attribute = attribute
def on_click(self):
setattr(
self.view,
self.attribute,
not getattr(
self.view,
self.attribute))
class UpgradeButton():
'''Custom button class used to upgrade a specific attribute of the subject, increase in cost of the upgrade can either
be increased by a fixed step with upgrade_step or a multiplier with upgrade_multiplier (leave as default value if one is unused).
Upgrade buttons should go inside a planet button list'''
def __init__(
self,
text,
upgrade,
subject,
cost,
cost_multiplier=1,
upgrade_step=0,
upgrade_multiplier=1):
self.center_x = 0
self.center_y = 0
self.text = text
self.upgrade = upgrade
self.subject = subject
self.cost = cost
self.cost_multiplier = cost_multiplier
self.upgrade_step = upgrade_step
self.upgrade_multiplier = upgrade_multiplier
self.mouse_over = True
def check_mouse(self, x, y, rocket_x, rocket_y, at_base):
'''Function to check if the mouse is positioned over the button'''
current_screen_width = arcade.get_window().width
current_screen_height = arcade.get_window().height
x = x - current_screen_width / 2 + rocket_x
y = y - current_screen_height / 2 + rocket_y
x = x / current_screen_width * 1280
y = y / current_screen_height * 720
if x <= self.center_x + 50 and x >= self.center_x - 50 and y <= self.center_y + \
15 and y >= self.center_y - 15 and at_base: # Checks if mouse is positioned over button
self.mouse_over = True
else:
self.mouse_over = False
def on_click(self):
'''Called when a button is clicked, checks if the player has enough coins and calls the subjects upgrade function if so'''
if self.subject.coins >= self.cost and self.mouse_over:
# Calls subject upgrade function - needs to be used to reassign
# outside object
self.subject.upgrade(
self.upgrade,
self.upgrade_step,
self.upgrade_multiplier)
self.subject.coins -= self.cost
# increases cost by multiplier (default is 1 so no change)
self.cost *= self.cost_multiplier
self.cost = int(self.cost)
def draw(self, at_base):
'''Draw the button, if mouse is positioned over it a white outline will appear'''
if at_base:
if self.mouse_over:
# If mouse is over the button a white box will be drawn around
# it
arcade.draw_rectangle_filled(
self.center_x, self.center_y, 110, 40, arcade.color.WHITE)
arcade.draw_rectangle_filled(
self.center_x,
self.center_y,
100,
30,
arcade.color.GRAY) # Draws grey box around text for visibility
arcade.draw_text(
self.text + f'\n({self.cost} Coins)',
self.center_x - 35,
self.center_y - 15,
arcade.color.BLACK) # Draw text for button including cost and name