Download App.py from mazeem123/Car: direct link, hf CLI and curl.
- Browser
- Download file 2.19 kB
-
https://huggingface.co/spaces/mazeem123/Car/resolve/main/App.py
- Command line
-
hf download hf://spaces/mazeem123/Car/App.py
-
curl -L -o App.py https://huggingface.co/spaces/mazeem123/Car/resolve/main/App.py
2.19 kB
| import pygame | |
| import random | |
| import sys | |
| # Initialize pygame | |
| pygame.init() | |
| # Screen settings | |
| WIDTH, HEIGHT = 400, 600 | |
| screen = pygame.display.set_mode((WIDTH, HEIGHT)) | |
| pygame.display.set_caption("Car Game 🚗") | |
| # Colors | |
| WHITE = (255, 255, 255) | |
| BLACK = (0, 0, 0) | |
| GRAY = (100, 100, 100) | |
| RED = (200, 0, 0) | |
| BLUE = (0, 0, 200) | |
| # Car settings | |
| car_width = 50 | |
| car_height = 80 | |
| car_x = WIDTH // 2 - car_width // 2 | |
| car_y = HEIGHT - car_height - 10 | |
| car_speed = 5 | |
| # Enemy car | |
| enemy_width = 50 | |
| enemy_height = 80 | |
| enemy_x = random.randint(0, WIDTH - enemy_width) | |
| enemy_y = -enemy_height | |
| enemy_speed = 5 | |
| # Score | |
| score = 0 | |
| font = pygame.font.SysFont(None, 40) | |
| # Game loop | |
| clock = pygame.time.Clock() | |
| running = True | |
| while running: | |
| screen.fill(GRAY) | |
| # Road lines | |
| pygame.draw.rect(screen, WHITE, (WIDTH // 2 - 5, 0, 10, HEIGHT)) | |
| # Handle events | |
| for event in pygame.event.get(): | |
| if event.type == pygame.QUIT: | |
| pygame.quit() | |
| sys.exit() | |
| # Key press | |
| keys = pygame.key.get_pressed() | |
| if keys[pygame.K_LEFT] and car_x > 0: | |
| car_x -= car_speed | |
| if keys[pygame.K_RIGHT] and car_x < WIDTH - car_width: | |
| car_x += car_speed | |
| # Move enemy | |
| enemy_y += enemy_speed | |
| # Respawn enemy | |
| if enemy_y > HEIGHT: | |
| enemy_y = -enemy_height | |
| enemy_x = random.randint(0, WIDTH - enemy_width) | |
| score += 1 | |
| enemy_speed += 0.3 # Increase difficulty | |
| # Collision detection | |
| if (car_y < enemy_y + enemy_height and | |
| car_y + car_height > enemy_y and | |
| car_x < enemy_x + enemy_width and | |
| car_x + car_width > enemy_x): | |
| text = font.render("Game Over!", True, RED) | |
| screen.blit(text, (WIDTH//2 - 100, HEIGHT//2)) | |
| pygame.display.update() | |
| pygame.time.wait(2000) | |
| pygame.quit() | |
| sys.exit() | |
| # Draw cars | |
| pygame.draw.rect(screen, BLUE, (car_x, car_y, car_width, car_height)) | |
| pygame.draw.rect(screen, RED, (enemy_x, enemy_y, enemy_width, enemy_height)) | |
| # Display score | |
| score_text = font.render(f"Score: {score}", True, WHITE) | |
| screen.blit(score_text, (10, 10)) | |
| # Update display | |
| pygame.display.update() | |
| clock.tick(60) |