-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTurtle Tressure Hunt
779 lines (677 loc) · 25 KB
/
Turtle Tressure Hunt
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
#-----Statement of Authorship----------------------------------------#
#
# This is an individual assessment item. By submitting this
# code I agree that it represents my own work. I am aware of
# the University rule that a student must not act in a manner
# which constitutes academic dishonesty as stated and explained
# in QUT's Manual of Policies and Procedures, Section C/5.3
# "Academic Integrity" and Section E/2.1 "Student Code of Conduct".
#
# Student no: n10355243
# Student name: Nihar Hasmukhbhai Rupareliya
#
# NB: Files submitted without a completed copy of this statement
# will not be marked. All files submitted will be subjected to
# software plagiarism analysis using the MoSS system
# (http://theory.stanford.edu/~aiken/moss/). 81023PT
#
#--------------------------------------------------------------------#
#-----Task Description-----------------------------------------------#
#
# TREASURE MAP
#
# This assignment tests your skills at processing data stored in
# lists, creating reusable code and following instructions to display
# a complex visual image. The incomplete Python program below is
# missing a crucial function, "follow_path". You are required to
# complete this function so that when the program is run it traces
# a path on the screen, drawing "tokens" to indicate discoveries made
# along the way, while using data stored in a list to determine the
# steps to be taken. See the instruction sheet accompanying this
# file for full details.
#
#--------------------------------------------------------------------#
#-----Preamble-------------------------------------------------------#
#
# This section imports necessary functions and defines constant
# values used for creating the drawing canvas. You should not change
# any of the code in this section.
#
# Import the functions needed to complete this assignment.
# You may not use any other modules for your solution.
from turtle import *
from math import *
from random import *
# Define constant values used in the main program that sets up
# the drawing canvas. Do not change any of these values.
grid_size = 100 # pixels
num_squares = 7 # to create a 7x7 map grid
margin = 50 # pixels, the size of the margin around the grid
legend_space = 400 # pixels, the space to leave for the legend
window_height = grid_size * num_squares + margin * 2
window_width = grid_size * num_squares + margin + legend_space
font_size = 18 # size of characters for the coords
starting_points = ['Top left', 'Top right', 'Centre',
'Bottom left', 'Bottom right']
#
#--------------------------------------------------------------------#
#-----Functions for Creating the Drawing Canvas----------------------#
#
# The functions in this section are called by the main program to
# manage the drawing canvas for your image. You should not change
# any of the code in this section. (Very keen students are welcome
# to draw their own background, provided they do not change the map's
# grid or affect the ability to see it.)
#
# Set up the canvas and draw the background for the overall image
def create_drawing_canvas():
# Set up the drawing window with enough space for the grid and
# legend
setup(window_width, window_height)
setworldcoordinates(-margin, -margin, window_width - margin,
window_height - margin)
# Draw as quickly as possible
tracer(False)
# Choose a neutral background colour (if you want to draw your
# own background put the code here, but do not change any of the
# following code that draws the grid)
bgcolor('light grey')
# Get ready to draw the grid
penup()
color('slate grey')
width(2)
# Draw the horizontal grid lines
setheading(0) # face east
for y_coord in range(0, (num_squares + 1) * grid_size, grid_size):
penup()
goto(0, y_coord)
pendown()
forward(num_squares * grid_size)
# Draw the vertical grid lines
setheading(90) # face north
for x_coord in range(0, (num_squares + 1) * grid_size, grid_size):
penup()
goto(x_coord, 0)
pendown()
forward(num_squares * grid_size)
# Draw each of the labels on the x axis
penup()
y_offset = -27 # pixels
for x_coord in range(0, (num_squares + 1) * grid_size, grid_size):
goto(x_coord, y_offset)
write(str(x_coord), align = 'center',
font=('Arial', font_size, 'normal'))
# Draw each of the labels on the y axis
penup()
x_offset, y_offset = -5, -10 # pixels
for y_coord in range(0, (num_squares + 1) * grid_size, grid_size):
goto(x_offset, y_coord + y_offset)
write(str(y_coord), align = 'right',
font=('Arial', font_size, 'normal'))
# Mark the space for drawing the legend
goto((num_squares * grid_size) + margin, (num_squares * grid_size) // 2)
write(' ', align = 'left',
font=('Arial', 24, 'normal'))
# Reset everything ready for the student's solution
pencolor('black')
width(1)
penup()
home()
tracer(True)
# End the program and release the drawing canvas to the operating
# system. By default the cursor (turtle) is hidden when the
# program ends - call the function with False as the argument to
# prevent this.
def release_drawing_canvas(hide_cursor = True):
tracer(True) # ensure any drawing still in progress is displayed
if hide_cursor:
hideturtle()
done()
#
#--------------------------------------------------------------------#
#-----Test Data for Use During Code Development----------------------#
#
# The "fixed" data sets in this section are provided to help you
# develop and test your code. You can use them as the argument to
# the follow_path function while perfecting your solution. However,
# they will NOT be used to assess your program. Your solution will
# be assessed using the random_path function appearing below. Your
# program must work correctly for any data set that can be generated
# by the random_path function.
#
# Each of the data sets is a list of instructions expressed as
# triples. The instructions have two different forms. The first
# instruction in the data set is always of the form
#
# ['Start', location, token_number]
#
# where the location may be 'Top left', 'Top right', 'Centre',
# 'Bottom left' or 'Bottom right', and the token_number is an
# integer from 0 to 4, inclusive. This instruction tells us where
# to begin our treasure hunt and the token that we find there.
# (Every square we visit will yield a token, including the first.)
#
# The remaining instructions, if any, are all of the form
#
# [direction, number_of_squares, token_number]
#
# where the direction may be 'North', 'South', 'East' or 'West',
# the number_of_squares is a positive integer, and the token_number
# is an integer from 0 to 4, inclusive. This instruction tells
# us where to go from our current location in the grid and the
# token that we will find in the target square. See the instructions
# accompanying this file for examples.
#
# Some starting points - the following fixed paths just start a path
# with each of the five tokens in a different location
fixed_path_0 = [['Start', 'Top left', 0]]
fixed_path_1 = [['Start', 'Top right', 1]]
fixed_path_2 = [['Start', 'Centre', 2]]
fixed_path_3 = [['Start', 'Bottom left', 3]]
fixed_path_4 = [['Start', 'Bottom right', 4]]
# Some miscellaneous paths which encounter all five tokens once
fixed_path_5 = [['Start', 'Top left', 0], ['East', 1, 1], ['East', 1, 2],
['East', 1, 3], ['East', 1, 4]]
fixed_path_6 = [['Start', 'Bottom right', 0], ['West', 1, 1], ['West', 1, 2],
['West', 1, 3], ['West', 1, 4]]
fixed_path_7 = [['Start', 'Centre', 4], ['North', 2, 3], ['East', 2, 2],
['South', 4, 1], ['West', 2, 0]]
# A path which finds each token twice
fixed_path_8 = [['Start', 'Bottom left', 1], ['East', 5, 2],
['North', 2, 3], ['North', 4, 0], ['South', 3, 2],
['West', 4, 0], ['West', 1, 4],
['East', 3, 1], ['South', 3, 4], ['East', 1, 3]]
# Some short paths
fixed_path_9 = [['Start', 'Centre', 0], ['East', 3, 2],
['North', 2, 1], ['West', 2, 3],
['South', 3, 4], ['West', 4, 1]]
fixed_path_10 = [['Start', 'Top left', 2], ['East', 6, 3], ['South', 1, 0],
['South', 1, 0], ['West', 6, 2], ['South', 4, 3]]
fixed_path_11 = [['Start', 'Top left', 2], ['South', 1, 0], ['East', 2, 4],
['South', 1, 1], ['East', 3, 4], ['West', 1, 3],
['South', 2, 0]]
# Some long paths
fixed_path_12 = [['Start', 'Top right', 2], ['South', 4, 0],
['South', 1, 1], ['North', 3, 4], ['West', 4, 0],
['West', 2, 0], ['South', 3, 4], ['East', 2, 3],
['East', 1, 1], ['North', 3, 2], ['South', 1, 3],
['North', 3, 2], ['West', 1, 2], ['South', 3, 4],
['East', 3, 0], ['South', 1, 1]]
fixed_path_13 = [['Start', 'Top left', 1], ['East', 5, 3], ['West', 4, 2],
['East', 1, 3], ['East', 2, 2], ['South', 5, 1],
['North', 2, 0], ['East', 2, 0], ['West', 1, 1],
['West', 5, 0], ['South', 1, 3], ['East', 3, 0],
['East', 1, 4], ['North', 3, 0], ['West', 1, 4],
['West', 3, 1], ['South', 4, 1], ['East', 5, 1],
['West', 4, 0]]
# "I've been everywhere, man!" - this path visits every square in
# the grid, with randomised choices of tokens
fixed_path_99 = [['Start', 'Top left', randint(0, 4)]] + \
[['East', 1, randint(0, 4)] for step in range(6)] + \
[['South', 1, randint(0, 4)]] + \
[['West', 1, randint(0, 4)] for step in range(6)] + \
[['South', 1, randint(0, 4)]] + \
[['East', 1, randint(0, 4)] for step in range(6)] + \
[['South', 1, randint(0, 4)]] + \
[['West', 1, randint(0, 4)] for step in range(6)] + \
[['South', 1, randint(0, 4)]] + \
[['East', 1, randint(0, 4)] for step in range(6)] + \
[['South', 1, randint(0, 4)]] + \
[['West', 1, randint(0, 4)] for step in range(6)] + \
[['South', 1, randint(0, 4)]] + \
[['East', 1, randint(0, 4)] for step in range(6)]
# If you want to create your own test data sets put them here
my_fixed_path = [['Start', 'Top left', randint(0, 4)], ['East',2,randint(0,4)] , ['South',2, randint(0,4)] , ['West',2, randint(0,4)]]
#--------------------------------------------------------------------#
#-----Function for Assessing Your Solution---------------------------#
#
# The function in this section will be used to assess your solution.
# Do not change any of the code in this section.
#
# The following function creates a random data set specifying a path
# to follow. Your program must work for any data set that can be
# returned by this function. The results returned by calling this
# function will be used as the argument to your follow_path function
# during marking. For convenience during code development and
# marking this function also prints the path to be followed to the
# shell window.
#
# Note: For brevity this function uses some Python features not taught
# in ITD104 (dictionaries and list generators). You do not need to
# understand this code to complete the assignment.
#
def random_path(print_path = True):
# Select one of the five starting points, with a random token
path = [['Start', choice(starting_points), randint(0, 4)]]
# Determine our location in grid coords (assuming num_squares is odd)
start_coords = {'Top left': [0, num_squares - 1],
'Bottom left': [0, 0],
'Top right': [num_squares - 1, num_squares - 1],
'Centre': [num_squares // 2, num_squares // 2],
'Bottom right': [num_squares - 1, 0]}
location = start_coords[path[0][1]]
# Keep track of squares visited
been_there = [location]
# Create a path up to 19 steps long (so at most there will be 20 tokens)
for step in range(randint(0, 19)):
# Find places to go in each possible direction, calculating both
# the new grid square and the instruction required to take
# us there
go_north = [[[location[0], new_square],
['North', new_square - location[1], token]]
for new_square in range(location[1] + 1, num_squares)
for token in [0, 1, 2, 3, 4]
if not ([location[0], new_square] in been_there)]
go_south = [[[location[0], new_square],
['South', location[1] - new_square, token]]
for new_square in range(0, location[1])
for token in [0, 1, 2, 3, 4]
if not ([location[0], new_square] in been_there)]
go_west = [[[new_square, location[1]],
['West', location[0] - new_square, token]]
for new_square in range(0, location[0])
for token in [0, 1, 2, 3, 4]
if not ([new_square, location[1]] in been_there)]
go_east = [[[new_square, location[1]],
['East', new_square - location[0], token]]
for new_square in range(location[0] + 1, num_squares)
for token in [0, 1, 2, 3, 4]
if not ([new_square, location[1]] in been_there)]
# Choose a free square to go to, if any exist
options = go_north + go_south + go_east + go_west
if options == []: # nowhere left to go, so stop!
break
target_coord, instruction = choice(options)
# Remember being there
been_there.append(target_coord)
location = target_coord
# Add the move to the list of instructions
path.append(instruction)
# To assist with debugging and marking, print the list of
# instructions to be followed to the shell window
print('Welcome to the Treasure Hunt!')
print('Here are the steps you must follow...')
for instruction in path:
print(instruction)
# Return the random path
return path
#
#--------------------------------------------------------------------#
#-----Student's Solution---------------------------------------------#
#
# Complete the assignment by replacing the dummy function below with
# your own "follow_path" function.
#
#___________________________________ DEFINING THE TOKENS ____________________________________#
def token(number):
# This is code to make the Instagram Logo
if number == 0:
pendown()
width(3)
color("black")
fillcolor("steel blue")
begin_fill()
speed("fastest")
# Making the basic outer layer of our Instagram logo i.e a square box
for line in range(4):
forward(100)
left(90)
end_fill()
penup()
forward(20)
left(90)
forward(20)
right(90)
pendown()
color("white")
width(5)
#making the inner box for instagram logo
for instagram in range(4):
forward(60)
left(90)
#going to different positions and draw our logo
fillcolor("white")
left(90)
forward(40)
right(90)
begin_fill()
forward(40)
left(90)
forward(20)
left(90)
forward(40)
left(90)
forward(20)
left(90)
forward(60)
end_fill()
left(90)
forward(5)
right(90)
penup()
backward(30)
pendown()
width(5)
circle(-15)
penup()
backward(50)
right(90)
forward(65)
seth(0)
#This code is going to make Feedspot logo for us :D
elif number == 1:
pendown()
width(3)
color("black")
fillcolor("orange red")
begin_fill()
speed("fastest")
#outer box for our feedspot logo
for line in range(4):
forward(100)
left(90)
#drawing the logo with the help of circle, forward, left and right.
end_fill()
forward(20)
left(90)
penup()
forward(20)
color("white")
dot(20)
right(90)
forward(35)
right(90)
forward(10)
right(270)
left(90)
pendown()
width(8)
pendown()
circle(50,85)
penup()
circle(50,-85)
right(90)
forward(25)
left(90)
pendown()
circle(70,85)
circle(70,-85)
penup()
backward(10)
setheading(0)
backward(80)
#This will create a bebo logo for us :)
elif number == 2:
pendown()
width(3)
color("black")
fillcolor("brown")
speed("fast")
begin_fill()
#outer box for bebo
for square in range(4):
forward(100)
left(90)
end_fill()
penup()
left(90)
forward(80)
right(90)
forward(27)
right(90)
color("white")
width(9)
pendown()
forward(40)
circle(24,195)
left(75)
forward(35)
penup()
forward(39)
left(90)
forward(46)
setheading(0)
#That's the code to make dribble
elif number == 3:
pendown()
width(3)
color("black")
fillcolor("deep pink")
speed("fast")
begin_fill()
for square in range(4):
forward(100)
left(90)
#I used circle function to goto locations in different part of the circle and again come back from the same path..
end_fill()
color("white")
penup()
forward(50)
left(90)
forward(6)
right(90)
pendown()
width(6)
circle(44)
penup()
circle(44, 25)
penup()
left(60)
pendown()
pendown()
circle(90,55)
circle(90,-55)
right(60)
circle(44,50)
left(90)
circle(100,45)
penup()
circle(100,-45)
right(90)
circle(44,180)
left(90)
pendown()
circle(100,45)
penup()
circle(100,-45)
right(90)
circle(44,105)
right(90)
forward(6)
left(90)
backward(50)
#This is how you make World's favourite music app :D (Spotify)
else:
pendown()
color("black")
width(3)
fillcolor("black")
speed("fastest")
begin_fill()
#making the outer box for spotify
for step in range(4):
forward(100)
left(90)
#making spotify logo using different functions,
end_fill()
penup()
forward(50)
left(90)
forward(3)
right(90)
pendown()
color("dark green")
fillcolor("dark green")
begin_fill()
circle(46)
end_fill()
penup()
backward(20)
left(90)
forward(26)
color("black")
right(65)
width(6)
pendown()
circle(-45,55)
penup()
circle(-45,-55)
left(65)
forward(20)
pendown()
right(65)
width(7)
circle(-45,60)
penup()
circle(-45,-60)
left(65)
forward(20)
right(65)
width(8)
pendown()
circle(-45,65)
penup()
circle(-45,-65)
left(65)
backward(69)
setheading(0)
backward(30)
#____________________________________________________________________________________________________________#
#__________________________________ DEFINING GENERAL FUNCTIONS TO MOVE THE TURTLE WITHIN THE GRID_____________________________#
#defining a variable name to all the coordinate just to make it less confusing and more creative
Centre = (300,300)
Bottom_left = (0,0)
Bottom_right =(600,0)
Top_left = (0,600)
Top_right = (600,600)
# This function is for first part of our instruction which will send the tokens to the corners or centre in the beginning.
def goto_position(string_position):
if string_position == "Centre":
goto(Centre)
elif string_position == "Bottom left":
goto(Bottom_left)
elif string_position == "Bottom right":
goto(Bottom_right)
elif string_position == "Top left":
goto(Top_left)
elif string_position == "Top right":
goto(Top_right)
#This function is for rest of the instruction to move the tokens to different direction in the given grid.
def set_heading_from_string(string_heading,number):
if string_heading == "East":
setheading(0)
forward((number)*100)
setheading(0)
elif string_heading == "West" :
setheading(180)
forward(number*100)
setheading(0)
elif string_heading == "North":
setheading(90)
forward(100*number)
setheading(0)
elif string_heading == "South":
setheading(270)
forward(100*number)
setheading(0)
#___________________________________________________________________________________________________________#
##__________________________________________________ DEFINING LEGEND __________________________________________#
#defining a function which draws the legend and the tokens in it.
def legend():
penup()
goto(780,0)
width(5)
pendown()
#drawing the legend box.
for step in range(2):
forward(310)
left(90)
forward(700)
left(90)
penup()
#title of out legend
goto(880,650)
write("App Store", move=False, align="left", font=("peignot", 20, "bold"))
#line to seperate our topic with the legend tokens.
goto(780,630)
pendown()
forward(310)
penup()
goto(820,490)
#defining a variable tokens which has all our tokens from 0 to 4
tokens = [0,1,2,3,4]
setheading(0)
#drawing tokens in our legend.
for one_token in tokens:
token(one_token)
setheading(-90)
forward(120)
setheading(0)
##Giving name to our tokens with the help of list and for loop :D
penup()
color("black")
goto(960, 530)
pendown()
right(90)
penup()
tags = ["Instagram", "Feedspot","Bebo","Dribble","Spotify"]
for tag in tags:
write(tag, font = ("Arial",15,"bold"))
forward(120)
#_________________________________________________________________________________________________#
###__________________________________MAIN FINCTION___________________________________#
##
## Follow the path as per the provided dataset
def follow_path(path):
#calling our legend function to draw the legend.
legend()
penup()
home()
#instruction for the first line of instruction with "Start" in it (which places a token on the given corner or centre)
start_instruction = path[0]
goto_position(start_instruction[1])
token(start_instruction[2])
#instruction for rest of the code with the directions (East, North, West, South) to move our tinnie tiny tokens inside the grid
for index in range(1,len(path)):
instruction = path[index]
set_heading_from_string(instruction[0],instruction[1])
token(instruction[2])
#
#--------------------------------------------------------------------#
#-----Main Program---------------------------------------------------#
#
# This main program sets up the background, ready for you to start
# drawing your solution. Do not change any of this code except
# as indicated by the comments marked '*****'.
#
# Set up the drawing canvas
create_drawing_canvas()
# Control the drawing speed
# ***** Modify the following argument if you want to adjust
# ***** the drawing speed
speed('slowest')
# Decide whether or not to show the drawing being done step-by-step
# ***** Set the following argument to False if you don't want to wait
# ***** forever while the cursor moves around the screen
tracer(False)
# Give the drawing canvas a title
# ***** Replace this title with a description of your solution's theme
# ***** and its tokens
title("Apps Treasure map")
### Call the student's function to follow the path
### ***** While developing your program you can call the follow_path
### ***** function with one of the "fixed" data sets, but your
### ***** final solution must work with "random_path()" as the
### ***** argument to the follow_path function. Your program must
### ***** work for any data set that can be returned by the
### ***** random_path function.
# follow_path(fixed_path_0) # <-- used for code development only, not marking
print("Hey tressure seeker", end=', ')
follow_path(random_path()) # <-- used for assessment
# Exit gracefully
# ***** Change the default argument to False if you want the
# ***** cursor (turtle) to remain visible at the end of the
# ***** program as a debugging aid
release_drawing_canvas()
#
#--------------------------------------------------------------------#