-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCar
More file actions
54 lines (39 loc) · 1.4 KB
/
Copy pathCar
File metadata and controls
54 lines (39 loc) · 1.4 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
import pygame
pygame.init()
FPS = 60
SCREEN = (600, 400)
WHITE = (255, 255, 255)
sc = pygame.display.set_mode(SCREEN)
sc.fill(WHITE)
car_surf_main = pygame.image.load('Python/car.png').convert_alpha()
car_surf = car_surf_main
car_rect = car_surf_main.get_rect(center=(300, 200))
sc.blit(car_surf_main, car_rect)
pygame.display.update()
play = True
while play:
for event in pygame.event.get():
if event.type == pygame.QUIT:
play = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
car_surf = pygame.transform.rotate(car_surf_main, -90)
elif event.key == pygame.K_LEFT:
car_surf = pygame.transform.rotate(car_surf_main, 90)
elif event.key == pygame.K_UP:
car_surf = car_surf_main
elif event.key == pygame.K_DOWN:
car_surf = pygame.transform.rotate(car_surf_main, 180)
keys = pygame.key.get_pressed()
if keys[pygame.K_RIGHT] and car_rect.x + car_rect.height < SCREEN[0]:
car_rect.x += 3
if keys[pygame.K_LEFT] and car_rect.x > 0:
car_rect.x -= 3
if keys[pygame.K_UP] and car_rect.y > 0:
car_rect.y -= 3
if keys[pygame.K_DOWN] and car_rect.y + car_rect.height < SCREEN[1]:
car_rect.y += 3
sc.fill(WHITE)
sc.blit(car_surf, car_rect)
pygame.display.update()
pygame.time.Clock().tick(FPS)