def init_rpi_buttons(gpio_pin_numbers): # Initialize Raspberry Pi input pins gpio_buttons = [] from gpiozero import Button from classes.GpioButton import GpioButton # Here you can configure which pins you use on your Raspberry Pi gpio_pins = gpio_pin_numbers # Max 4 pins bounce_time_in_sec = 0.1 gpio_buttons.append( GpioButton(Button(gpio_pins[0], bounce_time=bounce_time_in_sec))) gpio_buttons.append( GpioButton(Button(gpio_pins[1], bounce_time=bounce_time_in_sec))) gpio_buttons.append( GpioButton(Button(gpio_pins[2], bounce_time=bounce_time_in_sec))) gpio_buttons.append( GpioButton(Button(gpio_pins[3], bounce_time=bounce_time_in_sec))) print('The following pins are configured as (gpio) button inputs:', gpio_pins) return gpio_buttons
def gameIntro(): introSurface = pygame.Surface(screen.get_size()) introSurface = introSurface.convert() introSurface.fill(Colors.BLACK) introText = FONT.render(gv.GAME_CAPTION, True, Colors.WHITE) introTextRect = introText.get_rect(center=(gv.SCREEN_WIDTH / 2, gv.SCREEN_HEIGHT * 0.25)) introSurface.blit(introText, introTextRect) while gv.INTRO: for event in pygame.event.get(): if event.type == pygame.QUIT: gv.INTRO = False pygame.quit() os._exit(1) playButton = Button("btn_play") playButton.drawRectangleButton(introSurface, gv.SCREEN_WIDTH / 2, gv.SCREEN_HEIGHT / 2, 100, 50, "PLAY", FONT, Colors.WHITE, Colors.GREEN, gameStart) i = 0 while i < gv.LEVEL_COUNT: levelButton = Button(i + 1) if i + 1 != gv.SELECTED_LEVEL[0]: levelButton.drawCircleButton( introSurface, Colors.WHITE, gv.START_X + (i * ((2 * gv.RADIUS) + (2 * gv.BUTTON_MARGIN))), gv.START_Y, gv.RADIUS, 0, FONT, str(i + 1), Colors.BLACK) else: levelButton.drawCircleButton( introSurface, Colors.GREEN, gv.START_X + (i * ((2 * gv.RADIUS) + (2 * gv.BUTTON_MARGIN))), gv.START_Y, gv.RADIUS, 0, FONT, str(i + 1), Colors.BLACK) i += 1 text = FONT.render("Last Score: " + str(gv.LAST_SCORE), True, Colors.WHITE) textRect = text.get_rect(center=(gv.SURFACE_WIDTH / 2, 350)) introSurface.blit(text, textRect) screen.blit(introSurface, (0, 0)) pygame.display.flip() pygame.display.update()
def end_screen(): # Keeps track of all sprites to be updated every frame allsprites = pygame.sprite.Group() # Song to be used in game. Only one can be used. song = song_library.example_song_short # Short random song for debugging # song = song_library.example_song_long # Ode To Joy # Create game_state instance, this holds all required game info game_state = GameState(allsprites, song) loop = True replayButton = Button(500, 500, 200, 45, ' Replay', game_state.restart, song.get_font_filename(), allsprites, game_state) mainMenuButton = Button(650, 500, 200, 45, ' Main menu', game_state.menu_start, song.get_font_filename(), allsprites, game_state) top5 = score.get_top5_high_score() while loop: text_font.render_to(screen, (590, 200), "Highscores:", (153, 204, 255)) eventlist = pygame.event.get() for event in eventlist: # Checks if a mouse is clicked if event.type == pygame.MOUSEBUTTONDOWN: replayButton.check_click() mainMenuButton.check_click() quitButton.check_click() # This calls the update() function on all sprites allsprites.update() # Draw Everything screen.blit(game_state.get_background(), (0, 0)) # First draw a new background allsprites.draw(screen) # Next draw all updated sprites pygame.display.update() # Finally render everything to the display
from util.GPIOFuckUp import GPIOFuckUp # Alle GPIO pinnen worden op false gezet GPIOFuckUp() # Board mode. GPIO.setmode(GPIO.BOARD) # Instantieer de .env. load_dotenv(find_dotenv()) # Instantieer afstandsensor. afstandsensor = Afstandsensor(int(os.environ.get("DISTANCE_SENSOR_PIN"))) # Instantieer knop. button = Button(int(os.environ.get("BUTTON_PIN"))) # Instantieer ledjes. led_green = Led(int(os.environ.get("LED_GREEN_PIN"))) led_yellow = Led(int(os.environ.get("LED_YELLOW_PIN"))) led_red = Led(int(os.environ.get("LED_RED_PIN"))) # Instantieer reader. reader = Reader() # Instantieer display. display = Scherm() # Instantieer API. API = API(os.environ.get("BASE_URL"), os.environ.get("PRIVATE_KEY"))
def main(): # Initialize pygame pygame.init() # Set screen size. Don't change this unless you know what you are doing! screen = pygame.display.set_mode((1280, 720)) # Set the window title pygame.display.set_caption("Sweden - Interaction Hero") # Keeps track of all sprites to be updated every frame allsprites = pygame.sprite.Group() # Song to be used in game. Only one can be used. #song = song_library.example_song_short # Short random song for debugging #song = song_library.example_song_long # Ode To Joy song = song_library.du_gamla_du_fria # Create game_state instance, this holds all required game info game_state = GameState(allsprites, song) # Checks if the program is running on a Raspberry Pi is_running_on_rpi = utils.is_running_on_rpi() if is_running_on_rpi: # Below are some pin input numbers, feel free to change them. However, # !!! ALWAYS READ THE PIN DOCUMENTATION CAREFULLY !!! # Pay special attention to the difference between GPIO pin numbers and BOARD pin numbers # For example GPIO17 is addressed 17 rather than 11 (See pin numbering diagram.) # https://gpiozero.readthedocs.io/en/stable/recipes.html#pin-numbering gpio_pin_numbers = [22, 23, 24, 27] # Max 4 pins gpio_buttons = init_rpi_buttons(gpio_pin_numbers) game_state.add_gpio_pins(gpio_pin_numbers) # Prepare game objects clock = pygame.time.Clock() startButton = Button(570, 200, 140, 40, ' Börja', game_state.restart, "menu", song.get_font_filename(), allsprites, game_state) quitButton = Button(570, 300, 140, 40, ' Sluta', quit, "menu", song.get_font_filename(), allsprites, game_state) scoreButton = Button(570, 250, 140, 40, ' Score', game_state.open_score_menu, "menu", song.get_font_filename(), allsprites, game_state) scoreBackButton = Button(570, 350, 140, 40, ' Bakåt', game_state.open_menu, "score", song.get_font_filename(), allsprites, game_state) highscoreLabel = Label("", 100, 220, True, 36, song.get_font_filename(), (255, 255, 255), "score", allsprites, game_state) # Main loop going = True while going: # Update the clock, argument is max fps clock.tick(60) # Every 'tick' or programcycle the gamestate update() is called game_state.update() # Get all events from the last cycle and store them as variable # This is stored as a variable because pygame.even.get() empties this list eventlist = pygame.event.get() # Check if there are any global quit events for event in eventlist: # If yes, the game loop won't start again if event.type == pygame.QUIT: going = False elif event.type == pygame.KEYDOWN and event.unicode == pygame.K_ESCAPE: going = False # This runs before the user starts the game if game_state.state == 'prestart': for event in eventlist: # Checks if a mouse is clicked if event.type == pygame.MOUSEBUTTONDOWN: startButton.check_click() quitButton.check_click() scoreButton.check_click() elif game_state.state == 'score': highscoreLabel.text = "Highscore: " + str( game_state.scoreHandler.get_high_score()) for event in eventlist: if event.type == pygame.MOUSEBUTTONDOWN: scoreBackButton.check_click() # This runs when the users starts a game elif game_state.state == 'playing': # Loop through all potential hitboxes for hitbox in game_state.hitboxes: # Every hitbox needs to check all events for event in eventlist: if event.type == pygame.KEYDOWN and event.unicode == hitbox.event_key: game_state.check_for_hit(hitbox) elif event.type == pygame.KEYUP: hitbox.unpunch() elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE: game_state.state = 'prestart' hitbox.destroy_all_notes() # When on RPi also check for GPIO input if is_running_on_rpi: for button in gpio_buttons: # When a buttons is pressed in this loop and wasn't pressed in the last loop if button.is_pressed( ) and button.gpio_key is hitbox.gpio_event_key and button.is_available( ): button.use( ) # Set the button as unavailable for the next loop game_state.check_for_hit(hitbox) # When a button was not pressed in this loop elif not button.is_pressed(): button.wake() # Set the button as available again hitbox.unpunch() # This calls the update() function on all sprites allsprites.update() # Draw Everything screen.blit(game_state.get_background(), (0, 0)) # First draw a new background allsprites.draw(screen) # Next draw all updated sprites pygame.display.update() # Finally render everything to the display
for y in range(0, 500, Mesh.CELL_SIZE): pygame.draw.line(screen, (20, 20, 20), (0, y), (500, y)) pygame.init() font = pygame.font.SysFont('pc_cga', Mesh.CELL_SIZE) timer = pygame.time.Clock() # Init surfaces window = pygame.display.set_mode((500 + Mesh.CELL_SIZE, 500)) menu = pygame.Surface((Mesh.CELL_SIZE, window.get_height())) # Init meshes mesh = Mesh((500, 500)) menu_mesh = Mesh(menu.get_size()) for y, item in enumerate([Button(), Repeater(), Lever(), Piston(), StickyPiston(), Redstone(), RedstoneBlock(), Solid()]): menu_mesh.set_at(0, y, item) # Cursor cursor_item = Empty() while True: # Key & mouse event for event in pygame.event.get(): if event.type == pygame.QUIT: exit() if event.type == pygame.MOUSEBUTTONDOWN: mouse_x, mouse_y = pygame.mouse.get_pos() if mouse_x <= 500: mouse_x //= Mesh.CELL_SIZE mouse_y //= Mesh.CELL_SIZE
def main(): # Initialize pygame pygame.init() # Set screen size. Don't change this unless you know what you are doing! screen = pygame.display.set_mode((1280, 720)) # Set the window title pygame.display.set_caption("IAT Challengeweek: Interaction Hero | China") # Keeps track of all sprites to be updated every frame allsprites = pygame.sprite.Group() text_font = pygame.freetype.Font("data/RobotoMono-VariableFont_wght.ttf", 34) # Song to be used in game. Only one can be used. song = song_library.example_song_short # Short random song for debugging # song = song_library.example_song_long # Ode To Joy # Create game_state instance, this holds all required game info game_state = GameState(allsprites, song) # Checks if the program is running on a Raspberry Pi is_running_on_rpi = utils.is_running_on_rpi() if is_running_on_rpi: # Below are some pin input numbers, feel free to change them. However, # !!! ALWAYS READ THE PIN DOCUMENTATION CAREFULLY !!! # Pay special attention to the difference between GPIO pin numbers and BOARD pin numbers # For example GPIO17 is addressed 17 rather than 11 (See pin numbering diagram.) # https://gpiozero.readthedocs.io/en/stable/recipes.html#pin-numbering gpio_pin_numbers = [2, 3, 4, 17] # Max 4 pins gpio_buttons = init_rpi_buttons(gpio_pin_numbers) game_state.add_gpio_pins(gpio_pin_numbers) # Prepare game objects clock = pygame.time.Clock() startButton = Button(500, 300, 140, 40, ' Start', game_state.restart, song.get_font_filename(), allsprites, game_state) quitButton = Button(500, 350, 140, 40, ' Quit', quit, song.get_font_filename(), allsprites, game_state) # easyButton = Button(450, 250, 140, 40, ' Easy', select_difficulty, song.get_font_filename(), allsprites, game_state) # normalButton = Button(600, 250, 140, 40, ' Normal', select_difficulty, song.get_font_filename(), allsprites, game_state) # hardButton = Button(750, 250, 140, 40, ' Hard', select_difficulty, song.get_font_filename(), allsprites, game_state) # Main loop going = True while going: # Update the clock, argument is max fps clock.tick(60) # Every 'tick' or programcycle the gamestate update() is called game_state.update() # Get all events from the last cycle and store them as variable # This is stored as a variable because pygame.even.get() empties this list eventlist = pygame.event.get() # Check if there are any global quit events for event in eventlist: # If yes, the game loop won't start again if event.type == pygame.QUIT: going = False elif event.type == pygame.KEYDOWN and event.unicode == pygame.K_ESCAPE: going = False # This runs before the user starts the game if game_state.state == 'prestart': for event in eventlist: # Checks if a mouse is clicked if event.type == pygame.MOUSEBUTTONDOWN: startButton.check_click() quitButton.check_click() # Difficulty buttons # easyButton.check_click() # normalButton.check_click() # hardButton.check_click() # This runs when the users starts a game elif game_state.state == 'playing': stopButton = Button(100, 100, 140, 40, ' Stop', game_state.menu_start, song.get_font_filename(), allsprites, game_state) # Loop through all potential hitboxes for hitbox in game_state.hitboxes: # Every hitbox needs to check all events for event in eventlist: if event.type == pygame.KEYDOWN and event.unicode == hitbox.event_key: game_state.check_for_hit(hitbox) elif event.type == pygame.KEYUP: hitbox.unpunch() elif event.type == pygame.MOUSEBUTTONDOWN: stopButton.check_click() # When on RPi also check for GPIO input if is_running_on_rpi: for button in gpio_buttons: # When a buttons is pressed in this loop and wasn't pressed in the last loop if button.is_pressed( ) and button.gpio_key is hitbox.gpio_event_key and button.is_available( ): button.use( ) # Set the button as unavailable for the next loop game_state.check_for_hit(hitbox) # When a button was not pressed in this loop elif not button.is_pressed(): button.wake() # Set the button as available again hitbox.unpunch() # elif game_state.state == 'end_game': # going = False # end_screen() # End state elif game_state.state == 'end_game': replayButton = Button(500, 500, 200, 45, ' Replay', game_state.restart, song.get_font_filename(), allsprites, game_state) mainMenuButton = Button(650, 500, 200, 45, ' Main menu', game_state.menu_start, song.get_font_filename(), allsprites, game_state) top5 = score.get_top5_high_score() text_font.render_to(screen, (500, 200), "top 5 Score:", (0, 0, 0)) height = 250 for count, top in enumerate(top5): h = height + 30 * count text_font.render_to(screen, (500, h), str(top), (0, 0, 0)) # text_font.render_to(screen, (500, 250), "ss", (153, 204, 255)) # [90, 70, 65, 60, 60] for event in eventlist: # Checks if a mouse is clicked if event.type == pygame.MOUSEBUTTONDOWN: replayButton.check_click() mainMenuButton.check_click() quitButton.check_click() # text_font.render_to(screen, (40, 350), "Hello World!", (0, 0, 0)) # This calls the update() function on all sprites allsprites.update() pygame.display.flip() # Draw Everything screen.blit(game_state.get_background(), (0, 0)) # First draw a new background allsprites.draw(screen) # Next draw all updated sprites pygame.display.update() # Finally render everything to the display