[Tutor] moving objects in only one direction at a time
Alan Gauld
alan.gauld at btinternet.com
Thu Jun 11 20:01:37 CEST 2015
On 11/06/15 08:19, Tudor, Bogdan - tudby001 wrote:
> Sorry if this sends twice, I had to change subject line.
> I am using python 3.4.3 on windows 7 64bit.
OK, But always start a new topic with a new message. By replying to an
old post your message is now buried at the bottom of a very old thread
about formatting strings. This makes it unlikely that people will find
it easily.
> I'm using John M. Zelle graphics module.
>
> Graphics module:
> http://mcsp.wartburg.edu/zelle/python/graphics/graphics/graphics.html
>
> I am trying to create user movable blocks that can only move in one
> direction at a time. The arrow keys are used to move left or right.
> Currently while moving left, pressing the right arrow key will change
> the blocks direction and vice versa.
Do you really mean that once the block starts moving left it can *never*
be reversed? That's a very unusual request.
Normally there would either be the ability to reverse or
stop the movement.
> How can I fix my code to do this?
> def handleKeys(event):
> global direction
> global moveLEFT
> global moveRIGHT
> print('Key pressed', event.keysym, event.keycode)
>
> if event.keysym == 'Left':
> direction = 'Left'
> moveLEFT = True
> moveRIGHT = False
> elif event.keysym == 'Right':
> direction = 'Right'
> moveLEFT = False
> moveRIGHT = True
Notice that you never check whether the block is moving,
you always just set the flags, without question.
To do what you want, you need to add an if test to
first check if the flag is already set. If it's set
the wrong way you might want to think about reporting
an error message to the user?
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list