Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.00 KB | None | 0 0
  1. import pygame
  2. from time import *
  3. #Sæþór Bergsson
  4.  
  5.  
  6. # Class for the Player(the orange dude)
  7. class Player(object):
  8. def __init__(self):
  9. self.rect = pygame.Rect(32, 32, 16, 16)
  10.  
  11. def move(self, dx, dy):
  12. # Move each axis separately. Note that this checks for collisions both times.
  13. if dx != 0:
  14. self.move_single_axis(dx, 0)
  15. if dy != 0:
  16. self.move_single_axis(0, dy)
  17.  
  18. def move_single_axis(self, dx, dy):
  19. # Move the rectangle
  20. self.rect.x += dx
  21. self.rect.y += dy
  22.  
  23. # If you collide with a wall
  24. for a_brick in bricks:
  25. if self.rect.colliderect(a_brick.rect):
  26. if dx > 0: # Moving right; Hit the left side of the wall
  27. self.rect.right = a_brick.rect.left
  28. if dx < 0: # Moving left; Hit the right side of the wall
  29. self.rect.left = a_brick.rect.right
  30. if dy > 0: # Moving down; Hit the top side of the wall
  31. self.rect.bottom = a_brick.rect.top
  32. if dy < 0: # Moving up; Hit the bottom side of the wall
  33. self.rect.top = a_brick.rect.bottom
  34.  
  35. # if you collide with the end_rect and don't have 15 points, the end_rect will act like a brick
  36.  
  37.  
  38. if stig != 15:
  39. if self.rect.colliderect(end_rect):
  40. if dx > 1: # Moving right; Hit the left side of the wall
  41. self.rect.right = end_rect.left
  42. elif dx < 1: # Moving left; Hit the right side of the wall
  43. self.rect.left = end_rect.right
  44. elif dy > 1: # Moving down; Hit the top side of the wall
  45. self.rect.bottom = end_rect.top
  46. elif dy < 0: # Moving up; Hit the bottom side of the wall
  47. self.rect.top = end_rect.bottom
  48. #if you collide with the end_rect and have 15 points, the game will end
  49. elif self.rect.colliderect(end_rect):
  50. if stig == 15:
  51. print("-----YOU WIN-----")
  52. sleep(0.5)
  53. print(".")
  54. sleep(0.5)
  55. print(".")
  56. sleep(0.5)
  57. print(".")
  58. sleep(0.5)
  59. print("-----STATS-----")
  60. print("POINTS:", stig)
  61. print("TIME:", sec, "seconds")
  62. raise SystemExit
  63.  
  64.  
  65.  
  66.  
  67.  
  68. # A class to hold a wall rectangle
  69. class Brick(object):
  70. def __init__(self, pos):
  71. self.rect = pygame.Rect(pos[0], pos[1], 16, 16)
  72.  
  73.  
  74. class Magic(object):
  75. def __init__(self, pos):
  76. self.rect = pygame.Rect(pos[0], pos[1], 16, 16)
  77.  
  78.  
  79. # A class to hold the shields the player can pick up
  80. class Shield(object):
  81. def __init__(self, pos):
  82. self.rect = pygame.Rect(pos[0], pos[1], 16, 16)
  83.  
  84.  
  85. # Initialize
  86. pygame.init()
  87.  
  88. # Set up the display
  89. pygame.display.set_caption("Get to the red square!")
  90. screen = pygame.display.set_mode((673, 500))
  91.  
  92. clock = pygame.time.Clock()
  93. bricks = list() # List to hold the walls
  94. shields = list() # List to hold the shields
  95. magic = list() # List to hold the magic boxes
  96. playerInv = list() # list to hold the sheilds for player
  97. player = Player() # Create the player
  98.  
  99. # Holds the level layout in a list of strings.
  100. maze = [
  101. "WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW",
  102. "W W S M S W W",
  103. "WW WWWW WWWWWWWWWWWWWWWWWWWWMWW W W W",
  104. "W WSW W S W WS W WS W",
  105. "WMWW WW WWMWWWWWWWWWWWWW WWWWWWMWWWWWW",
  106. "W W W W W",
  107. "WW WWWWWWWWWWWWWWWWMWWWWWWWWWWWWWWWWW WWWW",
  108. "W W M S SWW W W",
  109. "W WWW WWWWWWWWWW W WWWWWWWWWW WWW W",
  110. "W W WWMWW SW WW W",
  111. "WWWWWWWW S W W SW W M W",
  112. "W WWWWWWWWWWWWWW WMMMWWW W WWMMW",
  113. "W W W W WS W",
  114. "W W WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW W",
  115. "W WWWWWS W W S M W W W SWWWWW",
  116. "W W M W W W W M M E",
  117. "WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW",
  118. ]
  119.  
  120. # Parse the maze string above. W = wall, E = exit, M = magic box, S = shield
  121. x = 0
  122. y = 0
  123. for row in maze:
  124. for col in row:
  125. if col == "W":
  126. bricks.append(Brick((x, y)))
  127. if col == "E":
  128. end_rect = pygame.Rect(x, y, 16, 16)
  129. if col == "M":
  130. magic.append(Magic((x, y)))
  131. if col == "S":
  132. shields.append(Shield((x, y)))
  133. x += 16
  134. y += 16
  135. x = 0
  136.  
  137. stig = 0# points
  138. start_ticks = pygame.time.get_ticks() #starter tick
  139. running = True
  140.  
  141. while running:
  142. clock.tick(60)
  143. sec = (pygame.time.get_ticks() - start_ticks) / 1000 # calculate how many seconds
  144.  
  145. for e in pygame.event.get():
  146. if e.type == pygame.QUIT:
  147. running = False
  148. if e.type == pygame.KEYDOWN and e.key == pygame.K_ESCAPE:
  149. running = False
  150.  
  151. # Move the player if an arrow key is pressed
  152. key = pygame.key.get_pressed()
  153. if key[pygame.K_LEFT]:
  154. player.move(-2, 0)
  155. if key[pygame.K_RIGHT]:
  156. player.move(2, 0)
  157. if key[pygame.K_UP]:
  158. player.move(0, -2)
  159. if key[pygame.K_DOWN]:
  160. player.move(0, 2)
  161.  
  162. # Draw the scene
  163. screen.fill((0, 0, 0))
  164. # every brick in the walls is drawn
  165. for brick in bricks:
  166. pygame.draw.rect(screen, (255, 255, 255), brick.rect)
  167.  
  168. # every shield is drawn
  169. for shield in shields:
  170. pygame.draw.rect(screen, (0, 0, 255), shield.rect)
  171. # every magic box is drawn
  172. for box in magic:
  173. pygame.draw.rect(screen, (0, 255, 0), box.rect)
  174.  
  175. # the others are drawn
  176.  
  177. #if player has more or equal to 1 point he changes color, yellow
  178. if len(playerInv) >= 1:
  179. pygame.draw.rect(screen, (255, 255, 0), player.rect)
  180. #if player does not have a point he will have the same color as he started with, stating he has no shield, pink
  181. else:
  182. pygame.draw.rect(screen, (255, 0, 255), player.rect)
  183.  
  184. if stig == 15:
  185. pygame.draw.rect(screen, (0, 255, 0), end_rect)
  186. else:
  187. pygame.draw.rect(screen, (255, 0, 0), end_rect)
  188.  
  189. #if you collide with shield
  190. for a_shield in shields:
  191. if player.rect.colliderect(a_shield.rect):
  192. print("Got myself a Shield")
  193. playerInv.append(a_shield)
  194. shields.remove(a_shield)
  195.  
  196. #if you collide with magic box
  197. for a_magic in magic:
  198. if len(playerInv) >= 1:
  199. if player.rect.colliderect(a_magic.rect):
  200. stig = stig + 1
  201. for i in magic:
  202. if player.rect.colliderect(i):
  203. magic.remove(i)
  204. print(stig)
  205. for x in playerInv:
  206. playerInv.remove(x)
  207.  
  208. #if player collides with magic box without having a shield, you loose
  209. elif len(playerInv) == 0:
  210. if player.rect.colliderect(a_magic.rect):
  211. print("-----GAME OVER-----")
  212. sleep(0.5)
  213. print(".")
  214. sleep(0.5)
  215. print(".")
  216. sleep(0.5)
  217. print(".")
  218. sleep(0.5)
  219. print("-----STATS-----")
  220. print("POINTS:", stig)
  221. print("TIME:", sec,"seconds")
  222. raise SystemExit
  223. #if the game timer runs out, you loose
  224. if sec > 150:
  225. print("-----GAME OVER-----")
  226. sleep(0.5)
  227. print(".")
  228. sleep(0.5)
  229. print(".")
  230. sleep(0.5)
  231. print(".")
  232. sleep(0.5)
  233. print("-----STATS-----")
  234. print("POINTS:",stig)
  235. print("TIME:",sec,"seconds")
  236. raise SystemExit
  237.  
  238.  
  239. pygame.display.flip()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement