-
Notifications
You must be signed in to change notification settings - Fork 0
/
Car-Simulation.py
188 lines (143 loc) · 6.5 KB
/
Car-Simulation.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
RIGHT = 1
LEFT = -1
REVERSE_DIRECTION = -6
FORWARD = 1
REVERSE = 0
#gears and allowable speeds are as follows:
#zero (reverse) (speed -1 to -10). Max reverse speed of car is -10
#one (speed 0 to 10)
#two (speed 5 to 25)
#three (speed 20 to 40)
#four (speed 40 to 60)
#five (speed 45 to 80). Max speed of car is 80
#gears change automatically, one gear at a time
#direction values are similar to numbers on clock face
#0 = 12 = straight on. All other directions = 1-11
class Car:
def __init__(self):
self.speed = 0
self.gear = 1
self.direction = 0
self.broken = False #indicates whether car is broken
self.simulation = []
self.simulation_loaded = False
def accelerate(self, ACCELERATE):
if self.broken: #checking is the car broken
print("The car is broken....")
return
print("Accelerating....") #creating a condition and based on the car is in reverse or forward
# the car will get either +5 or -5 to then speed.
if self.gear == "REVERSE":
self.speed -= 5
self.change_gear("REVERSE")
else:
self.speed += 5
self.change_gear("FORWARD")
if self.speed > 80: #setting up top speed limit for forward gear and reverse as well
self.speed = 80
print("You have reached the cars top speed!")
if self.speed < -10:
self.speed = -10
print("You have reached the cars top reverse speed!")
self.display_stats()
def brake(self):
if self.broken: #checking is the car broken
print("The car is broken....")
return
if self.gear == "FORWARD": #checking what gear that is currently in
self.change_gear("FORWARD")
print("The car is in forward gear")
if self.gear == "REVERSE":
self.change_gear("REVERSE")
print("The car is in reverse!")
print("Breaking...") #braking which will either take or add 5 to the speed variable
#based on the gear the car is in
if self.speed < 0:
self.speed += 5
if self.speed > 0:
self.speed -= 5
self.display_stats()
def turn_steering_wheel(self, direction_change):
if self.broken: #checking is the car broken
print("The car is broken....")
return
if self.gear == REVERSE: #checking what gear that is currently in
print("Car is in reverse")
if self.gear == FORWARD:
print("Car is in forward gear")
if direction_change == "LEFT": #Changing the direction variable based on the incoming
print("taking a left turn...") #directions
self.direction += LEFT
if direction_change == "RIGHT":
print("taking a right turn...")
self.direction += RIGHT
if direction_change == "REVERSE_DIRECTION":
print("Reverse direction...")
self.direction += REVERSE_DIRECTION
self.display_stats()
def change_gear(self, selected_gear): #selected_gear is either FORWARD or REVERSE
if self.broken: #checking is the car broken
print("The car is broken....")
return
if selected_gear == "FORWARD" and self.speed < 0: #setting up a condition that would brake the car if it's alredy in moovewment
print("You have selected the wrong gear for the speed of the car")
self.broken = True
return
if selected_gear == "REVERSE" and self.speed > 0:
print("You have selected the wrong gear for the speed of the car")
self.broken = True
return
target_gear = 0 #creating a variable for the gearbox
if self.speed < 0: #creating the speed limits for each gear
target_gear = 0
if self.speed >= 0 and self.speed < 10:
target_gear = 1
if self.speed > 5 and self.speed < 25:
target_gear = 2
if self.speed > 20 and self.speed <= 40:
target_gear = 3
if self.speed > 40 and self.speed <= 60:
target_gear = 4
if self.speed > 45 and self.speed <= 80:
target_gear = 5
while self.gear != target_gear: #creating a condition that would change the gear one-by-one
if self.gear < target_gear: #till it is in the required one, wile providing feedback to the user
self.gear+= 1
print("Changing up...")
print(f"current gear {self.gear}")
elif self.gear > target_gear:
self.gear-=1
print("Changing down....")
print(f"current gear {self.gear}")
def display_stats(self):
print(f"Speed = {self.speed}, Gear = {self.gear}, Direction = {self.direction}") #displaying the status of the car
def load_simulation(self, filename):
file = open(filename, 'r') #loading in the file that contains the commands for the car
line = file.readline()
while line != "":
line = line.strip()
self.simulation.append(line)
line = file.readline()
file.close()
return self.simulation
def run_simulation(self):
for x in self.simulation: #calling in the functions of the class based on the type of command
if x == "LEFT":
self.turn_steering_wheel("LEFT")
if x == "RIGHT":
self.turn_steering_wheel("RIGHT")
if x == "REVERSE_DIRECTION":
self.turn_steering_wheel("REVERSE_DIRECTION")
if x == "FORWARD":
self.change_gear("FORWARD")
if x == "REVERSE":
self.change_gear("REVERSE")
if x == "ACCELERATE":
self.accelerate("ACCELERATE")
if x == "BRAKE":
self.brake()
return self.turn_steering_wheel, self.change_gear, self.accelerate, self.brake
if __name__ == '__main__':
my_car = Car()
my_car.load_simulation("simulation.txt")
my_car.run_simulation()