-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
135 lines (104 loc) · 4.04 KB
/
main.py
File metadata and controls
135 lines (104 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import pygame
import random
import sys
pygame.init()
clock = pygame.time.Clock()
screen_width, screen_height = 1280, 720
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Collision Detector Game")
car_img = pygame.image.load("D:\Projects\Collision Detector\RightCar1.jpeg")
opponent_img = pygame.image.load("D:\Projects\Collision Detector\LeftCar1.jpeg")
road_img = pygame.image.load("D:\Projects\Collision Detector\Road.jpeg")
car_img = pygame.transform.scale(car_img, (100, 60))
opponent_img = pygame.transform.scale(opponent_img, (100, 60))
road_left = 208
road_right = screen_width - 208
road_top = 208
road_bottom = 600
font = pygame.font.SysFont("Arial", 32)
difficulty_level = {"EASY": 3000, "MEDIUM": 2000, "HARD": 1000}
difficulty = "MEDIUM"
car_x, car_y = 400, 500
speed_x = speed_y = 0
score = 0
is_finished = False
opponents = []
last_spawn_time = pygame.time.get_ticks()
class OpponentCar:
def __init__(self, x, y, speed):
self.rect = pygame.Rect(x, y, 100, 60)
self.speed = speed
def move(self):
self.rect.x -= self.speed
def spawn_opponent():
attempts = 10
while attempts > 0:
y = random.randint(road_top, road_bottom - 140)
if all(abs(op.rect.y - y) >= 120 for op in opponents):
speed = random.randint(5, 8) + (score // 5)
opponents.append(OpponentCar(road_right, y, speed))
break
attempts -= 1
def game_over_screen():
overlay = pygame.Surface((screen_width, screen_height))
overlay.set_alpha(180)
overlay.fill((0, 0, 0))
screen.blit(overlay, (0, 0))
game_over_text = font.render(f"Game Over! Score: {score}", True, (255, 0, 0))
restart_text = font.render("Press 'R' to Restart or 'E' to Exit", True, (255, 255, 255))
screen.blit(game_over_text, (screen_width // 2 - 150, screen_height // 2 - 50))
screen.blit(restart_text, (screen_width // 2 - 250, screen_height // 2 + 10))
pygame.display.flip()
def reset_game():
global car_x, car_y, speed_x, speed_y, score, is_finished, opponents, last_spawn_time
car_x, car_y = 400, 500
speed_x = speed_y = 0
score = 0
is_finished = False
opponents.clear()
last_spawn_time = pygame.time.get_ticks()
reset_game()
running = True
while running:
clock.tick(60)
screen.blit(pygame.transform.scale(road_img, (screen_width, screen_height)), (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
keys = pygame.key.get_pressed()
if not is_finished:
speed_x = speed_y = 0
if keys[pygame.K_UP]: speed_y = -4
if keys[pygame.K_DOWN]: speed_y = 4
if keys[pygame.K_LEFT]: speed_x = -4
if keys[pygame.K_RIGHT]: speed_x = 4
car_x += speed_x
car_y += speed_y
car_x = max(road_left, min(road_right - 100, car_x))
car_y = max(road_top, min(road_bottom - 140, car_y))
now = pygame.time.get_ticks()
if now - last_spawn_time > random.randint(1000, difficulty_level[difficulty]):
spawn_opponent()
last_spawn_time = now
player_hitbox = pygame.Rect(car_x + 20, car_y + 20, 60, 40)
for op in opponents[:]:
op.move()
screen.blit(opponent_img, op.rect.topleft)
opponent_hitbox = pygame.Rect(op.rect.x + 20, op.rect.y + 20, 60, 40)
if opponent_hitbox.colliderect(player_hitbox):
is_finished = True
elif op.rect.right < road_left:
opponents.remove(op)
score += 1
screen.blit(car_img, (car_x, car_y))
score_text = font.render(f"Score: {score}", True, (255, 255, 255))
screen.blit(score_text, (50, 50))
if is_finished:
game_over_screen()
if keys[pygame.K_r]:
reset_game()
elif keys[pygame.K_e]:
pygame.quit()
sys.exit()
pygame.display.flip()
pygame.quit()