Return to repo list

heart-of-gold

Tactical RPG written in python, using pygame.
Return to HMagellan.com

commit 0a0e2d4e85b29aa797c1bf6974253a8f327b182b
parent cfaa285d1750fb2adc60a5872656a9bdfa9af285
Author: Erik Letson <hmagellan@hmagellan.com>
Date:   Sun,  6 Sep 2020 04:22:44 -0500

Beginning a refactor. Everything must go.

Diffstat:
Ddata/json/maps/test1.json | 14--------------
Ddata/json/tiles/testtiles1.json | 26--------------------------
Dsrc/battlefield.py | 16----------------
Dsrc/constants.py | 21---------------------
Dsrc/game.py | 46----------------------------------------------
Dsrc/images.py | 194-------------------------------------------------------------------------------
Dsrc/init.py | 0
Dsrc/interface.py | 37-------------------------------------
Dsrc/vgo.py | 78------------------------------------------------------------------------------
9 files changed, 0 insertions(+), 432 deletions(-)

diff --git a/data/json/maps/test1.json b/data/json/maps/test1.json @@ -1,14 +0,0 @@ -{ - "name" : "Test 1", - "width" : 6, - "height" : 6, - "tilesheet" : "testtiles1", - "tilemap" : [ - "aaabbb", - "bbbbbb", - "bbbbbc", - "ccbbcc", - "cccccd", - "cccddd" - ] -} diff --git a/data/json/tiles/testtiles1.json b/data/json/tiles/testtiles1.json @@ -1,26 +0,0 @@ -{ - "name" : "testtiles1", - "image" : "testtiles1.png", - "tiles" : [ - "a" : { - "name" : "grass1", - "tile" : [0, 0], - "passable" : true - }, - "b" : { - "name" : "dirt1", - "tile" : [1, 0], - "passable" : true - }, - "c" : { - "name" : "sand1", - "tile" : [0, 1], - "passable" : true - }, - "d" : { - "name" : "water1", - "tile" : [1, 1], - "passable" : false - } - ] -} diff --git a/src/battlefield.py b/src/battlefield.py @@ -1,16 +0,0 @@ -import pygame, json -from .constants import JSON_PATH - -class Battlefield(object): - """ - Class that represents the area that battles take place - in. - """ - - def __init__(self, map_json): - - self.json = json.load(os.path.join(JSON_PATH, "map", map_json)) - - self.name = self.json["name"] - self.map_width = int(self.json["width"]) - self.map_height = int(self.json["height"]) diff --git a/src/constants.py b/src/constants.py @@ -1,21 +0,0 @@ -import pygame, os - -# NOTE: constants.py should not import anything from the local project to avoid circular dependencies - -############################################ -# Constants to be included across the game # -############################################ - -# Settings -SCREEN_WIDTH = 1024 -SCREEN_HEIGHT = 768 -FRAMERATE = 60 - -TILE_WIDTH = 64 -TILE_HEIGHT = 64 -COLORKEY = (255, 0, 255) - -# Paths -DATA_PATH = os.path.join(os.getcwd(), "data") -IMG_PATH = os.path.join(DATA_PATH, "img") -JSON_PATH = os.path.join(DATA_PATH, "json") diff --git a/src/game.py b/src/game.py @@ -1,46 +0,0 @@ -import pygame -from . import interface, vgo, images -from .constants import SCREEN_WIDTH, SCREEN_HEIGHT, FRAMERATE, COLORKEY - -class Game(object): - - def __init__(self): - - self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) - self.frame_clock = pygame.time.Clock() - self.framerate = FRAMERATE - self.on = True - - # Subsystems - self.interface = interface.Interface(self) - - # External calls as needed - images.convert_sheets(images.SHEETS) - - #TESTING - self.testvgo = vgo.Entity(images.SHEETS["jisella_1"], (100, 100)) - self.testvgo.set_animation(images.ANIMATIONS["jisella_1"]["stand_L"], True, 0) - self.anim_index = 0 - self.anim_index_max = len(images.ANIMATIONS["jisella_1"]) - 1 - - # Utility methods - def toggle_on(self): - self.on = not self.on - - # Mainloop methods - def shift_frames(self, framerate): - self.frame_clock.tick(framerate) - - def update_game(self): - self.screen.fill((66, 0, 0)) - self.testvgo.update(self.screen) - pygame.display.update() - - def mainloop(self): - - while self.on: - self.shift_frames(self.framerate) - self.interface.handle_events() - self.update_game() - - pygame.quit() diff --git a/src/images.py b/src/images.py @@ -1,194 +0,0 @@ -import pygame, os -from .constants import TILE_WIDTH, TILE_HEIGHT, COLORKEY, IMG_PATH - -class Sheet(object): - """ - Class for the spritesheets that entity images are loaded - from. Supports regularized spritesheets only (all sprites - feature the same dimensions). - """ - - def __init__(self, filename, sprite_size, total_sprites): - - #try and load all sprites on the sheet - success = self.load_sheet(filename, sprite_size, total_sprites) - - if success == 1: #failure, cleanup vals - - self.sprites = {} - print("Failed to load sheet: " + filename) #error message - - def load_sheet(self, filename, sprite_size, total_sprites): - """ - Load a sheet and divide it into subsurfaces for - use as images by sprite entities. - """ - - #remember important variables - self.filename = filename - self.sprite_size = sprite_size - self.total_sprites = total_sprites - - #Step 1: attempt to load the appropriate image file - try: - self.sheet = pygame.image.load(os.path.join(IMG_PATH, filename)) - - #catch a missing file error and stop, set up graceful failure - except: - self.sheet = None - - #Step 2: if sheet exists, divide it into sprites - if self.sheet != None: - self.sprites = {} # empty dict to hold our loaded images - - #vals to track our progress thru the sheet - x = 0 - y = 0 - - #while we still have more sprites to load, load them - while len(self.sprites) < total_sprites: - - #get a rect that can be used to make a subsurface of the sheet - new_rect = pygame.Rect((x * sprite_size[0], y * sprite_size[1]), sprite_size) - - #load image, store it in our dict, set its colorkey - self.sprites[(x, y)] = self.sheet.subsurface(new_rect) - self.sprites[(x, y)].set_colorkey(COLORKEY) - - x += 1 # scoot over to the right - - #if we're hanging off the right side, scoot down and start over - #again from the far left - if x * sprite_size[0] >= self.sheet.get_width(): - x = 0 - y += 1 - - return 0 # SUCCESS!! - - #No sheet exists - else: - - return 1 # failure :C - -############################################################################### -############################################################################### - -####################################### -# Spritesheets to be used in the game # -# THIS SHOULD PROBABLY BE IN ITS OWN # -# FILE SOMEWHERE!!! # -####################################### -# NOTE: All of this should be in JSON files -SHEETS = { - "jisella_1" : Sheet("jisella_1.png", (TILE_WIDTH, TILE_HEIGHT), 72) -} - -TILE_SHEETS = { - "testsheet1" : Sheet("testsheet1.png", (TILE_WIDTH, TILE_HEIGHT), 4) -} - -ANIMATIONS = { - "jisella_1" : { - "stand_L" : [ - { "sprite" : (0, 0), "timer" : 4 }, - { "sprite" : (1, 0), "timer" : 4 }, - { "sprite" : (2, 0), "timer" : 4 }, - { "sprite" : (3, 0), "timer" : 4 }, - { "sprite" : (0, 1), "timer" : 4 }, - { "sprite" : (1, 1), "timer" : 4 }, - { "sprite" : (2, 1), "timer" : 4 }, - { "sprite" : (3, 1), "timer" : 4 } - ], - "stand_R" : [ - { "sprite" : (4, 0), "timer" : 4 }, - { "sprite" : (5, 0), "timer" : 4 }, - { "sprite" : (6, 0), "timer" : 4 }, - { "sprite" : (7, 0), "timer" : 4 }, - { "sprite" : (4, 1), "timer" : 4 }, - { "sprite" : (5, 1), "timer" : 4 }, - { "sprite" : (6, 1), "timer" : 4 }, - { "sprite" : (7, 1), "timer" : 4 } - ], - "stand_B" : [ - { "sprite" : (8, 0), "timer" : 4 }, - { "sprite" : (9, 0), "timer" : 4 }, - { "sprite" : (10, 0), "timer" : 4 }, - { "sprite" : (11, 0), "timer" : 4 }, - { "sprite" : (8, 1), "timer" : 4 }, - { "sprite" : (9, 1), "timer" : 4 }, - { "sprite" : (10, 1), "timer" : 4 }, - { "sprite" : (11, 1), "timer" : 4 } - ], - "walk_L" : [ - { "sprite" : (0, 2), "timer" : 6 }, - { "sprite" : (1, 2), "timer" : 6 }, - { "sprite" : (2, 2), "timer" : 6 }, - { "sprite" : (3, 2), "timer" : 6 } - ], - "walk_R" : [ - { "sprite" : (4, 2), "timer" : 6 }, - { "sprite" : (5, 2), "timer" : 6 }, - { "sprite" : (6, 2), "timer" : 6 }, - { "sprite" : (7, 2), "timer" : 6 } - ], - "walk_B" : [ - { "sprite" : (8, 2), "timer" : 6 }, - { "sprite" : (9, 2), "timer" : 6 }, - { "sprite" : (10, 2), "timer" : 6 }, - { "sprite" : (11, 2), "timer" : 6 } - ], - "attack_L" : [ - { "sprite" : (0, 3), "timer" : 10 }, - { "sprite" : (1, 3), "timer" : 20 }, - { "sprite" : (2, 3), "timer" : 3 }, - { "sprite" : (3, 3), "timer" : 24 } - ], - "attack_R" : [ - { "sprite" : (4, 3), "timer" : 10 }, - { "sprite" : (5, 3), "timer" : 20 }, - { "sprite" : (6, 3), "timer" : 3 }, - { "sprite" : (7, 3), "timer" : 24 } - ], - "attack_B" : [ - { "sprite" : (8, 3), "timer" : 10 }, - { "sprite" : (9, 3), "timer" : 20 }, - { "sprite" : (10, 3), "timer" : 3 }, - { "sprite" : (11, 3), "timer" : 24 } - ], - "shoot_L" : [ - { "sprite" : (0, 4), "timer" : 44 }, - { "sprite" : (1, 4), "timer" : 20 } - ], - "shoot_R" : [ - { "sprite" : (4, 4), "timer" : 44 }, - { "sprite" : (5, 4), "timer" : 20 } - ], - "shoot_B" : [ - { "sprite" : (8, 4), "timer" : 44 }, - { "sprite" : (9, 4), "timer" : 20 } - ], - "hurt_L" : [ - { "sprite" : (2, 4), "timer" : -1 } - ], - "hurt_R" : [ - { "sprite" : (6, 4), "timer" : -1 } - ], - "hurt_B" : [ - { "sprite" : (10, 4), "timer" : -1 } - ], - "activate_L" : [ - { "sprite" : (3, 4), "timer" : -1 } - ], - "activate_R" : [ - { "sprite" : (7, 4), "timer" : -1 } - ], - "activate_B" : [ - { "sprite" : (11, 4), "timer" : -1 } - ], - } -} - -def convert_sheets(sheets): - for s in sheets: - for p in sheets[s].sprites: - sheets[s].sprites[p].convert() diff --git a/src/init.py b/src/init.py diff --git a/src/interface.py b/src/interface.py @@ -1,37 +0,0 @@ -import pygame -from . import images - -class Interface(object): - """ - Interface controls all event reaction. - """ - - def __init__(self, game): - self.game = game - - def handle_events(self): - - for event in pygame.event.get(): - if event.type == pygame.QUIT: - self.game.toggle_on() - elif event.type == pygame.KEYDOWN: - self.handle_key_press(event.key) - - def handle_key_press(self, key): - if key == pygame.K_LEFT: - self.game.anim_index -= 1 - if self.game.anim_index < 0: - self.game.anim_index = self.game.anim_index_max - for i, k in enumerate(images.ANIMATIONS["jisella_1"]): - if i == self.game.anim_index: - self.game.testvgo.set_animation(images.ANIMATIONS["jisella_1"][k], True, 0) - elif key == pygame.K_RIGHT: - self.game.anim_index += 1 - if self.game.anim_index > self.game.anim_index_max: - self.game.anim_index = 0 - for i, k in enumerate(images.ANIMATIONS["jisella_1"]): - if i == self.game.anim_index: - self.game.testvgo.set_animation(images.ANIMATIONS["jisella_1"][k], True, 0) - - def handle_key_release(self, key): - pass diff --git a/src/vgo.py b/src/vgo.py @@ -1,78 +0,0 @@ -import pygame - -class VisibleGameObject(pygame.sprite.Sprite): - """ - Extended sprite class used throughout game. - """ - - def __init__(self, sheet, pos): - - pygame.sprite.Sprite.__init__(self) - - self.sheet = sheet - self.image = sheet.sprites[(0, 0)] - self.rect = sheet.sprites[(0, 0)].get_rect() - self.rect.topleft = pos - - # Animations - self.animation = None - self.current_animation_frame = 0 - self.animation_timer = 0 - self.play_animation = False - - def set_animation(self, new_anim, play = False, initframe = 0): - self.animation = new_anim - self.current_animation_frame = initframe - self.animation_timer = new_anim[initframe]["timer"] - self.image = self.sheet.sprites[new_anim[initframe]["sprite"]] - self.play_animation = play - - def animate(self): - - if self.animation_timer > 0: - self.animation_timer -= 1 - elif self.animation_timer == 0: - if self.current_animation_frame < len(self.animation) - 1: - self.current_animation_frame += 1 - else: - self.current_animation_frame = 0 - self.animation_timer = self.animation[self.current_animation_frame]["timer"] - self.image = self.sheet.sprites[self.animation[self.current_animation_frame]["sprite"]] - - def act(self): - pass - - def update(self, surface = None): - if surface != None: - if self.play_animation: - self.animate() - surface.blit(self.image, self.rect) - -############################################################################### -############################################################################### - -class Tile(VisibleGameObject): - - def __init__(self, sheet, pos, passable, occupant = None): - - super().__init__(sheet, pos) - - self.passable = passable - self.occupant = occupant - -############################################################################### -############################################################################### - -class Entity(VisibleGameObject): - """ - Class representing non-tile, non-UI visible - game objects (player characters, obstacles, etc). - """ - - def __init__(self, sheet, pos, tile = None, solid = False): - - super().__init__(sheet, pos) - - self.tile = tile - self.solid = solid -