In: Computer Science
Your (turtle) program in python must include: Create four Red turtles and four Blue turtles using one or more lists. • Start each of the Red turtles at a different random location on the left side of the screen within the range of the square formed by (-100, 100) and (0, -100) and each of the Blue turtles at a random location on the right side of the screen within the range of the square formed by (0, 100) and (100, -100). • Each of the red and blue turtles should move randomly. • Draw a boundary for the game as a circle with a 300 unit radius. You will use an invisible turtle to do this. It does not count as one of the turtles on either team. • If a turtle hits the border the turtle should have its color changed to black and should jump back to the center of the screen (0, 0) and continue moving. • All the turtles will move the same speed. • The turtles should continue moving until 1000 time units have elapsed.
Code:
import random
import pandas as pd
import math
blue_turtle = [['blue1', 0, 0, 'blue'], ['blue2', 0, 0, 'blue'], ['blue3', 0, 0, 'blue'], ['blue4', 0, 0, 'blue']]
red_turtle = [['red1', 0, 0, 'red'], ['red2', 0, 0, 'red'], ['red3', 0, 0, 'red'], ['red4', 0, 0, 'red']]
red_data = pd.DataFrame(red_turtle, columns= ['RTurt','X', 'Y', 'Color']) # Red Turtles dataframe
blue_data = pd.DataFrame(blue_turtle, columns= ['BTurt','X', 'Y', 'Color']) # Blue Turtles dataframe
# Initial coordinate generator for blue turtles
for k in blue_data['BTurt']:
blue_data.loc[blue_data['BTurt'] == k, 'X'] = random.choices(range(0,100))
blue_data.loc[blue_data['BTurt'] == k, 'Y'] = random.choices(range(-100,0))
# Initial coordinate generator for red turtles
for m in red_data['RTurt']:
red_data.loc[red_data['RTurt'] == m, 'X'] = random.choices(range(-100,0))
red_data.loc[red_data['RTurt'] == m, 'Y'] = random.choices(range(0,100))
print(red_data)
print(blue_data)
def red_step():
''' This function is called to randomly generate the red turtles step direction
and update the dataframe with new X and Y coordinates
'''
l = 0 # Dummy Index Variable
for r in red_data['RTurt']:
rand = random.choice([1,2,3,4]) # The turtle can move in (x +/- 1) or (y +/- 1) at a time, this random generator decides
# direction in which the turtle moves
if rand == 1: # for x + 1
red_data.loc[red_data['RTurt'] == r, 'X'] = red_data['X'] + 1
elif rand == 2: # for x - 1
red_data.loc[red_data['RTurt'] == r, 'X'] = red_data['X'] - 1
elif rand == 3: # for y + 1
red_data.loc[red_data['RTurt'] == r, 'Y'] = red_data['Y'] + 1
elif rand == 4: # for y - 1
red_data.loc[red_data['RTurt'] == r, 'Y'] = red_data['Y'] - 1
dfromor = math.sqrt((math.pow(red_data['X'][l], 2)) + (math.pow(red_data['Y'][l], 2))) # This equation finds the distance
# between the origin and the current coordinate of the turtle,
# to check if the turtle has touched the circle of radius 300 units
l = l + 1 # Dummy index + 1
if dfromor == 300: # Checking if the turtle has touched the boundary
red_data.loc[red_data['RTurt'] == r, 'X'] = 0 # If it has touched the boundary we change it's position to x = 0 and
red_data.loc[red_data['RTurt'] == r, 'Y'] = 0 # y = 0 and
red_data.loc[red_data['RTurt'] == r, 'Color'] = 'black' # Change it's color to black
# Similar function as above for blue turtles.
def blue_step():
''' This function is called to randomly generate the red turtles step direction
and update the dataframe with new X and Y coordinates
'''
n = 0
for o in blue_data['BTurt']:
rand = random.choice([1,2,3,4])
if rand == 1: # for x + 1
blue_data.loc[blue_data['BTurt'] == o, 'X'] = blue_data['X'] + 1
elif rand == 2: # for x - 1
blue_data.loc[blue_data['BTurt'] == o, 'X'] = blue_data['X'] - 1
elif rand == 3: # for y + 1
blue_data.loc[blue_data['BTurt'] == o, 'Y'] = blue_data['Y'] + 1
elif rand == 4: # for y - 1
blue_data.loc[blue_data['BTurt'] == o, 'Y'] = blue_data['Y'] - 1
dfromob = math.sqrt((math.pow(blue_data['X'][n], 2)) + (math.pow(blue_data['Y'][n], 2)))
n = n + 1
if dfromob == 300:
blue_data.loc[blue_data['BTurt'] == o, 'X'] = 0
blue_data.loc[blue_data['BTurt'] == o, 'Y'] = 0
blue_data.loc[blue_data['BTurt'] == o, 'Color'] = 'black'
def main():
for u in range(1001):
red_step()
blue_step()
print(red_data)
print(blue_data)
main()
Output:
Before Run:(Initial Coordinates)
After Run:(Final Coordinates)