Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Main game loop
- running = True
- while running:
- screen.fill(WHITE)
- # Event handling
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- running = False
- # Player control
- keys = pygame.key.get_pressed()
- if keys[pygame.K_LEFT]:
- car_x -= car_speed
- if keys[pygame.K_RIGHT]:
- car_x += car_speed
- # Boundary checking for player car
- if car_x < 0:
- car_x = 0
- elif car_x > WIDTH - car_width:
- car_x = WIDTH - car_width
- # Generate obstacles
- if random.randrange(0, 100) < obstacle_frequency:
- obstacle_x = random.randrange(0, WIDTH - obstacle_width)
- obstacle_y = -obstacle_height
- obstacles.append([obstacle_x, obstacle_y])
- # Generate coins
- if random.randrange(0, 100) < coin_frequency:
- coin_x = random.randrange(0, WIDTH - coin_width)
- coin_y = -coin_height
- coins.append([coin_x, coin_y])
- # Move obstacles and coins
- for obstacle in obstacles:
- obstacle[1] += obstacle_speed
- screen.blit(obstacle_img, (obstacle[0], obstacle[1]))
- if obstacle[1] > HEIGHT:
- obstacles.remove(obstacle)
- score -= 10
- for coin in coins:
- coin[1] += obstacle_speed
- screen.blit(coin_img, (coin[0], coin[1]))
- if coin[1] > HEIGHT:
- coins.remove(coin)
- # Collision detection
- car_rect = pygame.Rect(car_x, car_y, car_width, car_height)
- for obstacle in obstacles:
- obstacle_rect = pygame.Rect(obstacle[0], obstacle[1], obstacle_width, obstacle_height)
- if car_rect.colliderect(obstacle_rect):
- obstacles.remove(obstacle)
- score -= 50
- for coin in coins:
- coin_rect = pygame.Rect(coin[0], coin[1], coin_width, coin_height)
- if car_rect.colliderect(coin_rect):
- coins.remove(coin)
- score += 10
- # Display score
- display_text("Score: " + str(score), BLACK, 10, 10)
- # Draw player car
- screen.blit(car_img, (car_x, car_y))
- pygame.display.flip()
- clock.tick(60)
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement