def animate(self, x): self.physics_engine.update() q = WINDOW_WIDTH / 4 if self.player_sprite.center_x - self.ortho_left > q * 3: self.ortho_left = self.player_sprite.center_x - q * 3 arcade.set_viewport(self.ortho_left, WINDOW_WIDTH + self.ortho_left, 0, WINDOW_HEIGHT) if self.player_sprite.center_x - self.ortho_left < q: self.ortho_left = self.player_sprite.center_x - q arcade.set_viewport(self.ortho_left, WINDOW_WIDTH + self.ortho_left, 0, WINDOW_HEIGHT) coins_hit = arcade.check_for_collision_with_list(self.player_sprite, self.coin_list) for coin in coins_hit: coin.kill() self.score += 1 arcade.draw_text("Score: {}".format(self.score), 5, 5, arcade.color.BLACK, 14)
def run(self): arcade.set_background_color((127, 127, 255)) arcade.set_viewport(self.ortho_left, WINDOW_WIDTH + self.ortho_left, 0, WINDOW_HEIGHT) self.setup_game() arcade.run()
def on_update(self, delta_time): self.physics_engine.update() self.coin_list.update() hit_list = arcade.check_for_collision_with_list( self.player_sprite, self.coin_list) if len(hit_list) > 0: for pening in hit_list: pening.remove_from_sprite_lists() self.score = self.score + 1 # --- Manage Scrolling --- # Keep track of if we changed the boundary. We don't want to call the # set_viewport command if we didn't change the view port. changed = False # Scroll left left_boundary = self.view_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 - 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 - 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 + VIEWPORT_MARGIN if self.player_sprite.bottom < bottom_boundary: self.view_bottom -= bottom_boundary - self.player_sprite.bottom changed = True # Make sure our boundaries are integer values. While the view port does # support floating point numbers, for this application we want every pixel # in the view port to map directly onto a pixel on the screen. We don't want # any rounding errors. self.view_left = int(self.view_left) self.view_bottom = int(self.view_bottom) # If we changed the boundary values, update the view port to match if changed: arcade.set_viewport(self.view_left, SCREEN_WIDTH + self.view_left - 1, self.view_bottom, SCREEN_HEIGHT + self.view_bottom - 1)
def set_viewport(self, left: float, right: float, bottom: float, top: float): """ Set the viewport. (What coordinates we can see. Used to scale and/or scroll the screen.) :param Number left: :param Number right: :param Number bottom: :param Number top: """ set_viewport(left, right, bottom, top)
def update(self, delta_time): super().update(delta_time) # --- Manage Scrolling --- # Track if we need to change the viewport changed = False # Scroll left left_boundary = self.view_left + LEFT_VIEWPORT_MARGIN if self.pc_sprite.left < left_boundary: self.view_left -= left_boundary - self.pc_sprite.left changed = True # Scroll right right_boundary = self.view_left + SCREEN_WIDTH - RIGHT_VIEWPORT_MARGIN if self.pc_sprite.right > right_boundary: self.view_left += self.pc_sprite.right - right_boundary changed = True # Scroll up top_boundary = self.view_bottom + SCREEN_HEIGHT - TOP_VIEWPORT_MARGIN if self.pc_sprite.top > top_boundary: self.view_bottom += self.pc_sprite.top - top_boundary changed = True # Scroll down bottom_boundary = self.view_bottom + BOTTOM_VIEWPORT_MARGIN if self.pc_sprite.bottom < bottom_boundary: self.view_bottom -= bottom_boundary - self.pc_sprite.bottom changed = True if changed: # Only scroll to integers. Otherwise we end up with pixels that # don't line up on the screen # Clamp the viewport to the level boundaries (vp_left, vp_right, vp_bottom, vp_top) = viewport = arcade.get_viewport() low_bottom = self.bottom high_bottom = self.top - (vp_top - vp_bottom) low_left = self.left high_left = self.right - (vp_right - vp_left) self.view_bottom = int( arcade.clamp(self.view_bottom, low_bottom, high_bottom)) self.view_left = int( arcade.clamp(self.view_left, low_left, high_left)) # Do the scrolling arcade.set_viewport(self.view_left, SCREEN_WIDTH + self.view_left, self.view_bottom, SCREEN_HEIGHT + self.view_bottom)
def setup(self): # set background color arcade.set_background_color(arcade.color.AMAZON) # Reset the view port self.view_left = 0 self.view_bottom = 0 # setup sprite lists self.player_list = arcade.SpriteList() self.enemy_list = arcade.SpriteList() self.player_bullet_list = arcade.SpriteList() self.enemy_bullet_list = arcade.SpriteList() self.wall_list = arcade.SpriteList() self.enemy_hp_list = arcade.SpriteList() # reset everything self.score = 0 self.player_hp = 100 self.game_state = 1 arcade.set_viewport(self.view_left, SCREEN_WIDTH + self.view_left - 1, self.view_bottom, SCREEN_HEIGHT + self.view_bottom - 1) # create the wall for i in range(len(self.map)): if self.map[i] == 1: wall = arcade.Sprite('image/wall.png', 1) wall.center_x = i % self.map_length * 100 + 50 wall.center_y = (len(self.map) - i - 1) // self.map_length * 100 + 50 self.wall_list.append(wall) # create AI for i in range(10): if randint(0, 2) == 0: bot = Archer() else: bot = Mage() hit_list = arcade.check_for_collision_with_list( bot, self.wall_list) while hit_list != [] or self.view_left <= bot.center_x <= self.view_left + SCREEN_WIDTH or self.view_bottom <= bot.center_y <= self.view_bottom + SCREEN_HEIGHT: if randint(0, 2) == 0: bot = Archer() else: bot = Mage() hit_list = arcade.check_for_collision_with_list( bot, self.wall_list) self.enemy_list.append(bot) hp_bar_sprite = arcade.Sprite('image/hp_bar.png', 1) self.enemy_hp_list.append(hp_bar_sprite)
def __init__(self): """ This is run once when we switch to this view """ super().__init__() self.texture = arcade.load_texture("maps/images/views/gameover.png") # load menu sounds self.select_sound = arcade.load_sound("sounds/select.wav") self.click_sound = arcade.load_sound("sounds/click.wav") # Reset the viewport, necessary if we have a scrolling game and we need # to reset the viewport back to the start so we can see what we draw. arcade.set_viewport(0, SCREEN_WIDTH - 1, 0, SCREEN_HEIGHT - 1) self.selected = 1
def on_draw(): arcade.start_render() # Draw in here... if SCREEN == "Main Menu": arcade.set_viewport(0, WIDTH, 0, HEIGHT) arcade.set_background_color(arcade.color.WHITE) main_screen.draw_main_screen() elif SCREEN == "Game": game = Background() game.draw_background() character.draw() zombie_1.draw() gun.draw()
def __init__(self): """ This is run once when we switch to this view """ super().__init__() self.texture = arcade.load_texture("lose-screen.png") # Reset the viewport, necessary if we have a scrolling game and we need # to reset the viewport back to the start so we can see what we draw. # Maybe use this line of code instead of the one above? self.view_left = 0 self.view_bottom = 0 arcade.set_viewport(self.view_left, SCREEN_WIDTH + self.view_left, self.view_bottom, SCREEN_HEIGHT + self.view_bottom)
def setup(): arcade.open_window(WIDTH, HEIGHT, "My Arcade Game") arcade.set_background_color(arcade.color.WHITE) arcade.schedule(update, 1/60) arcade.set_viewport(-WIDTH/2, WIDTH/2, -HEIGHT/2, HEIGHT/2) # Override arcade window methods window = arcade.get_window() window.on_draw = on_draw window.on_key_press = on_key_press window.on_key_release = on_key_release arcade.run()
def __init__(self): """ This is run once when we switch to this view """ super().__init__() self.texture = arcade.load_texture("intructions_screen.png") # Reset the viewport, necessary if we have a scrolling game and we need # to reset the viewport back to the start so we can see what we draw. arcade.set_viewport(0, SCREEN_WIDTH - 1, 0, SCREEN_HEIGHT - 1) # Reset the viewport, necessary if we have a scrolling game and we need # to reset the viewport back to the start so we can see what we draw. arcade.set_viewport(0, SCREEN_WIDTH - 1, 0, SCREEN_HEIGHT - 1)
def on_draw(self): arcade.set_background_color(arcade.color.GOLDENROD) arcade.set_viewport(0, SCREEN_WIDTH - 1, 0, SCREEN_HEIGHT - 1) """ Draw this view """ arcade.start_render() arcade.draw_text("You Won!", SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 + 80, arcade.color.WHITE, font_size=50, anchor_x="center") arcade.draw_text("You Collected All of the Coins and Flags!", SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, arcade.color.WHITE, font_size=45, anchor_x="center") arcade.draw_text("Click to Play Again", SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 - 70, arcade.color.WHITE, font_size=30, anchor_x="center")
def __init__(self): """ This is run once when we switch to this view """ super().__init__() self.texture = arcade.load_texture("win-screen.png") # Reset the viewport, necessary if we have a scrolling game and we need # to reset the viewport back to the start so we can see what we draw. """arcade.set_viewport(0, SCREEN_WIDTH - 1, 0, SCREEN_HEIGHT - 1)""" self.view_left = 0 self.view_bottom = 0 arcade.set_viewport(self.view_left, SCREEN_WIDTH + self.view_left, self.view_bottom, SCREEN_HEIGHT + self.view_bottom)
def on_key_press(self, key, _modifiers): if key == arcade.key.ENTER: self.game_view.player_sprite.change_x = 0 self.game_view.player_sprite.change_y = 0 self.game_view.return_to_start() self.game_view.life = 3 self.game_view.score = 0 self.game_view.setup() self.window.show_view(self.game_view) arcade.set_viewport(left=0, right=SCREEN_WIDTH, bottom=0, top=SCREEN_HEIGHT)
def __init__(self, players: int): """Create sprites and set up counters etc.""" super().__init__() arcade.set_viewport(0, WIDTH, 0, HEIGHT) arcade.set_background_color(BACKGROUND) self.left = 0 self.time_left = 90.0 self.randomblocks = 2 self.paused = False self.pause_screen = None self.blocks: arcade.SpriteList = arcade.SpriteList() self.gems: arcade.SpriteList = arcade.SpriteList() self.others: arcade.SpriteList = arcade.SpriteList() self.spikes: arcade.SpriteList = arcade.SpriteList() self.players: arcade.SpriteList = arcade.SpriteList() # sprites for n in range(players): self.players.append(player_module.Player(self, n)) size = int(128 * SCALING) for x in range(-SIDE, WIDTH + SIDE, size): Block(self, x, HEIGHT - TOP, False) Block(self, x, size // 2, True) for _ in range(3): Gem(self) for _ in range(2): RandomBlock(self) self.pauseplay = displays.PausePlay( self.left + WIDTH - 10, HEIGHT - (TOP - self.blocks[0].height // 2) // 2 + 15, self) self.buttons.append(self.pauseplay) self.engines = [] for player in self.players: blocks: arcade.SpriteList = arcade.SpriteList() for block in self.blocks: blocks.append(block) for other in self.players: if other != player: blocks.append(other) player.blocks = blocks engine = PhysicsEngine(player, blocks, GRAVITY) self.engines.append(engine) player.engine = engine self.sprite_lists = [self.blocks, self.gems, self.others, self.spikes] music.switch_track('game')
def on_resize(self, width, height): """ This method is automatically called when the window is resized. """ # Call the parent. Failing to do this will mess up the coordinates, and default to 0,0 at the center and the # edges being -1 to 1. super().on_resize(width, height) # global SCREEN_WIDTH # global SCREEN_HEIGHT # SCREEN_WIDTH, SCREEN_HEIGHT = width,height arcade.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 """ # Call update on all sprites (The sprites don't do much in this # example though.) self.physics_engine.update() self.player_list.update() self.player_list.update_animation() # --- 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.left < left_boundary: self.view_left -= left_boundary - self.player.left changed = True # Scroll right right_boundary = self.view_left + SCREEN_WIDTH - RIGHT_VIEWPORT_MARGIN if self.player.right > right_boundary: self.view_left += self.player.right - right_boundary changed = True # Scroll up top_boundary = self.view_bottom + SCREEN_HEIGHT - TOP_VIEWPORT_MARGIN if self.player.top > top_boundary: self.view_bottom += self.player.top - top_boundary changed = True # Scroll down bottom_boundary = self.view_bottom + BOTTOM_VIEWPORT_MARGIN if self.player.bottom < bottom_boundary: self.view_bottom -= bottom_boundary - self.player.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 arcade.set_viewport(self.view_left, SCREEN_WIDTH + self.view_left, self.view_bottom, SCREEN_HEIGHT + self.view_bottom)
def __init__(self): """ This is run once when we switch to this view """ super().__init__() file_path = os.path.dirname(os.path.abspath(__file__)) os.chdir(file_path) self.background = None self.gameover_sound = arcade.load_sound("musica/smb_gameover.wav") # Reset the viewport, necessary if we have a scrolling game and we need # to reset the viewport back to the start so we can see what we draw. arcade.set_viewport(0, SCREEN_WIDTH - 1, 0, SCREEN_HEIGHT - 1)
def update(self, delta_time): """Moves the map""" if (self.top_boundary + self.dy >= self.end_of_map) or (self.bottom_boundary + self.dy <= 0): self.dy = 0 if (self.right_boundary + self.dx >= self.end_of_map) or (self.left_boundary + self.dx <= 0): self.dx = 0 self.top_boundary += self.dy self.bottom_boundary += self.dy self.left_boundary += self.dx self.right_boundary += self.dx # set the viewport a.set_viewport(self.left_boundary, self.right_boundary, self.bottom_boundary, self.top_boundary)
def setup(self): self.environment.reset() self.dead = False self.current_platform_index = 0 self.last_height = self.environment.current_height self.physics_engine = arcade.PhysicsEnginePlatformer( self.environment.player, arcade.SpriteList(use_spatial_hash=True), GRAVITY) self.physics_engine.disable_multi_jump() arcade.set_viewport(0, VIEWPORT_WIDTH, 0, VIEWPORT_HEIGHT)
def update(self, delta_time): #Update coin list and player animation self.map.coin_list.update() self.player_list.update_animation() #Check if there is collisions with important sprite lists coin_hit_list = arcade.check_for_collision_with_list(self.player, self.map.coin_list ) killing_object_hit_list = arcade.check_for_collision_with_list(self.player, self.map.deadly_objects) finish_object_hit = arcade.check_for_collision_with_list(self.player, self.map.finish_list) finished = False #If player hit finish line if len(finish_object_hit) > 0: self.sound_handler.play_sound("NextLevel") self.load_new_level() finished = True #Handle collecting of coins, save position of coin as checkpoint if player dies for coin in coin_hit_list: self.sound_handler.play_sound("Coin") self.last_safe_coord[0] = self.player.center_x self.last_safe_coord[1] = self.player.center_y coin.remove_from_sprite_lists() self.score +=1 changed_View = False #Handle camera movement left_boundary = self.view_left + self.viewport_margin if self.player.left < left_boundary and self.player.left > 280: self.view_left -= left_boundary - self.player.left changed_View = True right_boundary = self.view_left + self.screen_width - self.viewport_margin if self.player.right > right_boundary: self.view_left += self.player.right - right_boundary changed_View = True self.view_left = int(self.view_left) #Handle player dying died = False if len(killing_object_hit_list) > 0: self.sound_handler.play_sound("Death") self.life_count -= 1 if self.life_count < 0: self.player_died() self.player.center_x = self.last_safe_coord[0] self.player.center_y = self.last_safe_coord[1] arcade.set_viewport(0, self.screen_width ,0 ,self.screen_height) died = True #Update camera and engine if changed_View and died == False and finished == False : arcade.set_viewport(self.view_left, self.screen_width + self.view_left,0 ,self.screen_height) self.physics_engine.update()
def set_viewport(self, left: float, right: float, bottom: float, top: float): """ Set the viewport. (What coordinates we can see. Used to scale and/or scroll the screen). See :py:func:`arcade.set_viewport` for more detailed information. :param Number left: :param Number right: :param Number bottom: :param Number top: """ set_viewport(left, right, bottom, top)
def on_update(self, delta_time): self.physics_engine.update() self.inventory.update() self.player.update() changed = False left_boundary = self.view_left + VIEWPORT['left'] if self.player.left < left_boundary: self.view_left -= left_boundary - self.player.left changed = True right_boundary = self.view_left + WINDOW_SIZE[0] - VIEWPORT['right'] if self.player.right > right_boundary: self.view_left += self.player.right - right_boundary changed = True top_boundary = self.view_bottom + WINDOW_SIZE[1] - VIEWPORT['top'] if self.player.top > top_boundary: self.view_bottom += self.player.top - top_boundary changed = True bottom_boundary = self.view_bottom + VIEWPORT['bottom'] if self.player.bottom < bottom_boundary: self.view_bottom -= bottom_boundary - self.player.bottom changed = True if changed: self.view_bottom = int(self.view_bottom) self.view_left = int(self.view_left) self.world.update(left=self.view_left, right=WINDOW_SIZE[0] + self.view_left, bottom=self.view_bottom, top=WINDOW_SIZE[1] + self.view_bottom) arcade.set_viewport(self.view_left, WINDOW_SIZE[0] + self.view_left, self.view_bottom, WINDOW_SIZE[1] + self.view_bottom) self.inventory.update_view(left=self.view_left, right=WINDOW_SIZE[0] + self.view_left, bottom=self.view_bottom, top=WINDOW_SIZE[1] + self.view_bottom) self.grid.update(left=self.view_left, right=WINDOW_SIZE[0] + self.view_left, bottom=self.view_bottom, top=WINDOW_SIZE[1] + self.view_bottom)
def on_draw(self): arcade.set_viewport(self.world.dot.x - SCREEN_WIDTH // 2, self.world.dot.x + SCREEN_WIDTH // 2, 0, SCREEN_HEIGHT) arcade.start_render() self.draw_platforms(self.world.platforms) self.draw_coins(self.world.coins) self.dot_sprite.draw() arcade.draw_text(str(self.world.score), self.world.dot.x + (SCREEN_WIDTH // 2) - 60, self.height - 30, arcade.color.WHITE, 20)
def set_viewport_on_player(self): """ Set the viewport to be over the player. If the Viewport would display the outside blackness, it is clamped with the game map. :return: """ clamped_x = min( SCREEN_WIDTH, max(0, self.player.center_x - HORIZONTAL_VIEWPORT_MARGIN)) clamped_y = min( SCREEN_HEIGHT, max(0, self.player.center_y - VERTICAL_VIEWPORT_MARGIN)) arcade.set_viewport(clamped_x, SCREEN_WIDTH + clamped_x, clamped_y, SCREEN_HEIGHT + clamped_y)
def 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() # --- Manage Scrolling --- # Keep track of if we changed the boundary. We don't want to call the # set_viewport command if we didn't change the view port. changed = False # Scroll left left_boundary = self.view_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 - 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 - 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 + VIEWPORT_MARGIN if self.player_sprite.bottom < bottom_boundary: self.view_bottom -= bottom_boundary - self.player_sprite.bottom changed = True # Make sure our boundaries are integer values. While the view port does # support floating point numbers, for this application we want every pixel # in the view port to map directly onto a pixel on the screen. We don't want # any rounding errors. self.view_left = int(self.view_left) self.view_bottom = int(self.view_bottom) # If we changed the boundary values, update the view port to match if changed: arcade.set_viewport(self.view_left, SCREEN_WIDTH + self.view_left - 1, self.view_bottom, SCREEN_HEIGHT + self.view_bottom - 1)
def update(self, delta_time): self.physics_engine.update() changed = False left_boundary = self.view_left + VIEWPORT_MARGIN if self.player_sprite.left < left_boundary: self.view_left -= left_boundary - self.player_sprite.left changed = True right_boundary = self.view_left + SCREEN_WIDTH - VIEWPORT_MARGIN if self.player_sprite.right > right_boundary: self.view_left += self.player_sprite.right - right_boundary changed = True top_boundary = self.view_bottom + SCREEN_HEIGHT - VIEWPORT_MARGIN if self.player_sprite.top > top_boundary: self.view_bottom += self.player_sprite.top - top_boundary changed = True bottom_boundary = self.view_bottom + VIEWPORT_MARGIN if self.player_sprite.bottom < bottom_boundary: self.view_bottom -= bottom_boundary - self.player_sprite.bottom changed = True self.view_left = int(self.view_left) self.view_bottom = int(self.view_bottom) if changed: arcade.set_viewport(self.view_left, SCREEN_WIDTH + self.view_left - 1, self.view_bottom, SCREEN_HEIGHT + self.view_bottom - 1) if self.player_sprite.center_x < 0: self.player_sprite.center_x = 0 if self.player_sprite.center_x > 5000: self.player_sprite.center_x = 5000 if self.player_sprite.center_y < 0: self.player_sprite.center_y = 0 if self.player_sprite.center_y > 5000: self.player_sprite.center_y = 5000 hit_list = arcade.check_for_collision_with_list( self.player_sprite, self.penny_list) for penny in hit_list: penny.remove_from_sprite_lists() self.score -= 1 arcade.play_sound(good_sound, 5)
def on_update(self, delta_time: float): self.physics.update() #following if self.player.center_x > WINDOW_HALF_WIDTH and self.player.center_x < MAP_WIDTH - TITLE_WIDTH - WINDOW_HALF_WIDTH: change_view = True else: change_view = False if change_view: # left, right, top,bottom arcade.set_viewport(self.player.center_x - WINDOW_HALF_WIDTH, self.player.center_x + WINDOW_HALF_WIDTH, 0, WINDOW_HEIGHT) if self.player.center_y < 0: self.player.set_position(center_x=100, center_y=175)
def update(self, delta_time): """ Movement and game logic """ # Call update on all sprites (The sprites don't do much in this # example though.) # Call update on all sprites (The sprites don't do much in this # example though.) self.physics_engine.update() # --- Manage Scrolling --- # Track if we need to change the viewport changed = False # Scroll left left_bndry = self.view_left + VIEWPORT_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_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 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 if self.player_sprite.bottom < bottom_bndry: self.view_bottom -= bottom_bndry - self.player_sprite.bottom changed = True if changed: arcade.set_viewport(self.view_left, SCREEN_WIDTH + self.view_left, self.view_bottom, SCREEN_HEIGHT + self.view_bottom) self.player_list.update() block_list = arcade.check_for_collision_with_list( self.player_sprite, self.wall_list) if len(block_list) > 0: self.player_sprite.speed = 0
def on_show(self): self.window = arcade.get_window() self.draw_restart_button_hover = False self.hovering = False self.clicking = False self.old_screen_center_x = int(self.window.get_size()[0] / 2) self.old_screen_center_y = int(self.window.get_size()[1] / 2) self.screen_center_x = int(self.window.get_size()[0] / 2) self.screen_center_y = int(self.window.get_size()[1] / 2) win_text = 'You Won! You Got More Coins Then On The Bar-Round!' self.game_over_text = arcade.draw_text( win_text, self.screen_center_x, self.screen_center_y + 150, anchor_x='center', anchor_y='center', color=arcade.csscolor.WHITE, font_size=32, font_name='fonts/RobotoMono-Regular.ttf') win_text = f'You had a stunning {self.coins} coins! Can you beat it?' self.game_over_text2 = arcade.draw_text( win_text, self.screen_center_x, self.screen_center_y + 100, anchor_x='center', anchor_y='center', color=arcade.csscolor.WHITE, font_size=32, font_name='fonts/RobotoMono-Regular.ttf') play_again_text = 'Play Again' self.restart_button = arcade.draw_text( play_again_text, self.screen_center_x, self.screen_center_y, anchor_x='center', anchor_y='center', color=arcade.csscolor.WHITE, font_size=64, font_name='fonts/RobotoMono-Regular.ttf') arcade.set_background_color([66, 245, 212, 255]) arcade.set_viewport(0, SCREEN_WIDTH, 0, SCREEN_HEIGHT)
def on_show(self): self.window = arcade.get_window() self.draw_restart_button_hover = False self.hovering = False self.clicking = False self.old_screen_center_x = int(self.window.get_size()[0] / 2) self.old_screen_center_y = int(self.window.get_size()[1] / 2) self.screen_center_x = int(self.window.get_size()[0] / 2) self.screen_center_y = int(self.window.get_size()[1] / 2) game_over_text = 'Game Over!' self.game_over_text = arcade.draw_text( game_over_text, self.screen_center_x, self.screen_center_y + 150, anchor_x='center', anchor_y='center', color=arcade.csscolor.WHITE, font_size=32, font_name='fonts/RobotoMono-Regular.ttf') game_over_text = 'You Couldn\'t Get More Coins Than You Did On The Bar-Round!' self.game_over_text2 = arcade.draw_text( game_over_text, self.screen_center_x, self.screen_center_y + 100, anchor_x='center', anchor_y='center', color=arcade.csscolor.WHITE, font_size=32, font_name='fonts/RobotoMono-Regular.ttf') restart_text = 'Restart' self.restart_button = arcade.draw_text( restart_text, self.screen_center_x, self.screen_center_y, anchor_x='center', anchor_y='center', color=arcade.csscolor.WHITE, font_size=64, font_name='fonts/RobotoMono-Regular.ttf') arcade.set_background_color([66, 245, 212, 255]) arcade.set_viewport(0, SCREEN_WIDTH, 0, SCREEN_HEIGHT)
def on_update(self, delta_time): """ Rörelse och spel logistik """ self.physics_engine.update() coin_hit_list = arcade.check_for_collision_with_list( self.player.sprite, self.coin_list) for coin in coin_hit_list: # Ta bort myntet coin.remove_from_sprite_lists() # Ljud för myntet arcade.play_sound(self.collect_coin_sound) # Score self.score += 1 changed = False # Skrolla vänster 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 # Skrolla höger 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 # Skrolla upp 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 # Skrolla ner 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: self.view_bottom = int(self.view_bottom) self.view_left = int(self.view_left) arcade.set_viewport(self.view_left, SCREEN_WIDTH + self.view_left, self.view_bottom, SCREEN_HEIGHT + self.view_bottom)
def animate(self, delta_time): """ Movement and game logic """ if self.view_left + self.player_sprite.right >= 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() # --- Manage Scrolling --- # Track if we need to change the viewport changed = False # Scroll left left_bndry = self.view_left + VIEWPORT_MARGIN if self.player_sprite.left < left_bndry: self.view_left -= int(left_bndry - self.player_sprite.left) changed = True # Scroll right right_bndry = self.view_left + SCREEN_WIDTH - RIGHT_MARGIN if self.player_sprite.right > right_bndry: self.view_left += int(self.player_sprite.right - right_bndry) changed = True # Scroll up top_bndry = self.view_bottom + SCREEN_HEIGHT - VIEWPORT_MARGIN if self.player_sprite.top > top_bndry: self.view_bottom += int(self.player_sprite.top - top_bndry) changed = True # Scroll down bottom_bndry = self.view_bottom + VIEWPORT_MARGIN if self.player_sprite.bottom < bottom_bndry: self.view_bottom -= int(bottom_bndry - self.player_sprite.bottom) changed = True # If we need to scroll, go ahead and do it. if changed: arcade.set_viewport(self.view_left, SCREEN_WIDTH + self.view_left, self.view_bottom, SCREEN_HEIGHT + self.view_bottom)
def animate(self, x): self.all_sprites_list.update() is_changed = False left_bndry = self.view_left + VIEWPORT_MARGIN if self.player_sprite.left < left_bndry: self.view_left -= left_bndry - self.player_sprite.left is_changed = True right_bndry = self.view_left + SCR_W - VIEWPORT_MARGIN if self.player_sprite.right > right_bndry: self.view_left += self.player_sprite.right - right_bndry is_changed = True top_bndry = self.view_bottom + SCR_H - VIEWPORT_MARGIN if self.player_sprite.top > top_bndry: self.view_bottom += self.player_sprite.top - top_bndry is_changed = True bottom_bndry = self.view_bottom + VIEWPORT_MARGIN if self.player_sprite.bottom < bottom_bndry: self.view_bottom -= bottom_bndry - self.player_sprite.bottom is_changed = True if is_changed: arcade.set_viewport(self.view_left, SCR_W + self.view_left, self.view_bottom, SCR_H + self.view_bottom)