forked from ryancollingwood/arcade-rabbit-herder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshape_test.py
More file actions
220 lines (167 loc) · 7.26 KB
/
shape_test.py
File metadata and controls
220 lines (167 loc) · 7.26 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import arcade
SCREEN_WIDTH = 640
SCREEN_HEIGHT = 480
HALF_SQUARE_WIDTH = 2.5
HALF_SQUARE_HEIGHT = 2.5
SQUARE_SPACING = 10
class ShapeSprite:
def __init__(self, position_x, position_y, square_size, lines):
# Take the parameters of the init function above, and create instance variables out of them.
self.position_x = position_x
self.position_y = position_y
self.shape_list = None
self.square_size = square_size
self.lines = lines
self.create_shape_list()
def create_shape_list(self):
self.shape_list = arcade.ShapeElementList()
point_list = []
color_list = []
colour_codes = [
arcade.color.BLACK, # 0
arcade.color.BLUE, # 1
arcade.color.GREEN, # 2
arcade.color.CYAN, # 3
arcade.color.RED, # 4
arcade.color.MAGENTA, # 5
arcade.color.BROWN, # 6
arcade.color.LIGHT_GRAY, # 7
arcade.color.DARK_GRAY, # 8
arcade.color.LIGHT_BLUE, # 9
arcade.color.LIGHT_GREEN, # 10
arcade.color.LIGHT_CYAN, # 11
arcade.color.LIGHT_RED_OCHRE, # 12
arcade.color.LIGHT_MEDIUM_ORCHID, # 13
arcade.color.WHITE
]
for row_index, row_value in enumerate(reversed(self.lines)):
for column_index, value in enumerate(row_value):
try:
value = int(value)
except ValueError:
continue
top_left = (column_index * self.square_size, row_index * self.square_size)
top_right = (top_left[0] + self.square_size, top_left[1])
bottom_right = (top_left[0] + self.square_size, top_left[1] + self.square_size)
bottom_left = (top_left[0], top_left[1] + self.square_size)
point_list.append(top_left)
point_list.append(top_right)
point_list.append(bottom_right)
point_list.append(bottom_left)
colour = colour_codes[value]
for i in range(4):
color_list.append(colour)
self.shape_list.append(
arcade.create_rectangles_filled_with_colors(point_list, color_list)
)
def draw(self):
""" Draw the balls with the instance variables we have. """
self.shape_list.center_x = self.position_x
self.shape_list.center_y = self.position_y
self.shape_list.draw()
class MyGame(arcade.Window):
def __init__(self, width, height, title):
# Call the parent class's init function
super().__init__(width, height, title)
# Make the mouse disappear when it is over the window.
# So we just see our object, not the pointer.
self.set_mouse_visible(False)
arcade.set_background_color(arcade.color.ASH_GREY)
self.odds = True
# Create our shape_sprite
self.shape_sprite = ShapeSprite(50, 50, 7, [
",14, 14, 14,, 14, 14, 14,".split(","),
", 14, 13, 14,, 14, 13, 14,".split(","),
", 14, 13, 14,, 14, 13, 14,".split(","),
", 14, 13, 14,, 14, 13, 14,".split(","),
", 14, 14, 14, 14, 14, 14, 14,".split(","),
"14, 7, 3, 1, 7, 3, 1, 14, 14".split(","),
"14, 14, 3, 3, 14, 3, 3, 14, 14".split(","),
"14, 14, 14, 14, 0, 14, 14, 14, 14".split(","),
", 14, 14, 8, 5, 8, 14, 14,".split(","),
", , , 14, 14, 14,, ,".split(",")
])
self.shape_list = None
def zoom_shape_sprite(self, zoom_value):
if self.shape_sprite.square_size + zoom_value > 1:
self.shape_sprite.square_size += zoom_value
self.shape_sprite.create_shape_list()
def rotate_shape_sprite(self, angle_modifier):
self.shape_sprite.shape_list.angle += angle_modifier
def toggle_squares(self):
self.odds = not self.odds
self.make_shapes()
def make_shapes(self):
odds = self.odds
self.shape_list = arcade.ShapeElementList()
# --- Create all the rectangles
# We need a list of all the points and colors
point_list = []
color_list = []
skip_x = odds
skip_y = not odds
# Now calculate all the points
for x in range(0, SCREEN_WIDTH, SQUARE_SPACING):
skip_x = not skip_x
if skip_x:
continue
for y in range(0, SCREEN_HEIGHT, SQUARE_SPACING):
skip_y = not skip_y
if skip_y:
continue
# Calculate where the four points of the rectangle will be if
# x and y are the center
top_left = (x - HALF_SQUARE_WIDTH, y + HALF_SQUARE_HEIGHT)
top_right = (x + HALF_SQUARE_WIDTH, y + HALF_SQUARE_HEIGHT)
bottom_right = (x + HALF_SQUARE_WIDTH, y - HALF_SQUARE_HEIGHT)
bottom_left = (x - HALF_SQUARE_WIDTH, y - HALF_SQUARE_HEIGHT)
# Add the points to the points list.
# ORDER MATTERS!
# Rotate around the rectangle, don't append points caty-corner
point_list.append(top_left)
point_list.append(top_right)
point_list.append(bottom_right)
point_list.append(bottom_left)
# Add a color for each point. Can be different colors if you want
# gradients.
for i in range(4):
color_list.append(arcade.color.DARK_BLUE)
shape = arcade.create_rectangles_filled_with_colors(point_list, color_list)
self.shape_list.append(shape)
def setup(self):
self.make_shapes()
def on_draw(self):
""" Called whenever we need to draw the window. """
arcade.start_render()
self.shape_list.draw()
self.shape_sprite.draw()
def on_mouse_motion(self, x, y, dx, dy):
""" Called to update our objects. Happens approximately 60 times per second."""
self.shape_sprite.position_x = x
self.shape_sprite.position_y = y
def on_mouse_scroll(self, x: int, y: int, scroll_x: int, scroll_y: int):
self.zoom_shape_sprite(scroll_y)
def on_mouse_press(self, x, y, button, modifiers):
"""
Called when the user presses a mouse button.
"""
print(f"You clicked button number: {button}")
if button == arcade.MOUSE_BUTTON_LEFT:
self.rotate_shape_sprite(1)
elif button == arcade.MOUSE_BUTTON_RIGHT:
self.rotate_shape_sprite(-1)
def on_key_press(self, symbol: int, modifiers: int):
if symbol == arcade.key.RIGHT:
self.rotate_shape_sprite(1)
elif symbol == arcade.key.LEFT:
self.rotate_shape_sprite(-1)
def on_mouse_release(self, x, y, button, modifiers):
self.toggle_squares()
def on_key_release(self, symbol: int, modifiers: int):
self.toggle_squares()
def main():
window = MyGame(640, 480, "Drawing Example")
window.setup()
arcade.run()
if __name__ == "__main__":
main()