3
3
# Title: WallTurtle
4
4
# Author: Adam Rumpf
5
5
# Version: 1.0.0
6
- # Date: 11/26/2020
6
+ # Date: 1/5/2021
7
7
8
+ import math
8
9
import game .tcturtle
9
10
10
11
class CombatTurtle (game .tcturtle .TurtleParent ):
11
12
"""Wall-hugging combat turtle.
12
13
13
- A turtle that attempts to navigate around obstacles by hugging walls using
14
- a left-hand rule .
14
+ A turtle that attempts to navigate around obstacles by "feeling" the walls
15
+ around it .
15
16
16
17
When it has direct line of sight to the opponent, it moves directly
17
18
towards it. Otherwise it moves towards the opponent until hitting a wall,
18
- at which point it wraps around the wall by keeping its left side against
19
- it.
19
+ at which point it attempts to turn so that the way ahead is free.
20
20
"""
21
21
22
22
#-------------------------------------------------------------------------
@@ -71,7 +71,9 @@ def setup(self):
71
71
Initialization code for Combat Turtle.
72
72
"""
73
73
74
- pass
74
+ # Define the relative polar coordinates around the turtle to scan
75
+ self .nose_rel = (8 , 0.0 ) # just ahead of turtle's front
76
+ self .hand_rel = (8 , math .pi / 2 ) # to left of turtle
75
77
76
78
#-------------------------------------------------------------------------
77
79
@@ -80,12 +82,47 @@ def step(self):
80
82
Step event code for Combat Turtle.
81
83
"""
82
84
83
- # Outline:
84
- # Turn and move towards the opponent as long as there's line of sight.
85
- # Otherwise, attempt to move towards opponent while scanning ahead of self.
86
- # If there's an obstacle, begin wall hugging behavior, which is based on scanning a point to left of self.
87
- # As long as that point is free, turn right while moving forward until it becomes blocked for the first time.
88
- # As long as that point is blocked, move forward.
89
- # After sticking to a wall, turn left whenever the point to the turtle's left is unblocked.
90
-
91
- pass
85
+ # Determine behavior based on whether there is line of sight
86
+ if (self .line_of_sight () == True ):
87
+ # If there is line of sight, move directly towards opponent
88
+
89
+ # Turn towards opponent
90
+ self .turn_towards ()
91
+
92
+ # Move towards opponent (or away if too close)
93
+ if self .distance () > 4 * self .missile_radius :
94
+ self .forward ()
95
+ else :
96
+ self .backward ()
97
+
98
+ # Shoot if facing opponent
99
+ if (self .can_shoot == True and
100
+ abs (self .relative_heading_towards ()) <= 10 ):
101
+ self .shoot ()
102
+
103
+ else :
104
+ # If no line of sight, attempt to navigate around obstacles
105
+
106
+ # Calculate Cartesian coordinates of nose and hand
107
+ nose = ((self .x + self .nose_rel [0 ]*
108
+ math .cos (math .radians (self .heading )+ self .nose_rel [1 ])),
109
+ (self .y - self .nose_rel [0 ]*
110
+ math .sin (math .radians (self .heading )+ self .nose_rel [1 ])))
111
+ hand = ((self .x + self .hand_rel [0 ]*
112
+ math .cos (math .radians (self .heading )+ self .hand_rel [1 ])),
113
+ (self .y - self .hand_rel [0 ]*
114
+ math .sin (math .radians (self .heading )+ self .hand_rel [1 ])))
115
+
116
+ # Determine behavior based on whether nose and hand are clear
117
+ if self .free_space (nose ) == True :
118
+ # Move forward when clear ahead
119
+ self .forward ()
120
+ else :
121
+ if self .free_space (hand ) == True :
122
+ # If free to left, turn left
123
+ self .left ()
124
+ self .forward ()
125
+ else :
126
+ # If blocked ahead and to left, turn right
127
+ self .right ()
128
+ self .forward ()
0 commit comments