summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--riseoftodmoldren/assets.py42
-rw-r--r--riseoftodmoldren/base.py12
-rw-r--r--riseoftodmoldren/screens/main.py19
-rw-r--r--riseoftodmoldren/screens/start.py14
4 files changed, 77 insertions, 10 deletions
diff --git a/riseoftodmoldren/assets.py b/riseoftodmoldren/assets.py
index 4d567a7..24801e8 100644
--- a/riseoftodmoldren/assets.py
+++ b/riseoftodmoldren/assets.py
@@ -6,22 +6,42 @@ asset_base = pathlib.Path(__file__).parent.absolute() / 'res'
class ImageAsset:
- def __init__(self, path: str):
- self.internal = pygame.image.load(path)
- self.internal.convert()
+ def __init__(self, path: str, lazy=True):
+ self._path = path
+ self._internal = None
+ if not lazy:
+ self.internal.get_size()
+
+ @property
+ def internal(self):
+ if self._internal is None:
+ self._internal = pygame.image.load(self._path)
+ self._internal.convert()
+ return self._internal
@classmethod
- def load(cls, name: str) -> 'ImageAsset':
+ def load_lazy(cls, name: str) -> 'ImageAsset':
asset_path = asset_base / name
return cls(str(asset_path))
def as_tiled_surface(self, size: pygame.Rect) -> pygame.SurfaceType:
- surface = pygame.Surface(size.size)
+ surface = pygame.Surface(size.size, pygame.SRCALPHA, 32)
for x in range(0, size.width, self.internal.get_width()):
for y in range(0, size.height, self.internal.get_height()):
surface.blit(self.internal, (x, y))
return surface
+ def as_surface(self) -> pygame.SurfaceType:
+ surface = pygame.Surface(self.internal.get_size(), pygame.SRCALPHA, 32)
+ surface.blit(self.internal, (0, 0))
+ return surface
+
+ def as_basic_sprite(self, **kwargs) -> pygame.sprite.Sprite:
+ sprite = pygame.sprite.Sprite()
+ sprite.image = self.internal
+ sprite.rect = sprite.image.get_rect(**kwargs)
+ return sprite
+
class SoundAsset:
def __init__(self, path: str):
@@ -35,4 +55,14 @@ class SoundAsset:
return cls(str(asset_path))
-MENU2 = ImageAsset.load('menu2.gif')
+MENU2 = ImageAsset.load_lazy('menu2.gif')
+FOREST1 = ImageAsset.load_lazy('forest1.gif')
+FOREST2 = ImageAsset.load_lazy('forest2.gif')
+FOREST3 = ImageAsset.load_lazy('forest3.gif')
+FOREST4 = ImageAsset.load_lazy('forest4.gif')
+FOREST5 = ImageAsset.load_lazy('forest5.gif')
+FOREST6 = ImageAsset.load_lazy('forest6.gif')
+FOREST7 = ImageAsset.load_lazy('forest7.gif')
+FOREST8 = ImageAsset.load_lazy('forest8.gif')
+SACK = ImageAsset.load_lazy('sack.gif')
+HEAL_POTION_ICON = ImageAsset.load_lazy('heal_potion_icon.gif')
diff --git a/riseoftodmoldren/base.py b/riseoftodmoldren/base.py
index c3549e5..c67fd04 100644
--- a/riseoftodmoldren/base.py
+++ b/riseoftodmoldren/base.py
@@ -64,6 +64,9 @@ class State:
def on_start(self, app: 'StateApp'):
pass
+ def on_keydown(self, app: 'StateApp', key):
+ pass
+
class StateApp(BaseApp):
@@ -73,6 +76,15 @@ class StateApp(BaseApp):
def on_event(self, event):
self.states[-1].on_event(self, event)
+ if event.type == pygame.QUIT:
+ self.should_close = True
+ if event.type == pygame.KEYDOWN:
+ self.on_keydown(event.key)
+
+ def on_keydown(self, key):
+ if key == pygame.K_ESCAPE:
+ self.should_close = True
+ self.states[-1].on_keydown(self, key)
def on_start(self):
self.states[-1].on_start(self)
diff --git a/riseoftodmoldren/screens/main.py b/riseoftodmoldren/screens/main.py
new file mode 100644
index 0000000..3ce0645
--- /dev/null
+++ b/riseoftodmoldren/screens/main.py
@@ -0,0 +1,19 @@
+from .. import assets
+from ..base import State, StateApp
+
+
+class MainScreen(State):
+ background = None
+ sack = None
+ potion = None
+
+ def on_start(self, app: 'StateApp'):
+ if self.background is None:
+ self.background = assets.FOREST1.as_tiled_surface(app.screen_rect)
+ self.sack = assets.SACK.as_surface()
+ self.potion = assets.HEAL_POTION_ICON.as_surface()
+
+ def on_render(self, app: 'StateApp'):
+ app.screen.blit(self.background, (0, 0))
+ app.screen.blit(self.sack, (25, 600))
+ app.screen.blit(self.potion, (760, 435))
diff --git a/riseoftodmoldren/screens/start.py b/riseoftodmoldren/screens/start.py
index cc33a47..7951a5c 100644
--- a/riseoftodmoldren/screens/start.py
+++ b/riseoftodmoldren/screens/start.py
@@ -1,17 +1,23 @@
+import pygame
+
+from .main import MainScreen
+from .. import assets
from ..base import State, StateApp
class StartScreen(State):
- def __init__(self):
- self.background = None
+ background = None
def on_start(self, app: 'StateApp'):
if self.background is None:
- from .. import assets
self.background = assets.MENU2.as_tiled_surface(app.screen_rect)
+ def on_keydown(self, app: 'StateApp', key):
+ app.replace_state(MainScreen())
+
def on_event(self, app: 'StateApp', event):
- pass
+ if event.type == pygame.MOUSEBUTTONUP:
+ app.replace_state(MainScreen())
def on_render(self, app: 'StateApp'):
app.screen.blit(self.background, (0, 0))