commit 0ff1d90262f68cdd0a13379dc5dde44caa1cd724
parent 5a0d1c691110692d32ff5a9ad67926678746592f
Author: Erik Letson <hmagellan@hmagellan.com>
Date: Sun, 6 Sep 2020 05:20:43 -0500
Rebuild of images.py and constants.py
Diffstat:
A | src/constants.py | | | 30 | ++++++++++++++++++++++++++++++ |
A | src/images.py | | | 90 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 files changed, 120 insertions(+), 0 deletions(-)
diff --git a/src/constants.py b/src/constants.py
@@ -0,0 +1,30 @@
+import os
+
+################
+# constants.py #
+################
+
+# This file contains:
+# 1. Important environment values that exist independent of any one source file
+
+# TODO: This file should source from a settings file
+
+#########################
+# Section 1 - Constants #
+#########################
+
+# Game settings
+SCREEN_WIDTH = 1024
+SCREEN_HEIGHT = 768
+FRAMERATE = 60
+
+# Tile constants
+TILE_WIDTH = 64
+TILE_HEIGHT = 64
+COLORKEY = (255, 0, 255)
+
+# File paths
+DATA_PATH = os.path.join(os.getcwd(), "data")
+IMAGE_PATH = os.path.join(DATA_PATH, "img")
+SOUND_PATH = os.path.join(DATA_PATH, "snd")
+FONT_PATH = os.path.join(DATA_PATH, "font")
diff --git a/src/images.py b/src/images.py
@@ -0,0 +1,90 @@
+import pygame, os
+from .constants import IMAGE_PATH, COLORKEY
+
+#############
+# images.py #
+#############
+
+# This file contains:
+# 1. Important environment values and functions for loading images
+# 2. The 'Sheet' class, which is used to represent enumerated spritesheets
+
+#####################################################
+# Section 1 - Environment Values and Image Loading #
+#####################################################
+
+def load_image(filename):
+ """
+ Load an image file as a PyGame image.
+ """
+ return pygame.image.load(os.path.join(IMAGE_PATH, filename)).convert()
+
+############################
+# Section 2 - Sheet Object #
+############################
+
+class Sheet(object):
+ """
+ Class for the enumerated spritesheets that are used to get
+ images for entities. Supports regularized spritesheets only
+ (that is, all sprites are the same dimensions).
+ """
+
+ def __init__(self, filename, sprite_size, total_sprites):
+
+ # Important values
+ self.filename = filename
+ self.sprite_dimensions = sprite_size
+ self.total_sprites = total_sprites
+
+ # Try and load all sprites on the sheet, or log if we failed
+ if not self.load_sheet(filename, sprite_size, total_sprites):
+ self.sprites = {}
+ print("Failed to load sheet: " + filename)
+
+ def load_sheet(self, filename, sprite_size, total_sprites):
+ """
+ Load a sheet and divide it into subsurfaces for use as
+ images by sprite entities.
+ """
+
+ # First, attempt to load the image file. Failing that, create
+ # a sheet as a NoneType object
+ try:
+ self.sheet = load_image(filename)
+ except:
+ self.sheet = None
+
+ # Next, if the sheet exists, divide it into sprites
+ if self.sheet != None:
+ self.sprites = {}
+
+ # Values to track progress
+ x = 0
+ y = 0
+
+ # While there are still 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 in our sprites dict, and set colorkey
+ self.sprites[(x, y)] = self.sheet.subsurface(new_rect).convert()
+ self.sprites[(x, y)].set_colorkey(COLORKEY)
+
+ # Scoot over to the right
+ x += 1
+
+ # 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
+
+ # After the while loop, return True for success
+ return True
+
+ # No sheet exists, return False for failure
+ else:
+ return False