-
Notifications
You must be signed in to change notification settings - Fork 2
/
virtualCar.py
136 lines (105 loc) · 3.74 KB
/
virtualCar.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
from motor import Forward, Backward, Right, Left, Stop
import pygame
from time import sleep
# initialize things
pygame.init()
window = pygame.display.set_mode((700,400))
#window = pygame.display.set_mode((1155,399))
bg = pygame.image.load("image/road1.png")
car = pygame.image.load("image/car.png")
car = pygame.transform.scale(car, (40, 40))
clock = pygame.time.Clock()
car_x = 30
car_y = 260
JUMP_VALUE = 25 # turning point value
direction = 'y_up' # cars current direction
run = 1
Forward()
DELAY = .400
# main loop
while run:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = 0
# position images
window.blit(bg, (0, 0)) # load the track image
window.blit(car, (car_x, car_y)) # the car image
last_x, last_y = car_x, car_y
# find the center of the car and draw a point on that
center_x, center_y = (int(car_x + 40 /2), int(car_y + 40 / 2))
pygame.draw.circle(window, (0,255,255), (center_x, center_y), 5, 5)
cal_value = 30
y_up = window.get_at((center_x, center_y - cal_value))[0]
y_down = window.get_at((center_x, center_y + cal_value))[0]
x_right = window.get_at((center_x + cal_value, center_y))[0]
x_left = window.get_at((center_x - cal_value, center_y))[0]
#print("y_up ", y_up)
#print("y_down ", y_down)
#print("x_right", x_right)
#print("x_left ", x_left)
#print("-----------")
# determine which way to go
# go up
if y_up == 255 and direction == 'y_up' and x_left != 255 and x_right != 255:
# move up
car_y -= 2 # decrease pixel and move the car on y axis
# make the turn
if y_up == 255 and direction == 'y_up' and x_left != 255 and x_right == 255:
# make a right turn
direction = 'x_right'
car_y -= JUMP_VALUE
car_x += JUMP_VALUE
car = pygame.transform.rotate(car, -90)
window.blit(car, (car_x, car_y))
print('Turn Right')
Right()
sleep(DELAY)
Forward()
# go x right
if y_up != 255 and direction == 'x_right' and y_down != 255 and x_right == 255:
car_x += 2
if y_down == 255 and direction == 'x_right' and x_left == 255 and x_right == 255:
# make a turn from x_right
car = pygame.transform.rotate(car, -90)
direction = 'y_down'
car_y += JUMP_VALUE + 5
car_x += JUMP_VALUE
window.blit(car, (car_x, car_y))
print('Turn Right')
Right()
sleep(DELAY)
Forward()
# go y down
if y_down == 255 and direction == 'y_down' and x_left != 255 and x_right != 255:
# move down
car_y += 2
# left turn
if y_down == 255 and direction == 'y_down' and x_left != 255 and x_right == 255:
# turn from y_down
car = pygame.transform.rotate(car, 90)
direction = 'x_right'
car_y += JUMP_VALUE
car_x += JUMP_VALUE
print('Turn left')
Left()
sleep(DELAY)
Forward()
# turn to y up
if y_up == 255 and direction == 'x_right' and x_left == 255 and x_right == 255:
# turn from y_down
car = pygame.transform.rotate(car, 90)
direction = 'y_up'
car_y -= JUMP_VALUE + 5
car_x += JUMP_VALUE
print('Turn left')
Left()
sleep(DELAY)
Forward()
# if car is stopped
if car_x == last_x and car_y == last_y:
# stop the engine sound
print("STOPPED")
Stop()
pygame.display.update() # update the window
pygame.quit() #close everything