-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCar Racing Game.py
157 lines (135 loc) · 4.77 KB
/
Car Racing Game.py
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# Importing the library
import pygame
import time
import random
import sys
# Initializing the pygame
pygame.init()
# window
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("CAR RACING")
# Loading all images
c1_img = pygame.image.load("car1.jpg")
clock = pygame.time.Clock()
grass = pygame.image.load("grass.jpg")
y_strip = pygame.image.load("y_strip.jpg")
strip = pygame.image.load("strip.jpg")
start = pygame.image.load("start.jpg")
# Function for getting all images
def background():
screen.blit(grass, (0, 0))
screen.blit(grass, (700, 0))
screen.blit(y_strip, (400, 0))
screen.blit(y_strip, (400, 100))
screen.blit(y_strip, (400, 200))
screen.blit(y_strip, (400, 300))
screen.blit(y_strip, (400, 400))
screen.blit(y_strip, (400, 500))
screen.blit(y_strip, (400, 600))
screen.blit(strip, (120, 0))
screen.blit(strip, (680, 0))
# Getting the car on screen
def car(x, y):
screen.blit(c1_img, (x, y))
x_change = 0
x = 400
y = 470
car_width = 56
op_speed = 10
obs = 0
y_change = 0
obs_x = random.randrange(200, 650)
obs_y = -750
op_width = 56
op_height = 125
car_passed = 0
score = 0
level = 0
# Function for enemy cars
def obstacle(obs_x, obs_y, obs):
if obs == 0:
obs_img = pygame.image.load("car2.jpg")
elif obs == 1:
obs_img = pygame.image.load("car3.jpg")
elif obs == 2:
obs_img = pygame.image.load("car4.jpg")
elif obs == 3:
obs_img = pygame.image.load("car5.jpg")
elif obs == 4:
obs_img = pygame.image.load("car6.jpg")
elif obs == 5:
obs_img = pygame.image.load("car7.jpg")
screen.blit(obs_img, (obs_x, obs_y))
# message to display
font = pygame.font.SysFont("None", 150)
render = font.render("CAR CRASHED", True, (0, 0, 0))
# Function to display score
def sc(car_passed, score):
s_font = pygame.font.SysFont(None, 35)
passed = s_font.render("Passed:" + str(car_passed), True, (0, 0, 0))
score = s_font.render("Score:" + str(score), True, (0, 0, 0))
screen.blit(passed, (0, 30))
screen.blit(score, (0, 70))
# Text on Buttons
def text(text, font):
texts = font.render(text, True, (255, 255, 255))
return texts, texts.get_rect()
# Game loop
def game_loop():
global op_speed, x, car_passed, level, x_change, y_change, y, obs_y, obs_x, obs, score, font
running = True
while running:
# for checking the events
for event in pygame.event.get():
if event.type == pygame.QUIT: # QUIT event
running = False
if event.type == pygame.KEYDOWN: # KEYDOWN event
if event.key == pygame.K_LEFT:
x_change = -5
if event.key == pygame.K_RIGHT:
x_change = 5
if event.key == pygame.K_s: # Increase speed
op_speed += 2
if event.key == pygame.K_b: # decrease speed
op_speed -= 2
if event.type == pygame.KEYUP: # KEYUP event
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
x += x_change
screen.fill((119, 119, 119))
background()
obs_y -= (op_speed / 4)
obstacle(obs_x, obs_y, obs)
obs_y += op_speed
car(x, y)
sc(car_passed, score)
# Restricting the movement
if x > 680 - car_width or x 600:
obs_y = 0 - op_height
obs_x = random.randrange(170, 600)
obs = random.randrange(0, 6)
car_passed += 1
score = car_passed * 10
if int(car_passed) % 10 == 0:
level += 1
op_speed += 2
font = pygame.font.SysFont(None, 50)
level_text = font.render("Level" + str(level), True, (0, 0, 0))
screen.blit(level_text, (0, 100))
pygame.display.flip()
# car crash logic
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:
pygame.draw.rect(screen, (255, 0, 0), (580, 540, 150, 50))
if click == (1, 0, 0):
pygame.quit()
t = pygame.font.SysFont("arial", 30, "bold")
texts, textr = text("START", t)
textr.center = ((80 + (150 / 2)), (540 + (50 / 2)))
screen.blit(texts, textr)
texts, textr = text("QUIT", t)
textr.center = ((580 + (150 / 2)), (540 + (50 / 2)))
screen.blit(texts, textr)
pygame.display.update()
intro() # calling the intro function
game_loop() # calling the game loop
pygame.quit()