commit 401023fb8f2d341e280b82e49a34ee4bec803630
parent ee287c66981b18caaf8266e55dec849058090070
Author: Erik Letson <hmagellan@hmagellan.com>
Date: Wed, 26 Aug 2020 02:25:18 -0500
Setting up for images
Diffstat:
2 files changed, 22 insertions(+), 11 deletions(-)
diff --git a/src/constants.py b/src/constants.py
@@ -1,10 +1,24 @@
+import pygame, os
+from . import images
+
############################################
# 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")
+
+# Spritesheets
+sheets = {
+ "jisella_1" : images.Sheet("jisella_1.png", (TILE_WIDTH, TILE_HEIGHT), 72)
+}
diff --git a/src/images.py b/src/images.py
@@ -1,4 +1,5 @@
import pygame, os
+from .constants import COLORKEY, IMG_PATH
class Sheet(object):
"""
@@ -7,33 +8,30 @@ class Sheet(object):
feature the same dimensions).
"""
- def __init__(self, game, name, sprite_size, total_sprites):
-
- self.game = game
+ def __init__(self, filename, sprite_size, total_sprites):
#try and load all sprites on the sheet
- success = self.load_sheet(name, sprite_size, total_sprites)
+ success = self.load_sheet(filename, sprite_size, total_sprites)
if success == 1: #failure, cleanup vals
self.sprites = {}
- print("Failed to load sheet: " + name) #error message
+ print("Failed to load sheet: " + filename) #error message
- def load_sheet(self, name, sprite_size, total_sprites):
+ 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.name = name
+ 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(os.getcwd(), "data", "img", name))
+ self.sheet = pygame.image.load(os.path.join(IMG_PATH, filename).convert()
#catch a missing file error and stop, set up graceful failure
except:
@@ -58,14 +56,13 @@ class Sheet(object):
#load image, store it in our dict, set its colorkey
self.sprites[(x, y)] = self.sheet.subsurface(new_rect).convert()
- self.sprites[(x, y)].set_colorkey((255, 0, 255))
+ 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