Advertisement
Guest User

Untitled

a guest
Dec 18th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. import colour
  2. import pygame
  3.  
  4. SCREEN_WIDTH = 1280
  5. SCREEN_HEIGHT = 720
  6.  
  7. class Wall(pygame.sprite.Sprite):
  8. def __init__(self, x, y, width, height, col):
  9. super().__init__()
  10.  
  11. # Colour and size
  12. self.image = pygame.Surface([width, height])
  13. self.image.fill(col)
  14.  
  15. # Starting location
  16. self.rect = self.image.get_rect()
  17. self.rect.y = y
  18. self.rect.x = x
  19.  
  20.  
  21. class Player(pygame.sprite.Sprite):
  22.  
  23. # Constructor Function
  24. def __init__(self, x, y):
  25. # Calls The Parent Constructor
  26. super().__init__()
  27.  
  28. # set image height/width
  29. self.image = pygame.Surface([15, 15])
  30. self.image.fill(colour.WHITE)
  31.  
  32. # Make our top-left corner the passed-in location.
  33. self.rect = self.image.get_rect()
  34. self.rect.y = y
  35. self.rect.x = x
  36.  
  37. # set speed vector
  38. self.change_x = 0
  39. self.change_y = 0
  40. self.walls = None
  41.  
  42. def changespeed(self, x, y):
  43. """ Change the speed of the player. """
  44. self.change_x += x
  45. self.change_y += y
  46.  
  47. def update(self):
  48. """ Gravity"""
  49. self.calc_grav()
  50.  
  51. """ Update the player position. """
  52. # move left/right
  53. self.rect.x += self.change_x
  54.  
  55. # did this update cause us to hit a wall?
  56. block_hit_list = pygame.sprite.spritecollide(self, self.walls, False)
  57. for block in block_hit_list:
  58. # if we are moving right, set our right side to the left side of the item we hit
  59. if self.change_x > 0:
  60. self.rect.right = block.rect.left
  61. else:
  62. # Otherwise if we are moving left, do the opposite
  63. self.rect.left = block.rect.right
  64.  
  65. # move up/down
  66. self.rect.y += self.change_y
  67.  
  68. # Check and see if we hit anything
  69. block_hit_list = pygame.sprite.spritecollide(self, self.walls, False)
  70. for block in block_hit_list:
  71.  
  72. # Reset our position based on the top/bottom of the object.
  73. if self.change_y > 0:
  74. self.rect.bottom = block.rect.top
  75. else:
  76. self.rect.top = block.rect.bottom
  77.  
  78. def calc_grav(self):
  79. """ Calculate effect of gravity. """
  80. if self.change_y == 0:
  81. self.change_y = 1
  82. else:
  83. self.change_y += .35
  84.  
  85. # See if we are on the ground.
  86. if self.rect.y >= SCREEN_HEIGHT - self.rect.height and self.change_y >= 0:
  87. self.change_y = 0
  88. self.rect.y = SCREEN_HEIGHT - self.rect.height
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement