Step 6 Python

../../_images/step_06.png
step_06.py
  1import random
  2
  3import arcade
  4from arcade.experimental import Shadertoy
  5
  6# Do the math to figure out our screen dimensions
  7SCREEN_WIDTH = 800
  8SCREEN_HEIGHT = 600
  9SCREEN_TITLE = "Ray-casting Demo"
 10
 11SPRITE_SCALING = 0.25
 12
 13# How fast the camera pans to the player. 1.0 is instant.
 14CAMERA_SPEED = 0.1
 15
 16PLAYER_MOVEMENT_SPEED = 7
 17BOMB_COUNT = 70
 18PLAYING_FIELD_WIDTH = 1600
 19PLAYING_FIELD_HEIGHT = 1600
 20
 21
 22class MyGame(arcade.Window):
 23
 24    def __init__(self, width, height, title):
 25        super().__init__(width, height, title)
 26
 27        # The shader toy and 'channels' we'll be using
 28        self.shadertoy = None
 29        self.channel0 = None
 30        self.channel1 = None
 31        self.load_shader()
 32
 33        # Sprites and sprite lists
 34        self.player_sprite = arcade.Sprite(
 35            ":resources:images/animated_characters/female_person/femalePerson_idle.png",
 36            scale=SPRITE_SCALING,
 37            center_x=256,
 38            center_y=512,
 39        )
 40        self.wall_list = arcade.SpriteList()
 41        self.player_list = arcade.SpriteList()
 42        self.bomb_list = arcade.SpriteList()
 43        self.physics_engine = None
 44
 45        self.generate_sprites()
 46        self.background_color = arcade.color.ARMY_GREEN
 47
 48    def load_shader(self):
 49        # Size of the window
 50        window_size = self.get_size()
 51
 52        # Create the shader toy, passing in a path for the shader source
 53        self.shadertoy = Shadertoy.create_from_file(window_size, "step_06.glsl")
 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        # Add the player to the player list
 91        self.player_list.append(self.player_sprite)
 92
 93        # Physics engine, so we don't run into walls
 94        self.physics_engine = arcade.PhysicsEngineSimple(self.player_sprite, self.wall_list)
 95
 96    def on_draw(self):
 97        # Select the channel 0 frame buffer to draw on
 98        self.channel0.use()
 99        self.channel0.clear()
100        # Draw the walls
101        self.wall_list.draw()
102
103        self.channel1.use()
104        self.channel1.clear(color=arcade.color.AMAZON)
105        # Draw the bombs
106        self.bomb_list.draw()
107
108        # Select this window to draw on
109        self.use()
110        # Clear to background color
111        self.clear()
112        # Run the shader and render to the window
113        self.shadertoy.program['lightPosition'] = self.player_sprite.position
114        self.shadertoy.program['lightSize'] = 300
115        self.shadertoy.render()
116
117        # Draw the walls
118        self.wall_list.draw()
119
120        # Draw the player
121        self.player_list.draw()
122
123    def on_key_press(self, key, modifiers):
124        """Called whenever a key is pressed. """
125
126        if key == arcade.key.UP:
127            self.player_sprite.change_y = PLAYER_MOVEMENT_SPEED
128        elif key == arcade.key.DOWN:
129            self.player_sprite.change_y = -PLAYER_MOVEMENT_SPEED
130        elif key == arcade.key.LEFT:
131            self.player_sprite.change_x = -PLAYER_MOVEMENT_SPEED
132        elif key == arcade.key.RIGHT:
133            self.player_sprite.change_x = PLAYER_MOVEMENT_SPEED
134
135    def on_key_release(self, key, modifiers):
136        """Called when the user releases a key. """
137
138        if key == arcade.key.UP or key == arcade.key.DOWN:
139            self.player_sprite.change_y = 0
140        elif key == arcade.key.LEFT or key == arcade.key.RIGHT:
141            self.player_sprite.change_x = 0
142
143    def on_update(self, delta_time):
144        """ Movement and game logic """
145
146        # Call update on all sprites (The sprites don't do much in this
147        # example though.)
148        self.physics_engine.update()
149
150
151if __name__ == "__main__":
152    MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
153    arcade.run()