|
| 1 | +import pygame |
| 2 | +import random |
| 3 | + |
| 4 | +# Constants |
| 5 | +SCREEN_WIDTH = 1000 |
| 6 | +SCREEN_HEIGHT = 700 |
| 7 | +CAR_WIDTH = 80 |
| 8 | +CAR_HEIGHT = 160 |
| 9 | +ROAD_WIDTH = 600 |
| 10 | +FPS = 60 |
| 11 | +WHITE = (255, 255, 255) |
| 12 | + |
| 13 | +class Car: |
| 14 | + def __init__(self): |
| 15 | + self.x = SCREEN_WIDTH // 2 - CAR_WIDTH // 2 |
| 16 | + self.y = SCREEN_HEIGHT - CAR_HEIGHT - 10 |
| 17 | + self.speed_x = 0 |
| 18 | + self.speed_y = 0 |
| 19 | + self.image = pygame.transform.scale(pygame.image.load('car_nfs.png'), (CAR_WIDTH, CAR_HEIGHT)) |
| 20 | + |
| 21 | + def move_left(self): |
| 22 | + if self.x > SCREEN_WIDTH // 4 - CAR_WIDTH: |
| 23 | + self.speed_x = -5 |
| 24 | + |
| 25 | + def move_right(self): |
| 26 | + if self.x < SCREEN_WIDTH // 2: |
| 27 | + self.speed_x = 5 |
| 28 | + |
| 29 | + def move_forward(self): |
| 30 | + self.speed_y = -5 |
| 31 | + |
| 32 | + def move_backward(self): |
| 33 | + self.speed_y = 5 |
| 34 | + |
| 35 | + def stop_x(self): |
| 36 | + self.speed_x = 0 |
| 37 | + |
| 38 | + def stop_y(self): |
| 39 | + self.speed_y = 0 |
| 40 | + |
| 41 | + def update_position(self): |
| 42 | + self.x += self.speed_x |
| 43 | + self.y += self.speed_y |
| 44 | + |
| 45 | + def draw(self): |
| 46 | + screen.blit(self.image, (self.x, self.y)) |
| 47 | + |
| 48 | +class Road: |
| 49 | + def __init__(self): |
| 50 | + self.y = 0 |
| 51 | + self.speed = 5 |
| 52 | + self.image = pygame.image.load('road_nfs.jpg') |
| 53 | + self.image = pygame.transform.scale(self.image, (ROAD_WIDTH, SCREEN_HEIGHT)) |
| 54 | + |
| 55 | + def move(self): |
| 56 | + self.y += self.speed |
| 57 | + if self.y > SCREEN_HEIGHT: |
| 58 | + self.y = 0 |
| 59 | + |
| 60 | + def draw(self): |
| 61 | + screen.blit(self.image, (SCREEN_WIDTH // 4, self.y)) |
| 62 | + screen.blit(self.image, (SCREEN_WIDTH // 4, self.y - SCREEN_HEIGHT)) |
| 63 | + |
| 64 | +class HurdleManager: |
| 65 | + def __init__(self): |
| 66 | + self.hurdles = [] |
| 67 | + self.speed = 5 |
| 68 | + |
| 69 | + def create_hurdle(self): |
| 70 | + size = random.randint(20, 50) |
| 71 | + x = random.randint(SCREEN_WIDTH // 4, SCREEN_WIDTH // 2 - size) |
| 72 | + y = -size |
| 73 | + self.hurdles.append(pygame.Rect(x, y, size, size)) |
| 74 | + |
| 75 | + def move_hurdles(self): |
| 76 | + for hurdle in self.hurdles: |
| 77 | + hurdle.y += self.speed |
| 78 | + if hurdle.y > SCREEN_HEIGHT: |
| 79 | + self.hurdles.remove(hurdle) |
| 80 | + |
| 81 | +class Game: |
| 82 | + def __init__(self): |
| 83 | + self.car = Car() |
| 84 | + self.road = Road() |
| 85 | + self.hurdle_manager = HurdleManager() |
| 86 | + self.score = 0 |
| 87 | + self.play_background_music() |
| 88 | + |
| 89 | + def handle_events(self): |
| 90 | + for event in pygame.event.get(): |
| 91 | + if event.type == pygame.QUIT: |
| 92 | + pygame.quit() |
| 93 | + quit() |
| 94 | + |
| 95 | + keys = pygame.key.get_pressed() |
| 96 | + if keys[pygame.K_LEFT]: |
| 97 | + self.car.move_left() |
| 98 | + if keys[pygame.K_RIGHT]: |
| 99 | + self.car.move_right() |
| 100 | + if keys[pygame.K_UP]: |
| 101 | + self.car.move_forward() |
| 102 | + if keys[pygame.K_DOWN]: |
| 103 | + self.car.move_backward() |
| 104 | + |
| 105 | + if not keys[pygame.K_LEFT] and not keys[pygame.K_RIGHT]: |
| 106 | + self.car.stop_x() |
| 107 | + if not keys[pygame.K_UP] and not keys[pygame.K_DOWN]: |
| 108 | + self.car.stop_y() |
| 109 | + |
| 110 | + def update_score(self): |
| 111 | + self.score += 1 |
| 112 | + |
| 113 | + def display_score(self): |
| 114 | + font = pygame.font.Font(None, 36) |
| 115 | + score_text = font.render("Score: " + str(self.score), True, WHITE) |
| 116 | + screen.blit(score_text, (10, 10)) |
| 117 | + |
| 118 | + def run(self): |
| 119 | + clock = pygame.time.Clock() |
| 120 | + |
| 121 | + while True: |
| 122 | + self.handle_events() |
| 123 | + |
| 124 | + self.road.move() |
| 125 | + |
| 126 | + screen.fill((0, 0, 0)) |
| 127 | + self.road.draw() |
| 128 | + |
| 129 | + self.hurdle_manager.create_hurdle() |
| 130 | + self.hurdle_manager.move_hurdles() |
| 131 | + self.car.update_position() |
| 132 | + self.car.draw() |
| 133 | + self.display_score() |
| 134 | + |
| 135 | + self.car.speed_x = self.road.speed * 2 |
| 136 | + self.car.speed_y = self.road.speed * 2 |
| 137 | + self.hurdle_manager.speed = self.road.speed * 1.5 |
| 138 | + |
| 139 | + self.update_score() |
| 140 | + |
| 141 | + pygame.display.update() |
| 142 | + clock.tick(FPS) |
| 143 | + |
| 144 | + def play_background_music(self): |
| 145 | + pygame.mixer.init() |
| 146 | + pygame.mixer.music.load('background_music.wav') |
| 147 | + pygame.mixer.music.play(-1) # -1 makes the music loop indefinitely |
| 148 | + |
| 149 | +# Initialize Pygame |
| 150 | +pygame.init() |
| 151 | + |
| 152 | +# Create the game window |
| 153 | +screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) |
| 154 | +pygame.display.set_caption("Need for Speed - Car Racing Game") |
| 155 | + |
| 156 | +# Run the game |
| 157 | +game = Game() |
| 158 | +game.run() |
0 commit comments