Question

In: Computer Science

Your (turtle) program in python must include: Create four Red turtles and four Blue turtles using...

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.

Solutions

Expert Solution

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)


Related Solutions

The coding for this program to run as described on Python: Your (turtle) program must include:...
The coding for this program to run as described on Python: Your (turtle) program must include: include import turtle on a line after the comments so you can use the various Turtle-related objects and methods. Create a turtle named after your favorite ice cream flavor. Make sure the name has no punctuation and consists only of letters, numbers and the underscore character. Write your python program so your turtle is constantly moving and can be turned left and right using...
The code that creates this program using Python: Your program must include: You will generate a...
The code that creates this program using Python: Your program must include: You will generate a random number between 1 and 100 You will repeatedly ask the user to guess a number between 1 and 100 until they guess the random number. When their guess is too high – let them know When their guess is too low – let them know If they use more than 5 guesses, tell them they lose, you only get 5 guesses. And stop...
What would the Python code be to create the following working program using turtle: -Make 4...
What would the Python code be to create the following working program using turtle: -Make 4 turtles that are YELLOW, and 4 turtles that are GREEN. Create these turtles using one or more lists. -Make the yellow turtles start at a different randomized location somewhere on the left side of the screen within the range of (-100,100) and (0, -100) -Make the green turtles start at a different randomized location somewhere on the right side of the screen within the...
Write a function that draw your initials. (Using python 3 turtle) (T, S)
Write a function that draw your initials. (Using python 3 turtle) (T, S)
Using Python, create a program that will act as a number convertor. This program will convert...
Using Python, create a program that will act as a number convertor. This program will convert an input decimal integer into binary and hexadecimal formats. a) Define a main function named numberconvertor(). Inside the main function, write all the logic of your code. [5% marks] b) Looping indefinitely (Hint: use infinite while loop), the program should give the user the 3 options given below and allow the user to select one among them. [15% marks] 1. converting a decimal number...
Program must use Python 3 Your program must have a welcome message for the user. Your...
Program must use Python 3 Your program must have a welcome message for the user. Your program must have one class called CashRegister. Your program will have an instance method called addItem which takes one parameter for price. The method should also keep track of the number of items in your cart. Your program should have two getter methods. getTotal – returns totalPrice getCount – returns the itemCount of the cart Your program must create an instance of the CashRegister...
Python please! Create one program to include all 10 elements: Create a function that prints a...
Python please! Create one program to include all 10 elements: Create a function that prints a sentence passed to the function as parameter. On the next line, print the same sentence without the 1st and the last character, use string slicing. Use the following sentence: This is the sentence. Given a string: This string was copied in an article. Replace the relevant words to read as follows: This string was discovered in the article xyz. Given a string: This string...
Create a Python program file and . Your program will draw random circles with filled and...
Create a Python program file and . Your program will draw random circles with filled and non-filled color (random colors) and rectangles, fill andnon-fill color (also random colors) on the canvas. At least 10 for each category
A couple must decide between a blue house and a red house. To purchase the red...
A couple must decide between a blue house and a red house. To purchase the red house they must tae a $155000 mortgage to be repaid over 20 years, interest rate is given as j2 = 7.8%. To purchase the blue house, they must take a mortgage to be repaid over 25 years at interest rate given as j12 - 8%. Both of those two mortgages have semi-annual payments and the semi annual mortgage payments are equal. (a) Determine the...
USING PYTHON Write a program to create a number list. It will call a function to...
USING PYTHON Write a program to create a number list. It will call a function to calculate the average values in the list. Define main ():                        Declare variables and initialize them                        Create a list containing numbers (int/float)                        Call get_avg function that will return the calculated average values in the list.                                       Use a for loop to loop through the values in the list and calculate avg                        End main()
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT