Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ASSETS: bit.ly/33mIVru
- import pygame
- pygame.init()
- clock = pygame.time.Clock()
- SCREEN_WIDTH = 640
- SCREEN_HEIGHT = 480
- HALF_SCREEN_HEIGHT = SCREEN_HEIGHT//2
- screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
- light_road = pygame.image.load('assets/imgs/light_road.png')
- dark_road = pygame.image.load('assets/imgs/dark_road.png')
- texture_position = 0
- ddz = 0.001
- dz = 0
- z = 0
- road_pos = 0
- road_acceleration = 80
- texture_position_acceleration = 4
- texture_position_threshold = 300
- half_texture_position_threshhold = texture_position_threshold//2
- running = True
- while running:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- running = False
- #pygame.quit()
- keys = pygame.key.get_pressed()
- if keys[pygame.K_UP]:
- road_pos += road_acceleration
- if road_pos >= texture_position_threshold:
- road_pos = 0
- texture_position = road_pos
- dz = 0
- z = 0
- screen.fill((60,40,250))
- for i in range(HALF_SCREEN_HEIGHT-1, -1, -1):
- if texture_position < half_texture_position_threshhold:
- screen.blit(light_road, (0,i+HALF_SCREEN_HEIGHT), (0,i,SCREEN_WIDTH,1))
- else:
- screen.blit(dark_road, (0, i+HALF_SCREEN_HEIGHT), (0,i,SCREEN_WIDTH,1))
- dz += ddz
- z += dz
- texture_position += texture_position_acceleration + z
- if texture_position >= texture_position_threshold:
- texture_position = 0
- clock.tick(20)
- pygame.display.update()
- --------------------------------------------------------------------------------------------------
- #ASSETS: bit.ly/33mIVru
- import pygame
- import random
- pygame.init()
- clock = pygame.time.Clock()
- SCREEN_WIDTH = 640
- SCREEN_HEIGHT = 480
- HALF_SCREEN_HEIGHT = SCREEN_HEIGHT//2
- screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
- light_road = pygame.image.load('assets/imgs/light_road.png')
- dark_road = pygame.image.load('assets/imgs/dark_road.png')
- car = pygame.image.load('assets/imgs/car.png')
- truck = pygame.image.load('assets/imgs/truck.png')
- rock = pygame.image.load('assets/imgs/rock.png')
- health = pygame.image.load('assets/imgs/health.png')
- health_sound = pygame.mixer.Sound('assets/sounds/health.wav')
- rock_sound = pygame.mixer.Sound('assets/sounds/rock.wav')
- car_x = 260
- car_y = 360
- rock_x = random.randint(250,350)
- rock_y = 240
- health_x = 300
- health_y = 240
- state = 0
- #SCORE
- score = 0
- font = pygame.font.Font('freesansbold.ttf', 32)
- sCoord = (10,10)
- def print_score(scr):
- screen.blit(font.render("Score: "+str(scr), True, (0,0,0)), sCoord)
- #COLLISION
- def isCollided(Cx,Cy,Sx,Sy):
- if Cx+20 < Sx+15 < Cx+110 and Cy+20 < Sy+11 < Cy+110:
- return True
- return False
- #LIVES
- def draw_lives(l):
- pygame.draw.rect(screen, (200,0,0), (600-30*4, 10, 30*5,15))
- for i in range(l):
- pygame.draw.rect(screen, (0,200,0), (600-30*i, 10, 30, 15))
- #GRADIENT
- ddz = 0.001
- dz = 0
- z = 0
- #our position on the road
- road_pos = 0
- #speed at which road moves
- road_acceleration = 60
- texture_position_acceleration = 4
- texture_position_threshold = 300
- half_texture_position_threshhold = texture_position_threshold//2
- texture_position = 0
- life = 5
- game = 1
- health_piece = 0
- start_time = pygame.time.get_ticks()
- life_time = pygame.time.get_ticks()
- running = True
- while running:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- running = False
- #pygame.quit()
- keys = pygame.key.get_pressed()
- if keys[pygame.K_LEFT]:
- car_x -= 5
- if car_x <=50:
- car_x += 5
- if keys[pygame.K_RIGHT]:
- car_x += 5
- if car_x >= 450:
- car_x -= 5
- texture_position = road_pos
- dz = 0
- z = 0
- screen.fill((60,40,250))
- if life >0:
- road_pos += road_acceleration
- if road_pos >= texture_position_threshold:
- road_pos = 0
- for i in range(HALF_SCREEN_HEIGHT-1, -1, -1):
- if texture_position < half_texture_position_threshhold:
- screen.blit(light_road, (0,i+HALF_SCREEN_HEIGHT), (0,i,SCREEN_WIDTH,1))
- else:
- screen.blit(dark_road, (0, i+HALF_SCREEN_HEIGHT), (0,i,SCREEN_WIDTH,1))
- dz += ddz
- z += dz
- texture_position += texture_position_acceleration + z
- if texture_position >= texture_position_threshold:
- texture_position = 0
- #rock
- game_time = pygame.time.get_ticks()
- if game_time - start_time > 2000 and state == 0 and health_piece == 0:
- state = 1
- rock_x = random.randint(250,350)
- rock_y = 240
- chng = 0
- if state == 1 and life>0 and game == 1:
- rock_y += 5
- if rock_x < 270: chng = -4
- elif rock_x > 330: chng = 4
- rock_x += chng
- screen.blit(rock, (rock_x, rock_y))
- collided = isCollided(car_x,car_y,rock_x,rock_y)
- if collided:
- rock_sound.play()
- state = 0
- life -= 1
- start_time = pygame.time.get_ticks()
- if rock_y >= 480 and not(collided):
- score += 1
- state = 0
- start_time = pygame.time.get_ticks()
- #HEALTH
- game_life_time = pygame.time.get_ticks()
- if game_life_time - life_time > 10000 or health_piece == 1:
- health_piece = 1
- game = 0
- health_y += 5
- if health_x <270: chng = -4
- elif health_x >330: chng = 4
- health_x += chng
- screen.blit(health, (health_x,health_y))
- collided_health = isCollided(car_x, car_y, health_x, health_y)
- if collided_health:
- score += 2
- health_sound.play()
- health_piece = 0
- game = 1
- life += 1
- if life > 5:
- life = 5
- health_y = 240
- if health_y > 480:
- health_x = random.randint(250,350)
- game = 1
- health_y = 240
- health_piece = 0
- life_time = pygame.time.get_ticks()
- draw_lives(life)
- print_score(score)
- screen.blit(car, (car_x,car_y))
- screen.blit(truck, (270,210))
- clock.tick(20)
- pygame.display.update()
Add Comment
Please, Sign In to add comment