Hello everyone, I need some help with instances and loops (I am a very new programmer). Basically, I have a problem where I need to make a program that has a circle move around a window every time the user clicks. This circle is suppose to have a text box inside it that states the number of times the circle has moved. When the circle gets to the end of the window, I am suppose to change the movement coordinates so it comes back. I am using graphics that an author from the book I am using wrote.

Anyways, I have written code that moves the circle when the user clicks:

from zellegraphics import *

##Write a program to animate a circle bouncing around a window. The basic idea
##is to start the circle somewhere in the interior of the window. Use variables
##dx and dy (both initalized to 10) to control the movement of the circle. Use a
##large counted loop (say 10000 iterations), and each time through the loop move
##the circle using dx and dy. When the x-value of the center of the circle gets
##too high (it hits the edge), change dx to -10. When it gets too low, change dx
##back to 10. Use a similar approach for dy.

def main():
    #Set up a window 400 by 400
    win = GraphWin("Circle Moving", 400, 400)
    #Set up circle that will travel around the window
    c = Circle(Point (200,200), 30)
    c.draw(win)
    x = 1
    l = Text(Point (200,200), x)
    l.draw(win)
    for i in range(100):
        win.getMouse()
        dx = 10
        dy = -10
        c.move(dx,dy)
        center = c.getCenter()
        l.move(dx,dy)
        
        

    
main()

I set the window to be 400 by 400. Variable c is a circle with the center point of (200,200) and a radius of 30. I made a text box which is drawn at the same place (200,200) and has a message of variable x. Variable x = 1. In the for loop I have made it so the circle will move (10,-10) every time the user clicks. Now, when I use the c.getCenter() I get back the center in the format of "Point(x,y)". The type of this is an instance.

How can I isolate point x so I can make an if statement that changes dx to -10 if x gets too high. I do not know how to work with instances. Also, I need variable x to change everytime I click. However, putting x = x+1 in the for loop won't change that. I assume this is because x in variable is outside the for loop. Can anyone help me?

Ok, I can't figure out how to edit my post, so I'll write my new, less confusing question here. Basically, I need to know how I can automatically change parameters in a function. Here is my code:

from zellegraphics import *

##Write a program to animate a circle bouncing around a window. The basic idea
##is to start the circle somewhere in the interior of the window. Use variables
##dx and dy (both initalized to 10) to control the movement of the circle. Use a
##large counted loop (say 10000 iterations), and each time through the loop move
##the circle using dx and dy. When the x-value of the center of the circle gets
##too high (it hits the edge), change dx to -1. When it gets too low, change dx
##back to 1. Use a similar approach for dy.

#Set up a window 400 by 400
win = GraphWin("Circle Moving", 400, 400)
#Set up circle that will travel around the window
c = Circle(Point (200,200), 30)
c.draw(win)
x = 1
l = Text(Point (200,200), x)
l.draw(win)

        
def move(x,y):
    win.getMouse()
    dx = x
    dy = y
    c.move(dx,dy)
    l.move(dx,dy)
    center = c.getCenter()
    center = center.getX()
    print center
    if center == 400.0:
        dx = -10
        return dx


def main():
    for i in range(100):
        move(10,-10)
        
        

main()

What I would like to do is have dx change to -10 when center = 400.0 . So function move(10,-10) will go until center = 400, then it would change to function move (-10,-10). Can anyone help with this? Thanks!

Your move function should return the next dx and dy. You could write this

from zellegraphics import *

##Write a program to animate a circle bouncing around a window. The basic idea
##is to start the circle somewhere in the interior of the window. Use variables
##dx and dy (both initalized to 10) to control the movement of the circle. Use a
##large counted loop (say 10000 iterations), and each time through the loop move
##the circle using dx and dy. When the x-value of the center of the circle gets
##too high (it hits the edge), change dx to -1. When it gets too low, change dx
##back to 1. Use a similar approach for dy.

#Set up a window 400 by 400
win = GraphWin("Circle Moving", 400, 400)
#Set up circle that will travel around the window
c = Circle(Point (200,200), 30)
c.draw(win)
x = 1
l = Text(Point (200,200), x)
l.draw(win)

        
def move(x,y):
    win.getMouse()
    dx = x
    dy = y
    c.move(dx,dy)
    l.move(dx,dy)
    center = c.getCenter()
    center = center.getX()
    print center
    if center == 400.0:
        dx = -10
    return dx, dy


def main():
    dx, dy = 10, -10
    for i in range(100):
        dx, dy = move(dx, dy)
        
main()

To change the text, we need to have some documentation about Text. Can you run the following code ?

from zellegraphics import *
from pydoc import TextDoc
textdoc = TextDoc()
out = open("mydoc.txt", "w")
out.write(textdoc.document(Text))

If it works, please put the content of the file mydoc.txt in a post between code tags.

Thanks, the first part ended up working great.

Here is the contents of that file, hard to read:

class TTeexxtt(GraphicsObject)
 |  Methods defined here:
 |  
 |  ____iinniitt____(self, p, text)
 |  
 |  ____rreepprr____(self)
 |  
 |  cclloonnee(self)
 |  
 |  ggeettAAnncchhoorr(self)
 |  
 |  ggeettTTeexxtt(self)
 |  
 |  sseettFFaaccee(self, face)
 |  
 |  sseettSSiizzee(self, size)
 |  
 |  sseettSSttyyllee(self, style)
 |  
 |  sseettTTeexxtt(self, text)
 |  
 |  sseettTTeexxttCCoolloorr(self, color)
 |  
 |  ----------------------------------------------------------------------
 |  Methods inherited from GraphicsObject:
 |  
 |  ddrraaww(self, graphwin)
 |      Draw the object in graphwin, which should be a GraphWin
 |      object.  A GraphicsObject may only be drawn into one
 |      window. Raises an error if attempt made to draw an object that
 |      is already visible.
 |  
 |  mmoovvee(self, dx, dy)
 |      move object dx units in x direction and dy units in y
 |      direction
 |  
 |  sseettFFiillll(self, color)
 |      Set interior color to color
 |  
 |  sseettOOuuttlliinnee(self, color)
 |      Set outline color to color
 |  
 |  sseettWWiiddtthh(self, width)
 |      Set line weight to width
 |  
 |  uunnddrraaww(self)
 |      Undraw the object (i.e. hide it). Returns silently if the
 |      object is not currently drawn.

Ok, you can change the text with

l.setText(str(300)) # the text now shows 300

note that the letter 'l' is a bad variable name. Good variable names have at least 3 characters (except for loop variables like i, j, ...)
For the doc, you could use

from zellegraphics import *
from pydoc import HTMLDoc
textdoc = HTMLDoc()
out = open("mydoc.html", "w")
out.write(textdoc.document(Text))

to get a file mydoc.html to view in your browser.

About the floating point test, you should use if center >= 400.0 rather that == because floating point numbers seldom happen to be equal. If the value is 400.00000001, dx should be set to -10 for example.

commented: Good help, very knowledgeable +1
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.