[Tutor] Motion
Magnus Lycka
magnus@thinkware.se
Thu Dec 19 18:15:23 2002
At 17:40 2002-12-19 -03-30, Adam Vardy wrote:
>Suppose I'd like to start off with something simple like moving a
>letter around the screen, pretend he's your man, and you move him
>around by some control, mouse or something. How do you start with
>that? How do you place the letter anywhere on screen?
What makes you think this is simple? :)
Let's just say it varies...
Performing calculations and logical tricks is one thing, but when
we move objects on the screen, everything depends on how the
operating system interfaces with the screen. There is unfortunately
no standard for that. (If there ever could be--user interfaces can
be very different.)
This means that you must decide what kind of environment you use.
The first thing to consider is probably whether you want a graphical
user interface of a text based user interface.
Surprisingly, the task you mention might be easier to do with a
complex graphical user interface, than with a plain simple text
based interface. At least in Windows, and certainly if you want it
to be cross platform.
The most common Python GUI toolkits, such as Tkinter and wxPython
works the same (well, close at least) in Windows, Linux and some
more operating systems.
The most common toolkit for screen manipulation in text mode, curses,
doesn't work in Windows as far as I know. At least not without a lot
of fiddling.
Here's something little to try...
>>> from Tkinter import *
>>> import time
>>> root = Tk()
>>> c = Canvas(root)
>>> c.pack()
>>> o = c.create_oval((10,10,20,20))
>>> for i in range(10):
... c.move(o,i,i)
... root.update()
... time.sleep(1)
(If you don't see anything happening, chances are that the GUI
window is hidden behind your Python interpreter as you put it
back in focus as you type. Check your taskbar if you are in
windows, and click on the Tk thingie...)
Unfortunately, the Tkinter docs in the standard library reference
is...well...it's better than it used to be...but it's still very
thin. But guess what! I think section 16.4 is just the thing for
you to start with. The turtle module!
Start by running the turtle.py file that you find in the lib-tk
directory. Then have a look at the contents of turtle.py. The
demo() function shows how to use this module.
Then you fire up python, import turtle and play... There you go.
The next step is to handle input from mouse or keyboard... Tkinter
can do that too...
--
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/ mailto:magnus@thinkware.se