kwhile
kwhile

Reputation: 23

Multiple Turtles, multiple random movements

I have an assignment where I have to make a game in python with turtles. I am able to generate a grid like I want to, and I am able to generate multiple different turtles. my - I have no clue where to start in terms of figuring out how to have each of my three different turtles each move throughout the board randomly, while staying on the grid line. - the "game" must stop after two of the turtles have hit the edge of the screen, I do not know how to do this, either.

here is what I have so far:

import turtle
import random

turtle.pensize(2)
turtle.penup()
turtle.goto(-160,-160)
turtle.pendown()
turtle.color("green")
turtle.speed(0)
for i in range(4):
    turtle.forward(320)
    turtle.left(90)

for y in range(-160,160,80):
    for x in range(-160,160,80):

        turtle.penup()
        turtle.goto(x,y)
        turtle.pendown()

        for k in range(4):
            turtle.forward(40)
            turtle.left(90)

        turtle.end_fill()       

for y in range(-120,160,80):
    for x in range(-120,160,80):

        turtle.penup()
        turtle.goto(x,y)
        turtle.pendown()
        for k in range(4):
            turtle.forward(40)
            turtle.left(90)


billy = turtle.Turtle()
billy.shape("turtle")
billy.color("red")
billy.penup()
billy.left(90)


rick = turtle.Turtle()
rick.shape("turtle")
rick.color("orange")
rick.penup()



mike = turtle.Turtle()
mike.shape("turtle")
mike.color("purple")
mike.penup()




turtle.done()

strong guidance would be VERY helpful.

Upvotes: 2

Views: 1290

Answers (1)

Red Cricket
Red Cricket

Reputation: 10470

You could take advantage of object orientation. Here is my version of your turtle script:

import turtle
import random
# I added randint. 
from random import randint

# Let's define a class so we can create turtles
class MyTurtle():

  # Here's the constructor. Where we give some initial values.
  def __init__(self, name, color):
    self.name = name
    self.color = color
    self.turtle = turtle.Turtle()
    self.turtle.shape("turtle")
    self.turtle.color(self.color)
    self.turtle.speed(1)
    self.turtle.pd()

  # We need to know if this turtle is off the board.
  def off_board(self):
    x = self.turtle.xcor()
    y = self.turtle.ycor()
    return x < -160 or 160 < x or y < -160 or 160 < y

  # calling this will move the turtle in a random direction.
  def move(self):
    turn = randint(0,2)
    if turn == 1:
      self.turtle.lt(45)
    elif turn == 2:
      self.turtle.rt(45)
    self.turtle.forward(5)

# Put your code for drawing the grid 

# Let's create some turtles and start moving them.
turtles = []
turtles.append( MyTurtle('billy', 'red'))
turtles.append( MyTurtle('chris', 'blue'))
turtles.append( MyTurtle('lilly', 'pink'))
turtles.append( MyTurtle('kevin', 'green'))

ok = True
while ok:
  for t in turtles:
    t.move()
    if t.off_board():
      print ("Turtle %s wins!!!" % (t.name))
      ok = False

turtle.done()

Come on! Just one upvote for this awesome turtle script? enter image description here

Upvotes: 1

Related Questions