Advertisement
Guest User

code

a guest
Jul 23rd, 2014
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.53 KB | None | 0 0
  1. """
  2. Sample Python/Pygame Programs
  3. Simpson College Computer Science
  4. http://programarcadegames.com/
  5. http://simpson.edu/computer-science/
  6.  
  7. From:
  8. http://programarcadegames.com/python_examples/f.php?file=platform_moving.py
  9.  
  10. Explanation video: http://youtu.be/YKdOD5VkY48
  11.  
  12. Part of a series:
  13. http://programarcadegames.com/python_examples/f.php?file=move_with_walls_example.py
  14. http://programarcadegames.com/python_examples/f.php?file=maze_runner.py
  15. http://programarcadegames.com/python_examples/f.php?file=platform_jumper.py
  16. http://programarcadegames.com/python_examples/f.php?file=platform_scroller.py
  17. http://programarcadegames.com/python_examples/f.php?file=platform_moving.py
  18. http://programarcadegames.com/python_examples/sprite_sheets/
  19. """
  20. import pygame
  21.  
  22. # Global constants
  23.  
  24. # Colors
  25. BLACK = ( 0, 0, 0)
  26. WHITE = ( 255, 255, 255)
  27. BLUE = ( 0, 0, 255)
  28. RED = ( 255, 0, 0)
  29. GREEN = ( 0, 255, 0)
  30.  
  31. # Screen dimensions
  32. SCREEN_WIDTH = 1024
  33. SCREEN_HEIGHT = 768
  34.  
  35. bg = pygame.image.load("MapTest.png")
  36.  
  37. class Player(pygame.sprite.Sprite):
  38. """
  39. This class represents the bar at the bottom that the player controls.
  40. """
  41.  
  42. # -- Attributes
  43. # Set speed vector of player
  44. change_x = 0
  45. change_y = 0
  46.  
  47. # List of sprites we can bump against
  48. level = None
  49.  
  50. # -- Methods
  51. def __init__(self):
  52. """ Constructor function """
  53.  
  54. # Call the parent's constructor
  55. pygame.sprite.Sprite.__init__(self)
  56.  
  57. # Create an image of the block, and fill it with a color.
  58. # This could also be an image loaded from the disk.
  59. width = 40
  60. height = 60
  61. self.image = pygame.Surface([width, height])
  62. self.image.fill(RED)
  63.  
  64. # Set a referance to the image rect.
  65. self.rect = self.image.get_rect()
  66.  
  67. def update(self):
  68. """ Move the player. """
  69. # Gravity
  70. self.calc_grav()
  71.  
  72. # Move left/right
  73. self.rect.x += self.change_x
  74.  
  75. # See if we hit anything
  76. block_hit_list = pygame.sprite.spritecollide(self, self.level.platform_list, False)
  77. for block in block_hit_list:
  78. # If we are moving right,
  79. # set our right side to the left side of the item we hit
  80. if self.change_x > 0:
  81. self.rect.right = block.rect.left
  82. elif self.change_x < 0:
  83. # Otherwise if we are moving left, do the opposite.
  84. self.rect.left = block.rect.right
  85.  
  86. # Move up/down
  87. self.rect.y += self.change_y
  88.  
  89. # Check and see if we hit anything
  90. block_hit_list = pygame.sprite.spritecollide(self, self.level.platform_list, False)
  91. for block in block_hit_list:
  92.  
  93. # Reset our position based on the top/bottom of the object.
  94. if self.change_y > 0:
  95. self.rect.bottom = block.rect.top
  96. elif self.change_y < 0:
  97. self.rect.top = block.rect.bottom
  98.  
  99. # Stop our vertical movement
  100. self.change_y = 0
  101.  
  102. if isinstance(block, MovingPlatform):
  103. self.rect.x += block.change_x
  104.  
  105. def calc_grav(self):
  106. """ Calculate effect of gravity.
  107. if self.change_y == 0:
  108. self.change_y = 0
  109. else:
  110. self.change_y = 0"""
  111.  
  112. """# See if we are on the ground.
  113. if self.rect.y >= SCREEN_HEIGHT - self.rect.height and self.change_y >= 0:
  114. self.change_y = 0
  115. self.rect.y = SCREEN_HEIGHT - self.rect.height
  116. """
  117. # Player-controlled movement:
  118. def go_left(self):
  119. """ Called when the user hits the left arrow. """
  120. self.change_x = -6
  121.  
  122. def go_right(self):
  123. """ Called when the user hits the right arrow. """
  124. self.change_x = 6
  125.  
  126. def go_down(self):
  127. """ Called when the user hits the left arrow. """
  128. self.change_y = 6
  129.  
  130. def go_up(self):
  131. """ Called when the user hits the right arrow. """
  132. self.change_y = -6
  133.  
  134. def stop(self):
  135. """ Called when the user lets off the keyboard. """
  136. self.change_x = 0
  137.  
  138. def stop2(self):
  139. """ Called when the user lets off the keyboard. """
  140. self.change_y = 0
  141.  
  142. class Platform(pygame.sprite.Sprite):
  143. """ Platform the user can jump on """
  144.  
  145. def __init__(self, width, height):
  146. """ Platform constructor. Assumes constructed with user passing in
  147. an array of 5 numbers like what's defined at the top of this code.
  148. """
  149. pygame.sprite.Sprite.__init__(self)
  150.  
  151. self.image = pygame.Surface([width, height])
  152. self.image.fill(GREEN)
  153.  
  154. self.rect = self.image.get_rect()
  155.  
  156.  
  157. class MovingPlatform(Platform):
  158. """ This is a fancier platform that can actually move. """
  159. change_x = 0
  160. change_y = 0
  161.  
  162. boundary_top = 0
  163. boundary_bottom = 0
  164. boundary_left = 0
  165. boundary_right = 0
  166.  
  167. player = None
  168.  
  169. level = None
  170.  
  171. def update(self):
  172. """ Move the platform.
  173. If the player is in the way, it will shove the player
  174. out of the way. This does NOT handle what happens if a
  175. platform shoves a player into another object. Make sure
  176. moving platforms have clearance to push the player around
  177. or add code to handle what happens if they don't. """
  178.  
  179. # Move left/right
  180. self.rect.x += self.change_x
  181.  
  182. # See if we hit the player
  183. hit = pygame.sprite.collide_rect(self, self.player)
  184. if hit:
  185. # We did hit the player. Shove the player around and
  186. # assume he/she won't hit anything else.
  187.  
  188. # If we are moving right, set our right side
  189. # to the left side of the item we hit
  190. if self.change_x < 0:
  191. self.player.rect.right = self.rect.left
  192. else:
  193. # Otherwise if we are moving left, do the opposite.
  194. self.player.rect.left = self.rect.right
  195.  
  196. # Move up/down
  197. self.rect.y += self.change_y
  198.  
  199. # Check and see if we the player
  200. hit = pygame.sprite.collide_rect(self, self.player)
  201. if hit:
  202. # We did hit the player. Shove the player around and
  203. # assume he/she won't hit anything else.
  204.  
  205. # Reset our position based on the top/bottom of the object.
  206. if self.change_y < 0:
  207. self.player.rect.bottom = self.rect.top
  208. else:
  209. self.player.rect.top = self.rect.bottom
  210.  
  211. # Check the boundaries and see if we need to reverse
  212. # direction.
  213. if self.rect.bottom > self.boundary_bottom or self.rect.top < self.boundary_top:
  214. self.change_y *= -1
  215.  
  216. cur_pos = self.rect.x - self.level.world_shift
  217. if cur_pos < self.boundary_left or cur_pos > self.boundary_right:
  218. self.change_x *= -1
  219.  
  220. class Level(object):
  221. """ This is a generic super-class used to define a level.
  222. Create a child class for each level with level-specific
  223. info. """
  224.  
  225. # Lists of sprites used in all levels. Add or remove
  226. # lists as needed for your game. """
  227. platform_list = None
  228. enemy_list = None
  229.  
  230. # Background image
  231. background = None
  232.  
  233. # How far this world has been scrolled left/right
  234. world_shift = 0
  235. world_shift2 = 0
  236. level_limit = -1000
  237.  
  238. def __init__(self, player):
  239. """ Constructor. Pass in a handle to player. Needed for when moving
  240. platforms collide with the player. """
  241. self.platform_list = pygame.sprite.Group()
  242. self.enemy_list = pygame.sprite.Group()
  243. self.player = player
  244.  
  245. # Update everythign on this level
  246. def update(self):
  247. """ Update everything in this level."""
  248. self.platform_list.update()
  249. self.enemy_list.update()
  250.  
  251. def draw(self, screen):
  252. """ Draw everything on this level. """
  253.  
  254. # Draw the background
  255. screen.fill(WHITE)
  256. screen.blit(bg,(0,0))
  257.  
  258.  
  259. # Draw all the sprite lists that we have
  260. self.platform_list.draw(screen)
  261. self.enemy_list.draw(screen)
  262.  
  263. def shift_world(self, shift_x):
  264. """ When the user moves left/right and we need to scroll everything:
  265. """
  266.  
  267. # Keep track of the shift amount
  268. self.world_shift += shift_x
  269.  
  270. # Go through all the sprite lists and shift
  271. for platform in self.platform_list:
  272. platform.rect.x += shift_x
  273.  
  274. for enemy in self.enemy_list:
  275. enemy.rect.x += shift_x
  276.  
  277. def shift_world2(self, shift_y):
  278. """ When the user moves left/right and we need to scroll everything:
  279. """
  280.  
  281. # Keep track of the shift amount
  282. self.world_shift2 += shift_y
  283.  
  284. # Go through all the sprite lists and shift
  285. for platform in self.platform_list:
  286. platform.rect.y += shift_y
  287.  
  288. for enemy in self.enemy_list:
  289. enemy.rect.y += shift_y
  290.  
  291. # Create platforms for the level
  292. class Level_01(Level):
  293. """ Definition for level 1. """
  294.  
  295. def __init__(self, player):
  296. """ Create level 1. """
  297.  
  298. # Call the parent constructor
  299. Level.__init__(self, player)
  300.  
  301. self.level_limit = -1500
  302.  
  303. # Array with width, height, x, and y of platform
  304. level = [[210, 70, 500, 500],
  305. [210, 70, 800, 400],
  306. [210, 70, 1000, 500],
  307. [210, 70, 1120, 280],
  308. ]
  309.  
  310.  
  311. # Go through the array above and add platforms
  312. for platform in level:
  313. block = Platform(platform[0], platform[1])
  314. block.rect.x = platform[2]
  315. block.rect.y = platform[3]
  316. block.player = self.player
  317. self.platform_list.add(block)
  318.  
  319. # Add a custom moving platform
  320. block = MovingPlatform(70, 40)
  321. block.rect.x = 1350
  322. block.rect.y = 280
  323. block.boundary_left = 1350
  324. block.boundary_right = 1600
  325. block.change_x = 1
  326. block.player = self.player
  327. block.level = self
  328. self.platform_list.add(block)
  329.  
  330.  
  331. # Create platforms for the level
  332. class Level_02(Level):
  333. """ Definition for level 2. """
  334.  
  335. def __init__(self, player):
  336. """ Create level 1. """
  337.  
  338. # Call the parent constructor
  339. Level.__init__(self, player)
  340.  
  341. self.level_limit = -1000
  342.  
  343. # Array with type of platform, and x, y location of the platform.
  344. level = [[210, 70, 500, 550],
  345. [210, 70, 800, 400],
  346. [210, 70, 1000, 500],
  347. [210, 70, 1120, 280],
  348. ]
  349.  
  350.  
  351. # Go through the array above and add platforms
  352. for platform in level:
  353. block = Platform(platform[0], platform[1])
  354. block.rect.x = platform[2]
  355. block.rect.y = platform[3]
  356. block.player = self.player
  357. self.platform_list.add(block)
  358.  
  359. # Add a custom moving platform
  360. block = MovingPlatform(70, 70)
  361. block.rect.x = 1500
  362. block.rect.y = 300
  363. block.boundary_top = 100
  364. block.boundary_bottom = 550
  365. block.change_y = -1
  366. block.player = self.player
  367. block.level = self
  368. self.platform_list.add(block)
  369.  
  370. def main():
  371. """ Main Program """
  372. pygame.init()
  373.  
  374. # Set the height and width of the screen
  375. size = [SCREEN_WIDTH, SCREEN_HEIGHT]
  376. screen = pygame.display.set_mode(size)
  377.  
  378. pygame.display.set_caption("Platformer with moving platforms")
  379.  
  380. # Create the player
  381. player = Player()
  382.  
  383. # Create all the levels
  384. level_list = []
  385. level_list.append(Level_01(player))
  386. level_list.append(Level_02(player))
  387.  
  388. # Set the current level
  389. current_level_no = 0
  390. current_level = level_list[current_level_no]
  391.  
  392. active_sprite_list = pygame.sprite.Group()
  393. player.level = current_level
  394.  
  395. player.rect.x = 340
  396. player.rect.y = SCREEN_HEIGHT - player.rect.height
  397. active_sprite_list.add(player)
  398.  
  399. #Loop until the user clicks the close button.
  400. done = False
  401.  
  402. # Used to manage how fast the screen updates
  403. clock = pygame.time.Clock()
  404.  
  405. # -------- Main Program Loop -----------
  406. while not done:
  407. for event in pygame.event.get(): # User did something
  408. if event.type == pygame.QUIT: # If user clicked close
  409. done = True # Flag that we are done so we exit this loop
  410.  
  411. if event.type == pygame.KEYDOWN:
  412. if event.key == pygame.K_LEFT:
  413. player.go_left()
  414. if event.key == pygame.K_RIGHT:
  415. player.go_right()
  416. if event.key == pygame.K_UP:
  417. player.go_up()
  418. if event.key == pygame.K_DOWN:
  419. player.go_down()
  420.  
  421. if event.type == pygame.KEYUP:
  422. if event.key == pygame.K_LEFT and player.change_x < 0:
  423. player.stop()
  424. if event.key == pygame.K_RIGHT and player.change_x > 0:
  425. player.stop()
  426. if event.key == pygame.K_UP and player.change_y < 0:
  427. player.stop2()
  428. if event.key == pygame.K_DOWN and player.change_y > 0:
  429. player.stop2()
  430.  
  431. # Update the player.
  432. active_sprite_list.update()
  433.  
  434. # Update items in the level
  435. current_level.update()
  436.  
  437. # If the player gets near the right side, shift the world left (-x)
  438. if player.rect.right >= 800:
  439. diff = player.rect.right - 800
  440. player.rect.right = 800
  441. current_level.shift_world(-diff)
  442.  
  443. if player.rect.top <= 100:
  444. diff = 100 - player.rect.top
  445. player.rect.top = 100
  446. current_level.shift_world2(diff)
  447.  
  448. if player.rect.bottom >= 600:
  449. diff = player.rect.bottom -600
  450. player.rect.bottom = 600
  451. current_level.shift_world2(-diff)
  452.  
  453. # If the player gets near the left side, shift the world right (+x)
  454. if player.rect.left <= 120:
  455. diff = 120 - player.rect.left
  456. player.rect.left = 120
  457. current_level.shift_world(diff)
  458.  
  459. # If the player gets to the end of the level, go to the next level
  460. current_position = player.rect.x + current_level.world_shift
  461. current_position = player.rect.y + current_level.world_shift2
  462. if current_position < current_level.level_limit:
  463. if current_level_no < len(level_list)-1:
  464. player.rect.x = 120
  465. current_level_no += 1
  466. current_level = level_list[current_level_no]
  467. player.level = current_level
  468. else:
  469. # Out of levels. This just exits the program.
  470. # You'll want to do something better.
  471. done = True
  472.  
  473. # ALL CODE TO DRAW SHOULD GO BELOW THIS COMMENT
  474. current_level.draw(screen)
  475. active_sprite_list.draw(screen)
  476.  
  477. # ALL CODE TO DRAW SHOULD GO ABOVE THIS COMMENT
  478.  
  479. # Limit to 60 frames per second
  480. clock.tick(60)
  481.  
  482. # Go ahead and update the screen with what we've drawn.
  483. pygame.display.flip()
  484.  
  485. # Be IDLE friendly. If you forget this line, the program will 'hang'
  486. # on exit.
  487. pygame.quit()
  488.  
  489. if __name__ == "__main__":
  490. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement