| | |
Instances and loops
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Sep 2009
Posts: 12
Reputation:
Solved Threads: 0
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:
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?
Anyways, I have written code that moves the circle when the user clicks:
Python Syntax (Toggle Plain Text)
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?
•
•
Join Date: Sep 2009
Posts: 12
Reputation:
Solved Threads: 0
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:
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!
Python Syntax (Toggle Plain Text)
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!
0
#3 Oct 20th, 2009
Your move function should return the next dx and dy. You could write this
To change the text, we need to have some documentation about Text. Can you run the following code ?
If it works, please put the content of the file mydoc.txt in a post between code tags.
python Syntax (Toggle Plain Text)
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()
python Syntax (Toggle Plain Text)
from zellegraphics import * from pydoc import TextDoc textdoc = TextDoc() out = open("mydoc.txt", "w") out.write(textdoc.document(Text))
•
•
Join Date: Sep 2009
Posts: 12
Reputation:
Solved Threads: 0
0
#4 Oct 20th, 2009
Thanks, the first part ended up working great.
Here is the contents of that file, hard to read:
Here is the contents of that file, hard to read:
Python Syntax (Toggle Plain Text)
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.
1
#5 Oct 20th, 2009
Ok, you can change the text with
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
to get a file mydoc.html to view in your browser.
About the floating point test, you should use
python Syntax (Toggle Plain Text)
l.setText(str(300)) # the text now shows 300
For the doc, you could use
python Syntax (Toggle Plain Text)
from zellegraphics import * from pydoc import HTMLDoc textdoc = HTMLDoc() out = open("mydoc.html", "w") out.write(textdoc.document(Text))
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.
![]() |
Similar Threads
- Program Help Using For Nested Loops (C++)
- Pointers (archived tutorial) (C++)
- Does this really generate multiple instances of a UNIX process (Perl)
- Loops (C++)
- Need some help with my do-while and while loops (Java)
- Help with loops (Java)
- merged:nesting loops (C++)
- help for program involving switch loops and file (C++)
- CSRSS Backspace Bug in Windows NT 4/NT 2000/NT XP (Windows NT / 2000 / XP)
Other Threads in the Python Forum
- Previous Thread: pygtk + py2exe + inno -> icon for shortcut
- Next Thread: python key call on list.
| Thread Tools | Search this Thread |
Tag cloud for Python
abrupt ansi anti apache approximation array backend beginner binary book builtin calculator chmod code converter countpasswordentry curved dan08 dictionaries dictionary drive dynamic examples excel file filename float format function gui heads homework import inches input java launcher library line lines linux list lists loop mouse mysql mysqlquery number numbers numeric output parsing path phonebook plugin pointer port prime programming progressbar projects py2exe pygame pyqt pysimplewizard python random recursion redirect refresh scrolledtext software statictext statistics string strings sum table terminal text textarea thread threading time tkinter tlapse trick tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable windows wordgame write wxpython





