/
turtle-map.py
137 lines (118 loc) · 3.13 KB
/
turtle-map.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
import turtle
import sys
import getopt
import csv
master_city = []
# CLASSES
# City Class
class City(object):
def __init__(self, name, xcoord, ycoord, pop):
self.name = name
self.xcoord = xcoord
self.ycoord = ycoord + 100
self.pop = pop
global master_city
master_city.append(self)
def getx(self):
return self.xcoord
def gety(self):
return self.ycoord
def getname(self):
return self.name
def getpop(self):
return self.pop
# Road Class
class Road(object):
def __init__(self, start, end):
self.start = start
self.end = end
# VARIOUS FUNCTIONS
# Create New City Object
def create_city(line_item):
print("N:", line_item[1], "X", line_item[2], "Y", line_item[3], "P", line_item[4])
#print("Master:", master_city)
new_city = City(line_item[1], float(line_item[2]), float(line_item[3]), float(line_item[4]))
city_circle(new_city)
city_name(new_city)
# Create New Road Object
def create_road(line_item):
new_road = Road(line_item[1], line_item[2])
draw_road(new_road)
# Print City Circle
def city_circle(city):
turtle.penup()
turtle.setx(city.xcoord)
turtle.sety(city.ycoord)
if city.getpop() > 0 and city.getpop() < 90001:
turtle.setx(turtle.xcor() + 2)
turtle.pendown()
turtle.circle(2)
turtle.penup()
turtle.setx(turtle.xcor() - 2)
if city.getpop() > 90000:
turtle.setx(turtle.xcor() + 3)
turtle.pendown()
turtle.begin_fill()
turtle.circle(3)
turtle.end_fill()
turtle.penup()
turtle.setx(turtle.xcor() - 3)
# Print City Name
def city_name(city):
if city.getname()[0] != "_":
turtle.setx(turtle.xcor() + 10)
turtle.sety(turtle.ycor() - 2)
turtle.write(city.getname(), align="left")
# Draw a Road
def draw_road(road):
startx = 0
starty = 0
endx = 0
endy = 0
for city in master_city:
if road.start == city.getname():
print("Start City:", city.getname())
startx = city.getx()
starty = city.gety()
if road.end == city.getname():
print("End City:", city.getname())
endx = city.getx()
endy = city.gety()
turtle.goto(startx, starty)
turtle.pendown()
turtle.goto(endx, endy)
turtle.penup()
# MAIN PROGRAM
# Main Function
def main(argv):
user_file = ""
# User GetOpt to Pull File from Command Line
try:
opts, args = getopt.getopt(argv,"hi:",["ifile="])
except getopt.GetoptError:
print("name-strip.py -i <input_file>")
sys.exit(2)
for opt, arg in opts:
if opt in ("-h", "--help"):
print("name-strip.py HELP\n\t-i <input_file>\t selects input file csv to interpret as map")
sys.exit()
elif opt in ("-i", "--ifile"):
user_file = arg
# Quit if no File Given
if user_file == "":
print("No file entered. Program terminating.")
sys.exit()
# Set Up CSV Reader
mapReader = csv.reader(open(user_file, newline=''), delimiter=',', quotechar='|')
# Iterate Through CSV
for map_item in mapReader:
#print("Map Item:", map_item)
if map_item[0] == "c":
create_city(map_item)
else:
create_road(map_item)
turtle.hideturtle()
turtle.done()
# Run Program from Command Line
if __name__ == "__main__":
main(sys.argv[1:])