Advertisement
cypherine

Cy Run Running Game

Jul 14th, 2023
1,122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.17 KB | Gaming | 0 0
  1. import pygame
  2. from sys import exit
  3. from random import randint
  4.  
  5. def display_score():
  6.     current_time = int(pygame.time.get_ticks() / 1000)- start_time
  7.     score_surf = font.render(f'{current_time}',False,('#ffffff'))
  8.     score_rect = score_surf.get_rect(center = (400,50))
  9.     screen.blit(score_surf,score_rect)
  10.     return current_time
  11.  
  12. def obstacle_movement(obstacle_list):
  13.     if obstacle_list:
  14.         for obstacle_rect in obstacle_list:
  15.             obstacle_rect.x -= 5
  16.  
  17.             if obstacle_rect.bottom == 315:
  18.                 screen.blit(bcat_surf,obstacle_rect)
  19.             else: screen.blit(bat_surf,obstacle_rect)
  20.  
  21.         obstacle_list = [obstacle for obstacle in obstacle_list if obstacle.x > -100]
  22.  
  23.         return obstacle_list
  24.     else: return []
  25.  
  26. def collisions(player, obstacles):
  27.     if obstacles:
  28.         for obstacle_rect in obstacles:
  29.             if player.colliderect(obstacle_rect):
  30.                 return False
  31.     return True
  32.  
  33. def player_animation():
  34.     global player_surf, player_index
  35.    
  36.     if player_rect.bottom < 250:
  37.         player_surf = player_jump
  38.     else:
  39.         player_index += 0.1
  40.         if player_index>= len(player_walk):player_index = 0
  41.         player_surf = player_walk[int(player_index)]
  42.         player_surf = pygame.transform.scale(player_surf, (115, 160))
  43.  
  44.  
  45. pygame.init()
  46. screen = pygame.display.set_mode((800,400))
  47. pygame.display.set_caption('Cy Run')
  48. clock = pygame.time.Clock()
  49. font = pygame.font.Font('fonts/Pixeltype.ttf', 50)
  50. font2 = pygame.font.Font('fonts/Arcade.otf', 50)
  51. game_active = False
  52. start_time = 0
  53. score = 0
  54.  
  55. title_music = pygame.mixer.Sound('audio/Title.mp3')
  56. game_music = pygame.mixer.Sound('audio/Mos_Eisley_Cantina.mp3')
  57. bg_music_playing = False
  58.  
  59. icon_image = pygame.image.load('graphics/icon.png')
  60. pygame.display.set_icon(icon_image)
  61.  
  62. startbg_surf = pygame.image.load('graphics/Start.png').convert()
  63.  
  64. sky_surface = pygame.image.load('graphics/Sky.png').convert()
  65. sky_x_pos = 0
  66.  
  67. ground_surf = pygame.image.load('graphics/ground.png').convert()
  68. ground_x_pos = 0
  69. ground_y_pos = screen.get_height() - ground_surf.get_height() + 24
  70.  
  71. #bcat
  72. bcat_1 = pygame.image.load('graphics/bcat/bcat1.png').convert_alpha()
  73. bcat_1 = pygame.transform.scale(bcat_1, (67,67))
  74. bcat_2 = pygame.image.load('graphics/bcat/bcat2.png').convert_alpha()
  75. bcat_2 = pygame.transform.scale(bcat_2, (67,67))
  76. bcat_3 = pygame.image.load('graphics/bcat/bcat3.png').convert_alpha()
  77. bcat_3 = pygame.transform.scale(bcat_3, (67,67))
  78. bcat_4 = pygame.image.load('graphics/bcat/bcat4.png').convert_alpha()
  79. bcat_4 = pygame.transform.scale(bcat_4, (67,67))
  80. bcat_frames = [bcat_1,bcat_2,bcat_3,bcat_4]
  81. bcat_index = 0
  82. bcat_surf = bcat_frames[bcat_index]
  83.  
  84. #bat
  85. bat_1 = pygame.image.load('graphics/bat/bat1.png').convert_alpha()
  86. bat_1 = pygame.transform.scale(bat_1, (80,80))
  87. bat_2 = pygame.image.load('graphics/bat/bat2.png').convert_alpha()
  88. bat_2 = pygame.transform.scale(bat_2, (80,80))
  89. bat_3 = pygame.image.load('graphics/bat/bat3.png').convert_alpha()
  90. bat_3 = pygame.transform.scale(bat_3, (80,80))
  91. bat_4 = pygame.image.load('graphics/bat/bat4.png').convert_alpha()
  92. bat_4 = pygame.transform.scale(bat_4, (80,80))
  93. bat_frames = [bat_1,bat_2,bat_3,bat_4]
  94. bat_index = 0
  95. bat_surf = bat_frames[bat_index]
  96.  
  97. obstacle_rect_list = []
  98.  
  99. player_walk1 = pygame.image.load('graphics/player/player_walk1.png').convert_alpha()
  100. player_walk2 = pygame.image.load('graphics/player/player_walk2.png').convert_alpha()
  101. player_walk3 = pygame.image.load('graphics/player/player_walk3.png').convert_alpha()
  102. player_walk = [player_walk1,player_walk2,player_walk3]
  103. player_index = 0
  104. player_jump = pygame.image.load('graphics/player/jump.png').convert_alpha()
  105. player_jump = pygame.transform.scale(player_jump, (115, 160))
  106. player_surf = player_walk[player_index]
  107. player_rect = player_surf.get_rect(midbottom = (80,300))
  108. player_gravity = 0
  109.  
  110. player_stand = pygame.image.load('graphics/player/player_stand.png').convert_alpha()
  111. player_stand = pygame.transform.scale2x(player_stand)
  112. player_stand_rect = player_stand.get_rect(center = (400,200))
  113.  
  114. game_name = font.render ('Cy Run', False, ('#ffffff'))
  115. game_name_rect = game_name.get_rect(center = (400,80))
  116.  
  117. game_message = font2.render('PRESS SPACE TO RUN', False,('#ffffff'))
  118. game_message = pygame.transform.scale(game_message, (int(game_message.get_width() * 0.4), int(game_message.get_height() * 0.4)))
  119. game_message_rect = game_message.get_rect(midright = (750,365))
  120.  
  121. #obs timer
  122. obstacle_timer = pygame.USEREVENT + 1
  123. pygame.time.set_timer(obstacle_timer,1500)
  124.  
  125. bcat_ani_timer = pygame.USEREVENT +2
  126. pygame.time.set_timer(bcat_ani_timer,200)
  127.  
  128. bat_ani_timer = pygame.USEREVENT +3
  129. pygame.time.set_timer(bat_ani_timer,200)
  130.  
  131. title_music.play()
  132.  
  133. while True:
  134.     for event in pygame.event.get():
  135.         if event.type == pygame.QUIT:
  136.             pygame.quit()
  137.             exit()
  138.  
  139.         if game_active:
  140.             if event.type == pygame.MOUSEMOTION:
  141.                 if player_rect.collidepoint(event.pos):
  142.                     player_gravity = -20
  143.  
  144.             if event.type == pygame.KEYDOWN:
  145.                 if event.key == pygame.K_SPACE and player_rect.bottom >= 250:
  146.                     player_gravity = -20  
  147.         else:
  148.             if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
  149.                 game_active = True
  150.                 start_time = int(pygame.time.get_ticks() / 1000)
  151.                 bg_music_playing = False
  152.                 title_music.play()
  153.        
  154.         if game_active:
  155.             if event.type == obstacle_timer and game_active:
  156.                 if randint(0,2):
  157.                     obstacle_rect_list.append(bcat_surf.get_rect(bottomright=(randint(900,1100),315)))
  158.                 else:
  159.                     obstacle_rect_list.append(bat_surf.get_rect(bottomright=(randint(900,1100),160)))
  160.             if event.type == bcat_ani_timer:
  161.                 if bcat_index == 0: bcat_index = 1
  162.                 else: bcat_index = 0
  163.                 bcat_surf = bcat_frames[bcat_index]
  164.             if event.type == bat_ani_timer:
  165.                 if bat_index == 0: bat_index = 1
  166.                 else: bat_index = 0
  167.                 bat_surf = bat_frames[bat_index]
  168.                
  169.  
  170.     if game_active:
  171.        
  172.         sky_surface = pygame.transform.scale(sky_surface, (800, 300))
  173.         screen.blit(sky_surface, (sky_x_pos, 0))
  174.         screen.blit(sky_surface, (sky_x_pos + sky_surface.get_width(), 0))
  175.  
  176.         sky_x_pos -= 2
  177.         if sky_x_pos <= -sky_surface.get_width():
  178.             sky_x_pos = 0
  179.  
  180.         score = display_score()
  181.  
  182.         ground_surf = pygame.transform.scale(ground_surf, (800, screen.get_height() - ground_y_pos))
  183.         screen.blit(ground_surf, (ground_x_pos, ground_y_pos))
  184.         screen.blit(ground_surf, (ground_x_pos + ground_surf.get_width(), ground_y_pos))
  185.  
  186.         ground_x_pos -= 2  
  187.         if ground_x_pos <= -ground_surf.get_width():
  188.             ground_x_pos = 0
  189.  
  190.         #player
  191.         player_gravity += 1.2
  192.         player_rect.y += player_gravity
  193.         if player_rect.bottom >= 250: player_rect.bottom = 250
  194.         player_animation()
  195.         screen.blit(player_surf, player_rect)
  196.  
  197.         #obstacle movement
  198.        
  199.         obstacle_rect_list = obstacle_movement(obstacle_rect_list)
  200.  
  201.         #collision
  202.         game_active = collisions(player_rect,obstacle_rect_list)
  203.  
  204.         if not bg_music_playing:
  205.             game_music.play()
  206.             bg_music_playing = True
  207.             title_music.stop()
  208.  
  209.     else:
  210.         startbg_surf = pygame.transform.scale(startbg_surf, (800, 400))
  211.         screen.blit(startbg_surf, (0, 0))
  212.         obstacle_rect_list.clear()
  213.         player_rect.midbottom = (80,300)
  214.         player_gravity = 0
  215.  
  216.         score_message = font.render(f'Score: {score}',False,('#ffffff'))
  217.         score_message_rect = score_message.get_rect(midright = (750,365))
  218.  
  219.         if score == 0:
  220.             screen.blit(game_message,game_message_rect)
  221.         else:
  222.             screen.blit(score_message,score_message_rect)
  223.            
  224.         if bg_music_playing:
  225.             game_music.stop()
  226.             bg_music_playing = False
  227.             title_music.play()
  228.  
  229.     pygame.display.update()
  230.     clock.tick(60)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement