Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys, pygame, math
- # Finished game author:
- #
- #
- def load_piskell_sprite(sprite_folder_name, number_of_frames):
- frames = []
- padding = math.ceil(math.log(number_of_frames,10))
- for frame in range(number_of_frames):
- folder_and_file_name = sprite_folder_name + "/sprite_" + str(frame).rjust(padding,'0') +".png"
- frames.append(pygame.image.load(folder_and_file_name).convert_alpha())
- return frames
- def bounce_rect_between_two_positions( rect, start_pos, end_pos, num_frame, frame_count ):
- if frame_count%num_frame < num_frame/2:
- new_pos_x = start_pos[0] + (end_pos[0] - start_pos[0]) * (frame_count%(num_frame/2))/(num_frame/2)
- new_pos_y = start_pos[1] + (end_pos[1] - start_pos[1]) * (frame_count%(num_frame/2))/(num_frame/2)
- else:
- new_pos_x = end_pos[0] + (start_pos[0] - end_pos[0]) * (frame_count%(num_frame/2))/(num_frame/2)
- new_pos_y = end_pos[1] + (start_pos[1] - end_pos[1]) * (frame_count%(num_frame/2))/(num_frame/2)
- rect.center = (new_pos_x, new_pos_y)
- def main():
- pygame.init()
- #-------TIMER----------
- font = pygame.font.Font(None, 54)
- font_color = pygame.Color('white')
- passed_time = 0
- timer_started = False
- #--------LEVEL 1 IMAGES-------------------
- map = pygame.image.load("AvoiderMap.png")
- map_size = map.get_size()
- map_rect = map.get_rect()
- screen = pygame.display.set_mode(map_size)
- map = map.convert()
- platform1 = pygame.image.load("Platform.png")
- platform1_rect = platform1.get_rect()
- angle = 0
- ded = pygame.image.load("death screen.png")
- ded_rect = ded.get_rect()
- win_screen = pygame.image.load("peepo Yay.png")
- win_rect = win_screen.get_rect()
- bad_guy = load_piskell_sprite("badguy", 21)
- bad_guy_rect = bad_guy[0].get_rect()
- star = load_piskell_sprite("star", 2)
- star_rect = star[0].get_rect()
- win_circle = load_piskell_sprite("Win Circle", 1)
- win_circle_rect = win_circle[0].get_rect()
- sword = load_piskell_sprite("sword", 8)
- sword_rect = sword[0].get_rect()
- #----------LEVEL 2 IMAGES-------------------
- map2 = pygame.image.load("Map2.png")
- map2_rect = map2.get_rect()
- gray_plat = pygame.image.load("platform2.png")
- gray_plat_rect = gray_plat.get_rect()
- win2 = pygame.image.load("peepo Yay2.png")
- win2_rect = win2.get_rect()
- kirby = load_piskell_sprite("kirby", 8)
- kirby_rect = kirby[0].get_rect()
- sans = load_piskell_sprite("snAS", 70)
- sans_rect = sans[0].get_rect()
- #-------------------------------------------
- frame_count = 0;
- clock = pygame.time.Clock()
- myfont = pygame.font.SysFont('monospace', 24)
- #------VARIABLES------------
- is_alive = True
- start_clicked = False
- died = False
- won = False
- to_lvl2 = False
- lvl2_ded = False
- lvl2_ez_clap = False
- #---------------------------
- pygame.mouse.set_visible(False)
- while is_alive:
- ev = pygame.event.get()
- if not to_lvl2:
- #---------LEVEL 1 MAP & SPRITES------
- screen.blit(map, map_rect)
- platform1_rect.centerx = 550
- platform1_rect.centery = 250
- angle += -1
- if angle <= -360:
- angle = 0
- rot_image = pygame.transform.rotate(platform1, angle)
- rot_im_rect = rot_image.get_rect()
- rot_im_rect.center = platform1_rect.center
- screen.blit(rot_image, rot_im_rect)
- bounce_rect_between_two_positions(bad_guy_rect, (600, 350), (600, 150), 200, frame_count)
- screen.blit(bad_guy[frame_count % len(bad_guy)], bad_guy_rect)
- bounce_rect_between_two_positions(win_circle_rect, (550, 250), (550, 250), 200, frame_count)
- screen.blit(win_circle[frame_count % len(win_circle)], win_circle_rect)
- bounce_rect_between_two_positions(sword_rect, (350, 425), (750, 425), 200, frame_count)
- screen.blit(sword[frame_count % len(sword)], sword_rect)
- label = myfont.render("Level 1", True, (0, 0, 0))
- screen.blit(label, (20, 20))
- else:
- #-----------LEVEL 2 MAP & SPRITES---------
- screen.blit(map2, map2_rect)
- gray_plat_rect.centerx = 415
- gray_plat_rect.centery = 240
- if start_clicked and color_at_cursor == (255, 255, 255, 255) and to_lvl2:
- screen.blit(gray_plat, gray_plat_rect)
- bounce_rect_between_two_positions(kirby_rect, (700, 100), (700, 400), 200, frame_count)
- screen.blit(kirby[frame_count % len(kirby)], kirby_rect)
- bounce_rect_between_two_positions(sans_rect, (250, 450), (250, 450), 200, frame_count)
- screen.blit(sans[frame_count % len(sans)], sans_rect)
- label = myfont.render("Level 2", True, (0, 0, 0))
- screen.blit(label, (20, 20))
- for event in ev:
- if event.type == pygame.QUIT:
- is_alive = False
- if start_clicked:
- timer_started = True
- if timer_started:
- start_time = pygame.time.get_ticks()
- if won:
- timer_started = False
- if timer_started:
- passed_time = pygame.time.get_ticks() - start_time
- text = font.render(str(passed_time / 1000), True, font_color)
- screen.blit(text, (20, 450))
- #
- pos = pygame.mouse.get_pos()
- color_at_cursor = screen.get_at(pos)
- if event.type == pygame.MOUSEBUTTONDOWN and color_at_cursor == (0, 0, 255, 255):
- print("Game Start")
- start_clicked = True
- if start_clicked and color_at_cursor == (0, 228, 255, 255):
- died = True
- if died:
- screen.blit(ded, ded_rect)
- if event.type == pygame.MOUSEBUTTONDOWN:
- is_alive = False
- if event.type == pygame.MOUSEBUTTONDOWN and color_at_cursor == (0, 255, 0, 255):
- if start_clicked:
- won = True
- print("You Win!")
- start_clicked = False
- if won:
- screen.blit(win_screen, win_rect)
- if event.type == pygame.KEYDOWN:
- to_lvl2 = True
- start_clicked = False
- won = False
- #----------LEVEL 2--------------
- pos = pygame.mouse.get_pos()
- color_at_cursor = screen.get_at(pos)
- if event.type == pygame.MOUSEBUTTONDOWN and color_at_cursor == (0, 0, 255, 255) and to_lvl2:
- print("Game Start")
- start_clicked = True
- if start_clicked and color_at_cursor == (0, 229, 255, 255) and to_lvl2:
- lvl2_ded = True
- if lvl2_ded and to_lvl2:
- screen.blit(ded, ded_rect)
- if event.type == pygame.MOUSEBUTTONDOWN and to_lvl2:
- start_clicked = False
- lvl2_ded = False
- if event.type == pygame.MOUSEBUTTONDOWN and color_at_cursor == (0, 254, 0, 255) and to_lvl2:
- if start_clicked and to_lvl2:
- lvl2_ez_clap = True
- print("You Win!")
- start_clicked = False
- if lvl2_ez_clap and to_lvl2:
- screen.blit(win2, win2_rect)
- if event.type == pygame.KEYDOWN:
- is_alive = False
- star_rect.center = pygame.mouse.get_pos()
- screen.blit(star[(frame_count//10)%len(star)], star_rect)
- frame_count += 1
- pygame.display.update()
- clock.tick(30)
- pygame.quit()
- sys.exit()
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement