title screen moves on now to sprite selection

This commit is contained in:
Nicholas Dyer 2023-05-11 13:09:55 -04:00
parent 2ffd9bd929
commit 296b6df4a0
4 changed files with 51 additions and 3 deletions

View File

@ -2,7 +2,17 @@ import pygame
import os import os
from pathlib import Path from pathlib import Path
import pocket_friends import pocket_friends
from pocket_friends.game_files.surfaces import title import importlib
valid_surfaces = [
'title',
'egg_select'
]
surface_modules = {}
for module in valid_surfaces:
surface_modules[module] = importlib.import_module('pocket_friends.game_files.surfaces.{0}'.format(module))
print('imported ' + module)
# FPS for the entire game to run at. # FPS for the entire game to run at.
game_fps = 16 game_fps = 16
@ -30,7 +40,7 @@ def game():
pygame.mouse.set_visible(False) pygame.mouse.set_visible(False)
pygame.display.set_caption('Pocket Friends {0}'.format(pocket_friends.__version__)) pygame.display.set_caption('Pocket Friends {0}'.format(pocket_friends.__version__))
window = pygame.display.set_mode((screen_size, screen_size)) window = pygame.display.set_mode((screen_size, screen_size))
surface = globals()[starting_surface].Surface((game_res, game_res), resources_dir) surface = surface_modules.get(starting_surface).Surface((game_res, game_res), resources_dir, game_fps)
# Add an icon to the pygame window. # Add an icon to the pygame window.
icon = pygame.image.load(script_dir + '/icon/icon.png').convert_alpha() icon = pygame.image.load(script_dir + '/icon/icon.png').convert_alpha()
@ -53,6 +63,12 @@ def game():
frame = pygame.transform.scale(surface, (screen_size, screen_size)) frame = pygame.transform.scale(surface, (screen_size, screen_size))
window.blit(frame, frame.get_rect()) window.blit(frame, frame.get_rect())
if not surface.running:
next_surface = surface.next_surface
if next_surface not in valid_surfaces:
raise Exception('Given surface is not listed in valid surfaces!')
surface = surface_modules.get(next_surface).Surface((game_res, game_res), resources_dir, game_fps)
print(surface.name)
pygame.display.flip() pygame.display.flip()

View File

@ -0,0 +1,6 @@
import os
__all__ = []
for module in os.listdir(os.path.dirname(os.path.abspath(__file__))):
if module != '__init__.py' and module[-3:] == '.py':
__all__.append(module)

View File

@ -0,0 +1,17 @@
import pygame
class Surface(pygame.Surface):
def __init__(self, window_size, resources_dir, game_fps):
super().__init__(window_size, pygame.SRCALPHA)
self.name = 'egg_select'
self.running = True
self.next_surface = None
self.bg = pygame.image.load(resources_dir + '/images/bg.png').convert_alpha()
self.sprites = pygame.sprite.Group()
def update(self):
self.blit(self.bg, (0, 0))
self.sprites.update()
self.sprites.draw(self)

View File

@ -1,16 +1,25 @@
import pygame import pygame
import time
class Surface(pygame.Surface): class Surface(pygame.Surface):
def __init__(self, window_size, resources_dir): def __init__(self, window_size, resources_dir, game_fps):
super().__init__(window_size, pygame.SRCALPHA) super().__init__(window_size, pygame.SRCALPHA)
self.name = 'title'
self.running = True self.running = True
self.next_surface = None self.next_surface = None
self.bg = pygame.image.load(resources_dir + '/images/bg.png').convert_alpha() self.bg = pygame.image.load(resources_dir + '/images/bg.png').convert_alpha()
self.title = pygame.image.load(resources_dir + '/images/title.png').convert_alpha() self.title = pygame.image.load(resources_dir + '/images/title.png').convert_alpha()
self.frames = 1
self.game_fps = game_fps
self.delay = 1
def update(self): def update(self):
self.blit(self.bg, (0, 0)) self.blit(self.bg, (0, 0))
self.blit(self.title, (0, 0)) self.blit(self.title, (0, 0))
self.frames += 1
if self.frames > self.game_fps * self.delay:
self.next_surface = 'egg_select'
self.running = False