Python Forum
Trouble with a turtle movement assignment.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trouble with a turtle movement assignment.
#1
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)
Yoriz write Jan-27-2022, 10:18 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
I figured it out myself. Sorry for any problems with the post.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  ROS ROBOTIC MOVEMENT PYTHON frkndmrsln 0 1,567 Apr-04-2022, 09:33 AM
Last Post: frkndmrsln
  Animating 2D object movement with matplotlib esamalihalil1993 0 1,787 Nov-23-2020, 05:49 PM
Last Post: esamalihalil1993
  Trouble with Loops in lab assignment jonstryder 6 3,389 Jul-28-2019, 06:40 PM
Last Post: ThomasL
  Trouble with "Weather Program" Assignment sarah_mb_sues 5 9,043 Aug-10-2018, 02:29 AM
Last Post: ichabod801
  How to hold a program in the turtle (Tic-Tac-Toe game assignment) kmchap 1 4,576 May-17-2018, 05:39 PM
Last Post: j.crater
  I'm having trouble with my assignment for creating a math quiz thewrongnarwhal 7 8,045 Nov-14-2016, 08:06 AM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020