Jarrod
Veteran Member
woot simpson
Posts: 1304 Reputation: 20
– / /
Joined: Sep 2006
|
RE: Python help, Solution :)
python code: import pyglet as pyg
import random
from pyglet.window import key
window=pyg.window.Window()
#map- when arrow keys are pressed in pyglet, the symbol that represents them is a number
#he numbers are:
#65361 = left = 0
#65362 = up = 1
#65363 = right = 2
#65364 = down = 3
#we now have 4 numbers 1-4, each representing an arrow key
#
keys=[False]*4
#so now we have a list of 4 False values, which will represent each arrow,
#python indexes an array/list starting from 0
#above we mention 4 numbers, 65361,65362,65363,65364
#instead of mapping each to a dictionary if we get the last digit and subtract 1 we end up with a set like
#0,1,2,3
#so now if we let this number index keys like this
#keys[((int(str(symbol)[-1]))-1)]
#it is the same as saying:
#
#def on_key_press(symbol,modifiers,keys=keys):
# if symbol == 65361:
# keys[0]=True #keys[0] is left
# if symbol == 65362:
# keys[1] == True
# #ect,ect
#keys are mapped ((int(str(symbol)[-1]))-1)
@window.event
def on_key_press(symbol,modifiers,keys=keys):
#print symbol,((int(str(symbol)[-1]))-1)
keys[((int(str(symbol)[-1]))-1)] = True
@window.event
def on_key_release(symbol,modifiers,keys=keys):
keys[((int(str(symbol)[-1]))-1)] = False
def update(dt,keys=keys):
#by using this statement we determine more complex events first (left + up) so it fulfils these instead fulfilling a less complex (left)
if keys[0] and keys[1]:
print 'left'+'up'
elif keys[1] and keys[2]:
print 'right'+'up'
elif keys[0] and keys[3]:
print 'down'+'left'
elif keys[2] and keys[3]:
print "down"+'right'
elif keys[0]:
print "left" #due to this, if two conflicting keys are help the upper most one will occur
elif keys[1]:
print "up" #up occurs if 'down' and 'up' are down, left occurs if 'left' and 'right' are down
elif keys[2]:
print 'right'
elif keys[3]:
print "down"
pyg.clock.schedule_interval(update, 0.1)
pyg.app.run()
try that,
sorry for the delay been a touch busy
This post was edited on 09-20-2009 at 09:30 PM by Jarrod.
|
|