Advertisement
Guest User

Untitled

a guest
Jan 21st, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.54 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. #Obstacle Variables
  10. health_x = 100
  11. lives = 3
  12. #Variables
  13. level = 0
  14. health_x = 100
  15.  
  16. #Clock
  17. clock = pygame.time.Clock()
  18. timerDisplaySeconds = 0
  19. timerDisplayMinutes = 0
  20. timerDisplayMilliseconds = 0
  21.  
  22.  
  23. #Play background music
  24. pygame.mixer.music.load("Rick.mp3")
  25. pygame.mixer.music.set_volume(0.5)
  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 = 570
  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. #goal
  43. imageg = pygame.image.load("goal.png")
  44. goal = imageg.get_rect()
  45. goal.x = 500
  46. goal.y = 0
  47. #Jump Variables
  48. speed_x = 0
  49. speed_y = 0
  50. grav = 0.5
  51. accel = -7
  52. jumping = False
  53. playerFalling = False
  54. duckjumpupdate = ()
  55.  
  56. #Screen Background
  57. WIDTH = 600
  58. HEIGHT = 600
  59. screen_size=(WIDTH,HEIGHT)
  60. screen=pygame.display.set_mode(screen_size)
  61. background = (0,0,0)
  62. #Health Bar
  63. health = pygame.Rect(25, 20, health_x, 15)
  64. health_bar = pygame.Rect(20,15, 170, 25)
  65.  
  66. game_run = True
  67. while game_run:
  68.  
  69. if level == 0:
  70. game_menu()
  71.  
  72. #Background
  73. screen.fill(background)
  74.  
  75. speed_y += grav
  76.  
  77. events = pygame.event.get()
  78.  
  79. for event in events:
  80. if pygame.event == pygame.QUIT:
  81. exit()
  82. elif event.type == pygame.KEYDOWN:
  83. if not jumping and not playerFalling:
  84. if event.key == pygame.K_UP:
  85. # jumping acceleration
  86. speed_y = accel
  87. jumping = True
  88.  
  89.  
  90. #Get a list of all the buttons that were pressed
  91. keys_pressed = pygame.key.get_pressed()
  92.  
  93. #If cursor keys pressed
  94. if not jumping:
  95. speed_x = 0
  96.  
  97. if keys_pressed[pygame.K_LEFT] == 1:
  98. duckjumpupdate = ("Left")
  99. speed_x = -1
  100.  
  101. if keys_pressed[pygame.K_RIGHT] == 1:
  102. duckjumpupdate = ("Right")
  103. speed_x = 1
  104.  
  105. player = player.move(speed_x, speed_y)
  106.  
  107. #Set Restrictors
  108. if jumping or not jumping:
  109. if player.colliderect(restrictions[0]):
  110. player.x = 0
  111. if player.colliderect(restrictions[1]):
  112. player.x = 565
  113.  
  114. if level == 1:
  115. # Draw platforms
  116. for plat2 in platforms1_a:
  117. pygame.draw.rect(screen, (250, 250, 250), plat2)
  118.  
  119. if player.colliderect(plat2) and speed_y <= 0:
  120. speed_x = 0
  121. jumping = False
  122. speed_y += grav
  123. player.top = plat2.bottom
  124. playerFalling = True
  125.  
  126. for plat1 in platforms1_b:
  127. pygame.draw.rect(screen, (250, 0, 250), plat1)
  128.  
  129. if player.colliderect(plat1) and speed_y >= 0:
  130. speed_x = 0
  131. speed_y = 0
  132. player.bottom = plat1.top
  133. jumping = False
  134. playerFalling = False
  135.  
  136. if player.colliderect(floor1):
  137. speed_x = 0
  138. speed_y = 0
  139. player.bottom = floor1.top
  140. jumping = False
  141. playerFalling = False
  142.  
  143. #Draw door to next level
  144. screen.blit(imaged1, door1)
  145. #Draw obstacles
  146. for obs in obstacles1:
  147. pygame.draw.rect(screen, (210,220,20), obs)
  148.  
  149. #Draw floor
  150. pygame.draw.rect(screen, (250,0,250), floor1)
  151.  
  152. #move obstacles
  153. obstacles1[0] = obstacles1[0].move(-1,0)
  154. obstacles1[1] = obstacles1[1].move(1,0)
  155. obstacles1[2] = obstacles1[2].move(1.5,0)
  156. obstacles1[3] = obstacles1[3].move(0,2.5)
  157. obstacles1[4] = obstacles1[4].move(0,2.5)
  158. obstacles1[5] = obstacles1[5].move(0,2.5)
  159.  
  160. #If obstacles collide
  161. if obstacles1[0].colliderect(restrictions[0]):
  162. obstacles1[0] = pygame.Rect(600, 370, 10,10)
  163. if obstacles1[1].colliderect(restrictions[1]):
  164. obstacles1[1] = pygame.Rect(1,450,10,10)
  165. if obstacles1[2].colliderect(restrictions[1]):
  166. obstacles1[2] = pygame.Rect(1, 290, 10,10)
  167. if obstacles1[3].colliderect(plat1):
  168. obstacles1[3] = pygame.Rect(450, 0, 10,10)
  169. if obstacles1[4].colliderect(plat1):
  170. obstacles1[4] = pygame.Rect(350, 0, 10,10)
  171. if obstacles1[5].colliderect(plat1):
  172. obstacles1[5] = pygame.Rect(250, 0, 10,10)
  173. #move platforms
  174. platforms1_a[4] = platforms1_a[4].move(1,0)
  175. platforms1_b[4] = platforms1_b[4].move(1,0)
  176. if player.bottom == platforms1_b[4].top:
  177. player = player.move(1,0)
  178. platforms1_a[5] = platforms1_a[5].move(-1,0)
  179. platforms1_b[5] = platforms1_b[5].move(-1,0)
  180. if player.bottom == platforms1_b[5].top:
  181. player = player.move(-1,0)
  182.  
  183. if platforms1_a[4].left > 600:
  184. platforms1_a[4] = pygame.Rect(-100, 320, 100, 2)
  185. platforms1_b[4] = pygame.Rect(-100, 313, 100, 7)
  186. if platforms1_a[5].right < 0:
  187. platforms1_a[5] = pygame.Rect(600, 240, 100, 2)
  188. platforms1_b[5] = pygame.Rect(600, 233, 100, 7)
  189.  
  190. #Lose health when damaged
  191. for obs in obstacles1:
  192. if (player.colliderect(obs)) and health_x > 0:
  193. health_x = health_x - 1
  194. health = pygame.Rect(25, 20, health_x, 15)
  195. if health_x == 0 and lives > 1:
  196. lives = lives - 1
  197. health_x = 100
  198. if health_x == 0 and lives == 1:
  199. game_run = False
  200.  
  201. if player.colliderect(door1):
  202. level, player = level_counter(level,player)
  203.  
  204. if level == 2:
  205.  
  206. #platforms
  207. for plat2a in platforms2_a:
  208. pygame.draw.rect(screen, (250, 250, 250), plat2a)
  209.  
  210. if player.colliderect(plat2a) and (speed_y <= 0):
  211. speed_x = 0
  212. jumping = False
  213. speed_y += grav
  214. player.top = plat2a.bottom
  215. playerFalling = True
  216.  
  217. for plat2b in platforms2_b:
  218. pygame.draw.rect(screen, (65, 105, 225), plat2b)
  219.  
  220. if player.colliderect(plat2b) and (speed_y >= 0):
  221. speed_x = 0
  222. speed_y = 0
  223. player.bottom = plat2b.top
  224. jumping = False
  225. playerFalling = False
  226.  
  227. #Floor
  228. pygame.draw.rect(screen, (65,105,225), floor2)
  229.  
  230. #Draw door to next level
  231. screen.blit(imaged2, door2)
  232.  
  233. #if collide floor
  234. if player.colliderect(floor2):
  235. speed_x = 0
  236. speed_y = 0
  237. player.bottom = floor2.top
  238. jumping = False
  239. playerFalling = False
  240.  
  241.  
  242.  
  243. platforms2_a[1] = platforms2_a[1].move(-2,0)
  244. platforms2_b[1] = platforms2_b[1].move(-2,0)
  245. platforms2_a[2] = platforms2_a[2].move(2,0)
  246. platforms2_b[2] = platforms2_b[2].move(2,0)
  247. platforms2_a[7] = platforms2_a[7].move(0,2.3)
  248. platforms2_b[7] = platforms2_b[7].move(0,2.3)
  249.  
  250.  
  251. if platforms2_a[1].right < 0:
  252. platforms2_a[1] = pygame.Rect(600, 410, 40, 2)
  253. platforms2_b[1] = pygame.Rect(600, 403, 40, 7)
  254. if platforms2_a[2].left > 600:
  255. platforms2_a[2] = pygame.Rect(-80, 336, 40, 2)
  256. platforms2_b[2] = pygame.Rect(-80, 329, 40, 7)
  257. if platforms2_a[7].bottom > 187:
  258. platforms2_a[7] = pygame.Rect(120, 0,108, 2)
  259. platforms2_b[7] = pygame.Rect(120,-7,108, 7)
  260.  
  261. if player.bottom == platforms2_b[1].top:
  262. speed_x = -2
  263. if player.bottom == platforms2_b[2].top:
  264. speed_x = 2
  265. if player.bottom == platforms2_b[7].top:
  266. player = player.move(0,2.3)
  267.  
  268. for obs2 in obstacles2:
  269. pygame.draw.rect(screen, (210,160, 20), obs2)
  270.  
  271. #move obstacles
  272. obstacles2[0] = obstacles2[0].move(2.5,0)
  273. obstacles2[1] = obstacles2[1].move(2.5,0)
  274. obstacles2[2] = obstacles2[2].move(0,3)
  275. obstacles2[3] = obstacles2[3].move(0,3)
  276. obstacles2[4] = obstacles2[4].move(0,2.5)
  277. obstacles2[5] = obstacles2[5].move(0,2.5)
  278.  
  279. #if obs collide
  280. if obstacles2[0].colliderect(restrictions[1]):
  281. obstacles2[0] = pygame.Rect(0, 458, 10,10)
  282. if obstacles2[1].colliderect(restrictions[1]):
  283. obstacles2[1] = pygame.Rect(0, 458, 10,10)
  284. if obstacles2[2].colliderect(platforms2_b[3]):
  285. obstacles2[2] = pygame.Rect(525, 0, 10,10)
  286. if obstacles2[3].colliderect(platforms2_b[3]):
  287. obstacles2[3] = pygame.Rect(565, 0, 10,10)
  288. if obstacles2[4].colliderect(platforms2_b[5]):
  289. obstacles2[4] = pygame.Rect(442, 0, 10,10)
  290. if obstacles2[5].colliderect(platforms2_b[5]):
  291. obstacles2[5] = pygame.Rect(370, 0, 10,10)
  292.  
  293. for obs2 in obstacles2:
  294. if (player.colliderect(obs2)) and health_x > 0:
  295. health_x = health_x - 2
  296. health = pygame.Rect(25, 20, health_x, 15)
  297.  
  298. if player.colliderect(door2):
  299. level, player = level_counter(level,player)
  300.  
  301. if level == 3:
  302. #floor
  303. pygame.draw.rect(screen, (0,255,0), floor3)
  304. #if collide floor
  305. if player.colliderect(floor3):
  306. speed_x = 0
  307. speed_y = 0
  308. player.bottom = floor3.top
  309. jumping = False
  310. playerFalling = False
  311.  
  312. for plat3a in platforms3_a:
  313. pygame.draw.rect(screen, (250,250,250), plat3a)
  314.  
  315. if player.colliderect(plat3a) and (speed_y <= 0):
  316. speed_x = 0
  317. jumping = False
  318. speed_y += grav
  319. player.top = plat3a.bottom
  320. playerFalling = True
  321.  
  322. for plat3b in platforms3_b:
  323. pygame.draw.rect(screen, (0,255,0), plat3b)
  324. if player.colliderect(plat3b) and (speed_y >= 0):
  325. speed_x = 0
  326. speed_y = 0
  327. player.bottom = plat3b.top
  328. jumping = False
  329. playerFalling = False
  330.  
  331. platforms3_b[1] = platforms3_b[1].move(0,-1)
  332. platforms3_a[1] = platforms3_a[1].move(0,-1)
  333.  
  334. if platforms3_b[1].top < 418:
  335. platforms3_a[1] = pygame.Rect(0,555,100,2)
  336. platforms3_b[1] = pygame.Rect(0,548,100,7)
  337. #obstacles
  338. for obs3 in obstacles3:
  339. pygame.draw.rect(screen, (210,160,20), obs3)
  340. if player.colliderect(obs3):
  341. health_x = health_x - 1
  342. health = pygame.Rect(25, 20, health_x, 15)
  343. for superobs in superobstacle:
  344. pygame.draw.rect(screen, (255,0,0), superobs)
  345. if player.colliderect(superobs):
  346. health_x = health_x - 6
  347. health = health = pygame.Rect(25, 20, health_x, 15)
  348.  
  349. #Draw goal
  350. screen.blit(imageg, goal)
  351.  
  352. #Move objects
  353. #Move obstacles
  354. obstacles3[0] = obstacles3[0].move(0,1)
  355. obstacles3[1] = obstacles3[1].move(0,1)
  356. obstacles3[2] = obstacles3[2].move(0,1)
  357. obstacles3[3] = obstacles3[3].move(0,1)
  358. obstacles3[4] = obstacles3[4].move(-5,0)
  359. obstacles3[5] = obstacles3[5].move(3.5,0)
  360. superobstacle[0] = superobstacle[0].move(-4,0)
  361. superobstacle[1] = superobstacle[1].move(4,0)
  362. #Move platforms
  363. platforms3_a[2] = platforms3_a[2].move(-2,0)
  364. platforms3_b[2] = platforms3_b[2].move(-2,0)
  365. if platforms3_a[2].left < 0:
  366. platforms3_a[2].right = 600
  367. platforms3_b[2].right = 600
  368. platforms3_a[3] = platforms3_a[3].move(2,0)
  369. platforms3_b[3] = platforms3_b[3].move(2,0)
  370. if platforms3_a[3].right > 600:
  371. platforms3_a[3].left = 0
  372. platforms3_b[3].left = 0
  373. platforms3_a[4] = platforms3_a[4].move(0,-2)
  374. platforms3_b[4] = platforms3_b[4].move(0,-2)
  375. if platforms3_a[4].top < 105:
  376. platforms3_a[4].top = 250
  377. platforms3_b[4].top = 243
  378.  
  379. #Move player with mobile platforms
  380. if player.bottom == platforms3_b[2].top:
  381. player = player.move(-2,0)
  382. if player.bottom == platforms3_b[3].top:
  383. player = player.move(2,0)
  384. if player.bottom == platforms3_b[4].top:
  385. player = player.move(0,2)
  386.  
  387.  
  388.  
  389. if obstacles3[0].colliderect(floor3):
  390. obstacles3[0] = pygame.Rect(510, 427, 10,10)
  391. if obstacles3[1].colliderect(floor3):
  392. obstacles3[1] = pygame.Rect(410, 427, 10,10)
  393. if obstacles3[2].colliderect(floor3):
  394. obstacles3[2] = pygame.Rect(310, 427, 10,10)
  395. if obstacles3[3].colliderect(floor3):
  396. obstacles3[3] = pygame.Rect(210, 427, 10,10)
  397. if obstacles3[4].colliderect(restrictions[0]):
  398. obstacles3[4] = pygame.Rect(600, 520, 10,10)
  399. if obstacles3[5].colliderect(restrictions[1]):
  400. obstacles3[5] = pygame.Rect(0,387,10,10)
  401. if superobstacle[0].colliderect(restrictions[0]):
  402. superobstacle[0] = pygame.Rect(600,280,20,20)
  403. if superobstacle[1].colliderect(restrictions[1]):
  404. superobstacle[1] = pygame.Rect(0,200,20,20)
  405. if player.colliderect(goal):
  406. level = 0
  407.  
  408. if level != 0 and level != 4 :
  409.  
  410. #Update duck image
  411. x, y = duck_update(image, duckjumpupdate, player, jumping)
  412. player = player.move(speed_x, speed_y)
  413. # Draw health bar
  414. pygame.draw.rect(screen, (250, 250, 250), health_bar)
  415. # Draw health
  416. pygame.draw.rect(screen, (0, 255, 0), health)
  417. if lives > 2:
  418. screen.blit(IMAGE, life[0])
  419. if lives > 1:
  420. screen.blit(IMAGE, life[1])
  421. if lives > 0:
  422. screen.blit(IMAGE, life[2])
  423.  
  424. #Manage health
  425. if health_x <= 0 and lives > 1:
  426. lives = lives - 1
  427. health_x = 100
  428. if health_x <= 0 and lives == 1:
  429. game_run = False
  430. if health_x < 60:
  431. pygame.draw.rect(screen, (255, 165, 0), health)
  432. if health_x < 30:
  433. pygame.draw.rect(screen, (255,0,0), health)
  434.  
  435. textsurface = timer.render(str(timerDisplayMinutes) + ":" + "{:02}".format(timerDisplaySeconds), False, (255, 255, 255))
  436. screen.blit(textsurface,(200,16))
  437.  
  438. #Change clock
  439. clock.tick(60)
  440. timerDisplayMilliseconds += 1
  441. if timerDisplayMilliseconds == 60:
  442. timerDisplaySeconds += 1
  443. timerDisplayMilliseconds = 0
  444. if timerDisplaySeconds == 60:
  445. timerDisplayMinutes = min(59, timerDisplayMinutes + 1)
  446. timerDisplaySeconds = 0
  447.  
  448. #Display
  449. pygame.display.update()
  450. #Time delay
  451. pygame.time.delay(10)
  452.  
  453.  
  454. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement