Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2019
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.62 KB | None | 0 0
  1. import sys, pygame, math
  2.  
  3.  
  4. # Finished game author:
  5. #
  6. #
  7.  
  8. def load_piskell_sprite(sprite_folder_name, number_of_frames):
  9. frames = []
  10. padding = math.ceil(math.log(number_of_frames,10))
  11. for frame in range(number_of_frames):
  12. folder_and_file_name = sprite_folder_name + "/sprite_" + str(frame).rjust(padding,'0') +".png"
  13. frames.append(pygame.image.load(folder_and_file_name).convert_alpha())
  14. return frames
  15.  
  16. def bounce_rect_between_two_positions( rect, start_pos, end_pos, num_frame, frame_count ):
  17. if frame_count%num_frame < num_frame/2:
  18. new_pos_x = start_pos[0] + (end_pos[0] - start_pos[0]) * (frame_count%(num_frame/2))/(num_frame/2)
  19. new_pos_y = start_pos[1] + (end_pos[1] - start_pos[1]) * (frame_count%(num_frame/2))/(num_frame/2)
  20. else:
  21. new_pos_x = end_pos[0] + (start_pos[0] - end_pos[0]) * (frame_count%(num_frame/2))/(num_frame/2)
  22. new_pos_y = end_pos[1] + (start_pos[1] - end_pos[1]) * (frame_count%(num_frame/2))/(num_frame/2)
  23.  
  24. rect.center = (new_pos_x, new_pos_y)
  25.  
  26. def main():
  27.  
  28. pygame.init()
  29.  
  30. #-------TIMER----------
  31. font = pygame.font.Font(None, 54)
  32. font_color = pygame.Color('white')
  33.  
  34. passed_time = 0
  35. timer_started = False
  36.  
  37. #--------LEVEL 1 IMAGES-------------------
  38. map = pygame.image.load("AvoiderMap.png")
  39. map_size = map.get_size()
  40. map_rect = map.get_rect()
  41. screen = pygame.display.set_mode(map_size)
  42. map = map.convert()
  43.  
  44. platform1 = pygame.image.load("Platform.png")
  45. platform1_rect = platform1.get_rect()
  46. angle = 0
  47.  
  48. ded = pygame.image.load("death screen.png")
  49. ded_rect = ded.get_rect()
  50.  
  51. win_screen = pygame.image.load("peepo Yay.png")
  52. win_rect = win_screen.get_rect()
  53.  
  54. bad_guy = load_piskell_sprite("badguy", 21)
  55. bad_guy_rect = bad_guy[0].get_rect()
  56.  
  57. star = load_piskell_sprite("star", 2)
  58. star_rect = star[0].get_rect()
  59.  
  60. win_circle = load_piskell_sprite("Win Circle", 1)
  61. win_circle_rect = win_circle[0].get_rect()
  62.  
  63. sword = load_piskell_sprite("sword", 8)
  64. sword_rect = sword[0].get_rect()
  65. #----------LEVEL 2 IMAGES-------------------
  66. map2 = pygame.image.load("Map2.png")
  67. map2_rect = map2.get_rect()
  68.  
  69. gray_plat = pygame.image.load("platform2.png")
  70. gray_plat_rect = gray_plat.get_rect()
  71.  
  72. win2 = pygame.image.load("peepo Yay2.png")
  73. win2_rect = win2.get_rect()
  74.  
  75. kirby = load_piskell_sprite("kirby", 8)
  76. kirby_rect = kirby[0].get_rect()
  77.  
  78. sans = load_piskell_sprite("snAS", 70)
  79. sans_rect = sans[0].get_rect()
  80. #-------------------------------------------
  81. frame_count = 0;
  82. clock = pygame.time.Clock()
  83.  
  84. myfont = pygame.font.SysFont('monospace', 24)
  85.  
  86. #------VARIABLES------------
  87. is_alive = True
  88. start_clicked = False
  89. died = False
  90. won = False
  91. to_lvl2 = False
  92. lvl2_ded = False
  93. lvl2_ez_clap = False
  94. #---------------------------
  95. pygame.mouse.set_visible(False)
  96.  
  97. while is_alive:
  98.  
  99. ev = pygame.event.get()
  100.  
  101. if not to_lvl2:
  102. #---------LEVEL 1 MAP & SPRITES------
  103. screen.blit(map, map_rect)
  104.  
  105. platform1_rect.centerx = 550
  106. platform1_rect.centery = 250
  107.  
  108. angle += -1
  109. if angle <= -360:
  110. angle = 0
  111. rot_image = pygame.transform.rotate(platform1, angle)
  112. rot_im_rect = rot_image.get_rect()
  113. rot_im_rect.center = platform1_rect.center
  114. screen.blit(rot_image, rot_im_rect)
  115.  
  116. bounce_rect_between_two_positions(bad_guy_rect, (600, 350), (600, 150), 200, frame_count)
  117. screen.blit(bad_guy[frame_count % len(bad_guy)], bad_guy_rect)
  118.  
  119. bounce_rect_between_two_positions(win_circle_rect, (550, 250), (550, 250), 200, frame_count)
  120. screen.blit(win_circle[frame_count % len(win_circle)], win_circle_rect)
  121.  
  122. bounce_rect_between_two_positions(sword_rect, (350, 425), (750, 425), 200, frame_count)
  123. screen.blit(sword[frame_count % len(sword)], sword_rect)
  124.  
  125. label = myfont.render("Level 1", True, (0, 0, 0))
  126. screen.blit(label, (20, 20))
  127. else:
  128. #-----------LEVEL 2 MAP & SPRITES---------
  129. screen.blit(map2, map2_rect)
  130. gray_plat_rect.centerx = 415
  131. gray_plat_rect.centery = 240
  132.  
  133. if start_clicked and color_at_cursor == (255, 255, 255, 255) and to_lvl2:
  134. screen.blit(gray_plat, gray_plat_rect)
  135.  
  136. bounce_rect_between_two_positions(kirby_rect, (700, 100), (700, 400), 200, frame_count)
  137. screen.blit(kirby[frame_count % len(kirby)], kirby_rect)
  138.  
  139. bounce_rect_between_two_positions(sans_rect, (250, 450), (250, 450), 200, frame_count)
  140. screen.blit(sans[frame_count % len(sans)], sans_rect)
  141.  
  142. label = myfont.render("Level 2", True, (0, 0, 0))
  143. screen.blit(label, (20, 20))
  144.  
  145. for event in ev:
  146. if event.type == pygame.QUIT:
  147. is_alive = False
  148. if start_clicked:
  149. timer_started = True
  150. if timer_started:
  151. start_time = pygame.time.get_ticks()
  152. if won:
  153. timer_started = False
  154. if timer_started:
  155. passed_time = pygame.time.get_ticks() - start_time
  156. text = font.render(str(passed_time / 1000), True, font_color)
  157. screen.blit(text, (20, 450))
  158.  
  159. #
  160.  
  161. pos = pygame.mouse.get_pos()
  162. color_at_cursor = screen.get_at(pos)
  163.  
  164. if event.type == pygame.MOUSEBUTTONDOWN and color_at_cursor == (0, 0, 255, 255):
  165. print("Game Start")
  166. start_clicked = True
  167. if start_clicked and color_at_cursor == (0, 228, 255, 255):
  168. died = True
  169. if died:
  170. screen.blit(ded, ded_rect)
  171. if event.type == pygame.MOUSEBUTTONDOWN:
  172. is_alive = False
  173. if event.type == pygame.MOUSEBUTTONDOWN and color_at_cursor == (0, 255, 0, 255):
  174. if start_clicked:
  175. won = True
  176. print("You Win!")
  177. start_clicked = False
  178. if won:
  179. screen.blit(win_screen, win_rect)
  180. if event.type == pygame.KEYDOWN:
  181. to_lvl2 = True
  182. start_clicked = False
  183. won = False
  184. #----------LEVEL 2--------------
  185. pos = pygame.mouse.get_pos()
  186. color_at_cursor = screen.get_at(pos)
  187.  
  188. if event.type == pygame.MOUSEBUTTONDOWN and color_at_cursor == (0, 0, 255, 255) and to_lvl2:
  189. print("Game Start")
  190. start_clicked = True
  191. if start_clicked and color_at_cursor == (0, 229, 255, 255) and to_lvl2:
  192. lvl2_ded = True
  193. if lvl2_ded and to_lvl2:
  194. screen.blit(ded, ded_rect)
  195. if event.type == pygame.MOUSEBUTTONDOWN and to_lvl2:
  196. start_clicked = False
  197. lvl2_ded = False
  198. if event.type == pygame.MOUSEBUTTONDOWN and color_at_cursor == (0, 254, 0, 255) and to_lvl2:
  199. if start_clicked and to_lvl2:
  200. lvl2_ez_clap = True
  201. print("You Win!")
  202. start_clicked = False
  203. if lvl2_ez_clap and to_lvl2:
  204. screen.blit(win2, win2_rect)
  205. if event.type == pygame.KEYDOWN:
  206. is_alive = False
  207.  
  208. star_rect.center = pygame.mouse.get_pos()
  209. screen.blit(star[(frame_count//10)%len(star)], star_rect)
  210.  
  211. frame_count += 1
  212. pygame.display.update()
  213. clock.tick(30)
  214.  
  215. pygame.quit()
  216. sys.exit()
  217.  
  218.  
  219. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement