-
Notifications
You must be signed in to change notification settings - Fork 0
/
hello.py
189 lines (155 loc) · 4.86 KB
/
hello.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import pygame
import time
import sys
import random
pygame.init()
#color
white=(255,255,255)
black=(0,0,0)
grey=(119,118,110)
red=(200,0,0)
bright_red=(255,0,0)
green=(0,200,0)
bright_green=(0,255,0)
blue=(0,0,255)
window_width=800
window_hight=600
l=[]
#display surface
wd=pygame.display.set_mode((window_width,window_hight))
carimg=pygame.image.load('Screenshot (209).jpg')
car1=pygame.transform.scale(carimg,(100,100))
clock=pygame.time.Clock()
#message to be printed on the window
def message(mess,colour,size,x,y):
font=pygame.font.SysFont(None,size)
screen_text=font.render(mess,True,colour)
wd.blit(screen_text,(x,y))
pygame.display.update()
#making load function to load images
def load(name,x_pos,y_pos):
v = pygame.image.load(name)
wd.blit(v, (x_pos, y_pos))
pygame.display.update()
#making 2 switches displayed on the window
def button(x,y,w,h,mess,mess_color,actc,noc,size,tx,ty,func):
mouse = pygame.mouse.get_pos()
click= pygame.mouse.get_pressed()
if x < mouse[0] < x+w and y < mouse[1] < y+h:
pygame.draw.rect(wd, actc, [x, y, w, h])
message(mess, mess_color, size, tx, ty)
pygame.display.update()
if click==(1,0,0):
func()
else:
pygame.draw.rect(wd, noc, [x, y, w, h])
message(mess, mess_color, size, tx, ty)
pygame.display.update()
pygame.display.update()
def quit1():
pygame.quit()
quit()
#making a function for the game introduction i.e before game starts
def game_intro():
load('background 1.png', 0, 0)
gameintro=False
while gameintro==False:
for i in pygame.event.get():
if i.type == pygame.QUIT:
gameintro = True
game_over=True
window_width = 800
window_hight = 600
message('MAIN MENU',green,100,(window_width/2 - 220),100)
button(100, 400, 70, 30, 'GO!', white, bright_red, red,25,106,406,gameloop)
button(600, 400, 70, 30, 'QUIT', white, bright_green, green,25,606,406,quit1)
pygame.display.update()
pygame.display.update()
def back():
blue_strip=pygame.image.load('border.png')
img=pygame.transform.scale(blue_strip,(100,600))
wd.blit(img,(0,0))
wd.blit(img,(700,0))
# making a function for game over when car touches boundaries of the road
def crash(x):
if x<90 or x+90>700:
font = pygame.font.SysFont(None, 100)
screen_text = font.render('game_over', True, white)
wd.blit(screen_text, (250, 280))
pygame.display.update()
time.sleep(1)
game_intro()
for j in pygame.event.get():
if j.type == pygame.QUIT:
pygame.quit()
quit()
pygame.display.update()
#making a function for crashed when player car collides with the other car
def car_crash(x,y,y_en,x_en):
if x<x_en+57<x+150 and (y<y_en+100<y+100 or y<y_en<y+100):
message('CRASHED!', red, 100, 250, 280)
time.sleep(1)
game_intro()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
pygame.display.update()
#making a function for score.
def score(count):
font = pygame.font.SysFont(None, 30)
screen_text = font.render('score :' + str(count), True, white)
wd.blit(screen_text, (0, 0))
pygame.display.update()
#putting random cars on road
def other_car(y_en):
enmy1=pygame.image.load('Screenshot (209).jpg')
enmy=pygame.transform.scale(enmy1,(70,100))
global x_en
if y_en==0:
x_en=random.randrange(100,600)
l.clear()
l.append(x_en)
else:
x_en=l[0]
wd.blit(enmy,(x_en,y_en))
pygame.display.update()
#making the game
def gameloop():
x = 300
y = 400
x_change = 0
y_change = 0
global game_over
game_over=False
count = 0
y_en=0
while game_over==False:
#code for the functioning of right and left keys
for i in pygame.event.get():
if i.type==pygame.QUIT:
game_over=True
if i.type==pygame.KEYDOWN:
if i.key==pygame.K_LEFT:
x_change=-10
elif i.key==pygame.K_RIGHT:
x_change=+10
if i.type==pygame.KEYUP:
if i.key == pygame.K_LEFT or i.key == pygame.K_RIGHT:
x_change=0
x+=x_change
wd.fill(grey)
back()
wd.blit(car1, (x, y))
if y_en>600:
y_en=0
count += 1
other_car(y_en)
y_en+=10
crash(x)
car_crash(x,y,y_en,x_en)
score(count)
clock.tick(30)
pygame.display.update()
game_intro()
pygame.quit()
quit()