Step 7 Python#
step_07.py#
1import random
2from pathlib import Path
3from pyglet.math import Vec2
4
5import arcade
6from arcade.experimental import Shadertoy
7
8# Do the math to figure out our screen dimensions
9SCREEN_WIDTH = 800
10SCREEN_HEIGHT = 600
11SCREEN_TITLE = "Ray-casting Demo"
12
13SPRITE_SCALING = 0.25
14
15# How fast the camera pans to the player. 1.0 is instant.
16CAMERA_SPEED = 0.1
17
18PLAYER_MOVEMENT_SPEED = 7
19BOMB_COUNT = 70
20PLAYING_FIELD_WIDTH = 1600
21PLAYING_FIELD_HEIGHT = 1600
22
23
24class MyGame(arcade.Window):
25
26 def __init__(self, width, height, title):
27 super().__init__(width, height, title, resizable=True)
28
29 # The shader toy and 'channels' we'll be using
30 self.shadertoy = None
31 self.channel0 = None
32 self.channel1 = None
33 self.load_shader()
34
35 # Sprites and sprite lists
36 self.player_sprite = None
37 self.wall_list = arcade.SpriteList()
38 self.player_list = arcade.SpriteList()
39 self.bomb_list = arcade.SpriteList()
40 self.physics_engine = None
41
42 self.generate_sprites()
43 arcade.set_background_color(arcade.color.ARMY_GREEN)
44
45 def load_shader(self):
46 # Where is the shader file? Must be specified as a path.
47 shader_file_path = Path("step_06.glsl")
48
49 # Size of the window
50 window_size = self.get_size()
51
52 # Create the shader toy
53 self.shadertoy = Shadertoy.create_from_file(window_size, shader_file_path)
54
55 # Create the channels 0 and 1 frame buffers.
56 # Make the buffer the size of the window, with 4 channels (RGBA)
57 self.channel0 = self.shadertoy.ctx.framebuffer(
58 color_attachments=[self.shadertoy.ctx.texture(window_size, components=4)]
59 )
60 self.channel1 = self.shadertoy.ctx.framebuffer(
61 color_attachments=[self.shadertoy.ctx.texture(window_size, components=4)]
62 )
63
64 # Assign the frame buffers to the channels
65 self.shadertoy.channel_0 = self.channel0.color_attachments[0]
66 self.shadertoy.channel_1 = self.channel1.color_attachments[0]
67
68 def generate_sprites(self):
69 # -- Set up several columns of walls
70 for x in range(0, PLAYING_FIELD_WIDTH, 128):
71 for y in range(0, PLAYING_FIELD_HEIGHT, int(128 * SPRITE_SCALING)):
72 # Randomly skip a box so the player can find a way through
73 if random.randrange(2) > 0:
74 wall = arcade.Sprite(":resources:images/tiles/boxCrate_double.png", SPRITE_SCALING)
75 wall.center_x = x
76 wall.center_y = y
77 self.wall_list.append(wall)
78
79 # -- Set some hidden bombs in the area
80 for i in range(BOMB_COUNT):
81 bomb = arcade.Sprite(":resources:images/tiles/bomb.png", 0.25)
82 placed = False
83 while not placed:
84 bomb.center_x = random.randrange(PLAYING_FIELD_WIDTH)
85 bomb.center_y = random.randrange(PLAYING_FIELD_HEIGHT)
86 if not arcade.check_for_collision_with_list(bomb, self.wall_list):
87 placed = True
88 self.bomb_list.append(bomb)
89
90 # Create the player
91 self.player_sprite = arcade.Sprite(":resources:images/animated_characters/female_person/femalePerson_idle.png",
92 scale=SPRITE_SCALING)
93 self.player_sprite.center_x = 256
94 self.player_sprite.center_y = 512
95 self.player_list.append(self.player_sprite)
96
97 # Physics engine, so we don't run into walls
98 self.physics_engine = arcade.PhysicsEngineSimple(self.player_sprite, self.wall_list)
99
100 def on_draw(self):
101 # Select the channel 0 frame buffer to draw on
102 self.channel0.use()
103 self.channel0.clear()
104 # Draw the walls
105 self.wall_list.draw()
106
107 self.channel1.use()
108 self.channel1.clear()
109 # Draw the bombs
110 self.bomb_list.draw()
111
112 # Select this window to draw on
113 self.use()
114 # Clear to background color
115 self.clear()
116 # Run the shader and render to the window
117 self.shadertoy.program['lightPosition'] = self.player_sprite.position
118 self.shadertoy.program['lightSize'] = 300
119 self.shadertoy.render()
120
121 # Draw the walls
122 self.wall_list.draw()
123
124 # Draw the player
125 self.player_list.draw()
126
127 def on_key_press(self, key, modifiers):
128 """Called whenever a key is pressed. """
129
130 if key == arcade.key.UP:
131 self.player_sprite.change_y = PLAYER_MOVEMENT_SPEED
132 elif key == arcade.key.DOWN:
133 self.player_sprite.change_y = -PLAYER_MOVEMENT_SPEED
134 elif key == arcade.key.LEFT:
135 self.player_sprite.change_x = -PLAYER_MOVEMENT_SPEED
136 elif key == arcade.key.RIGHT:
137 self.player_sprite.change_x = PLAYER_MOVEMENT_SPEED
138
139 def on_key_release(self, key, modifiers):
140 """Called when the user releases a key. """
141
142 if key == arcade.key.UP or key == arcade.key.DOWN:
143 self.player_sprite.change_y = 0
144 elif key == arcade.key.LEFT or key == arcade.key.RIGHT:
145 self.player_sprite.change_x = 0
146
147 def on_update(self, delta_time):
148 """ Movement and game logic """
149
150 # Call update on all sprites (The sprites don't do much in this
151 # example though.)
152 self.physics_engine.update()
153
154 def on_resize(self, width: int, height: int):
155 super().on_resize(width, height)
156 self.shadertoy.resize((width, height))
157
158
159if __name__ == "__main__":
160 MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
161 arcade.run()