Last active
April 3, 2016 22:11
-
-
Save awesomebytes/1b8779f8dd881c2e66eb760082d615f9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/env python | |
| """Paint a black background (for a projector to not project), | |
| over it paint white circles with a red number in them | |
| identifying the order of it. | |
| When hitting RIGHT key you advance apose | |
| if you did it by mistake you can press LEFT to go back. | |
| When you hit the last one the time will stop in green. | |
| TODO: scoring | |
| TODO: dynamic sizes | |
| TODO: move circles once set | |
| TODO: webcam based auto check the poses | |
| Up to now video: https://youtu.be/7sC72jNlcoo""" | |
| import sys | |
| import pygame | |
| import time | |
| BLACK = 0, 0, 0 | |
| WHITE = 255, 255, 255 | |
| RED = 255, 0, 0 | |
| GREEN = 0, 255, 0 | |
| # TODO: | |
| # Must be able to tune up the size of | |
| # Circle untouched | |
| # Circle touched | |
| # font size | |
| # screen size | |
| # dynamic reconfigure? | |
| # Must be able to store the route we created | |
| # this can be done just storing the list of circles | |
| # parameters | |
| # Must be able to move the circles once set | |
| # this implies quite a rework | |
| # Using a webcam | |
| # We can try to give the collision box of the climber | |
| # if the light is stable enough | |
| # so it automatically detects the climber touching | |
| # the things | |
| class CirclePainter(object): | |
| def __init__(self, window_size=(1024 * 2, 768 * 2), | |
| fontsize=64, circle_radius=100): | |
| pygame.init() | |
| self.window_size = window_size | |
| self.fontsize = fontsize | |
| self.cradius = 100 | |
| self.cradius_tiny = 40 | |
| self.screen = pygame.display.set_mode(self.window_size) | |
| pygame.display.set_caption('Circle with number on click') | |
| self.font = pygame.font.Font(None, fontsize) | |
| self.score_size = fontsize * 2 | |
| self.score_font = pygame.font.Font(None, self.score_size) | |
| self.score_pos = 50, 50 | |
| self.circles_list = [] | |
| self.circles_count = 0 | |
| self.current_circle = 0 | |
| self.ini_time = 0.0 | |
| self.game_started = False | |
| self.game_finished = False | |
| self.fin_time = 0.0 | |
| def circle_with_text(self, pos, radius, circle_color, font_color, text): | |
| circle_rect = pygame.draw.circle(self.screen, | |
| circle_color, | |
| pos, | |
| radius, | |
| 0) # width, 0 means filled | |
| # Write the text in the center | |
| center_circle = (circle_rect[0] + radius, | |
| circle_rect[1] + radius) | |
| # But take into account fount size | |
| center_circle = (center_circle[0] - self.fontsize / 4, | |
| center_circle[1] - self.fontsize / 4) | |
| text = self.font.render(str(text), 1, font_color) | |
| self.screen.blit(text, center_circle) | |
| def time_text(self, pos, font_color, t_end): | |
| t = t_end - self.ini_time | |
| t_str = str(round(t, 2)) | |
| text = self.score_font.render(t_str, 1, font_color) | |
| self.screen.blit(text, self.score_pos) | |
| def run(self): | |
| while True: | |
| for event in pygame.event.get(): | |
| if event.type == pygame.QUIT: | |
| sys.exit() | |
| if event.type == pygame.MOUSEBUTTONDOWN: | |
| self.circles_count += 1 | |
| pos = pygame.mouse.get_pos() | |
| this_circle_params = [pos, self.cradius, | |
| WHITE, RED, | |
| str(self.circles_count)] | |
| self.circles_list.append(this_circle_params) | |
| # When pressing right key we turn the next circle text to green | |
| if event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT: | |
| if self.current_circle < len(self.circles_list): | |
| self.circles_list[self.current_circle][3] = GREEN | |
| self.circles_list[self.current_circle][ | |
| 1] = self.cradius_tiny | |
| self.current_circle += 1 | |
| if self.current_circle == 1: | |
| self.game_started = True | |
| self.ini_time = time.time() | |
| # When getting the last one mark as finished and show time | |
| if self.current_circle == len(self.circles_list): | |
| self.game_finished = True | |
| self.fin_time = time.time() | |
| if event.type == pygame.KEYDOWN and event.key == pygame.K_LEFT: | |
| if self.current_circle > 0: | |
| self.current_circle -= 1 | |
| self.circles_list[self.current_circle][3] = RED | |
| self.circles_list[self.current_circle][ | |
| 1] = self.cradius | |
| if event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN: | |
| self.ini_time = time.time() | |
| # Escape resets game | |
| if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE: | |
| self.ini_time = 0.0 | |
| self.game_finished = False | |
| self.game_started = False | |
| self.fin_time = 0.0 | |
| self.current_circle = 0 | |
| for idx in range(0, len(self.circles_list)): | |
| self.circles_list[idx][3] = RED | |
| self.circles_list[idx][1] = self.cradius | |
| self.screen.fill(BLACK) | |
| # Paint all circles | |
| for c in self.circles_list: | |
| self.circle_with_text(*c) | |
| # Paint time | |
| if not self.game_finished: | |
| if not self.game_started: | |
| self.time_text(self.score_pos, WHITE, 0.0) | |
| else: | |
| self.time_text(self.score_pos, WHITE, time.time()) | |
| else: | |
| self.time_text(self.score_pos, GREEN, self.fin_time) | |
| pygame.display.flip() | |
| if __name__ == '__main__': | |
| pygame.init() | |
| cp = CirclePainter() | |
| cp.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment