def on_update(self, delta_time): """ Movement and game logic """ for coin in self.coin_list: coin.center_x += coin.change_x walls_hit = arcadeplus.check_for_collision_with_list( coin, self.wall_list) for wall in walls_hit: if coin.change_x > 0: coin.right = wall.left elif coin.change_x < 0: coin.left = wall.right if len(walls_hit) > 0: coin.change_x *= -1 coin.center_y += coin.change_y walls_hit = arcadeplus.check_for_collision_with_list( coin, self.wall_list) for wall in walls_hit: if coin.change_y > 0: coin.top = wall.bottom elif coin.change_y < 0: coin.bottom = wall.top if len(walls_hit) > 0: coin.change_y *= -1
def process_enemy_bullets(self): # Move the bullets self.enemy_bullet_list.update() # Loop through each bullet for bullet in self.enemy_bullet_list: # Check this bullet to see if it hit a shield hit_list = arcadeplus.check_for_collision_with_list( bullet, self.shield_list) # If it did, get rid of the bullet and shield blocks if len(hit_list) > 0: bullet.remove_from_sprite_lists() for shield in hit_list: shield.remove_from_sprite_lists() continue # See if the player got hit with a bullet if arcadeplus.check_for_collision_with_list( self.player_sprite, self.enemy_bullet_list): self.game_state = GAME_OVER # If the bullet falls off the screen get rid of it if bullet.top < 0: bullet.remove_from_sprite_lists()
def _circular_check(player, walls): """ This is a horrible kludge to 'guess' our way out of a collision Returns: """ original_x = player.center_x original_y = player.center_y vary = 1 while True: try_list = [[original_x, original_y + vary], [original_x, original_y - vary], [original_x + vary, original_y], [original_x - vary, original_y], [original_x + vary, original_y + vary], [original_x + vary, original_y - vary], [original_x - vary, original_y + vary], [original_x - vary, original_y - vary]] for my_item in try_list: x, y = my_item player.center_x = x player.center_y = y check_hit_list = check_for_collision_with_list(player, walls) # print(f"Vary {vary} ({self.player_sprite.center_x} {self.player_sprite.center_y}) " # f"= {len(check_hit_list)}") if len(check_hit_list) == 0: return vary *= 2
def on_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 # --- 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)
def on_update(self, delta_time): self.time_taken += delta_time # Call update on all sprites (The sprites don't do much in this # example though.) self.coin_list.update() self.player_list.update() # Generate a list of all sprites that collided with the player. hit_list = arcadeplus.check_for_collision_with_list(self.player_sprite, self.coin_list) # Loop through each colliding sprite, remove it, and add to the # score. for coin in hit_list: coin.kill() self.score += 1 self.window.total_score += 1 # If we've collected all the games, then move to a "GAME_OVER" # state. if len(self.coin_list) == 0: game_over_view = GameOverView() game_over_view.time_taken = self.time_taken self.window.set_mouse_visible(True) self.window.show_view(game_over_view)
def can_jump(self, y_distance=5) -> bool: """ Method that looks to see if there is a floor under the player_sprite. If there is a floor, the player can jump and we return a True. :returns: True if there is a platform below us :rtype: bool """ # Move down to see if we are on a platform self.player_sprite.center_y -= y_distance # Check for wall hit hit_list = check_for_collision_with_list(self.player_sprite, self.platforms) self.player_sprite.center_y += y_distance if len(hit_list) > 0: self.jumps_since_ground = 0 if len( hit_list ) > 0 or self.allow_multi_jump and self.jumps_since_ground < self.allowed_jumps: return True else: return False
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.coin_list.update() # Generate a list of all sprites that collided with the player. coins_hit_list = arcadeplus.check_for_collision_with_list( self.player_sprite, self.coin_list) # Loop through each colliding sprite to set intensity=bright for coin in coins_hit_list: coin.intensity = 'bright' coin.alpha = 255 hit_trigger = arcadeplus.check_for_collision(self.player_sprite, self.trigger_sprite) if hit_trigger: intense_sprites = [ sprite for sprite in self.coin_list if sprite.intensity == 'bright' ] for coin in intense_sprites: coin.remove_from_sprite_lists()
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.player_list.update() self.coin_list.update() # Generate a list of all sprites that collided with the player. hit_list = arcadeplus.check_for_collision_with_list( self.player_sprite, self.coin_list) # Loop through each colliding sprite, change it, and add to the score. for coin in hit_list: # Have we collected this? if not coin.changed: # No? Then do so coin.append_texture( arcadeplus.load_texture( ":resources:images/pinball/bumper.png")) coin.set_texture(1) coin.changed = True coin.width = 30 coin.height = 30 self.score += 1
def on_update(self, delta_time): """ Movement and game logic """ # Call update on bullet sprites self.bullet_list.update() # Loop through each bullet for bullet in self.bullet_list: # Check this bullet to see if it hit a coin hit_list = arcadeplus.check_for_collision_with_list( bullet, self.coin_list) # If it did, get rid of the bullet if len(hit_list) > 0: bullet.remove_from_sprite_lists() # For every coin we hit, add to the score and remove the coin for coin in hit_list: coin.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 is_on_ladder(self): # Check for touching a ladder if self.ladders: hit_list = check_for_collision_with_list(self.player_sprite, self.ladders) if len(hit_list) > 0: return True return False
def on_update(self, delta_time): """ Movement and game logic """ if self.player_sprite.right >= self.end_of_map: self.game_over = True # Call update on all sprites (The sprites don't do much in this # example though.) if not self.game_over: self.physics_engine.update() coins_hit = arcadeplus.check_for_collision_with_list( self.player_sprite, self.coin_list) for coin in coins_hit: coin.remove_from_sprite_lists() self.score += 1 # --- Manage Scrolling --- # Track if we need to change the view port changed = False # Scroll left left_bndry = self.view_left + VIEWPORT_LEFT_MARGIN if self.player_sprite.left < left_bndry: self.view_left -= left_bndry - self.player_sprite.left changed = True # Scroll right right_bndry = self.view_left + SCREEN_WIDTH - VIEWPORT_RIGHT_MARGIN if self.player_sprite.right > right_bndry: self.view_left += self.player_sprite.right - right_bndry changed = True # Scroll up top_bndry = self.view_bottom + SCREEN_HEIGHT - VIEWPORT_MARGIN_TOP if self.player_sprite.top > top_bndry: self.view_bottom += self.player_sprite.top - top_bndry changed = True # Scroll down bottom_bndry = self.view_bottom + VIEWPORT_MARGIN_BOTTOM if self.player_sprite.bottom < bottom_bndry: self.view_bottom -= bottom_bndry - self.player_sprite.bottom changed = True # If we need to scroll, go ahead and do it. if changed: self.view_left = int(self.view_left) self.view_bottom = int(self.view_bottom) arcadeplus.set_viewport(self.view_left, SCREEN_WIDTH + self.view_left, self.view_bottom, SCREEN_HEIGHT + self.view_bottom)
def on_update(self, x): """ Move everything """ self.frame_count += 1 if not self.game_over: self.all_sprites_list.update() for bullet in self.bullet_list: asteroids_plain = arcadeplus.check_for_collision_with_list( bullet, self.asteroid_list) asteroids_spatial = arcadeplus.check_for_collision_with_list( bullet, self.asteroid_list) if len(asteroids_plain) != len(asteroids_spatial): print("ERROR") asteroids = asteroids_spatial for asteroid in asteroids: self.split_asteroid( cast(AsteroidSprite, asteroid )) # expected AsteroidSprite, got Sprite instead asteroid.remove_from_sprite_lists() bullet.remove_from_sprite_lists() if not self.player_sprite.respawning: asteroids = arcadeplus.check_for_collision_with_list( self.player_sprite, self.asteroid_list) if len(asteroids) > 0: if self.lives > 0: self.lives -= 1 self.player_sprite.respawn() self.split_asteroid(cast(AsteroidSprite, asteroids[0])) asteroids[0].remove_from_sprite_lists() self.ship_life_list.pop().remove_from_sprite_lists() print("Crash") else: self.game_over = True print("Game over")
def update(self, delta_time): self.coin_list.update() self.character_list.update() self.frame_counter += 1 if self.frame_counter == 5: self.character_list.pop() self.coin_list[0].scale = 2.0 coin_hit_list = arcadeplus.check_for_collision_with_list( self.character_sprite, self.coin_list) for coin in coin_hit_list: coin.kill()
def on_update(self, delta_time): """ Movement and game logic """ for coin in self.coin_list: coin.follow_sprite(self.player_sprite) # Generate a list of all sprites that collided with the player. hit_list = arcadeplus.check_for_collision_with_list( self.player_sprite, self.coin_list) # Loop through each colliding sprite, remove it, and add to the score. for coin in hit_list: coin.kill() self.score += 1
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.coin_list.update() # Generate a list of all sprites that collided with the player. hit_list = arcadeplus.check_for_collision_with_list(self.player_sprite, self.coin_list) # Loop through each colliding sprite, remove it, and add to the score. for coin in hit_list: coin.remove_from_sprite_lists() self.score += 1
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_update(self, delta_time): """ Movement and game logic """ # Call update on bullet sprites self.bullet_list.update() self.explosions_list.update() # Loop through each bullet for bullet in self.bullet_list: # Check this bullet to see if it hit a coin hit_list = arcadeplus.check_for_collision_with_list( bullet, self.coin_list) # If it did... if len(hit_list) > 0: # Make an explosion explosion = Explosion(self.explosion_texture_list) # Move it to the location of the coin explosion.center_x = hit_list[0].center_x explosion.center_y = hit_list[0].center_y # Call update() because it sets which image we start on explosion.update() # Add to a list of sprites that are explosions self.explosions_list.append(explosion) # Get rid of the bullet bullet.remove_from_sprite_lists() # For every coin we hit, add to the score and remove the coin for coin in hit_list: coin.remove_from_sprite_lists() self.score += 1 # Hit Sound arcadeplus.sound.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_update(self, delta_time): self.tick += 1 if self.game_over: return self.bullet_cooldown += 1 for enemy in self.enemy_list: cast(Enemy, enemy).follow_sprite(self.player) if self.joy: # Joystick input - movement move_x, move_y, move_angle = get_joy_position( self.joy.move_stick_x, self.joy.move_stick_y) if move_angle: self.player.change_x = move_x * MOVEMENT_SPEED self.player.change_y = move_y * MOVEMENT_SPEED self.player.angle = move_angle + ROTATE_OFFSET else: self.player.change_x = 0 self.player.change_y = 0 # Joystick input - shooting shoot_x, shoot_y, shoot_angle = get_joy_position( self.joy.shoot_stick_x, self.joy.shoot_stick_y) if shoot_angle: self.spawn_bullet(shoot_angle) else: # Keyboard input - shooting if self.player.shoot_right_pressed and self.player.shoot_up_pressed: self.spawn_bullet(0 + 45) elif self.player.shoot_up_pressed and self.player.shoot_left_pressed: self.spawn_bullet(90 + 45) elif self.player.shoot_left_pressed and self.player.shoot_down_pressed: self.spawn_bullet(180 + 45) elif self.player.shoot_down_pressed and self.player.shoot_right_pressed: self.spawn_bullet(270 + 45) elif self.player.shoot_right_pressed: self.spawn_bullet(0) elif self.player.shoot_up_pressed: self.spawn_bullet(90) elif self.player.shoot_left_pressed: self.spawn_bullet(180) elif self.player.shoot_down_pressed: self.spawn_bullet(270) self.enemy_list.update() self.player.update() self.bullet_list.update() ship_death_hit_list = arcadeplus.check_for_collision_with_list( self.player, self.enemy_list) if len(ship_death_hit_list) > 0: self.game_over = True for bullet in self.bullet_list: bullet_killed = False enemy_shot_list = arcadeplus.check_for_collision_with_list( bullet, self.enemy_list) # Loop through each colliding sprite, remove it, and add to the score. for enemy in enemy_shot_list: enemy.remove_from_sprite_lists() bullet.remove_from_sprite_lists() bullet_killed = True self.score += 1 if bullet_killed: continue
def setup(self): """ Set up the game and initialize the variables. """ # Sprite lists self.all_sprites_list = arcadeplus.SpriteList() self.wall_list = arcadeplus.SpriteList() self.coin_list = arcadeplus.SpriteList() # Set up the player self.player_sprite = arcadeplus.Sprite( ":resources:images/animated_characters/female_person/femalePerson_idle.png", SPRITE_SCALING) self.player_sprite.center_x = 50 self.player_sprite.center_y = 64 # -- Set up the walls # Create a series of horizontal walls for y in range(0, 800, 200): for x in range(100, 700, 64): wall = arcadeplus.Sprite( ":resources:images/tiles/boxCrate_double.png", SPRITE_SCALING) wall.center_x = x wall.center_y = y self.wall_list.append(wall) # -- Randomly place coins where there are no walls # Create the coins for i in range(NUMBER_OF_COINS): # Create the coin instance # Coin image from kenney.nl coin = arcadeplus.Sprite(":resources:images/items/coinGold.png", SPRITE_SCALING_COIN) # --- IMPORTANT PART --- # Boolean variable if we successfully placed the coin coin_placed_successfully = False # Keep trying until success while not coin_placed_successfully: # Position the coin coin.center_x = random.randrange(SCREEN_WIDTH) coin.center_y = random.randrange(SCREEN_HEIGHT) # See if the coin is hitting a wall wall_hit_list = arcadeplus.check_for_collision_with_list( coin, self.wall_list) # See if the coin is hitting another coin coin_hit_list = arcadeplus.check_for_collision_with_list( coin, self.coin_list) if len(wall_hit_list) == 0 and len(coin_hit_list) == 0: # It is! coin_placed_successfully = True # Add the coin to the lists self.coin_list.append(coin) # --- END OF IMPORTANT PART --- self.physics_engine = arcadeplus.PhysicsEngineSimple( self.player_sprite, self.wall_list) # Set the background color arcadeplus.set_background_color(arcadeplus.color.AMAZON)
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 _move_sprite(moving_sprite: Sprite, walls: SpriteList, ramp_up: bool): # Rotate moving_sprite.angle += moving_sprite.change_angle hit_list = check_for_collision_with_list(moving_sprite, walls) if len(hit_list) > 0: # Resolve any collisions by this weird kludge _circular_check(moving_sprite, walls) # --- Move in the y direction moving_sprite.center_y += moving_sprite.change_y # Check for wall hit hit_list_x = check_for_collision_with_list(moving_sprite, walls) # print(f"Post-y move {hit_list_x}") complete_hit_list = hit_list_x # If we hit a wall, move so the edges are at the same point if len(hit_list_x) > 0: if moving_sprite.change_y > 0: while len(check_for_collision_with_list(moving_sprite, walls)) > 0: moving_sprite.center_y -= 1 # print(f"Spot X ({self.player_sprite.center_x}, {self.player_sprite.center_y})" # f" {self.player_sprite.change_y}") elif moving_sprite.change_y < 0: # Reset number of jumps for item in hit_list_x: while check_for_collision(moving_sprite, item): # self.player_sprite.bottom = item.top <- Doesn't work for ramps moving_sprite.center_y += 0.25 if item.change_x != 0: moving_sprite.center_x += item.change_x # print(f"Spot Y ({self.player_sprite.center_x}, {self.player_sprite.center_y})") else: pass # TODO: The code below can't execute, as "item" doesn't # exist. In theory, this condition should never be arrived at. # Collision while player wasn't moving, most likely # moving platform. # if self.player_sprite.center_y >= item.center_y: # self.player_sprite.bottom = item.top # else: # self.player_sprite.top = item.bottom moving_sprite.change_y = min(0.0, hit_list_x[0].change_y) # print(f"Spot D ({self.player_sprite.center_x}, {self.player_sprite.center_y})") moving_sprite.center_y = round(moving_sprite.center_y, 2) # print(f"Spot Q ({self.player_sprite.center_x}, {self.player_sprite.center_y})") # --- Move in the x direction moving_sprite.center_x += moving_sprite.change_x check_again = True while check_again: check_again = False # Check for wall hit hit_list_y = check_for_collision_with_list(moving_sprite, walls) complete_hit_list = hit_list_x for sprite in hit_list_y: if sprite not in complete_hit_list: complete_hit_list.append(sprite) # If we hit a wall, move so the edges are at the same point if len(hit_list_y) > 0: change_x = moving_sprite.change_x if change_x > 0: if ramp_up: for _ in hit_list_y: # print(f"Spot 1 ({self.player_sprite.center_x}, {self.player_sprite.center_y})") # See if we can "run up" a ramp moving_sprite.center_y += change_x if len( check_for_collision_with_list( moving_sprite, walls)) > 0: # No, ramp run-up doesn't work. moving_sprite.center_y -= change_x moving_sprite.center_x -= 1 # print(f"Spot R ({self.player_sprite.center_x}, {self.player_sprite.center_y})") check_again = True break # else: # print("Run up ok 1") # print(f"Spot 2 ({self.player_sprite.center_x}, {self.player_sprite.center_y})") else: while len( check_for_collision_with_list( moving_sprite, walls)) > 0: moving_sprite.center_x -= 1 elif change_x < 0: if ramp_up: for item in hit_list_y: # See if we can "run up" a ramp moving_sprite.center_y -= change_x if len( check_for_collision_with_list( moving_sprite, walls)) > 0: # Can't run up the ramp, reverse moving_sprite.center_y += change_x moving_sprite.left = max(item.right, moving_sprite.left) # print(f"Reverse 1 {item.right}, {self.player_sprite.left}") # Ok, if we were shoved back to the right, we need to check this whole thing again. check_again = True break # print(f"Spot 4 ({self.player_sprite.center_x}, {self.player_sprite.center_y})") else: while len( check_for_collision_with_list( moving_sprite, walls)) > 0: moving_sprite.center_x += 1 else: print( "Error, x collision while player wasn't moving.\n" "Make sure you aren't calling multiple updates, like " "a physics engine update and an all sprites list update.") # print(f"Spot E ({self.player_sprite.center_x}, {self.player_sprite.center_y})") return complete_hit_list
def setup(self): self.wall_list = arcadeplus.SpriteList(use_spatial_hash=True) self.player_list = arcadeplus.SpriteList() # Create cave system using a 2D grid self.grid = create_grid(GRID_WIDTH, GRID_HEIGHT) initialize_grid(self.grid) for step in range(NUMBER_OF_STEPS): self.grid = do_simulation_step(self.grid) # Create sprites based on 2D grid if not MERGE_SPRITES: # This is the simple-to-understand method. Each grid location # is a sprite. for row in range(GRID_HEIGHT): for column in range(GRID_WIDTH): if self.grid[row][column] == 1: wall = arcadeplus.Sprite( ":resources:images/tiles/grassCenter.png", SPRITE_SCALING) wall.center_x = column * SPRITE_SIZE + SPRITE_SIZE / 2 wall.center_y = row * SPRITE_SIZE + SPRITE_SIZE / 2 self.wall_list.append(wall) else: # This uses new arcadeplus 1.3.1 features, that allow me to create a # larger sprite with a repeating texture. So if there are multiple # cells in a row with a wall, we merge them into one sprite, with a # repeating texture for each cell. This reduces our sprite count. for row in range(GRID_HEIGHT): column = 0 while column < GRID_WIDTH: while column < GRID_WIDTH and self.grid[row][column] == 0: column += 1 start_column = column while column < GRID_WIDTH and self.grid[row][column] == 1: column += 1 end_column = column - 1 column_count = end_column - start_column + 1 column_mid = (start_column + end_column) / 2 wall = arcadeplus.Sprite( ":resources:images/tiles/grassCenter.png", SPRITE_SCALING, repeat_count_x=column_count) wall.center_x = column_mid * SPRITE_SIZE + SPRITE_SIZE / 2 wall.center_y = row * SPRITE_SIZE + SPRITE_SIZE / 2 wall.width = SPRITE_SIZE * column_count self.wall_list.append(wall) # Set up the player self.player_sprite = arcadeplus.Sprite( ":resources:images/animated_characters/female_person/femalePerson_idle.png", SPRITE_SCALING) self.player_list.append(self.player_sprite) # Randomly place the player. If we are in a wall, repeat until we aren't. placed = False while not placed: # Randomly position max_x = GRID_WIDTH * SPRITE_SIZE max_y = GRID_HEIGHT * SPRITE_SIZE self.player_sprite.center_x = random.randrange(max_x) self.player_sprite.center_y = random.randrange(max_y) # Are we in a wall? walls_hit = arcadeplus.check_for_collision_with_list( self.player_sprite, self.wall_list) if len(walls_hit) == 0: # Not in a wall! Success! placed = True self.physics_engine = arcadeplus.PhysicsEngineSimple( self.player_sprite, self.wall_list)
def setup(self): """ Set up the game and initialize the variables. """ # Sprite lists self.player_list = arcadeplus.SpriteList() self.wall_list = arcadeplus.SpriteList() self.score = 0 # Create the maze maze = make_maze_depth_first(MAZE_WIDTH, MAZE_HEIGHT) # Create sprites based on 2D grid if not MERGE_SPRITES: # This is the simple-to-understand method. Each grid location # is a sprite. for row in range(MAZE_HEIGHT): for column in range(MAZE_WIDTH): if maze[row][column] == 1: wall = arcadeplus.Sprite( ":resources:images/tiles/grassCenter.png", SPRITE_SCALING) wall.center_x = column * SPRITE_SIZE + SPRITE_SIZE / 2 wall.center_y = row * SPRITE_SIZE + SPRITE_SIZE / 2 self.wall_list.append(wall) else: # This uses new arcadeplus 1.3.1 features, that allow me to create a # larger sprite with a repeating texture. So if there are multiple # cells in a row with a wall, we merge them into one sprite, with a # repeating texture for each cell. This reduces our sprite count. for row in range(MAZE_HEIGHT): column = 0 while column < len(maze): while column < len(maze) and maze[row][column] == 0: column += 1 start_column = column while column < len(maze) and maze[row][column] == 1: column += 1 end_column = column - 1 column_count = end_column - start_column + 1 column_mid = (start_column + end_column) / 2 wall = arcadeplus.Sprite( ":resources:images/tiles/grassCenter.png", SPRITE_SCALING, repeat_count_x=column_count) wall.center_x = column_mid * SPRITE_SIZE + SPRITE_SIZE / 2 wall.center_y = row * SPRITE_SIZE + SPRITE_SIZE / 2 wall.width = SPRITE_SIZE * column_count self.wall_list.append(wall) # Set up the player self.player_sprite = arcadeplus.Sprite( ":resources:images/animated_characters/female_person/femalePerson_idle.png", SPRITE_SCALING) self.player_list.append(self.player_sprite) # Randomly place the player. If we are in a wall, repeat until we aren't. placed = False while not placed: # Randomly position self.player_sprite.center_x = random.randrange(MAZE_WIDTH * SPRITE_SIZE) self.player_sprite.center_y = random.randrange(MAZE_HEIGHT * SPRITE_SIZE) # Are we in a wall? walls_hit = arcadeplus.check_for_collision_with_list( self.player_sprite, self.wall_list) if len(walls_hit) == 0: # Not in a wall! Success! placed = True self.physics_engine = arcadeplus.PhysicsEngineSimple( self.player_sprite, self.wall_list) # Set the background color arcadeplus.set_background_color(arcadeplus.color.AMAZON) # Set the viewport boundaries # These numbers set where we have 'scrolled' to. self.view_left = 0 self.view_bottom = 0 print(f"Total wall blocks: {len(self.wall_list)}")