Skip to content

Commit ff99ff3

Browse files
authored
🌐
1 parent cb34382 commit ff99ff3

File tree

1 file changed

+157
-0
lines changed

1 file changed

+157
-0
lines changed

Car Racing Game.py

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
# Importing the library
2+
import pygame
3+
import time
4+
import random
5+
import sys
6+
7+
# Initializing the pygame
8+
pygame.init()
9+
10+
# window
11+
screen = pygame.display.set_mode((800, 600))
12+
pygame.display.set_caption("CAR RACING")
13+
14+
# Loading all images
15+
c1_img = pygame.image.load("car1.jpg")
16+
clock = pygame.time.Clock()
17+
grass = pygame.image.load("grass.jpg")
18+
y_strip = pygame.image.load("y_strip.jpg")
19+
strip = pygame.image.load("strip.jpg")
20+
start = pygame.image.load("start.jpg")
21+
22+
23+
# Function for getting all images
24+
def background():
25+
screen.blit(grass, (0, 0))
26+
screen.blit(grass, (700, 0))
27+
screen.blit(y_strip, (400, 0))
28+
screen.blit(y_strip, (400, 100))
29+
screen.blit(y_strip, (400, 200))
30+
screen.blit(y_strip, (400, 300))
31+
screen.blit(y_strip, (400, 400))
32+
screen.blit(y_strip, (400, 500))
33+
screen.blit(y_strip, (400, 600))
34+
screen.blit(strip, (120, 0))
35+
screen.blit(strip, (680, 0))
36+
37+
38+
# Getting the car on screen
39+
def car(x, y):
40+
screen.blit(c1_img, (x, y))
41+
42+
43+
x_change = 0
44+
x = 400
45+
y = 470
46+
car_width = 56
47+
op_speed = 10
48+
obs = 0
49+
y_change = 0
50+
obs_x = random.randrange(200, 650)
51+
obs_y = -750
52+
op_width = 56
53+
op_height = 125
54+
car_passed = 0
55+
score = 0
56+
level = 0
57+
58+
59+
# Function for enemy cars
60+
def obstacle(obs_x, obs_y, obs):
61+
if obs == 0:
62+
obs_img = pygame.image.load("car2.jpg")
63+
elif obs == 1:
64+
obs_img = pygame.image.load("car3.jpg")
65+
elif obs == 2:
66+
obs_img = pygame.image.load("car4.jpg")
67+
elif obs == 3:
68+
obs_img = pygame.image.load("car5.jpg")
69+
elif obs == 4:
70+
obs_img = pygame.image.load("car6.jpg")
71+
elif obs == 5:
72+
obs_img = pygame.image.load("car7.jpg")
73+
screen.blit(obs_img, (obs_x, obs_y))
74+
75+
76+
# message to display
77+
font = pygame.font.SysFont("None", 150)
78+
render = font.render("CAR CRASHED", True, (0, 0, 0))
79+
80+
81+
# Function to display score
82+
def sc(car_passed, score):
83+
s_font = pygame.font.SysFont(None, 35)
84+
passed = s_font.render("Passed:" + str(car_passed), True, (0, 0, 0))
85+
score = s_font.render("Score:" + str(score), True, (0, 0, 0))
86+
screen.blit(passed, (0, 30))
87+
screen.blit(score, (0, 70))
88+
89+
90+
# Text on Buttons
91+
def text(text, font):
92+
texts = font.render(text, True, (255, 255, 255))
93+
return texts, texts.get_rect()
94+
95+
96+
# Game loop
97+
def game_loop():
98+
global op_speed, x, car_passed, level, x_change, y_change, y, obs_y, obs_x, obs, score, font
99+
running = True
100+
while running:
101+
102+
# for checking the events
103+
for event in pygame.event.get():
104+
if event.type == pygame.QUIT: # QUIT event
105+
running = False
106+
if event.type == pygame.KEYDOWN: # KEYDOWN event
107+
if event.key == pygame.K_LEFT:
108+
x_change = -5
109+
if event.key == pygame.K_RIGHT:
110+
x_change = 5
111+
if event.key == pygame.K_s: # Increase speed
112+
op_speed += 2
113+
if event.key == pygame.K_b: # decrease speed
114+
op_speed -= 2
115+
if event.type == pygame.KEYUP: # KEYUP event
116+
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
117+
x_change = 0
118+
x += x_change
119+
screen.fill((119, 119, 119))
120+
background()
121+
obs_y -= (op_speed / 4)
122+
obstacle(obs_x, obs_y, obs)
123+
obs_y += op_speed
124+
car(x, y)
125+
sc(car_passed, score)
126+
# Restricting the movement
127+
if x > 680 - car_width or x 600:
128+
obs_y = 0 - op_height
129+
obs_x = random.randrange(170, 600)
130+
obs = random.randrange(0, 6)
131+
car_passed += 1
132+
score = car_passed * 10
133+
if int(car_passed) % 10 == 0:
134+
level += 1
135+
op_speed += 2
136+
font = pygame.font.SysFont(None, 50)
137+
level_text = font.render("Level" + str(level), True, (0, 0, 0))
138+
screen.blit(level_text, (0, 100))
139+
pygame.display.flip()
140+
# car crash logic
141+
if y obs_x and x obs_x and x + car_width 80 and mouse[0] 490 and mouse[1] 580 and mouse[0] 490 and mouse[1] < 590:
142+
pygame.draw.rect(screen, (255, 0, 0), (580, 540, 150, 50))
143+
if click == (1, 0, 0):
144+
pygame.quit()
145+
t = pygame.font.SysFont("arial", 30, "bold")
146+
texts, textr = text("START", t)
147+
textr.center = ((80 + (150 / 2)), (540 + (50 / 2)))
148+
screen.blit(texts, textr)
149+
texts, textr = text("QUIT", t)
150+
textr.center = ((580 + (150 / 2)), (540 + (50 / 2)))
151+
screen.blit(texts, textr)
152+
pygame.display.update()
153+
154+
155+
intro() # calling the intro function
156+
game_loop() # calling the game loop
157+
pygame.quit()

0 commit comments

Comments
 (0)