Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import arcade
- from arcade.draw_commands import load_texture
- from arcade.draw_commands import draw_texture_rectangle
- from arcade.draw_commands import Texture
- # Constants
- SCREEN_WIDTH = 1000
- SCREEN_HEIGHT = 650
- SCREEN_TITLE = "Platformer"
- # Constants used to scale our sprites from their original size
- CHARACTER_SCALING = 0.5
- TILE_SCALING = 0.5
- PLAYER_MOVEMENT_SPEED = 5
- GRAVITY = 1
- PLAYER_JUMP_SPEED = 15
- # How many pixels to keep as a minimum margin between the character
- # and the edge of the screen.
- LEFT_VIEWPORT_MARGIN = 150
- RIGHT_VIEWPORT_MARGIN = SCREEN_WIDTH - 150
- BOTTOM_VIEWPORT_MARGIN = 50
- TOP_VIEWPORT_MARGIN = 100
- class MyGame(arcade.Window):
- """
- Main application class.
- """
- def __init__(self):
- # Call the parent class and set up the window
- super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
- # These are 'lists' that keep track of our sprites. Each sprite should
- # go into a list.
- self.player_list = None
- self.wall_list = None
- # Separate variable that holds the player sprite
- self.player_sprite = None
- # Used to keep track of our scrolling
- self.view_bottom = 0
- self.view_left = 0
- arcade.set_background_color(arcade.csscolor.CORNFLOWER_BLUE)
- def setup(self):
- """ Set up the game here. Call this function to restart the game. """
- # Create the Sprite lists
- self.player_list = arcade.SpriteList()
- self.wall_list = arcade.SpriteList()
- #self.player_sprite = arcade.Sprite();
- # Set up the player, specifically placing it at these coordinates.
- self.player_sprite = arcade.AnimatedTimeSprite(CHARACTER_SCALING)
- self.player_sprite.append_texture( load_texture("data/dinorun0000.png") )
- self.player_sprite.append_texture( load_texture("data/dinorun0001.png") )
- self.player_sprite.center_x = 64
- self.player_sprite.center_y = 120
- self.player_list.append(self.player_sprite)
- # Create the ground
- for x in range(0, 125000, 64):
- wall = arcade.Sprite("data/grass.png", TILE_SCALING)
- wall.center_x = x
- wall.center_y = 32
- self.wall_list.append(wall)
- # Create the 'physics engine'
- self.physics_engine = arcade.PhysicsEnginePlatformer(self.player_sprite, self.wall_list, GRAVITY)
- # enable the double-jump
- self.physics_engine.enable_multi_jump(2)
- def on_draw(self):
- """ Render the screen. """
- # Clear the screen to the background color
- arcade.start_render()
- # Draw our sprites
- self.wall_list.draw()
- self.player_sprite.draw()
- def on_key_press(self, key, modifiers):
- if key == arcade.key.UP or key == arcade.key.W:
- if self.physics_engine.can_jump():
- self.player_sprite.change_y = PLAYER_JUMP_SPEED
- self.physics_engine.increment_jump_counter()
- #elif key == arcade.key.LEFT or key == arcade.key.A:
- # self.player_sprite.change_x = -PLAYER_MOVEMENT_SPEED
- elif key == arcade.key.RIGHT or key == arcade.key.D:
- self.player_sprite.change_x = PLAYER_MOVEMENT_SPEED
- def on_key_release(self, key, modifiers):
- if key == arcade.key.LEFT or key == arcade.key.A:
- self.player_sprite.change_x = 0
- elif key == arcade.key.RIGHT or key == arcade.key.D:
- self.player_sprite.change_x = 0
- def update(self, delta_time):
- self.physics_engine.update()
- self.player_sprite.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_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
- arcade.set_viewport(self.view_left,
- SCREEN_WIDTH + self.view_left,
- self.view_bottom,
- SCREEN_HEIGHT + self.view_bottom)
- def main():
- window = MyGame()
- window.setup()
- arcade.run()
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement