rishabbansal21

Day-12

Jan 21st, 2021 (edited)
465
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #ASSETS: bit.ly/33mIVru
  2.  
  3. import pygame
  4.  
  5. pygame.init()
  6.  
  7. clock = pygame.time.Clock()
  8.  
  9. SCREEN_WIDTH = 640
  10. SCREEN_HEIGHT = 480
  11. HALF_SCREEN_HEIGHT = SCREEN_HEIGHT//2
  12.  
  13. screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
  14.  
  15. light_road = pygame.image.load('assets/imgs/light_road.png')
  16. dark_road = pygame.image.load('assets/imgs/dark_road.png')
  17.  
  18. texture_position = 0
  19. ddz = 0.001
  20. dz = 0
  21. z = 0
  22.  
  23. road_pos = 0
  24. road_acceleration = 80
  25. texture_position_acceleration = 4
  26. texture_position_threshold = 300
  27. half_texture_position_threshhold = texture_position_threshold//2
  28.  
  29.  
  30.  
  31.  
  32. running = True
  33. while running:
  34.  
  35.     for event in pygame.event.get():
  36.         if event.type == pygame.QUIT:
  37.             running = False
  38.             #pygame.quit()
  39.  
  40.     keys = pygame.key.get_pressed()
  41.     if keys[pygame.K_UP]:
  42.         road_pos += road_acceleration
  43.         if road_pos >= texture_position_threshold:
  44.             road_pos = 0
  45.  
  46.     texture_position = road_pos
  47.     dz = 0
  48.     z = 0
  49.     screen.fill((60,40,250))
  50.  
  51.  
  52.     for i in range(HALF_SCREEN_HEIGHT-1, -1, -1):
  53.         if texture_position < half_texture_position_threshhold:
  54.             screen.blit(light_road, (0,i+HALF_SCREEN_HEIGHT), (0,i,SCREEN_WIDTH,1))
  55.         else:
  56.             screen.blit(dark_road, (0, i+HALF_SCREEN_HEIGHT), (0,i,SCREEN_WIDTH,1))
  57.  
  58.         dz += ddz
  59.         z += dz
  60.  
  61.         texture_position += texture_position_acceleration + z
  62.         if texture_position >= texture_position_threshold:
  63.             texture_position = 0
  64.  
  65.     clock.tick(20)
  66.     pygame.display.update()
  67.  
  68.  
  69.  
  70.  
  71. --------------------------------------------------------------------------------------------------
  72.  
  73. #ASSETS: bit.ly/33mIVru
  74.  
  75. import pygame
  76. import random
  77.  
  78. pygame.init()
  79.  
  80. clock = pygame.time.Clock()
  81.  
  82. SCREEN_WIDTH = 640
  83. SCREEN_HEIGHT = 480
  84. HALF_SCREEN_HEIGHT = SCREEN_HEIGHT//2
  85.  
  86. screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
  87.  
  88. light_road = pygame.image.load('assets/imgs/light_road.png')
  89. dark_road = pygame.image.load('assets/imgs/dark_road.png')
  90. car = pygame.image.load('assets/imgs/car.png')
  91. truck = pygame.image.load('assets/imgs/truck.png')
  92. rock = pygame.image.load('assets/imgs/rock.png')
  93. health = pygame.image.load('assets/imgs/health.png')
  94.  
  95. health_sound = pygame.mixer.Sound('assets/sounds/health.wav')
  96. rock_sound = pygame.mixer.Sound('assets/sounds/rock.wav')
  97.  
  98. car_x = 260
  99. car_y = 360
  100. rock_x = random.randint(250,350)
  101. rock_y = 240
  102. health_x = 300
  103. health_y = 240
  104.  
  105. state = 0
  106.  
  107. #SCORE
  108. score = 0
  109. font = pygame.font.Font('freesansbold.ttf', 32)
  110. sCoord = (10,10)
  111. def print_score(scr):
  112.     screen.blit(font.render("Score: "+str(scr), True, (0,0,0)), sCoord)
  113.  
  114. #COLLISION
  115. def isCollided(Cx,Cy,Sx,Sy):
  116.     if Cx+20 < Sx+15 < Cx+110 and Cy+20 < Sy+11 < Cy+110:
  117.         return True
  118.     return False
  119.  
  120. #LIVES
  121. def draw_lives(l):
  122.     pygame.draw.rect(screen, (200,0,0), (600-30*4, 10, 30*5,15))
  123.     for i in range(l):
  124.         pygame.draw.rect(screen, (0,200,0), (600-30*i, 10, 30, 15))
  125.  
  126. #GRADIENT
  127. ddz = 0.001
  128. dz = 0
  129. z = 0
  130.  
  131. #our position on the road
  132. road_pos = 0
  133. #speed at which road moves
  134. road_acceleration = 60
  135. texture_position_acceleration = 4
  136. texture_position_threshold = 300
  137. half_texture_position_threshhold = texture_position_threshold//2
  138. texture_position = 0
  139.  
  140. life = 5
  141. game = 1
  142. health_piece = 0
  143.  
  144. start_time = pygame.time.get_ticks()
  145. life_time = pygame.time.get_ticks()
  146.  
  147. running = True
  148. while running:
  149.  
  150.     for event in pygame.event.get():
  151.         if event.type == pygame.QUIT:
  152.             running = False
  153.             #pygame.quit()
  154.  
  155.     keys = pygame.key.get_pressed()
  156.     if keys[pygame.K_LEFT]:
  157.         car_x -= 5
  158.         if car_x <=50:
  159.             car_x += 5
  160.  
  161.     if keys[pygame.K_RIGHT]:
  162.         car_x += 5
  163.         if car_x >= 450:
  164.             car_x -= 5
  165.  
  166.     texture_position = road_pos
  167.     dz = 0
  168.     z = 0
  169.     screen.fill((60,40,250))
  170.  
  171.     if life >0:
  172.         road_pos += road_acceleration
  173.         if road_pos >= texture_position_threshold:
  174.             road_pos = 0  
  175.  
  176.     for i in range(HALF_SCREEN_HEIGHT-1, -1, -1):
  177.         if texture_position < half_texture_position_threshhold:
  178.             screen.blit(light_road, (0,i+HALF_SCREEN_HEIGHT), (0,i,SCREEN_WIDTH,1))
  179.         else:
  180.             screen.blit(dark_road, (0, i+HALF_SCREEN_HEIGHT), (0,i,SCREEN_WIDTH,1))
  181.  
  182.         dz += ddz
  183.         z += dz
  184.  
  185.         texture_position += texture_position_acceleration + z
  186.         if texture_position >= texture_position_threshold:
  187.             texture_position = 0
  188.  
  189.     #rock
  190.     game_time = pygame.time.get_ticks()
  191.     if game_time - start_time > 2000 and state == 0 and health_piece == 0:
  192.         state = 1
  193.         rock_x = random.randint(250,350)
  194.         rock_y = 240
  195.         chng = 0
  196.  
  197.     if state == 1 and life>0 and game == 1:
  198.         rock_y += 5
  199.         if rock_x < 270: chng = -4
  200.         elif rock_x > 330: chng = 4
  201.         rock_x += chng
  202.         screen.blit(rock, (rock_x, rock_y))
  203.  
  204.         collided = isCollided(car_x,car_y,rock_x,rock_y)
  205.         if collided:
  206.             rock_sound.play()
  207.             state = 0
  208.             life -= 1
  209.             start_time = pygame.time.get_ticks()
  210.  
  211.         if rock_y >= 480 and not(collided):
  212.             score += 1
  213.             state = 0
  214.             start_time = pygame.time.get_ticks()
  215.  
  216.  
  217.     #HEALTH
  218.     game_life_time = pygame.time.get_ticks()
  219.     if game_life_time - life_time > 10000 or health_piece == 1:
  220.         health_piece = 1
  221.         game = 0
  222.         health_y += 5
  223.         if health_x <270: chng = -4
  224.         elif health_x >330: chng = 4
  225.         health_x += chng
  226.         screen.blit(health, (health_x,health_y))
  227.  
  228.         collided_health = isCollided(car_x, car_y, health_x, health_y)
  229.         if collided_health:
  230.             score += 2
  231.             health_sound.play()
  232.             health_piece = 0
  233.             game = 1
  234.             life += 1
  235.             if life > 5:
  236.                 life = 5
  237.             health_y = 240
  238.  
  239.         if health_y > 480:
  240.             health_x = random.randint(250,350)
  241.             game = 1
  242.             health_y = 240
  243.             health_piece = 0
  244.  
  245.         life_time = pygame.time.get_ticks()
  246.  
  247.     draw_lives(life)
  248.     print_score(score)
  249.     screen.blit(car, (car_x,car_y))
  250.     screen.blit(truck, (270,210))
  251.     clock.tick(20)
  252.     pygame.display.update()
RAW Paste Data