2023-05-11 13:09:55 -04:00
|
|
|
import pygame
|
2023-05-11 13:23:55 -04:00
|
|
|
from . import sprites
|
2023-05-11 14:08:22 -04:00
|
|
|
from pocket_friends.game_files.io_helpers.gpio_handler import Constants
|
2023-05-11 14:05:53 -04:00
|
|
|
from ..io_helpers.input_handler import InputHandler
|
2023-05-11 13:09:55 -04:00
|
|
|
|
|
|
|
|
|
|
|
class Surface(pygame.Surface):
|
2023-05-11 15:24:17 -04:00
|
|
|
def __init__(self, window_size, resources_dir, game_fps, **kwargs):
|
2023-05-11 13:09:55 -04:00
|
|
|
super().__init__(window_size, pygame.SRCALPHA)
|
|
|
|
self.name = 'egg_select'
|
|
|
|
self.running = True
|
|
|
|
self.next_surface = None
|
2023-05-11 14:03:16 -04:00
|
|
|
self.resource_dir = resources_dir
|
|
|
|
self.clock = pygame.time.Clock()
|
|
|
|
self.game_fps = game_fps
|
|
|
|
self.input_handler = InputHandler(self.clock)
|
2023-05-11 15:24:17 -04:00
|
|
|
self.additional_args = {}
|
2023-05-11 13:09:55 -04:00
|
|
|
|
2023-05-11 14:03:16 -04:00
|
|
|
self.bg = pygame.image.load(self.resource_dir + '/images/bg.png').convert_alpha()
|
2023-05-11 13:09:55 -04:00
|
|
|
self.sprites = pygame.sprite.Group()
|
|
|
|
|
2023-05-11 15:24:17 -04:00
|
|
|
preselected_color = None
|
|
|
|
for key in kwargs.keys():
|
2023-05-11 15:25:22 -04:00
|
|
|
if key == 'selected_color':
|
2023-05-11 15:24:17 -04:00
|
|
|
preselected_color = kwargs.get(key)
|
|
|
|
|
2023-05-11 13:23:55 -04:00
|
|
|
egg_list = [
|
|
|
|
'dev_egg',
|
|
|
|
'blue',
|
|
|
|
'rainbow'
|
|
|
|
]
|
2023-05-11 14:03:16 -04:00
|
|
|
self.eggs = []
|
2023-05-11 13:23:55 -04:00
|
|
|
for egg in egg_list:
|
2023-05-11 14:03:16 -04:00
|
|
|
self.eggs.append(sprites.SelectionEgg(egg, self.resource_dir))
|
2023-05-11 13:23:55 -04:00
|
|
|
|
2023-05-11 14:03:16 -04:00
|
|
|
self.eggs_per_row = 3
|
|
|
|
distance_between_eggs = 36 / self.eggs_per_row
|
2023-05-11 13:23:55 -04:00
|
|
|
|
|
|
|
# Count the total rows.
|
2023-05-11 14:03:16 -04:00
|
|
|
self.total_rows = -(-len(self.eggs) // self.eggs_per_row)
|
|
|
|
distance_between_rows = 32 / self.eggs_per_row
|
2023-05-11 13:23:55 -04:00
|
|
|
|
|
|
|
# Determine the location of each egg.
|
2023-05-11 14:03:16 -04:00
|
|
|
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)
|
2023-05-11 13:23:55 -04:00
|
|
|
|
|
|
|
x_offset = 32
|
|
|
|
y_offset = 30
|
|
|
|
|
|
|
|
# The x coordinate of an egg is determined by which egg in the row it is, and how many eggs
|
|
|
|
# are in that row. If there is only 1 egg in a row, it is in the middle of the screen. If
|
|
|
|
# there are two, they're on equal halves and so on.
|
|
|
|
x = x_offset - (eggs_after * distance_between_eggs) + (egg_in_row * distance_between_eggs)
|
|
|
|
y = y_offset - (rows_after * distance_between_rows) + (current_row * distance_between_rows)
|
|
|
|
|
|
|
|
egg.rect.x = x
|
|
|
|
egg.rect.y = y
|
|
|
|
|
|
|
|
# Add the egg to the sprite list.
|
|
|
|
self.sprites.add(egg)
|
|
|
|
|
2023-05-11 14:03:16 -04:00
|
|
|
self.selected_egg = 0
|
|
|
|
self.selected_color = ''
|
2023-05-11 15:24:17 -04:00
|
|
|
if preselected_color is not None:
|
|
|
|
self.selected_color = self.eggs[self.selected_egg].egg_color
|
|
|
|
for i in range(len(self.eggs)):
|
|
|
|
if self.eggs[i].egg_color == preselected_color:
|
|
|
|
self.selected_egg = i
|
2023-05-11 14:03:16 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2023-05-11 13:09:55 -04:00
|
|
|
def update(self):
|
2023-05-11 14:03:16 -04:00
|
|
|
self.clock.tick(self.game_fps)
|
|
|
|
|
2023-05-11 13:09:55 -04:00
|
|
|
self.blit(self.bg, (0, 0))
|
|
|
|
self.sprites.update()
|
|
|
|
self.sprites.draw(self)
|
2023-05-11 14:03:16 -04:00
|
|
|
|
|
|
|
self.input_handler.update()
|
|
|
|
|
2023-05-11 15:24:17 -04:00
|
|
|
cursor = pygame.image.load(
|
|
|
|
self.resource_dir + '/images/gui/egg_selector.png').convert_alpha()
|
|
|
|
self.blit(cursor, self.get_cursor_coords())
|
|
|
|
|
|
|
|
self.selected_color = self.eggs[self.selected_egg].egg_color
|
|
|
|
|
2023-05-11 14:03:16 -04:00
|
|
|
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'):
|
2023-05-11 15:24:17 -04:00
|
|
|
self.additional_args = {'selected_egg': self.selected_color}
|
|
|
|
self.next_surface = 'selection_info'
|
|
|
|
self.running = False
|