Ray-Casting Starting File

../../_images/start4.png
start.py
  1import random
  2import arcade
  3
  4# Do the math to figure out our screen dimensions
  5SCREEN_WIDTH = 800
  6SCREEN_HEIGHT = 600
  7SCREEN_TITLE = "Ray-casting Demo"
  8
  9SPRITE_SCALING = 0.25
 10
 11# How fast the camera pans to the player. 1.0 is instant.
 12CAMERA_SPEED = 0.1
 13
 14PLAYER_MOVEMENT_SPEED = 7
 15BOMB_COUNT = 70
 16PLAYING_FIELD_WIDTH = 1600
 17PLAYING_FIELD_HEIGHT = 1600
 18
 19
 20class MyGame(arcade.Window):
 21
 22    def __init__(self, width, height, title):
 23        super().__init__(width, height, title, resizable=True)
 24
 25        # Sprites and sprite lists
 26        self.player_sprite = arcade.Sprite(
 27            ":resources:images/animated_characters/female_person/femalePerson_idle.png",
 28            scale=SPRITE_SCALING,
 29            center_x=256,
 30            center_y=512,
 31        )
 32        self.wall_list = arcade.SpriteList()
 33        self.player_list = arcade.SpriteList()
 34        self.bomb_list = arcade.SpriteList()
 35        self.physics_engine = None
 36
 37        self.generate_sprites()
 38        self.background_color = arcade.color.ARMY_GREEN
 39
 40    def generate_sprites(self):
 41        # -- Set up several columns of walls (that will cast shadows)
 42        for x in range(0, PLAYING_FIELD_WIDTH, 128):
 43            for y in range(0, PLAYING_FIELD_HEIGHT, int(128 * SPRITE_SCALING)):
 44                # Randomly skip a box so the player can find a way through
 45                if random.randrange(2) > 0:
 46                    wall = arcade.Sprite(":resources:images/tiles/boxCrate_double.png", SPRITE_SCALING)
 47                    wall.center_x = x
 48                    wall.center_y = y
 49                    self.wall_list.append(wall)
 50
 51        # -- Set some hidden bombs in the area
 52        for i in range(BOMB_COUNT):
 53            bomb = arcade.Sprite(":resources:images/tiles/bomb.png", 0.25)
 54            placed = False
 55            while not placed:
 56                bomb.center_x = random.randrange(PLAYING_FIELD_WIDTH)
 57                bomb.center_y = random.randrange(PLAYING_FIELD_HEIGHT)
 58                if not arcade.check_for_collision_with_list(bomb, self.wall_list):
 59                    placed = True
 60            self.bomb_list.append(bomb)
 61
 62        # Add player to spritelist
 63        self.player_list.append(self.player_sprite)
 64
 65        # Physics engine, so we don't run into walls
 66        self.physics_engine = arcade.PhysicsEngineSimple(self.player_sprite, self.wall_list)
 67
 68    def on_draw(self):
 69        self.clear()
 70
 71        self.wall_list.draw()
 72        self.bomb_list.draw()
 73        self.player_list.draw()
 74
 75    def on_key_press(self, key, modifiers):
 76        """Called whenever a key is pressed. """
 77
 78        if key == arcade.key.UP:
 79            self.player_sprite.change_y = PLAYER_MOVEMENT_SPEED
 80        elif key == arcade.key.DOWN:
 81            self.player_sprite.change_y = -PLAYER_MOVEMENT_SPEED
 82        elif key == arcade.key.LEFT:
 83            self.player_sprite.change_x = -PLAYER_MOVEMENT_SPEED
 84        elif key == arcade.key.RIGHT:
 85            self.player_sprite.change_x = PLAYER_MOVEMENT_SPEED
 86
 87    def on_key_release(self, key, modifiers):
 88        """Called when the user releases a key. """
 89
 90        if key == arcade.key.UP or key == arcade.key.DOWN:
 91            self.player_sprite.change_y = 0
 92        elif key == arcade.key.LEFT or key == arcade.key.RIGHT:
 93            self.player_sprite.change_x = 0
 94
 95    def on_update(self, delta_time):
 96        """ Movement and game logic """
 97
 98        # Call update on all sprites (The sprites don't do much in this
 99        # example though.)
100        self.physics_engine.update()
101
102
103if __name__ == "__main__":
104    MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
105    arcade.run()