-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathmain.py
More file actions
59 lines (58 loc) · 1.99 KB
/
main.py
File metadata and controls
59 lines (58 loc) · 1.99 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
import pygame
pygame.init()
window = pygame.display.set_mode((1200, 400))
track = pygame.image.load('track6.png')
car = pygame.image.load('tesla.png')
car = pygame.transform.scale(car, (30, 60))
car_x = 155
car_y = 300
focal_dis = 25
cam_x_offset = 0
cam_y_offset = 0
direction = 'up'
drive = True
clock = pygame.time.Clock()
while drive:
for event in pygame.event.get():
if event.type == pygame.QUIT:
drive = False
clock.tick(60)
cam_x = car_x + cam_x_offset + 15
cam_y = car_y + cam_y_offset + 15
up_px = window.get_at((cam_x, cam_y - focal_dis))[0]
down_px = window.get_at((cam_x, cam_y + focal_dis))[0]
right_px = window.get_at((cam_x + focal_dis, cam_y))[0]
print(up_px, right_px, down_px)
# change direction (take turn)
if direction == 'up' and up_px != 255 and right_px == 255:
direction = 'right'
cam_x_offset = 30
car = pygame.transform.rotate(car, -90)
elif direction == 'right' and right_px != 255 and down_px == 255:
direction = 'down'
car_x = car_x + 30
cam_x_offset = 0
cam_y_offset = 30
car = pygame.transform.rotate(car, -90)
elif direction == 'down' and down_px != 255 and right_px == 255:
direction = 'right'
car_y = car_y + 30
cam_x_offset = 30
cam_y_offset = 0
car = pygame.transform.rotate(car, 90)
elif direction == 'right' and right_px != 255 and up_px == 255:
direction = 'up'
car_x = car_x + 30
cam_x_offset = 0
car = pygame.transform.rotate(car, 90)
# drive
if direction == 'up' and up_px == 255:
car_y = car_y - 2
elif direction == 'right' and right_px == 255:
car_x = car_x + 2
elif direction == 'down' and down_px == 255:
car_y = car_y + 2
window.blit(track, (0, 0))
window.blit(car, (car_x, car_y))
pygame.draw.circle(window, (0, 255, 0), (cam_x, cam_y), 5, 5)
pygame.display.update()