Advertisement
Guest User

Untitled

a guest
Jan 21st, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.31 KB | None | 0 0
  1. import pygame
  2. import random
  3. from objects import *
  4. from functions import *
  5. pygame.mixer.pre_init()
  6. pygame.init()
  7. pygame.font.init()
  8. timer = pygame.font.SysFont('arial', 20)
  9.  
  10. #Obstacle Variables
  11. health_x = 100
  12. lives = 3
  13. #Variables
  14. level = 1
  15. health_x = 100
  16.  
  17. #Clock
  18. clock = pygame.time.Clock()
  19. timerDisplaySeconds = 0
  20. timerDisplayMinutes = 0
  21. timerDisplayMilliseconds = 0
  22.  
  23. #Play background music
  24. pygame.mixer.music.load("Rick.mp3")
  25. pygame.mixer.music.set_volume(0)
  26. pygame.mixer.music.play(-1)
  27. #Player
  28. #Player
  29. image = pygame.image.load("duckleft.png")
  30. player = image.get_rect()
  31. player.x = 500
  32. player.y = 555
  33. #Doors
  34. imaged1 = pygame.image.load("Teleporter1.png")
  35. door1 = imaged1.get_rect()
  36. door1.x = 570
  37. door1.y = 30
  38. imaged2 = pygame.image.load("Teleporter2.png")
  39. door2 = imaged2.get_rect()
  40. door2.x = 2
  41. door2.y = 59
  42. #Jump Variables
  43. speed_x = 0
  44. speed_y = 0
  45. grav = 0.5
  46. accel = -7
  47. jumping = False
  48. duckjumpupdate = ()
  49.  
  50. #Screen Background
  51. WIDTH = 600
  52. HEIGHT = 600
  53. screen_size=(WIDTH,HEIGHT)
  54. screen=pygame.display.set_mode(screen_size)
  55. background = (0,0,0)
  56. #Health Bar
  57. health = pygame.Rect(25, 20, health_x, 15)
  58. health_bar = pygame.Rect(20,15, 170, 25)
  59.  
  60. game_run = True
  61. while game_run:
  62.  
  63. #Background
  64. screen.fill(background)
  65.  
  66. speed_y += grav
  67.  
  68. events = pygame.event.get()
  69.  
  70. for event in events:
  71. if pygame.event == pygame.QUIT:
  72. exit()
  73. elif event.type == pygame.KEYDOWN:
  74. if not jumping:
  75. if event.key == pygame.K_UP:
  76. # jumping acceleration
  77. speed_y = accel
  78. jumping = True
  79.  
  80. #Get a list of all the buttons that were pressed
  81. keys_pressed = pygame.key.get_pressed()
  82.  
  83. #If cursor keys pressed
  84. if not jumping:
  85. speed_x = 0
  86.  
  87. if keys_pressed[pygame.K_LEFT] == 1:
  88. duckjumpupdate = ("Left")
  89. speed_x = -1
  90.  
  91. if keys_pressed[pygame.K_RIGHT] == 1:
  92. duckjumpupdate = ("Right")
  93. speed_x = 1
  94.  
  95. player = player.move(speed_x, speed_y)
  96.  
  97. #Set Restrictors
  98. if jumping or not jumping:
  99. if player.colliderect(restrictions[0]):
  100. player.x = 0
  101. if player.colliderect(restrictions[1]):
  102. player.x = 565
  103.  
  104. if level == 1:
  105. # Draw platforms
  106. for plat2 in platforms1_a:
  107. pygame.draw.rect(screen, (250, 250, 250), plat2)
  108.  
  109. if player.colliderect(plat2) and (speed_y <= 0):
  110. speed_x = 0
  111. jumping = False
  112. speed_y += grav
  113. player.top = plat2.bottom
  114.  
  115. for plat1 in platforms1_b:
  116. pygame.draw.rect(screen, (250, 0, 250), plat1)
  117.  
  118. if player.colliderect(plat1) and (speed_y >= 0):
  119. speed_x = 0
  120. speed_y = 0
  121. player.bottom = plat1.top
  122. jumping = False
  123.  
  124. if player.colliderect(floor1):
  125. speed_x = 0
  126. speed_y = 0
  127. player.bottom = floor1.top
  128. jumping = False
  129. #Draw door to next level
  130. screen.blit(imaged1, door1)
  131. #Draw obstacles
  132. for obs in obstacles1:
  133. pygame.draw.rect(screen, (210,220,20), obs)
  134.  
  135. #Draw floor
  136. pygame.draw.rect(screen, (250,0,250), floor1)
  137.  
  138. #move obstacles
  139. obstacles1[0] = obstacles1[0].move(-1,0)
  140. obstacles1[1] = obstacles1[1].move(1,0)
  141. obstacles1[2] = obstacles1[2].move(1.5,0)
  142. obstacles1[3] = obstacles1[3].move(0,2.5)
  143. obstacles1[4] = obstacles1[4].move(0,2.5)
  144. obstacles1[5] = obstacles1[5].move(0,2.5)
  145.  
  146. #If obstacles collide
  147. if obstacles1[0].colliderect(restrictions[0]):
  148. obstacles1[0] = pygame.Rect(600, 370, 10,10)
  149. if obstacles1[1].colliderect(restrictions[1]):
  150. obstacles1[1] = pygame.Rect(1,450,10,10)
  151. if obstacles1[2].colliderect(restrictions[1]):
  152. obstacles1[2] = pygame.Rect(1, 290, 10,10)
  153. if obstacles1[3].colliderect(plat1):
  154. obstacles1[3] = pygame.Rect(450, 0, 10,10)
  155. if obstacles1[4].colliderect(plat1):
  156. obstacles1[4] = pygame.Rect(350, 0, 10,10)
  157. if obstacles1[5].colliderect(plat1):
  158. obstacles1[5] = pygame.Rect(250, 0, 10,10)
  159. #move platforms
  160. platforms1_a[4] = platforms1_a[4].move(1,0)
  161. platforms1_b[4] = platforms1_b[4].move(1,0)
  162. if player.bottom == platforms1_b[4].top:
  163. player = player.move(1,0)
  164. platforms1_a[5] = platforms1_a[5].move(-1,0)
  165. platforms1_b[5] = platforms1_b[5].move(-1,0)
  166. if player.bottom == platforms1_b[5].top:
  167. player = player.move(-1,0)
  168.  
  169. if platforms1_a[4].left > 600:
  170. platforms1_a[4] = pygame.Rect(-100, 320, 100, 2)
  171. platforms1_b[4] = pygame.Rect(-100, 313, 100, 7)
  172. if platforms1_a[5].right < 0:
  173. platforms1_a[5] = pygame.Rect(600, 240, 100, 2)
  174. platforms1_b[5] = pygame.Rect(600, 233, 100, 7)
  175.  
  176. #Lose health when damaged
  177. for obs in obstacles1:
  178. if (player.colliderect(obs)) and health_x > 0:
  179. health_x = health_x - 1
  180. health = pygame.Rect(25, 20, health_x, 15)
  181. if health_x == 0 and lives > 1:
  182. lives = lives - 1
  183. health_x = 100
  184. if health_x == 0 and lives == 1:
  185. game_run = False
  186.  
  187. if player.colliderect(door1):
  188. level, player = level_counter(level,player)
  189.  
  190. if level == 2:
  191.  
  192. #platforms
  193. for plat2a in platforms2_a:
  194. pygame.draw.rect(screen, (250, 250, 250), plat2a)
  195.  
  196. if player.colliderect(plat2a) and (speed_y <= 0):
  197. speed_x = 0
  198. jumping = False
  199. speed_y += grav
  200. player.top = plat2a.bottom
  201.  
  202. for plat2b in platforms2_b:
  203. pygame.draw.rect(screen, (65, 105, 225), plat2b)
  204.  
  205. if player.colliderect(plat2b) and (speed_y >= 0):
  206. speed_x = 0
  207. speed_y = 0
  208. player.bottom = plat2b.top
  209. jumping = False
  210.  
  211. #Floor
  212. pygame.draw.rect(screen, (65,105,225), floor2)
  213.  
  214. #Draw door to next level
  215. screen.blit(imaged2, door2)
  216.  
  217. #if collide floor
  218. if player.colliderect(floor2):
  219. speed_x = 0
  220. speed_y = 0
  221. player.bottom = floor2.top
  222. jumping = False
  223.  
  224.  
  225.  
  226. platforms2_a[1] = platforms2_a[1].move(-2,0)
  227. platforms2_b[1] = platforms2_b[1].move(-2,0)
  228. platforms2_a[2] = platforms2_a[2].move(2,0)
  229. platforms2_b[2] = platforms2_b[2].move(2,0)
  230. platforms2_a[7] = platforms2_a[7].move(0,2.3)
  231. platforms2_b[7] = platforms2_b[7].move(0,2.3)
  232.  
  233.  
  234. if platforms2_a[1].right < 0:
  235. platforms2_a[1] = pygame.Rect(600, 410, 40, 2)
  236. platforms2_b[1] = pygame.Rect(600, 403, 40, 7)
  237. if platforms2_a[2].left > 600:
  238. platforms2_a[2] = pygame.Rect(-80, 336, 40, 2)
  239. platforms2_b[2] = pygame.Rect(-80, 329, 40, 7)
  240. if platforms2_a[7].bottom > 187:
  241. platforms2_a[7] = pygame.Rect(120, 0,108, 2)
  242. platforms2_b[7] = pygame.Rect(120,-7,108, 7)
  243.  
  244. if player.bottom == platforms2_b[1].top:
  245. speed_x = -2
  246. if player.bottom == platforms2_b[2].top:
  247. speed_x = 2
  248. if player.bottom == platforms2_b[7].top:
  249. player = player.move(0,2.3)
  250.  
  251. for obs2 in obstacles2:
  252. pygame.draw.rect(screen, (210,160, 20), obs2)
  253.  
  254. #move obstacles
  255. obstacles2[0] = obstacles2[0].move(2.5,0)
  256. obstacles2[1] = obstacles2[1].move(2.5,0)
  257. obstacles2[2] = obstacles2[2].move(0,3)
  258. obstacles2[3] = obstacles2[3].move(0,3)
  259. obstacles2[4] = obstacles2[4].move(0,2.5)
  260. obstacles2[5] = obstacles2[5].move(0,2.5)
  261.  
  262. #if obs collide
  263. if obstacles2[0].colliderect(restrictions[1]):
  264. obstacles2[0] = pygame.Rect(0, 458, 10,10)
  265. if obstacles2[1].colliderect(restrictions[1]):
  266. obstacles2[1] = pygame.Rect(0, 458, 10,10)
  267. if obstacles2[2].colliderect(platforms2_b[3]):
  268. obstacles2[2] = pygame.Rect(525, 0, 10,10)
  269. if obstacles2[3].colliderect(platforms2_b[3]):
  270. obstacles2[3] = pygame.Rect(565, 0, 10,10)
  271. if obstacles2[4].colliderect(platforms2_b[5]):
  272. obstacles2[4] = pygame.Rect(442, 0, 10,10)
  273. if obstacles2[5].colliderect(platforms2_b[5]):
  274. obstacles2[5] = pygame.Rect(370, 0, 10,10)
  275.  
  276. for obs2 in obstacles2:
  277. if (player.colliderect(obs2)) and health_x > 0:
  278. health_x = health_x - 2
  279. health = pygame.Rect(25, 20, health_x, 15)
  280.  
  281. if player.colliderect(door2):
  282. level, player = level_counter(level,player)
  283.  
  284. if level == 3:
  285. #floor
  286. pygame.draw.rect(screen, (0,255,0), floor3)
  287. #if collide floor
  288. if player.colliderect(floor3):
  289. speed_x = 0
  290. speed_y = 0
  291. player.bottom = floor3.top
  292. jumping = False
  293.  
  294. for plat3a in platforms3_a:
  295. pygame.draw.rect(screen (250,250,250), plat3a)
  296.  
  297. for plat3b in platforms3_b:
  298. pygame.draw.rect(screen (0,255,0), plat3b)
  299. if player.colliderect(plat3b) and (speed_y >= 0):
  300. speed_x = 0
  301. speed_y = 0
  302. player.bottom = plat3b.top
  303. jumping = False
  304.  
  305. #Update duck image
  306. x, y = duck_update(image, duckjumpupdate, player, jumping)
  307. player = player.move(speed_x, speed_y)
  308. # Draw health bar
  309. pygame.draw.rect(screen, (250, 250, 250), health_bar)
  310. # Draw health
  311. pygame.draw.rect(screen, (0, 255, 0), health)
  312. if lives > 2:
  313. screen.blit(IMAGE, life[0])
  314. if lives > 1:
  315. screen.blit(IMAGE, life[1])
  316. if lives > 0:
  317. screen.blit(IMAGE, life[2])
  318.  
  319. #Manage health
  320. if health_x <= 0 and lives > 1:
  321. lives = lives - 1
  322. health_x = 100
  323. if health_x <= 0 and lives == 1:
  324. game_run = False
  325. if health_x < 60:
  326. pygame.draw.rect(screen, (255, 165, 0), health)
  327. if health_x < 30:
  328. pygame.draw.rect(screen, (255,0,0), health)
  329.  
  330. textsurface = timer.render(str(timerDisplayMinutes) + ":" + "{:02}".format(timerDisplaySeconds), False, (255, 255, 255))
  331. screen.blit(textsurface,(200,16))
  332.  
  333. #Change clock
  334. clock.tick(60)
  335. timerDisplayMilliseconds += 1
  336. if timerDisplayMilliseconds == 60:
  337. timerDisplaySeconds += 1
  338. timerDisplayMilliseconds = 0
  339. if timerDisplaySeconds == 60:
  340. timerDisplayMinutes = min(59, timerDisplayMinutes + 1)
  341. timerDisplaySeconds = 0
  342. #Display
  343. pygame.display.update()
  344. #Time delay
  345. pygame.time.delay(10)
  346.  
  347.  
  348. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement