DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Python (http://www.daniweb.com/forums/forum114.html)
-   -   Python Graphics Help (http://www.daniweb.com/forums/thread33456.html)

qwester Oct 6th, 2005 7:24 am
Python Graphics Help
 
1 Attachment(s)
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.

vegaseat Oct 6th, 2005 5:38 pm
Re: Python Graphics Help
 
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.

vegaseat Oct 6th, 2005 7:01 pm
Re: Python Graphics Help
 
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.

shanenin Oct 7th, 2005 12:52 am
Re: Python Graphics Help
 
Quote:

Originally Posted by vegaseat
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.

bumsfeld Oct 7th, 2005 12:41 pm
Re: Python Graphics Help
 
I have use the space/tab feature too, but don't always know how many spaces each tab is.


All times are GMT -4. The time now is 6:45 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC