Hello, I'm having trouble with a project that I am doing in turtle graphics.
I'm making a graphic of a soccer field with a ball bouncing around, but I want the ball to stop when it hits the net. I can get the ball to stop when it hits the net though, it keeps on bouncing. Any suggestions would be appreciated. The if statement that I am trying to use is

if -100>x>100 and -25>y>25:\
return

and I have an attachment of the work I have done.

Recommended Answers

All 4 Replies

I am experimenting with your attachment. Please do not mix tabs and spaces for indentations, stick with spaces! A few comments would be nice! It just makes it nicer for other people to use and check your code.

What do you want to use to start the program? Just for initial testing I am using soccer(1000, 1, 30)

You could score goals as your x,y coordinates of the turtle fall within the range of the goal-fronts.

The turtles are active! This is sure an interesting game. I changed the soccer function to this ...

def soccer(num_steps, step_size, heading):
    turtle.reset()
    draw_table()
    top = 100/2
    bottom = top * -1
    right = 200/2
    left = right * -1
    turtle.left(heading)
    leftGoal = 0
    rightGoal = 0
    for step in range(num_steps):
        turtle.forward(step_size)
        x, y = turtle.position()
        # make the ball/turtle bounce
        if left >= x or x >= right:
            turtle.left(180-2*turtle.heading())
        else:
            if y <= bottom or top <= y:
                turtle.right(2*turtle.heading())
        if x <= -100 and -25 <= y <= 25:
            #print turtle.position()    # test
            leftGoal += 1
            print "leftGoal =", leftGoal
        if x >= 100 and -25 <= y <= 25:
            #print turtle.position()    # test
            rightGoal += 1
            print "rightGoal =", rightGoal
    print "Final score is right %d and left %d" % (rightGoal, leftGoal)

This way you can keep track of right and left goal scores. To make the game more interesting you may want to add a small random bounce to your deflections.

Please do not mix tabs and spaces for indentations, stick with spaces!

I usually use the option "fill tabs with spaces" . Most editers offer this.

I have use the space/tab feature too, but don't always know how many spaces each tab is.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.