-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
66 lines (54 loc) · 1.78 KB
/
main.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
import pygame,sys
class Car(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.original_image = pygame.image.load('Audi.png')
self.image = self.original_image
self.rect = self.image.get_rect(center = (640,360))
self.angle = 0
self.rotation_speed = 1.8
self.direction = 0
self.forward = pygame.math.Vector2(0,-1)
self.active = False
def set_rotation(self):
if self.direction == 1:
self.angle -= self.rotation_speed
if self.direction == -1:
self.angle += self.rotation_speed
self.image = pygame.transform.rotozoom(self.original_image,self.angle,0.25)
self.rect = self.image.get_rect(center = self.rect.center)
def get_rotation(self):
if self.direction == 1:
self.forward.rotate_ip(self.rotation_speed)
if self.direction == -1:
self.forward.rotate_ip(-self.rotation_speed)
def accelerate(self):
if self.active:
self.rect.center += self.forward * 5
def update(self):
self.set_rotation()
self.get_rotation()
self.accelerate()
pygame.init()
screen = pygame.display.set_mode((1280,720))
clock = pygame.time.Clock()
bg_track = pygame.image.load('Track.png')
car = pygame.sprite.GroupSingle(Car())
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT: car.sprite.direction += 1
if event.key == pygame.K_LEFT: car.sprite.direction -= 1
if event.key == pygame.K_SPACE: car.sprite.active = True
if event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT: car.sprite.direction -= 1
if event.key == pygame.K_LEFT: car.sprite.direction += 1
if event.key == pygame.K_SPACE: car.sprite.active = False
screen.blit(bg_track,(0,0))
car.draw(screen)
car.update()
pygame.display.update()
clock.tick(120)