Python Forum

Full Version: Trouble with a turtle movement assignment.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
In this assignment we are supposed to put ten balls in a list and have the bounce around in a set area defined by a border. We then make another turtle which is controled by the player. When the player's turtle contacts one of the balls the ball is supposed to be hidden, moved off screen, and removed from the balls list. I think my code should be working but it isn't. If y'all could look at the code and tell me what might be wrong it would be much appreciated.

This is the code:
import turtle
import random



def move(t):
	t.forward(3)
	if t.xcor()>200 or t.xcor()<-200:
		t.setheading(180-t.heading())
	if t.ycor()>200 or t.ycor()<-200:
		t.setheading(-t.heading())


def right(t):
	t.heading(0)
	t.forward(10)
def left(t):
	t.heading(180)
	t.forward(10)
def up(t):
	t.heading(90)
	t.forward(10)
def down(t):
	t.heading(-90)
	t.forward(10)



def collisions(t1, t2):
	if t1.distance(t2)<20:
		delete(t2)

def delete(t):
	t.hideturtle()
	t.goto(1000,1000)
	balls.remove(t)

def border():
	border = turtle.Turtle()
	border.shape("square")
	border.hideturtle()
	border.width(10)
	border.speed(0)
	border.penup()
	border.forward(200)
	border.pendown()
	border.setheading(90)
	border.forward(200)
	border.setheading(180)
	border.forward(400)
	border.setheading(-90)
	border.forward(400)
	border.setheading(0)
	border.forward(400)
	border.setheading(90)
	border.forward(200)	


screen = turtle.Screen()
screen.tracer(10)
screen.listen()
screen.onkey(lambda: right(player), "Right")
screen.onkey(lambda: left(player), "Left")
screen.onkey(lambda: up(player), "Up")
screen.onkey(lambda: down(player), "Down")

player = turtle.Turtle()
player.speed(0)

balls = []

for b in range(10):
	balls.append(turtle.Turtle())

for b in balls:
	b.speed(0)
	b.shape("circle")
	b.setheading(random.randint(0,360))



border()
while True:
	for b in balls:
		move(b)
		collisions(player,b)
I figured it out myself. Sorry for any problems with the post.