def scroll_viewport(self): """ Manage scrolling of the viewport. """ # Flipped to true if we need to scroll changed = False # Scroll left left_bndry = self.view_left + VIEWPORT_MARGIN if self.player.left < left_bndry: self.view_left -= left_bndry - self.player.left changed = True # Scroll right right_bndry = self.view_left + SCREEN_WIDTH - VIEWPORT_MARGIN if self.player.right > right_bndry: self.view_left += self.player.right - right_bndry changed = True # Scroll up top_bndry = self.view_bottom + SCREEN_HEIGHT - VIEWPORT_MARGIN if self.player.top > top_bndry: self.view_bottom += self.player.top - top_bndry changed = True # Scroll down bottom_bndry = self.view_bottom + VIEWPORT_MARGIN if self.player.bottom < bottom_bndry: self.view_bottom -= bottom_bndry - self.player.bottom changed = True if changed: 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 """ # 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): """ 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 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 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() # --- 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: arcadeplus.set_viewport(self.view_left, SCREEN_WIDTH + self.view_left - 1, self.view_bottom, SCREEN_HEIGHT + self.view_bottom - 1)
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() # --- 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: 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 """ start_time = timeit.default_timer() # 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: arcadeplus.set_viewport(self.view_left, SCREEN_WIDTH + self.view_left, self.view_bottom, SCREEN_HEIGHT + self.view_bottom) # Save the time it took to do this. self.processing_time = timeit.default_timer() - start_time
def on_update(self, delta_time): """ Movement and game logic """ # Move the player with the physics engine self.physics_engine.update() # --- 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): """ Movement and game logic """ # Call update on all sprites self.physics_engine.update() # --- Manage Scrolling --- # Track if we need to change the viewport 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 - RIGHT_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 # If we need to scroll, go ahead and do it. if changed: 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 """ # 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 __init__(self, width: int = 800, height: int = 600, title: str = 'ArcadePlus Window', fullscreen: bool = False, resizable: bool = False, update_rate: Optional[float] = 1 / 60, antialiasing: bool = True): """ Construct a new window :param int width: Window width :param int height: Window height :param str title: Title (appears in title bar) :param bool fullscreen: Should this be full screen? :param bool resizable: Can the user resize the window? :param float update_rate: How frequently to update the window. :param bool antialiasing: Should OpenGL's anti-aliasing be enabled? """ if antialiasing: config = pyglet.gl.Config(major_version=3, minor_version=3, double_buffer=True, sample_buffers=1, samples=4) else: config = pyglet.gl.Config(major_version=3, minor_version=3, double_buffer=True) try: super().__init__(width=width, height=height, caption=title, resizable=resizable, config=config, vsync=False) except pyglet.window.NoSuchConfigException: raise NoOpenGLException( "Unable to create an OpenGL 3.3+ context. " "Check to make sure your system supports OpenGL 3.3 or higher." ) if antialiasing: try: gl.glEnable(gl.GL_MULTISAMPLE_ARB) except pyglet.gl.GLException: print("Warning: Anti-aliasing not supported on this computer.") # Required for transparency gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) if update_rate: from pyglet import compat_platform if compat_platform == 'darwin' or compat_platform == 'linux': # Set vsync to false, or we'll be limited to a 1/30 sec update rate possibly self.context.set_vsync(False) self.set_update_rate(update_rate) super().set_fullscreen(fullscreen) # This used to be necessary on Linux, but no longer appears to be. # With Pyglet 2.0+, setting this to false will not allow the screen to # update. It does, however, cause flickering if creating a window that # isn't derived from the Window class. # self.invalid = False set_window(self) set_viewport(0, self.width - 1, 0, self.height - 1) self.current_view: Optional[View] = None self.button_list: List[TextButton] = [] self.dialogue_box_list: List[DialogueBox] = [] self.text_list: List[TextLabel] = [] self.textbox_list: List[TextBox] = [] self.textbox_time = 0.0 self.key: Optional[int] = None
def on_resize(self, width, height): arcadeplus.set_viewport(self.view_left, self.width + self.view_left, self.view_bottom, self.height + self.view_bottom)