Merge pull request #24 from n0remac/refactor

General Refactoring PR
This commit is contained in:
2020-04-20 01:03:25 -05:00
committed by GitHub
4 changed files with 31 additions and 21 deletions
+17 -2
View File
@@ -3,7 +3,11 @@ config.py
Holds all constants used for setting up the game.
May later hold functions for loading/saving configuration files.
"""
import os
BASE_PATH = os.path.dirname(os.path.abspath(__file__))
RESOURCES = os.path.join(BASE_PATH, "resources")
IMAGES = os.path.join(RESOURCES, "images")
class Config(object):
"""
@@ -25,9 +29,20 @@ class Config(object):
# Movement speed of player, in pixels per frame
PLAYER_MOVEMENT_SPEED = 7
# How many pixels to keep as a minimum margin between the character
# and the edge of the screen.
# How many pixels to keep as a minimum margin between the character and the edge of the screen.
LEFT_VIEWPORT_MARGIN = 250
RIGHT_VIEWPORT_MARGIN = 250
BOTTOM_VIEWPORT_MARGIN = 50
TOP_VIEWPORT_MARGIN = 100
class Sprites(object):
"""
Simple class for holding sprite paths.
"""
__MONSTERS = os.path.join(IMAGES, "monsters")
SKELETON = os.path.join(__MONSTERS, "skeleton.png")
GHOST = os.path.join(__MONSTERS, "ghost", "ghost1.png")
FROG = os.path.join(__MONSTERS, "frog", "frog1.png")
+3 -2
View File
@@ -6,9 +6,9 @@ Holds the main game window, as well as manages basic functions for organizing th
import arcade
from config import Config
from map import Dungeon
from mobs import Player, Enemy
from config import Config, Sprites
class Game(arcade.Window):
@@ -35,7 +35,7 @@ class Game(arcade.Window):
# Our physics engine
self.physics_engine = None
# Used to keep track of our scrolling
self.view_bottom = 0
self.view_left = 0
@@ -61,6 +61,7 @@ class Game(arcade.Window):
# Create the dungeon
dungeon = Dungeon()
self.floor_list = dungeon.floor_list
self.wall_list = dungeon.wall_list
+10 -15
View File
@@ -11,6 +11,7 @@ import arcade
import json
import numpy as np
class Dungeon(object):
"""
Organizes Level objects into an easy to render and path through object.
@@ -33,11 +34,10 @@ class Dungeon(object):
center = Level()
center.load_file('resources/levels/map1/center.json')
center.render()
center_floor, center_wall = center.get_lists()
center_floor, center_wall = center.floor_list, center.wall_list
self.floor_list.extend(center_floor)
self.wall_list.extend(center_wall)
# get a side room
room = Level()
room.load_file('resources/levels/map1/room.json')
@@ -59,10 +59,6 @@ class Dungeon(object):
self.floor_list.extend(room_floor)
self.wall_list.extend(room_wall)
#self.level_count, self.size = level_count, size
#self.levels = [[None for y in range(size)] for x in range(size)] # array[x][y] style access
def add_level(self, sprit_list):
for x in sprit_list:
self.levels.append(x)
@@ -102,10 +98,9 @@ class Level:
# the dungeon can be mapped by a proper pathfinding system.
self.floor_list = []
self.wall_list = []
#self.entrances = []
# self.entrances = []
#@staticmethod
def load_file(self, path: str) -> Level:
def load_file(self, path: str):
"""
Builds a Level from a given file path.
@@ -114,10 +109,10 @@ class Level:
"""
self.floor_list = arcade.SpriteList()
self.wall_list = arcade.SpriteList()
with open(path) as file:
level = json.load(file)
self.sprites = level['elements']
self.level = level['structure']
@@ -138,19 +133,19 @@ class Level:
floor = arcade.Sprite(sprite, Config.TILE_SCALING)
floor.center_x = x_pos
floor.center_y = y_pos
if(cur_tile == ' '):
if cur_tile == ' ':
self.floor_list.append(floor)
elif(cur_tile == 'w'):
elif cur_tile == 'w':
self.wall_list.append(floor)
x += 1
x = 0
y += 1
def get_lists(self):
return (self.floor_list, self.wall_list)
return self.floor_list, self.wall_list
def rotate_level(self, times_rotated):
m = np.array(self.level)
for i in range(0, times_rotated):
for i in range(0, times_rotated % 4):
m = np.rot90(m)
self.level = m.tolist()
self.level = m.tolist()
+1 -2
View File
@@ -5,7 +5,7 @@ Organizes all classes related to Mobs, Entities, Enemies, Players and Items.
import arcade
from config import Config
from config import Config, Sprites
# Constants used to track if the player is facing left or right
RIGHT_FACING = 0
@@ -107,7 +107,6 @@ class Player(Mob):
self.cur_texture = 0
self.texture = self.walking_textures[self.cur_texture // Config.RUN_UPDATES_PER_FRAME][self.character_face_direction]
def tick(self):
"""
While Player objects do not have any AI (they are controlled by the user),