Instances and loops

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2009
Posts: 12
Reputation: A_Dubbs is an unknown quantity at this point 
Solved Threads: 0
A_Dubbs A_Dubbs is offline Offline
Newbie Poster

Instances and loops

 
0
  #1
Oct 20th, 2009
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:

  1. from zellegraphics import *
  2.  
  3. ##Write a program to animate a circle bouncing around a window. The basic idea
  4. ##is to start the circle somewhere in the interior of the window. Use variables
  5. ##dx and dy (both initalized to 10) to control the movement of the circle. Use a
  6. ##large counted loop (say 10000 iterations), and each time through the loop move
  7. ##the circle using dx and dy. When the x-value of the center of the circle gets
  8. ##too high (it hits the edge), change dx to -10. When it gets too low, change dx
  9. ##back to 10. Use a similar approach for dy.
  10.  
  11. def main():
  12. #Set up a window 400 by 400
  13. win = GraphWin("Circle Moving", 400, 400)
  14. #Set up circle that will travel around the window
  15. c = Circle(Point (200,200), 30)
  16. c.draw(win)
  17. x = 1
  18. l = Text(Point (200,200), x)
  19. l.draw(win)
  20. for i in range(100):
  21. win.getMouse()
  22. dx = 10
  23. dy = -10
  24. c.move(dx,dy)
  25. center = c.getCenter()
  26. l.move(dx,dy)
  27.  
  28.  
  29.  
  30.  
  31. 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?
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 12
Reputation: A_Dubbs is an unknown quantity at this point 
Solved Threads: 0
A_Dubbs A_Dubbs is offline Offline
Newbie Poster
 
0
  #2
Oct 20th, 2009
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:

  1. from zellegraphics import *
  2.  
  3. ##Write a program to animate a circle bouncing around a window. The basic idea
  4. ##is to start the circle somewhere in the interior of the window. Use variables
  5. ##dx and dy (both initalized to 10) to control the movement of the circle. Use a
  6. ##large counted loop (say 10000 iterations), and each time through the loop move
  7. ##the circle using dx and dy. When the x-value of the center of the circle gets
  8. ##too high (it hits the edge), change dx to -1. When it gets too low, change dx
  9. ##back to 1. Use a similar approach for dy.
  10.  
  11. #Set up a window 400 by 400
  12. win = GraphWin("Circle Moving", 400, 400)
  13. #Set up circle that will travel around the window
  14. c = Circle(Point (200,200), 30)
  15. c.draw(win)
  16. x = 1
  17. l = Text(Point (200,200), x)
  18. l.draw(win)
  19.  
  20.  
  21. def move(x,y):
  22. win.getMouse()
  23. dx = x
  24. dy = y
  25. c.move(dx,dy)
  26. l.move(dx,dy)
  27. center = c.getCenter()
  28. center = center.getX()
  29. print center
  30. if center == 400.0:
  31. dx = -10
  32. return dx
  33.  
  34.  
  35. def main():
  36. for i in range(100):
  37. move(10,-10)
  38.  
  39.  
  40.  
  41. 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!
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 963
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 221
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Posting Shark
 
0
  #3
Oct 20th, 2009
Your move function should return the next dx and dy. You could write this
  1. from zellegraphics import *
  2.  
  3. ##Write a program to animate a circle bouncing around a window. The basic idea
  4. ##is to start the circle somewhere in the interior of the window. Use variables
  5. ##dx and dy (both initalized to 10) to control the movement of the circle. Use a
  6. ##large counted loop (say 10000 iterations), and each time through the loop move
  7. ##the circle using dx and dy. When the x-value of the center of the circle gets
  8. ##too high (it hits the edge), change dx to -1. When it gets too low, change dx
  9. ##back to 1. Use a similar approach for dy.
  10.  
  11. #Set up a window 400 by 400
  12. win = GraphWin("Circle Moving", 400, 400)
  13. #Set up circle that will travel around the window
  14. c = Circle(Point (200,200), 30)
  15. c.draw(win)
  16. x = 1
  17. l = Text(Point (200,200), x)
  18. l.draw(win)
  19.  
  20.  
  21. def move(x,y):
  22. win.getMouse()
  23. dx = x
  24. dy = y
  25. c.move(dx,dy)
  26. l.move(dx,dy)
  27. center = c.getCenter()
  28. center = center.getX()
  29. print center
  30. if center == 400.0:
  31. dx = -10
  32. return dx, dy
  33.  
  34.  
  35. def main():
  36. dx, dy = 10, -10
  37. for i in range(100):
  38. dx, dy = move(dx, dy)
  39.  
  40. main()
To change the text, we need to have some documentation about Text. Can you run the following code ?
  1. from zellegraphics import *
  2. from pydoc import TextDoc
  3. textdoc = TextDoc()
  4. out = open("mydoc.txt", "w")
  5. out.write(textdoc.document(Text))
If it works, please put the content of the file mydoc.txt in a post between code tags.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 12
Reputation: A_Dubbs is an unknown quantity at this point 
Solved Threads: 0
A_Dubbs A_Dubbs is offline Offline
Newbie Poster
 
0
  #4
Oct 20th, 2009
Thanks, the first part ended up working great.

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

  1. class TTeexxtt(GraphicsObject)
  2. | Methods defined here:
  3. |
  4. | ____iinniitt____(self, p, text)
  5. |
  6. | ____rreepprr____(self)
  7. |
  8. | cclloonnee(self)
  9. |
  10. | ggeettAAnncchhoorr(self)
  11. |
  12. | ggeettTTeexxtt(self)
  13. |
  14. | sseettFFaaccee(self, face)
  15. |
  16. | sseettSSiizzee(self, size)
  17. |
  18. | sseettSSttyyllee(self, style)
  19. |
  20. | sseettTTeexxtt(self, text)
  21. |
  22. | sseettTTeexxttCCoolloorr(self, color)
  23. |
  24. | ----------------------------------------------------------------------
  25. | Methods inherited from GraphicsObject:
  26. |
  27. | ddrraaww(self, graphwin)
  28. | Draw the object in graphwin, which should be a GraphWin
  29. | object. A GraphicsObject may only be drawn into one
  30. | window. Raises an error if attempt made to draw an object that
  31. | is already visible.
  32. |
  33. | mmoovvee(self, dx, dy)
  34. | move object dx units in x direction and dy units in y
  35. | direction
  36. |
  37. | sseettFFiillll(self, color)
  38. | Set interior color to color
  39. |
  40. | sseettOOuuttlliinnee(self, color)
  41. | Set outline color to color
  42. |
  43. | sseettWWiiddtthh(self, width)
  44. | Set line weight to width
  45. |
  46. | uunnddrraaww(self)
  47. | Undraw the object (i.e. hide it). Returns silently if the
  48. | object is not currently drawn.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 963
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 221
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Posting Shark
 
1
  #5
Oct 20th, 2009
Ok, you can change the text with
  1. 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
  1. from zellegraphics import *
  2. from pydoc import HTMLDoc
  3. textdoc = HTMLDoc()
  4. out = open("mydoc.html", "w")
  5. 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.
Last edited by Gribouillis; Oct 20th, 2009 at 6:27 am.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC