place character randomly in one of the Dungeon levels, remove player_list and just use player, add Dungeon.levelList property function, add in generated level loading

This commit is contained in:
Xevion
2020-04-20 04:46:50 -05:00
parent 5cf2f5e7f7
commit 6f1161ab75
2 changed files with 38 additions and 32 deletions

View File

@@ -3,6 +3,7 @@ main.py
The main class used to load the game. The main class used to load the game.
Holds the main game window, as well as manages basic functions for organizing the game. Holds the main game window, as well as manages basic functions for organizing the game.
""" """
import random
import arcade import arcade
@@ -25,9 +26,6 @@ class Game(arcade.Window):
self.wall_list = None self.wall_list = None
self.floor_list = None self.floor_list = None
self.enemy_list = None self.enemy_list = None
self.player_list = None
# Separate variable that holds the player sprite
self.player = None self.player = None
self.dungeon = None self.dungeon = None
@@ -48,23 +46,21 @@ class Game(arcade.Window):
""" Set up the game here. Call this function to restart the game. """ """ Set up the game here. Call this function to restart the game. """
# Create the Sprite lists # Create the Sprite lists
self.player_list = arcade.SpriteList()
self.enemy_list = arcade.SpriteList() self.enemy_list = arcade.SpriteList()
# Set up the player, specifically placing it at these coordinates. # Set up the player, specifically placing it at these coordinates.
self.player = Player() self.player = Player()
self.player.scale = 1 self.player.scale = 1
self.player.center_x = Config.SCREEN_WIDTH / 2
self.player.center_y = Config.SCREEN_HEIGHT / 2
self.player_list = self.player
# Create the dungeon # Create the dungeon
self.dungeon = Dungeon() self.dungeon = Dungeon()
self.player.center_x, self.player.center_y = random.choice(self.dungeon.levelList).center()
# Create monsters # Create monsters
# This needs to be updated to comply with the new mobs.py code # This needs to be updated to comply with the new mobs.py code
# self.enemy_list.append(Enemy("resources/images/monsters/ghost/ghost1.png", 200, 200, 4).get_enemy()) # self.enemy_list.append(Enemy("resources/images/monsters/ghost/ghost1.png", 200, 200, 4))
# self.enemy_list.append(Enemy("resources/images/monsters/frog/frog1.png", 200, 1000, 4).get_enemy()) # self.enemy_list.append(Enemy("resources/images/monsters/frog/frog1.png", 200, 1000, 4))
# Create the 'physics engine' # Create the 'physics engine'
self.physics_engine = arcade.PhysicsEngineSimple(self.player, self.dungeon.getWalls()) self.physics_engine = arcade.PhysicsEngineSimple(self.player, self.dungeon.getWalls())
@@ -80,7 +76,7 @@ class Game(arcade.Window):
# Draw our sprites # Draw our sprites
self.dungeon.render() self.dungeon.render()
self.player_list.draw() self.player.draw()
self.enemy_list.draw() self.enemy_list.draw()
self.wall_list.draw() self.wall_list.draw()
@@ -88,16 +84,16 @@ class Game(arcade.Window):
"""Called whenever a key is pressed. """ """Called whenever a key is pressed. """
if key == arcade.key.UP or key == arcade.key.W: if key == arcade.key.UP or key == arcade.key.W:
self.player_list.change_y = Config.PLAYER_MOVEMENT_SPEED self.player.change_y = Config.PLAYER_MOVEMENT_SPEED
self.prev_keypress.append(key) self.prev_keypress.append(key)
elif key == arcade.key.DOWN or key == arcade.key.S: elif key == arcade.key.DOWN or key == arcade.key.S:
self.player_list.change_y = -Config.PLAYER_MOVEMENT_SPEED self.player.change_y = -Config.PLAYER_MOVEMENT_SPEED
self.prev_keypress.append(key) self.prev_keypress.append(key)
elif key == arcade.key.LEFT or key == arcade.key.A: elif key == arcade.key.LEFT or key == arcade.key.A:
self.player_list.change_x = -Config.PLAYER_MOVEMENT_SPEED self.player.change_x = -Config.PLAYER_MOVEMENT_SPEED
self.prev_keypress.append(key) self.prev_keypress.append(key)
elif key == arcade.key.RIGHT or key == arcade.key.D: elif key == arcade.key.RIGHT or key == arcade.key.D:
self.player_list.change_x = Config.PLAYER_MOVEMENT_SPEED self.player.change_x = Config.PLAYER_MOVEMENT_SPEED
self.prev_keypress.append(key) self.prev_keypress.append(key)
elif key == 65307: elif key == 65307:
self.close() self.close()
@@ -106,16 +102,16 @@ class Game(arcade.Window):
"""Called when the user releases a key. """ """Called when the user releases a key. """
if key == arcade.key.UP or key == arcade.key.W: if key == arcade.key.UP or key == arcade.key.W:
self.player_list.change_y = 0 self.player.change_y = 0
self.prev_keypress.remove(key) self.prev_keypress.remove(key)
elif key == arcade.key.DOWN or key == arcade.key.S: elif key == arcade.key.DOWN or key == arcade.key.S:
self.player_list.change_y = 0 self.player.change_y = 0
self.prev_keypress.remove(key) self.prev_keypress.remove(key)
elif key == arcade.key.LEFT or key == arcade.key.A: elif key == arcade.key.LEFT or key == arcade.key.A:
self.player_list.change_x = 0 self.player.change_x = 0
self.prev_keypress.remove(key) self.prev_keypress.remove(key)
elif key == arcade.key.RIGHT or key == arcade.key.D: elif key == arcade.key.RIGHT or key == arcade.key.D:
self.player_list.change_x = 0 self.player.change_x = 0
self.prev_keypress.remove(key) self.prev_keypress.remove(key)
if self.prev_keypress: if self.prev_keypress:
self.on_key_press(self.prev_keypress.pop(0), 0) self.on_key_press(self.prev_keypress.pop(0), 0)
@@ -126,29 +122,29 @@ class Game(arcade.Window):
# Move the player with the physics engine # Move the player with the physics engine
self.physics_engine.update() self.physics_engine.update()
self.player_list.update_animation() self.player.update_animation()
changed = False # Track if we need to change the viewport changed = False # Track if we need to change the viewport
# Below manages all scrolling mechanics # Below manages all scrolling mechanics
# Scroll left # Scroll left
left_boundary = self.view_left + Config.LEFT_VIEWPORT_MARGIN left_boundary = self.view_left + Config.LEFT_VIEWPORT_MARGIN
if self.player_list.left < left_boundary: if self.player.left < left_boundary:
self.view_left -= left_boundary - self.player_list.left self.view_left -= left_boundary - self.player.left
changed = True changed = True
# Scroll right # Scroll right
right_boundary = self.view_left + Config.SCREEN_WIDTH - Config.RIGHT_VIEWPORT_MARGIN right_boundary = self.view_left + Config.SCREEN_WIDTH - Config.RIGHT_VIEWPORT_MARGIN
if self.player_list.right > right_boundary: if self.player.right > right_boundary:
self.view_left += self.player_list.right - right_boundary self.view_left += self.player.right - right_boundary
changed = True changed = True
# Scroll up # Scroll up
top_boundary = self.view_bottom + Config.SCREEN_HEIGHT - Config.TOP_VIEWPORT_MARGIN top_boundary = self.view_bottom + Config.SCREEN_HEIGHT - Config.TOP_VIEWPORT_MARGIN
if self.player_list.top > top_boundary: if self.player.top > top_boundary:
self.view_bottom += self.player_list.top - top_boundary self.view_bottom += self.player.top - top_boundary
changed = True changed = True
# Scroll down # Scroll down
bottom_boundary = self.view_bottom + Config.BOTTOM_VIEWPORT_MARGIN bottom_boundary = self.view_bottom + Config.BOTTOM_VIEWPORT_MARGIN
if self.player_list.bottom < bottom_boundary: if self.player.bottom < bottom_boundary:
self.view_bottom -= bottom_boundary - self.player_list.bottom self.view_bottom -= bottom_boundary - self.player.bottom
changed = True changed = True
if changed: if changed:

View File

@@ -35,13 +35,12 @@ class Dungeon(object):
self.floor_list = arcade.SpriteList() self.floor_list = arcade.SpriteList()
self.wall_list = arcade.SpriteList() self.wall_list = arcade.SpriteList()
center = Level.load_file(1, 1, 'resources/levels/map1/center.json') # center = Level.load_file(1, 1, 'resources/levels/map1/center.json')
side = Level.load_file(2, 1, 'resources/levels/map1/room.json') # side = Level.load_file(2, 1, 'resources/levels/map1/room.json')
center = "resources/levels/map1/center.json"
self.levels = [ self.levels = [
[None, None, None], [Level.load_file(x, y, center) for y in range(size)] for x in range(size)
[center, side, None],
[None, None, None]
] ]
def getWalls(self): def getWalls(self):
@@ -65,6 +64,17 @@ class Dungeon(object):
level.floorSprites.draw() level.floorSprites.draw()
level.wallSprites.draw() level.wallSprites.draw()
@property
def levelList(self) -> list:
"""
Retrieves all Level objects from Dungeon intance.
:return: A list containing all Level objects.
"""
return list(filter(
lambda level: level is not None, chain.from_iterable(self.levels)
))
class Level: class Level:
""" """