class MainMenu(game.State): def __init__(self): super(MainMenu, self).__init__() self.title = Line((game.screen_size[0] / 2, game.screen_size[1] / 4), "Houd de knop ingedrukt", game.get_font('title')) self.cursor = Cursor() self.start_button = Button( (game.screen_size[0] / 2, game.screen_size[1] / 2), self.start_button_pressed) game.global_state_changed.subscribe(self.global_state_changed) def update(self, delta): self.title.update(delta) self.cursor.update(delta) self.start_button.update(delta) def draw(self, surface): self.title.draw(surface) self.start_button.draw(surface) self.cursor.draw(surface) def start_button_pressed(self): game.set_global_state('tutorial') def global_state_changed(self, previous_state, new_state): if new_state is 'main_menu': self.cursor.set_position((0, 0)) self.cursor.cursor_up()
class Tutorial(game.State): def __init__(self): super(Tutorial, self).__init__() self.title = Line((game.screen_size[0] / 2, 150), "Tutorial", game.get_font('title')) self.cursor = Cursor() self.hand_screen = HandScreen( (3 * game.screen_size[0] / 4, game.screen_size[1] / 2)) self.set_letter() game.global_state_changed.subscribe(self.global_state_changed) def update(self, delta): self.title.update(delta) self.cursor.update(delta) self.hand_screen.update(delta) self.pose_tutorial.update(delta) if not self.pose_tutorial.is_active: game.set_global_state('game') def draw(self, surface): self.title.draw(surface) self.hand_screen.draw(surface) self.pose_tutorial.draw(surface) self.cursor.draw(surface) def global_state_changed(self, previous_state, new_state): if new_state is 'tutorial': self.cursor.set_position((0, 0)) self.cursor.cursor_up() self.set_letter() def set_letter(self): self.letter = random.sample(game.get_property('available_letters'), 1)[0] self.title.set_text('De letter %s' % self.letter.upper()) self.pose_tutorial = PoseTutorial( (game.screen_size[0] / 4, game.screen_size[1] / 2), self.hand_screen.get_pose, self.letter)
class Cursor(Actor): """Block that takes input from the detector and passes this to the BlockManager""" def __init__(self, position, listener, activator, index=0): super(Cursor, self).__init__(position) self.listener = listener self.activator = activator self.index = index self.key = None self.set_image(game.get_image('cursor_block')) self.text = Line((64, 72), self.key, game.get_font('screen_large')) self.text.set_color(pg.Color(62, 255, 4)) self.start_pos = np.array(position) self.state = 'input' self.state_time = 0.0 self.progress_time = 0 self.load_icon = AnimatedActor((64, 64), mode='custom') self.load_icon.set_animation(game.get_animation('load'), (64, 64), 20) self.load_icon_rect = self.load_icon.surface.get_rect() game.key_down.subscribe(self.key_down) def update(self, delta): super(Cursor, self).update(delta) if self.state == 'input': if self.activator() == self.key: if self.progress_time < 1: self.progress_time += delta else: self.progress_time = 0 self.listener(self.index) else: if self.progress_time > 0: self.progress_time -= delta if self.progress_time < 0: self.progress_time = 0 elif self.progress_time > 1: self.progress_time = 1 elif self.state == 'change_mode': self.position[1] = self.start_pos[1] + 16 * np.sin( 2 * np.pi * self.state_time) if self.state_time >= 0.5: self.position = self.start_pos.copy() self.set_state('wait') self.load_icon.frame_time = self.progress_time self.load_icon.update(delta) self.text.update(delta) self.state_time += delta def draw(self, surface): if not self.is_active: return self.rect.center = self.position self.surface.fill(pg.Color(0, 0, 0, 0)) self.surface.blit(self.image, self.image_rect) self.text.draw(self.surface) self.load_icon.draw(self.surface) surface.blit(self.surface, self.rect) # TODO: Create detector activator def key_down(self, key): if self.state == 'input': if key == ord(self.key): self.listener(self.index) def set_key(self, key): self.key = key self.text.set_text(str(key)) def set_state(self, new_state): previous_state = self.state self.state = new_state self.state_time = 0
class HandScreen(Button): def __init__(self, position): super(Button, self).__init__(position) self.button_up_image = game.get_image('screen_empty') self.button_down_image = game.get_image('screen_active') self.set_image(self.button_up_image) self.is_pressed = False self.wait_time = 0 self.hand_surface = pg.Surface((256, 256)) self.hand_rect = self.hand_surface.get_rect() self.label_text = Line((315, 450), '?', game.get_font('screen_small')) self.label_text.set_color(pg.Color(50, 240, 0)) def update(self, delta): super(Button, self).update(delta) if self.cursor_over(): if not self.is_pressed: self.press() elif self.is_pressed: self.unpress() if self.is_pressed: self.label_text.set_text(detect.detector.get_current_pose()) self.label_text.update(delta) def draw(self, surface): if not self.is_active: return self.rect.center = self.position self.hand_rect.center = (256, 192) self.surface.fill(pg.Color(0, 0, 0, 0)) if self.is_pressed: frame = detect.detector.get_fixed_frame( (256, 256), self.position[::-1], (256, 256)) pg.surfarray.blit_array(self.hand_surface, frame) self.surface.blit(self.hand_surface, self.hand_rect) self.surface.blit(self.image, self.image_rect) for child in self.children: child.draw(surface) if self.is_pressed: self.label_text.draw(self.surface) surface.blit(self.surface, self.rect) def cursor_over(self): cursor_position = game.get_cursor_position() return ( cursor_position[0] >= self.position[0] - self.image.get_width() / 2 and cursor_position[0] <= self.position[0] + self.image.get_width() / 2 and cursor_position[1] >= self.position[1] - self.image.get_height() / 2 and cursor_position[1] <= self.position[1] + self.image.get_height() / 2) def press(self): self.is_pressed = True self.set_image(self.button_down_image) game.cursor_down(type(self)) def get_pose(self): if self.is_pressed: return detect.detector.get_current_pose() else: return None