形状列表-天际线#

shape_list_demo_skylines.py#
1"""
2City Scape Generator
3
4If Python and Arcade are installed, this example can be run from the command line with:
5python -m arcade.examples.shape_list_demo_skylines
6"""
7import random
8import arcade
9from arcade.shape_list import (
10 ShapeElementList,
11 create_rectangle_filled,
12 create_polygon,
13 create_rectangles_filled_with_colors,
14)
15
16SCREEN_WIDTH = 1200
17SCREEN_HEIGHT = 600
18SCREEN_TITLE = "Skyline Using Buffered Shapes"
19
20
21def make_star_field(star_count):
22 """Make a bunch of circles for stars"""
23 shape_list = ShapeElementList()
24
25 for _ in range(star_count):
26 x = random.randrange(SCREEN_WIDTH)
27 y = random.randrange(SCREEN_HEIGHT)
28 radius = random.randrange(1, 4)
29 brightness = random.randrange(127, 256)
30 color = (brightness, brightness, brightness)
31 shape = create_rectangle_filled(x, y, radius, radius, color)
32 shape_list.append(shape)
33
34 return shape_list
35
36
37def make_skyline(width, skyline_height, skyline_color,
38 gap_chance=0.70, window_chance=0.30, light_on_chance=0.5,
39 window_color=(255, 255, 200), window_margin=3, window_gap=2,
40 cap_chance=0.20):
41 """Make a skyline of buildings"""
42 shape_list = ShapeElementList()
43
44 # Add the "base" that we build the buildings on
45 shape = create_rectangle_filled(width / 2, skyline_height / 2, width, skyline_height, skyline_color)
46 shape_list.append(shape)
47
48 building_center_x = 0
49
50 skyline_point_list = []
51 color_list = []
52
53 while building_center_x < width:
54
55 # Is there a gap between the buildings?
56 if random.random() < gap_chance:
57 gap_width = random.randrange(10, 50)
58 else:
59 gap_width = 0
60
61 # Figure out location and size of building
62 building_width = random.randrange(20, 70)
63 building_height = random.randrange(40, 150)
64 building_center_x += gap_width + (building_width / 2)
65 building_center_y = skyline_height + (building_height / 2)
66
67 x1 = building_center_x - building_width / 2
68 x2 = building_center_x + building_width / 2
69 y1 = skyline_height
70 y2 = skyline_height + building_height
71
72 skyline_point_list.append([x1, y1])
73 skyline_point_list.append([x1, y2])
74 skyline_point_list.append([x2, y2])
75 skyline_point_list.append([x2, y1])
76
77 for i in range(4):
78 color_list.append([skyline_color[0], skyline_color[1], skyline_color[2]])
79
80 if random.random() < cap_chance:
81 x1 = building_center_x - building_width / 2
82 x2 = building_center_x + building_width / 2
83 x3 = building_center_x
84
85 y1 = y2 = building_center_y + building_height / 2
86 y3 = y1 + building_width / 2
87
88 # Roof
89 shape = create_polygon([[x1, y1], [x2, y2], [x3, y3]], skyline_color)
90 shape_list.append(shape)
91
92 # See if we should have some windows
93 if random.random() < window_chance:
94 # Yes windows! How many windows?
95 window_rows = random.randrange(10, 15)
96 window_columns = random.randrange(1, 7)
97
98 # Based on that, how big should they be?
99 window_height = (building_height - window_margin * 2) / window_rows
100 window_width = (building_width - window_margin * 2 - window_gap * (window_columns - 1)) / window_columns
101
102 # Find the bottom left of the building so we can start adding widows
103 building_base_y = building_center_y - building_height / 2
104 building_left_x = building_center_x - building_width / 2
105
106 # Loop through each window
107 for row in range(window_rows):
108 for column in range(window_columns):
109 if random.random() < light_on_chance:
110 x1 = building_left_x + column * (window_width + window_gap) + window_margin
111 x2 = building_left_x + column * (window_width + window_gap) + window_width + window_margin
112 y1 = building_base_y + row * window_height
113 y2 = building_base_y + row * window_height + window_height * .8
114
115 skyline_point_list.append([x1, y1])
116 skyline_point_list.append([x1, y2])
117 skyline_point_list.append([x2, y2])
118 skyline_point_list.append([x2, y1])
119
120 for i in range(4):
121 color_list.append((window_color[0], window_color[1], window_color[2]))
122
123 building_center_x += (building_width / 2)
124
125 shape = create_rectangles_filled_with_colors(skyline_point_list, color_list)
126 shape_list.append(shape)
127
128 return shape_list
129
130
131class MyGame(arcade.Window):
132 """ Main application class. """
133
134 def __init__(self):
135 """ Initializer """
136 # Call the parent class initializer
137 super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
138 # Enable vertical sync to make scrolling smoother
139 self.set_vsync(True)
140
141 self.stars = make_star_field(150)
142 self.skyline1 = make_skyline(SCREEN_WIDTH * 5, 250, (80, 80, 80))
143 self.skyline2 = make_skyline(SCREEN_WIDTH * 5, 150, (50, 50, 50))
144
145 self.background_color = arcade.color.BLACK
146
147 def on_draw(self):
148 """Draw to screen"""
149 self.clear()
150
151 self.stars.draw()
152 self.skyline1.draw()
153 self.skyline2.draw()
154
155 def on_update(self, delta_time):
156 """Per frame update logic"""
157 # Scroll each shape list with a slight offset to give a parallax effect
158 self.skyline1.center_x -= 0.5 * 60 * delta_time
159 self.skyline2.center_x -= 1 * 60 * delta_time
160
161 def on_mouse_drag(self, x: int, y: int, dx: int, dy: int, buttons: int, modifiers: int):
162 """Make it possible scroll the scene around by dragging the mouse"""
163 self.skyline1.center_x += dx
164 self.skyline1.center_y += dy
165
166 self.skyline2.center_x += dx
167 self.skyline2.center_y += dy
168
169
170def main():
171 window = MyGame()
172 window.run()
173
174
175if __name__ == "__main__":
176 main()