added selection sprite, moved clock out of game.py and into individual screens

This commit is contained in:
Nicholas Dyer 2023-05-11 14:03:16 -04:00
parent 7b8898b77b
commit 4e14204392
3 changed files with 90 additions and 15 deletions

View File

@ -46,8 +46,6 @@ def game():
icon = pygame.image.load(script_dir + '/icon/icon.png').convert_alpha()
pygame.display.set_icon(icon)
clock = pygame.time.Clock()
# Default game state when the game first starts.
running = True
@ -58,7 +56,6 @@ def game():
last_input_tick = 0
while running:
clock.tick(game_fps)
surface.update()
frame = pygame.transform.scale(surface, (screen_size, screen_size))
window.blit(frame, frame.get_rect())

View File

@ -1,5 +1,7 @@
import pygame
from . import sprites
from pocket_friends.hardware.gpio_handler import Constants
from ..classes.input_handler import InputHandler
class Surface(pygame.Surface):
@ -8,8 +10,12 @@ class Surface(pygame.Surface):
self.name = 'egg_select'
self.running = True
self.next_surface = None
self.resource_dir = resources_dir
self.clock = pygame.time.Clock()
self.game_fps = game_fps
self.input_handler = InputHandler(self.clock)
self.bg = pygame.image.load(resources_dir + '/images/bg.png').convert_alpha()
self.bg = pygame.image.load(self.resource_dir + '/images/bg.png').convert_alpha()
self.sprites = pygame.sprite.Group()
egg_list = [
@ -17,23 +23,23 @@ class Surface(pygame.Surface):
'blue',
'rainbow'
]
eggs = []
self.eggs = []
for egg in egg_list:
eggs.append(sprites.SelectionEgg(egg, resources_dir))
self.eggs.append(sprites.SelectionEgg(egg, self.resource_dir))
eggs_per_row = 3
distance_between_eggs = 36 / eggs_per_row
self.eggs_per_row = 3
distance_between_eggs = 36 / self.eggs_per_row
# Count the total rows.
total_rows = -(-len(eggs) // eggs_per_row)
distance_between_rows = 32 / eggs_per_row
self.total_rows = -(-len(self.eggs) // self.eggs_per_row)
distance_between_rows = 32 / self.eggs_per_row
# Determine the location of each egg.
for egg in eggs:
current_row = eggs.index(egg) // eggs_per_row
rows_after = total_rows - (current_row + 1)
egg_in_row = eggs.index(egg) % eggs_per_row
eggs_after = min(len(eggs) - (current_row * eggs_per_row), eggs_per_row) - (egg_in_row + 1)
for egg in self.eggs:
current_row = self.eggs.index(egg) // self.eggs_per_row
rows_after = self.total_rows - (current_row + 1)
egg_in_row = self.eggs.index(egg) % self.eggs_per_row
eggs_after = min(len(self.eggs) - (current_row * self.eggs_per_row), self.eggs_per_row) - (egg_in_row + 1)
x_offset = 32
y_offset = 30
@ -50,7 +56,77 @@ class Surface(pygame.Surface):
# Add the egg to the sprite list.
self.sprites.add(egg)
self.selected_egg = 0
self.selected_color = ''
def get_cursor_coords(self):
"""
Gets the coordinates of an egg on the selection screen by index and returns it as a tuple
:return: tuple of the coordinates of the selected egg
"""
cursor_x_offset = -2
cursor_y_offset = -2
return (self.eggs[self.selected_egg].rect.x + cursor_x_offset,
self.eggs[self.selected_egg].rect.y + cursor_y_offset)
def sel_left(self):
"""
Select the egg to the left with constraints.
"""
if self.selected_egg % self.eggs_per_row != 0:
self.selected_egg -= 1
def sel_right(self):
"""
Select the egg to the right with constraints.
"""
row = self.selected_egg // self.eggs_per_row
eggs_in_row = min(len(self.eggs) - (row * self.eggs_per_row), self.eggs_per_row)
if self.selected_egg % self.eggs_per_row != eggs_in_row - 1:
self.selected_egg += 1
def sel_up(self):
"""
Select the egg above with constraints.
"""
if self.selected_egg // self.eggs_per_row != 0:
self.selected_egg -= self.eggs_per_row
def sel_down(self):
"""
Select the egg below with constraints.
"""
if self.selected_egg // self.eggs_per_row != self.total_rows - 1:
self.selected_egg += self.eggs_per_row
def update(self):
self.clock.tick(self.game_fps)
self.blit(self.bg, (0, 0))
self.sprites.update()
self.sprites.draw(self)
self.input_handler.update()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == Constants.buttons.get('j_r'):
self.sel_right()
if event.key == Constants.buttons.get('j_l'):
self.sel_left()
if event.key == Constants.buttons.get('j_d'):
self.sel_down()
if event.key == Constants.buttons.get('j_u'):
self.sel_up()
if event.key == Constants.buttons.get('a'):
pass
cursor = pygame.image.load(
self.resource_dir + '/images/gui/egg_selector.png').convert_alpha()
self.blit(cursor, self.get_cursor_coords())

View File

@ -8,6 +8,7 @@ class Surface(pygame.Surface):
self.name = 'title'
self.running = True
self.next_surface = None
self.clock = pygame.time.Clock()
self.bg = pygame.image.load(resources_dir + '/images/bg.png').convert_alpha()
self.title = pygame.image.load(resources_dir + '/images/title.png').convert_alpha()
@ -16,6 +17,7 @@ class Surface(pygame.Surface):
self.delay = 1
def update(self):
self.clock.tick(self.game_fps)
self.blit(self.bg, (0, 0))
self.blit(self.title, (0, 0))