Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.19 KB | None | 0 0
  1. import arcade
  2. from arcade.draw_commands import load_texture
  3. from arcade.draw_commands import draw_texture_rectangle
  4. from arcade.draw_commands import Texture
  5.  
  6. # Constants
  7. SCREEN_WIDTH = 1000
  8. SCREEN_HEIGHT = 650
  9. SCREEN_TITLE = "Platformer"
  10.  
  11. # Constants used to scale our sprites from their original size
  12. CHARACTER_SCALING = 0.5
  13. TILE_SCALING = 0.5
  14.  
  15. PLAYER_MOVEMENT_SPEED = 5
  16. GRAVITY = 1
  17. PLAYER_JUMP_SPEED = 15
  18.  
  19. # How many pixels to keep as a minimum margin between the character
  20. # and the edge of the screen.
  21. LEFT_VIEWPORT_MARGIN = 150
  22. RIGHT_VIEWPORT_MARGIN = SCREEN_WIDTH - 150
  23. BOTTOM_VIEWPORT_MARGIN = 50
  24. TOP_VIEWPORT_MARGIN = 100
  25.  
  26.  
  27. class MyGame(arcade.Window):
  28. """
  29. Main application class.
  30. """
  31.  
  32. def __init__(self):
  33.  
  34. # Call the parent class and set up the window
  35. super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
  36.  
  37. # These are 'lists' that keep track of our sprites. Each sprite should
  38. # go into a list.
  39. self.player_list = None
  40. self.wall_list = None
  41.  
  42. # Separate variable that holds the player sprite
  43. self.player_sprite = None
  44.  
  45. # Used to keep track of our scrolling
  46. self.view_bottom = 0
  47. self.view_left = 0
  48.  
  49. arcade.set_background_color(arcade.csscolor.CORNFLOWER_BLUE)
  50.  
  51. def setup(self):
  52. """ Set up the game here. Call this function to restart the game. """
  53. # Create the Sprite lists
  54. self.player_list = arcade.SpriteList()
  55. self.wall_list = arcade.SpriteList()
  56.  
  57. #self.player_sprite = arcade.Sprite();
  58.  
  59. # Set up the player, specifically placing it at these coordinates.
  60. self.player_sprite = arcade.AnimatedTimeSprite(CHARACTER_SCALING)
  61. self.player_sprite.append_texture( load_texture("data/dinorun0000.png") )
  62. self.player_sprite.append_texture( load_texture("data/dinorun0001.png") )
  63. self.player_sprite.center_x = 64
  64. self.player_sprite.center_y = 120
  65. self.player_list.append(self.player_sprite)
  66.  
  67.  
  68. # Create the ground
  69. for x in range(0, 125000, 64):
  70. wall = arcade.Sprite("data/grass.png", TILE_SCALING)
  71. wall.center_x = x
  72. wall.center_y = 32
  73. self.wall_list.append(wall)
  74.  
  75. # Create the 'physics engine'
  76. self.physics_engine = arcade.PhysicsEnginePlatformer(self.player_sprite, self.wall_list, GRAVITY)
  77. # enable the double-jump
  78. self.physics_engine.enable_multi_jump(2)
  79.  
  80. def on_draw(self):
  81. """ Render the screen. """
  82.  
  83. # Clear the screen to the background color
  84. arcade.start_render()
  85.  
  86. # Draw our sprites
  87. self.wall_list.draw()
  88. self.player_sprite.draw()
  89.  
  90. def on_key_press(self, key, modifiers):
  91. if key == arcade.key.UP or key == arcade.key.W:
  92. if self.physics_engine.can_jump():
  93. self.player_sprite.change_y = PLAYER_JUMP_SPEED
  94. self.physics_engine.increment_jump_counter()
  95. #elif key == arcade.key.LEFT or key == arcade.key.A:
  96. # self.player_sprite.change_x = -PLAYER_MOVEMENT_SPEED
  97. elif key == arcade.key.RIGHT or key == arcade.key.D:
  98. self.player_sprite.change_x = PLAYER_MOVEMENT_SPEED
  99.  
  100. def on_key_release(self, key, modifiers):
  101. if key == arcade.key.LEFT or key == arcade.key.A:
  102. self.player_sprite.change_x = 0
  103. elif key == arcade.key.RIGHT or key == arcade.key.D:
  104. self.player_sprite.change_x = 0
  105.  
  106. def update(self, delta_time):
  107. self.physics_engine.update()
  108. self.player_sprite.update_animation()
  109.  
  110. # --- Manage Scrolling ---
  111.  
  112. # Track if we need to change the viewport
  113.  
  114. changed = False
  115.  
  116. # Scroll left
  117. left_boundary = self.view_left + LEFT_VIEWPORT_MARGIN
  118. if self.player_sprite.left < left_boundary:
  119. self.view_left -= left_boundary - self.player_sprite.left
  120. changed = True
  121.  
  122. # Scroll right
  123. right_boundary = self.view_left + SCREEN_WIDTH - RIGHT_VIEWPORT_MARGIN
  124. if self.player_sprite.right > right_boundary:
  125. self.view_left += self.player_sprite.right - right_boundary
  126. changed = True
  127.  
  128. # Scroll up
  129. top_boundary = self.view_bottom + SCREEN_HEIGHT - TOP_VIEWPORT_MARGIN
  130. if self.player_sprite.top > top_boundary:
  131. self.view_bottom += self.player_sprite.top - top_boundary
  132. changed = True
  133.  
  134. # Scroll down
  135. bottom_boundary = self.view_bottom + BOTTOM_VIEWPORT_MARGIN
  136. if self.player_sprite.bottom < bottom_boundary:
  137. self.view_bottom -= bottom_boundary - self.player_sprite.bottom
  138. changed = True
  139.  
  140. if changed:
  141. # Only scroll to integers. Otherwise we end up with pixels that
  142. # don't line up on the screen
  143. self.view_bottom = int(self.view_bottom)
  144. self.view_left = int(self.view_left)
  145.  
  146. # Do the scrolling
  147. arcade.set_viewport(self.view_left,
  148. SCREEN_WIDTH + self.view_left,
  149. self.view_bottom,
  150. SCREEN_HEIGHT + self.view_bottom)
  151.  
  152. def main():
  153. window = MyGame()
  154. window.setup()
  155. arcade.run()
  156.  
  157.  
  158. if __name__ == "__main__":
  159. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement