Repository navigation
Expand file tree
/
Copy pathmove_car.py
More file actions
75 lines (61 loc) · 2.55 KB
/
Copy pathmove_car.py
File metadata and controls
75 lines (61 loc) · 2.55 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
60
61
62
63
64
65
66
67
68
69
70
71
# Author: Chanin Woods
# Built: python v.3.3
# Updated: 24 February 2017
############################################################################
# Class for vehicle object
# Parameters: x (float position), y (float position), dir (string direction)
# Functions:
# turn_left(self)
# turn_right(self)
# move(self)
# reverse(self, distance)
############################################################################
class Vehicle():
def __init__(self, x, y, face):
self.x = x
self.y = y
self.dir = face
############################################################################
# Functions to turn vehicle toward left or right
# Input: self object
# Output: self object
############################################################################
def turn_left(self):
self.dir = directions[(directions.index(self.dir)-1)%len(directions)]
def turn_right(self):
self.dir = directions[(directions.index(self.dir)+1)%len(directions)]
############################################################################
# Function to move object one unit forward
# Input: self object
# Output: self object (position x or position y changes)
############################################################################
def move(self):
#initialize possible movements
movement = {'N': (0,1), 'E': (1,0), 'S': (0,-1), 'W':(-1,0)}
#move forward
new_x = self.x + movement[self.dir][0]
new_y = self.y + movement[self.dir][1]
#update position
if new_x != self.x:
self.x = new_x
if new_y != self.y:
self.y = new_y
############################################################################
# Function to reverse the vehicle (moves backward, but doesn't flip around)
# Input: distance (float distance to reverse)
# Output: self object (position x or position y changes)
############################################################################
def reverse(self, distance):
#check inputs
if not isinstance(distance, float) and not isinstance(distance, int):
raise ValueError('distance variable must be a float')
#reverse vehicle
direction = self.dir
if direction == 'N':
self.y = self.y - distance
elif direction == 'S':
self.y = self.y + distance
elif direction == 'W':
self.x = self.x + distance
elif direction == 'E':
self.x = self.x - distance