 # Issues with smooth 2D movement in python

The objective I’m trying to reach is to make smooth, 2-D movement in processing’s python mode. I understand that python processing isn’t nearly as popular as Java, but I need to do it for a school project.

I don’t know Java at all, and because python is so unpopular on processing, I wasn’t able to find much help on legacy forum posts. My movement, while fishy, still works in the X and Y axis, but diagonals are a complete no-go, and overall is really uncomfortable to use.

I’m aiming for something like this:

If you want to try my code: have a look for yourself!

``````PlayX = 1280 / 2
PlayY = 720 / 2

def setup():
size(1280, 720)
background(255)
frameRate(60)

def draw():
global PlayX, PlayY
if keyPressed:
if key == 'w':
PlayY -= 10
pushMatrix() # activating translation grid
background(255) # painting over previous circle
ellipse(PlayX, PlayY, 55, 55) #drawing the player
translate(10, 0) # translation
popMatrix() # deactivating translation grid
if key == 'a':
PlayX -= 10
pushMatrix()
background(255)
ellipse(PlayX, PlayY, 55, 55)
translate(-10, 0)
popMatrix()
if key == 's':
PlayY += 10
pushMatrix()
background(255)
ellipse(PlayX, PlayY, 55, 55)
translate(0, 10)
popMatrix()
if key == 'd':
PlayX += 10
pushMatrix()
background(255)
ellipse(PlayX, PlayY, 55, 55)
translate(0, -10)
popMatrix()
``````

Any help would be really appreciated.

1 Like

Hi @webbm1,

How about storing the directions in a dictionary and having a `toggle` function to activate them when triggered.

Interactive demo

``````W, H = 1280, 720

p = PVector(W>>1, H>>1) #similar to (W/2, H/2)

D = {'w': [0, PVector(0, -10)], #0 means False
'a': [0, PVector(-10, 0)],
's': [0, PVector(0, 10)],
'd': [0, PVector(10, 0)]}

def setup():
size(W, H, P2D)

def draw():
background('#FFFFFF')

controls()

pushMatrix() # activating translation grid
translate(p.x, p.y)
ellipse(0, 0, 55, 55) #drawing the player
popMatrix() # deactivating translation grid

def keyPressed():
toggle(key)

def keyReleased():
toggle(key)

def toggle(k):

if k in D:
D[k] = not D[k]

def controls():

for k in D:
b, dir = D[k] #boolean (is it activated ?), direction
if b:
``````
4 Likes

Thanks! I’ll try this solution out, and if it works, I’ll mark it as answer. Really appreciate the help, @solub

That’s a very old sketch of mine. BtW, I did a followup w/ 2 players instead of 1 both for Java Mode & p5js flavors: And now I’ve decided to do a Python Mode version as well. Check it out: ``````#!/bin/python2

"""
Players Movement (v1.1.0)
by GoToLoop (2020/Mar/11)

Discourse.Processing.org/t/issues-with-smooth-2d-movement-in-python/18556/4

GoToLoop.Trinket.io/sites/players-movement
Trinket.io/processing/62fab051ed

how_would_i_make_a_keypressed_function_support/eor5epo/

OpenProcessing.org/sketch/718355
OpenProcessing.org/sketch/717760

"""

original_set = set # Save Python's builtin set() before Processing's import

from processing import *

ONLINE = '__papplet__' not in globals()

W = 750 if ONLINE else 1200
H = 450 if ONLINE else 600

PLAYERS = 2 # Number of players
BG = 0xff00FFFF # Cyan background

DIAMS = 50, 40 # Diameters
SPDS = 4, 5 # Speeds
INKS = 0xff0000FF, 0xffFF0000 # Blue, Red

KEYS = (
tuple(ord(k) for k in 'WSAD'), # Player 1's keys
(UP, DOWN, LEFT, RIGHT)) # Player 2's keys

players = PLAYERS * [ None ]
keysDown = original_set()

def setup():
size(W, H)

stroke(Player.OUTLINE)
strokeWeight(Player.BOLD)
ellipseMode(Player.MODE)

for i in range(PLAYERS):
d = DIAMS[i]
dd = d << i
players[i] = Player(dd, dd, d, SPDS[i], INKS[i], KEYS[i])

def draw():
background(BG)
for p in players: p.move().display()

class Player:
OUTLINE, BOLD, MODE = 0, 2, CENTER
UP, DOWN, LEFT, RIGHT = range(4)

def __init__(p, x, y, d, v, ink, keys):
p.x = x
p.y = y
p.d = d
p.v = v
p.ink = color(ink)
p.keys = keys

def display(p):
fill(p.ink)
ellipse(p.x, p.y, p.d, p.d)
return p

def move(p):
r = p.d >> 1
v = p.v
k = p.keys

hor = (k[p.RIGHT] in keysDown) - (k[p.LEFT] in keysDown)
ver = (k[p.DOWN]  in keysDown) - (k[p.UP]   in keysDown)

p.x = constrain(p.x + v*hor, r, width  - r)
p.y = constrain(p.y + v*ver, r, height - r)

return p

ONLINE and run()
``````
3 Likes

(me when I see GoToLoop posting in Python: … ‍♂        …)

Joke aside:

``````hor = (k[p.RIGHT] in keysDown) - (k[p.LEFT] in keysDown)
ver = (k[p.DOWN]  in keysDown) - (k[p.UP]   in keysDown)

p.x = constrain(p.x + v*hor,...
p.y = constrain(p.y + v*ver,...
``````

I like that.

3 Likes