Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- from sys import exit
- from random import randint
- def display_score():
- current_time = int(pygame.time.get_ticks() / 1000)- start_time
- score_surf = font.render(f'{current_time}',False,('#ffffff'))
- score_rect = score_surf.get_rect(center = (400,50))
- screen.blit(score_surf,score_rect)
- return current_time
- def obstacle_movement(obstacle_list):
- if obstacle_list:
- for obstacle_rect in obstacle_list:
- obstacle_rect.x -= 5
- if obstacle_rect.bottom == 315:
- screen.blit(bcat_surf,obstacle_rect)
- else: screen.blit(bat_surf,obstacle_rect)
- obstacle_list = [obstacle for obstacle in obstacle_list if obstacle.x > -100]
- return obstacle_list
- else: return []
- def collisions(player, obstacles):
- if obstacles:
- for obstacle_rect in obstacles:
- if player.colliderect(obstacle_rect):
- return False
- return True
- def player_animation():
- global player_surf, player_index
- if player_rect.bottom < 250:
- player_surf = player_jump
- else:
- player_index += 0.1
- if player_index>= len(player_walk):player_index = 0
- player_surf = player_walk[int(player_index)]
- player_surf = pygame.transform.scale(player_surf, (115, 160))
- pygame.init()
- screen = pygame.display.set_mode((800,400))
- pygame.display.set_caption('Cy Run')
- clock = pygame.time.Clock()
- font = pygame.font.Font('fonts/Pixeltype.ttf', 50)
- font2 = pygame.font.Font('fonts/Arcade.otf', 50)
- game_active = False
- start_time = 0
- score = 0
- title_music = pygame.mixer.Sound('audio/Title.mp3')
- game_music = pygame.mixer.Sound('audio/Mos_Eisley_Cantina.mp3')
- bg_music_playing = False
- icon_image = pygame.image.load('graphics/icon.png')
- pygame.display.set_icon(icon_image)
- startbg_surf = pygame.image.load('graphics/Start.png').convert()
- sky_surface = pygame.image.load('graphics/Sky.png').convert()
- sky_x_pos = 0
- ground_surf = pygame.image.load('graphics/ground.png').convert()
- ground_x_pos = 0
- ground_y_pos = screen.get_height() - ground_surf.get_height() + 24
- #bcat
- bcat_1 = pygame.image.load('graphics/bcat/bcat1.png').convert_alpha()
- bcat_1 = pygame.transform.scale(bcat_1, (67,67))
- bcat_2 = pygame.image.load('graphics/bcat/bcat2.png').convert_alpha()
- bcat_2 = pygame.transform.scale(bcat_2, (67,67))
- bcat_3 = pygame.image.load('graphics/bcat/bcat3.png').convert_alpha()
- bcat_3 = pygame.transform.scale(bcat_3, (67,67))
- bcat_4 = pygame.image.load('graphics/bcat/bcat4.png').convert_alpha()
- bcat_4 = pygame.transform.scale(bcat_4, (67,67))
- bcat_frames = [bcat_1,bcat_2,bcat_3,bcat_4]
- bcat_index = 0
- bcat_surf = bcat_frames[bcat_index]
- #bat
- bat_1 = pygame.image.load('graphics/bat/bat1.png').convert_alpha()
- bat_1 = pygame.transform.scale(bat_1, (80,80))
- bat_2 = pygame.image.load('graphics/bat/bat2.png').convert_alpha()
- bat_2 = pygame.transform.scale(bat_2, (80,80))
- bat_3 = pygame.image.load('graphics/bat/bat3.png').convert_alpha()
- bat_3 = pygame.transform.scale(bat_3, (80,80))
- bat_4 = pygame.image.load('graphics/bat/bat4.png').convert_alpha()
- bat_4 = pygame.transform.scale(bat_4, (80,80))
- bat_frames = [bat_1,bat_2,bat_3,bat_4]
- bat_index = 0
- bat_surf = bat_frames[bat_index]
- obstacle_rect_list = []
- player_walk1 = pygame.image.load('graphics/player/player_walk1.png').convert_alpha()
- player_walk2 = pygame.image.load('graphics/player/player_walk2.png').convert_alpha()
- player_walk3 = pygame.image.load('graphics/player/player_walk3.png').convert_alpha()
- player_walk = [player_walk1,player_walk2,player_walk3]
- player_index = 0
- player_jump = pygame.image.load('graphics/player/jump.png').convert_alpha()
- player_jump = pygame.transform.scale(player_jump, (115, 160))
- player_surf = player_walk[player_index]
- player_rect = player_surf.get_rect(midbottom = (80,300))
- player_gravity = 0
- player_stand = pygame.image.load('graphics/player/player_stand.png').convert_alpha()
- player_stand = pygame.transform.scale2x(player_stand)
- player_stand_rect = player_stand.get_rect(center = (400,200))
- game_name = font.render ('Cy Run', False, ('#ffffff'))
- game_name_rect = game_name.get_rect(center = (400,80))
- game_message = font2.render('PRESS SPACE TO RUN', False,('#ffffff'))
- game_message = pygame.transform.scale(game_message, (int(game_message.get_width() * 0.4), int(game_message.get_height() * 0.4)))
- game_message_rect = game_message.get_rect(midright = (750,365))
- #obs timer
- obstacle_timer = pygame.USEREVENT + 1
- pygame.time.set_timer(obstacle_timer,1500)
- bcat_ani_timer = pygame.USEREVENT +2
- pygame.time.set_timer(bcat_ani_timer,200)
- bat_ani_timer = pygame.USEREVENT +3
- pygame.time.set_timer(bat_ani_timer,200)
- title_music.play()
- while True:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- pygame.quit()
- exit()
- if game_active:
- if event.type == pygame.MOUSEMOTION:
- if player_rect.collidepoint(event.pos):
- player_gravity = -20
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_SPACE and player_rect.bottom >= 250:
- player_gravity = -20
- else:
- if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
- game_active = True
- start_time = int(pygame.time.get_ticks() / 1000)
- bg_music_playing = False
- title_music.play()
- if game_active:
- if event.type == obstacle_timer and game_active:
- if randint(0,2):
- obstacle_rect_list.append(bcat_surf.get_rect(bottomright=(randint(900,1100),315)))
- else:
- obstacle_rect_list.append(bat_surf.get_rect(bottomright=(randint(900,1100),160)))
- if event.type == bcat_ani_timer:
- if bcat_index == 0: bcat_index = 1
- else: bcat_index = 0
- bcat_surf = bcat_frames[bcat_index]
- if event.type == bat_ani_timer:
- if bat_index == 0: bat_index = 1
- else: bat_index = 0
- bat_surf = bat_frames[bat_index]
- if game_active:
- sky_surface = pygame.transform.scale(sky_surface, (800, 300))
- screen.blit(sky_surface, (sky_x_pos, 0))
- screen.blit(sky_surface, (sky_x_pos + sky_surface.get_width(), 0))
- sky_x_pos -= 2
- if sky_x_pos <= -sky_surface.get_width():
- sky_x_pos = 0
- score = display_score()
- ground_surf = pygame.transform.scale(ground_surf, (800, screen.get_height() - ground_y_pos))
- screen.blit(ground_surf, (ground_x_pos, ground_y_pos))
- screen.blit(ground_surf, (ground_x_pos + ground_surf.get_width(), ground_y_pos))
- ground_x_pos -= 2
- if ground_x_pos <= -ground_surf.get_width():
- ground_x_pos = 0
- #player
- player_gravity += 1.2
- player_rect.y += player_gravity
- if player_rect.bottom >= 250: player_rect.bottom = 250
- player_animation()
- screen.blit(player_surf, player_rect)
- #obstacle movement
- obstacle_rect_list = obstacle_movement(obstacle_rect_list)
- #collision
- game_active = collisions(player_rect,obstacle_rect_list)
- if not bg_music_playing:
- game_music.play()
- bg_music_playing = True
- title_music.stop()
- else:
- startbg_surf = pygame.transform.scale(startbg_surf, (800, 400))
- screen.blit(startbg_surf, (0, 0))
- obstacle_rect_list.clear()
- player_rect.midbottom = (80,300)
- player_gravity = 0
- score_message = font.render(f'Score: {score}',False,('#ffffff'))
- score_message_rect = score_message.get_rect(midright = (750,365))
- if score == 0:
- screen.blit(game_message,game_message_rect)
- else:
- screen.blit(score_message,score_message_rect)
- if bg_music_playing:
- game_music.stop()
- bg_music_playing = False
- title_music.play()
- pygame.display.update()
- clock.tick(60)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement