[Tutor] moving objects in only one direction at a time
Tudor, Bogdan - tudby001
tudby001 at mymail.unisa.edu.au
Thu Jun 11 11:22:20 CEST 2015
Hi,
Sorry if this sends twice.
I am using python 3.4.3 on windows 7 64bit.
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. I'm trying to get the block to continue moving in only one direction determined by the arrow keys, i.e. pressing right should not move the blocks right if they already moving left.
How can I fix my code to do this?
I expected adding True/False variables (moveLEFT, moveRIGHT) would allow me to disable one key press while another is in action, unfortunately has not worked.
I hope its OK that I attached files as they are long.
--> blockgame.py
import graphics
import game_functions
import time
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
elif event.keysym == 'q':
win.close()
def game():
global direction
global square
global moveLEFT
global moveRIGHT
block = game_functions.GameBlock(WIN_WIDTH//2,
WIN_HEIGHT//2, BLOCK_SIZE,
BLOCK_SIZE, 'Red', None, None, 0)
block.draw(win)
square.append(block)
block = game_functions.GameBlock(WIN_WIDTH//2,
WIN_HEIGHT//2, BLOCK_SIZE,
BLOCK_SIZE, 'Red', None, None, 0)
block.draw(win)
square.append(block)
speed = 0.1
playing = True
while playing:
# Move squares
for k in range(len(square)-1, 0, -1):
moveX = square[k-1].getCentreX() - square[k].getCentreX()
moveY = square[k-1].getCentreY() - square[k].getCentreY()
square[k].move(moveX, moveY)
# Update pos
if moveLEFT == True and moveRIGHT == False:
square[0].move(-20, 0)
if moveRIGHT and moveLEFT == False:
square[0].move(20, 0)
time.sleep(speed)
moveLEFT = False
moveRIGHT = False
WIN_WIDTH = 500
WIN_HEIGHT = 500
BLOCK_SIZE = 20
direction = ""
square = []
win = graphics.GraphWin("Snake", WIN_WIDTH, WIN_HEIGHT)
win.setBackground("white")
win.bind_all('<Key>', handleKeys)
game()
win.mainloop()
win.getMouse()
win.close()
--> game_functions.py
import graphics
class GameBlock():
def __init__(self, dx, dy, width, height, colour, imageName, blockType, points):
self.centreX = dx
self.centreY = dy
self.width = width
self.height = height
self.colour = colour
self.imageName = imageName
self.blockType = blockType
self.points = points
self.scrollSpeed = 10
self.block = graphics.Rectangle(graphics.Point(self.centreX-self.width//2, self.centreY-self.height//2),
graphics.Point(self.centreX+self.width//2, self.centreY+self.height//2))
self.block.setFill(colour)
if not(imageName == None):
self.image = graphics.Image(graphics.Point(dx,dy), imageName)
else:
self.image = None
def getCentreX(self):
return self.centreX
def getCentreY(self):
return self.centreY
def move(self, dx, dy):
self.centreX = self.centreX + dx
self.centreY = self.centreY + dy
self.block.move(dx,dy)
if not(self.image == None):
self.image.move(dx,dy)
def draw(self, canvas):
if self.image == None:
self.block.draw(canvas)
else:
self.image.draw(canvas)
__________________________
I will greatly appreciate any help!
Kind Regards
More information about the Tutor
mailing list