-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCars
More file actions
89 lines (70 loc) · 2.32 KB
/
Copy pathCars
File metadata and controls
89 lines (70 loc) · 2.32 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
from random import randint
import pygame
pygame.init()
pygame.time.set_timer(pygame.USEREVENT, 250)
FPS = 24
W = 400
H = 400
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
CARS = ('car1.png', 'car2.png',
'car3.png')
CARS_SURF = []
pygame.mixer.music.load('C:\Music.wav')
sound_crash = pygame.mixer.Sound('C:\Crash.wav')
clock = pygame.time.Clock()
font = pygame.font.Font(pygame.font.match_font('dejavusans'), 36)
text1 = font.render('Game over', 1, (180, 0, 0))
sc = pygame.display.set_mode((W, H))
for i in range(len(CARS)):
CARS_SURF.append(pygame.image.load(CARS[i]).convert_alpha())
class Car(pygame.sprite.Sprite):
def __init__(self, x, surf, group):
pygame.sprite.Sprite.__init__(self)
self.image = surf
self.rect = self.image.get_rect(center=(x, 0))
self.add(group)
self.speed = randint(1, 3)
def update(self):
if self.rect.y < H:
self.rect.y += self.speed
else:
self.kill()
class User_car(pygame.sprite.Sprite):
def __init__(self, x, surf):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.transform.rotate(surf, 180)
self.rect = self.image.get_rect(center=(x, H))
cars = pygame.sprite.Group()
Car(randint(1, W), CARS_SURF[randint(0, 2)], cars)
user_car1 = User_car(randint(1, W), CARS_SURF[randint(0, 2)])
while 1:
for i in pygame.event.get():
if i.type == pygame.QUIT:
pygame.quit()
break
elif i.type == pygame.USEREVENT:
Car(randint(1, W), CARS_SURF[randint(0, 2)], cars)
elif i.type == pygame.KEYDOWN:
if i.key == pygame.K_LEFT:
user_car1.rect[0] -= 10
elif i.key == pygame.K_RIGHT:
user_car1.rect[0] += 10
elif i.key == pygame.K_UP:
user_car1.rect[1] -= 10
elif i.key == pygame.K_DOWN:
user_car1.rect[1] += 10
if (pygame.sprite.spritecollideany(user_car1, cars)) != None:
sound_crash.play()
sc.fill(BLACK)
sc.blit(text1, (W // 2, H // 2))
pygame.mixer.music.stop()
pygame.display.update()
break
else:
sc.fill(WHITE)
sc.blit(user_car1.image, user_car1.rect)
cars.draw(sc)
pygame.display.update()
cars.update()
pygame.time.delay(FPS)