def on_key_press(self, key, modifiers): """Called whenever a key is pressed. """ if key == arcadeplus.key.UP or key == arcadeplus.key.W: if self.physics_engine.can_jump(): self.player_sprite.change_y = PLAYER_JUMP_SPEED arcadeplus.play_sound(self.jump_sound) elif key == arcadeplus.key.LEFT or key == arcadeplus.key.A: self.player_sprite.change_x = -PLAYER_MOVEMENT_SPEED elif key == arcadeplus.key.RIGHT or key == arcadeplus.key.D: self.player_sprite.change_x = PLAYER_MOVEMENT_SPEED
def process_player_bullets(self): # Move the bullets self.player_bullet_list.update() # Loop through each bullet for bullet in self.player_bullet_list: # Check this bullet to see if it hit a enemy hit_list = arcadeplus.check_for_collision_with_list( bullet, self.shield_list) # If it did, get rid of the bullet if len(hit_list) > 0: bullet.remove_from_sprite_lists() for shield in hit_list: shield.remove_from_sprite_lists() continue # Check this bullet to see if it hit a enemy hit_list = arcadeplus.check_for_collision_with_list( bullet, self.enemy_list) # If it did, get rid of the bullet if len(hit_list) > 0: bullet.remove_from_sprite_lists() # For every enemy we hit, add to the score and remove the enemy for enemy in hit_list: enemy.remove_from_sprite_lists() self.score += 1 # Hit Sound arcadeplus.play_sound(self.hit_sound) # If the bullet flies off-screen, remove it. if bullet.bottom > SCREEN_HEIGHT: bullet.remove_from_sprite_lists()
def on_mouse_press(self, x, y, button, modifiers): """ Called whenever the mouse button is clicked. """ # Gunshot sound arcadeplus.play_sound(self.gun_sound) # Create a bullet bullet = arcadeplus.Sprite( ":resources:images/space_shooter/laserBlue01.png", SPRITE_SCALING_LASER) # The image points to the right, and we want it to point up. So # rotate it. bullet.angle = 90 # Give the bullet a speed bullet.change_y = BULLET_SPEED # Position the bullet bullet.center_x = self.player_sprite.center_x bullet.bottom = self.player_sprite.top # Add the bullet to the appropriate lists self.bullet_list.append(bullet)
def on_update(self, delta_time): """ Movement and game logic """ # Move the player with the physics engine self.physics_engine.update() # Update animations if self.physics_engine.can_jump(): self.player_sprite.can_jump = False else: self.player_sprite.can_jump = True if self.physics_engine.is_on_ladder() and not self.physics_engine.can_jump(): self.player_sprite.is_on_ladder = True self.process_keychange() else: self.player_sprite.is_on_ladder = False self.process_keychange() self.coin_list.update_animation(delta_time) self.background_list.update_animation(delta_time) self.player_list.update_animation(delta_time) # Update walls, used with moving platforms self.wall_list.update() # See if the moving wall hit a boundary and needs to reverse direction. for wall in self.wall_list: if wall.boundary_right and wall.right > wall.boundary_right and wall.change_x > 0: wall.change_x *= -1 if wall.boundary_left and wall.left < wall.boundary_left and wall.change_x < 0: wall.change_x *= -1 if wall.boundary_top and wall.top > wall.boundary_top and wall.change_y > 0: wall.change_y *= -1 if wall.boundary_bottom and wall.bottom < wall.boundary_bottom and wall.change_y < 0: wall.change_y *= -1 # See if we hit any coins coin_hit_list = arcadeplus.check_for_collision_with_list(self.player_sprite, self.coin_list) # Loop through each coin we hit (if any) and remove it for coin in coin_hit_list: # Figure out how many points this coin is worth if 'Points' not in coin.properties: print("Warning, collected a coin without a Points property.") else: points = int(coin.properties['Points']) self.score += points # Remove the coin coin.remove_from_sprite_lists() arcadeplus.play_sound(self.collect_coin_sound) # Track if we need to change the viewport changed_viewport = False # --- Manage Scrolling --- # Scroll left left_boundary = self.view_left + LEFT_VIEWPORT_MARGIN if self.player_sprite.left < left_boundary: self.view_left -= left_boundary - self.player_sprite.left changed_viewport = True # Scroll right right_boundary = self.view_left + SCREEN_WIDTH - RIGHT_VIEWPORT_MARGIN if self.player_sprite.right > right_boundary: self.view_left += self.player_sprite.right - right_boundary changed_viewport = True # Scroll up top_boundary = self.view_bottom + SCREEN_HEIGHT - TOP_VIEWPORT_MARGIN if self.player_sprite.top > top_boundary: self.view_bottom += self.player_sprite.top - top_boundary changed_viewport = True # Scroll down bottom_boundary = self.view_bottom + BOTTOM_VIEWPORT_MARGIN if self.player_sprite.bottom < bottom_boundary: self.view_bottom -= bottom_boundary - self.player_sprite.bottom changed_viewport = True if changed_viewport: # Only scroll to integers. Otherwise we end up with pixels that # don't line up on the screen self.view_bottom = int(self.view_bottom) self.view_left = int(self.view_left) # Do the scrolling arcadeplus.set_viewport(self.view_left, SCREEN_WIDTH + self.view_left, self.view_bottom, SCREEN_HEIGHT + self.view_bottom)
def update(self, delta_time): """ Movement and game logic """ # Move the player with the physics engine self.physics_engine.update() # See if we hit any coins coin_hit_list = arcadeplus.check_for_collision_with_list( self.player_sprite, self.coin_list) # Loop through each coin we hit (if any) and remove it for coin in coin_hit_list: # Remove the coin coin.remove_from_sprite_lists() # Play a sound arcadeplus.play_sound(self.collect_coin_sound) # Add one to the score self.score += 1 # Track if we need to change the viewport changed_viewport = False # Did the player fall off the map? if self.player_sprite.center_y < -100: self.player_sprite.center_x = PLAYER_START_X self.player_sprite.center_y = PLAYER_START_Y # Set the camera to the start self.view_left = 0 self.view_bottom = 0 changed_viewport = True arcadeplus.play_sound(self.game_over) # Did the player touch something they should not? if arcadeplus.check_for_collision_with_list(self.player_sprite, self.dont_touch_list): self.player_sprite.change_x = 0 self.player_sprite.change_y = 0 self.player_sprite.center_x = PLAYER_START_X self.player_sprite.center_y = PLAYER_START_Y # Set the camera to the start self.view_left = 0 self.view_bottom = 0 changed_viewport = True arcadeplus.play_sound(self.game_over) # See if the user got to the end of the level if self.player_sprite.center_x >= self.end_of_map: # Advance to the next level self.level += 1 # Load the next level self.setup(self.level) # Set the camera to the start self.view_left = 0 self.view_bottom = 0 changed_viewport = True # --- Manage Scrolling --- # Scroll left left_boundary = self.view_left + LEFT_VIEWPORT_MARGIN if self.player_sprite.left < left_boundary: self.view_left -= left_boundary - self.player_sprite.left changed_viewport = True # Scroll right right_boundary = self.view_left + SCREEN_WIDTH - RIGHT_VIEWPORT_MARGIN if self.player_sprite.right > right_boundary: self.view_left += self.player_sprite.right - right_boundary changed_viewport = True # Scroll up top_boundary = self.view_bottom + SCREEN_HEIGHT - TOP_VIEWPORT_MARGIN if self.player_sprite.top > top_boundary: self.view_bottom += self.player_sprite.top - top_boundary changed_viewport = True # Scroll down bottom_boundary = self.view_bottom + BOTTOM_VIEWPORT_MARGIN if self.player_sprite.bottom < bottom_boundary: self.view_bottom -= bottom_boundary - self.player_sprite.bottom changed_viewport = True if changed_viewport: # Only scroll to integers. Otherwise we end up with pixels that # don't line up on the screen self.view_bottom = int(self.view_bottom) self.view_left = int(self.view_left) # Do the scrolling arcadeplus.set_viewport(self.view_left, SCREEN_WIDTH + self.view_left, self.view_bottom, SCREEN_HEIGHT + self.view_bottom)
def on_update(self, delta_time): """ Movement and game logic """ # Call update on all sprites (The sprites don't do much in this # example though.) self.physics_engine.update() # See if we hit any coins coin_hit_list = arcadeplus.check_for_collision_with_list( self.player_sprite, self.coin_list) # Loop through each coin we hit (if any) and remove it for coin in coin_hit_list: # Remove the coin coin.remove_from_sprite_lists() # Play a sound arcadeplus.play_sound(self.collect_coin_sound) # Add one to the score self.score += 1 # --- Manage Scrolling --- # Track if we need to change the viewport changed = False # Scroll left left_boundary = self.view_left + LEFT_VIEWPORT_MARGIN if self.player_sprite.left < left_boundary: self.view_left -= left_boundary - self.player_sprite.left changed = True # Scroll right right_boundary = self.view_left + SCREEN_WIDTH - RIGHT_VIEWPORT_MARGIN if self.player_sprite.right > right_boundary: self.view_left += self.player_sprite.right - right_boundary changed = True # Scroll up top_boundary = self.view_bottom + SCREEN_HEIGHT - TOP_VIEWPORT_MARGIN if self.player_sprite.top > top_boundary: self.view_bottom += self.player_sprite.top - top_boundary changed = True # Scroll down bottom_boundary = self.view_bottom + BOTTOM_VIEWPORT_MARGIN if self.player_sprite.bottom < bottom_boundary: self.view_bottom -= bottom_boundary - self.player_sprite.bottom changed = True if changed: # Only scroll to integers. Otherwise we end up with pixels that # don't line up on the screen self.view_bottom = int(self.view_bottom) self.view_left = int(self.view_left) # Do the scrolling arcadeplus.set_viewport(self.view_left, SCREEN_WIDTH + self.view_left, self.view_bottom, SCREEN_HEIGHT + self.view_bottom)
""" Sound Demo If Python and arcadeplus are installed, this example can be run from the command line with: python -m arcadeplus.examples.sound """ import arcadeplus import os # Set the working directory (where we expect to find files) to the same # directory this .py file is in. You can leave this out of your own # code, but it is needed to easily run the examples using "python -m" # as mentioned at the top of this program. file_path = os.path.dirname(os.path.abspath(__file__)) os.chdir(file_path) arcadeplus.open_window(300, 300, "Sound Demo") laser_sound = arcadeplus.load_sound(":resources:sounds/laser1.wav") arcadeplus.play_sound(laser_sound) arcadeplus.run()
def on_mouse_press(x, y, button, modifiers): global frames, current_frame, linked_scenes, captured global chosen_color_column, chosen_shape_column, chosen_color_row, chosen_shape_row global start_x, start_y, end_x, end_y global snd_btn_press # Determine what to do based on the location of the user's click if 100 < x < 900: start_x = x start_y = y captured[current_frame - 1] = False elif x <= 50: if 450 <= y <= 750: arcadeplus.play_sound(snd_btn_press) chosen_shape_row = y // 50 + 1 chosen_shape_column = 1 elif y <= 450: arcadeplus.play_sound(snd_btn_press) chosen_color_row = y // 50 + 1 chosen_color_column = 1 elif x <= 100: if 450 <= y <= 750: arcadeplus.play_sound(snd_btn_press) chosen_shape_row = y // 50 + 1 chosen_shape_column = 2 elif y <= 450: arcadeplus.play_sound(snd_btn_press) chosen_color_row = y // 50 + 1 chosen_color_column = 2 elif x >= 950: if 450 < y < 500: gui.theme('Dark Blue 3') layout = [[gui.Text("Scenes:")], [gui.Listbox(values=list(list_scenes()), size=(30, 6))], [gui.Text("New Scene Name:"), gui.InputText()], [gui.Button('Create New Scene'), gui.Button('Cancel')]] window = gui.Window('AnimationCreator', layout) while True: event, values = window.read() if event in (None, 'Cancel'): break if str(values[1]).strip() != "": image = arcadeplus.get_image(100, 0, 800, 800) image.save(f"data/scenes/{str(values[1]).strip()}.png", "PNG") linked_scenes[current_frame - 1] = str(values[1]).strip() frames[current_frame - 1] = arcadeplus.ShapeElementList() print('New Scene Created From Current Frame:', str(values[1]).strip()) break window.close() elif 500 < y < 550: arcadeplus.play_sound(snd_btn_press) frames.append(arcadeplus.ShapeElementList()) current_frame = len(frames) print("New Frame Created") captured.append(False) elif 550 < y < 600: arcadeplus.play_sound(snd_btn_press) frames[current_frame - 1] = arcadeplus.ShapeElementList() if (current_frame - 1) in linked_scenes: del linked_scenes[current_frame - 1] print("Current Frame Cleared") captured[current_frame - 1] = False elif 600 < y < 650: arcadeplus.play_sound(snd_btn_press) if current_frame < len(frames): current_frame += 1 print("Forward Frame") else: print("Cannot Forward Frame - Reached End of Timeline") elif x >= 900: if 450 < y < 500: gui.theme('Dark Blue 3') layout = [[gui.Text("Scenes:")], [gui.Listbox(values=list(list_scenes()), size=(30, 6))], [gui.Button('Load Scene'), gui.Button('Cancel')]] window = gui.Window('AnimationCreator', layout) while True: event, values = window.read() if event in (None, 'Cancel'): break if str(values[0])[2:-2] != "": linked_scenes[current_frame - 1] = str(values[0])[2:-2] frames[current_frame - 1] = arcadeplus.ShapeElementList() print('Loaded Scene:', str(values[0])[2:-2]) break window.close() captured[current_frame - 1] = False elif 500 < y < 550: arcadeplus.play_sound(snd_btn_press) if len(frames) > 1: del frames[current_frame - 1] if str(current_frame - 1) in linked_scenes.keys(): del linked_scenes[current_frame - 1] print("Deleted Current Frame") captured[current_frame - 1] = False for i in range(current_frame - 1, len(captured)): captured[i] = False if current_frame > 1: current_frame -= 1 else: current_frame = 1 else: print("Cannot Delete Frame - At Least One Frame Must Exist") elif 550 < y < 600: arcadeplus.play_sound(snd_btn_press) try: frames[current_frame - 1].remove(frames[current_frame - 1][-1]) print("Last Drawing on Current Frame Undone") captured[current_frame - 1] = False except: print("Cannot Undo Last Drawing on Frame - No Moves to Undo") pass elif 600 < y < 650: arcadeplus.play_sound(snd_btn_press) if current_frame > 1: current_frame -= 1 print("Backward Frame") else: print("Cannot Backward Frame - Reached Beginning of Timeline") # Capture functionality if x > 900 and y > 750: arcadeplus.play_sound(snd_btn_press) image = arcadeplus.get_image(100, 0, 800, 800) current_frame_name = (10 - len(str(current_frame))) * "0" + str( current_frame) image.save(f"data/frames/{current_frame_name}.png", "PNG") print("Captured Frame") captured[current_frame - 1] = True # Render functionality if x < 100 and y > 750: if all(captured): gui.theme('Dark Blue 3') layout = [[ gui.Text("Frames per Image (positive integer): "), gui.InputText("20") ], [gui.Button('Start Render'), gui.Button('Cancel')]] window = gui.Window('AnimationCreator', layout) while True: event, values = window.read() if event in (None, 'Cancel'): break if event in (None, 'Start Render'): if str(values[0]) != "" and str(values[0]).isdigit(): print( f"Rendering at {int(values[0])} frames per image..." ) render_video.run(int(values[0]), len(captured)) else: print(f"Rendering at 20 frames per image...") render_video.run(20, len(captured)) print("Render Complete") break window.close() else: gui.theme('Dark Blue 3') layout = [[gui.Text("Please Capture All Frames Before Rendering")], [gui.Button('Okay')]] window = gui.Window('AnimationCreator', layout) while True: event, values = window.read() if event in (None, 'Okay'): break window.close() # About button functionality if x > 900 and y < 50: gui.theme('Dark Blue 3') layout = [ [gui.Text("AnimationCreator was created by George Shao")], [ gui.Text( "Find out more at: https://github.com/GeorgeShao/AnimationCreator" ) ], [gui.Button('Okay')] ] window = gui.Window('AnimationCreator', layout) while True: event, values = window.read() if event in (None, 'Okay'): break window.close()