[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
Re: [pygame] my rpg game (so far)
try this:
from euclid import Vector2
def input_loop():
"""your events input"""
newloc = Vector2(0,0) # euclid
keys = pygame.key.get_pressed()
# get player's wanted location
if keys[K_UP]:
newpos.y -= 1
if keys[K_DOWN]:
newpos.y += 1
if keys[K_LEFT]:
newpos.x -= 1
if keys[K_RIGHT]:
newpos.x += 1
# check if new tile is a collision
if not map.collide(newpos):
player.move(newpos)
if you want to exclude diagonal movement on purpose, add an extra check:
#force no diagonal movement
if x != 0 and y != 0:
y = 0
In the above code, 'map' would be your map class, and .collide(x,y)
will check if it would be a collision to walk onto tile x,y.
player.move(x,y) sets player location to x,y.
Note: If you want to pass a Vector2 to a function that is expecting
two values "x,y" instead of a vector, add a '*' infront of the
variable:
v = Vector2(1,2)
some_func( *v )
it causes the equivalent of:
some_func( v.x, v.y )
--
Jake