# Issues with smooth 2D movement in python

**URL:** <https://discourse.processing.org/t/issues-with-smooth-2d-movement-in-python/18556>\
**Category:** Processing.py\
**Tags:** homework\
**Created:** [March 10, 2020, 10:10am UTC](https://discourse.processing.org/t/issues-with-smooth-2d-movement-in-python/18556 "2020-03-10T10:10:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![webbm1](https://avatars.discourse-cdn.com/v4/letter/w/97f17d/32.png) [@webbm1](https://discourse.processing.org/u/webbm1)\
**Post date:** [March 10, 2020, 10:10am UTC](https://discourse.processing.org/t/issues-with-smooth-2d-movement-in-python/18556/1 "2020-03-10T10:10:14Z")

</div>

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:  
[http://studio.processingtogether.com/sp/pad/export/ro.91tcpPtI9LrXp](http://studio.processingtogether.com/sp/pad/export/ro.91tcpPtI9LrXp)

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![solub](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/solub/32/333_2.png) [@solub](https://discourse.processing.org/u/solub)\
**Post date:** [March 10, 2020, 3:09pm UTC](https://discourse.processing.org/t/issues-with-smooth-2d-movement-in-python/18556/2 "2020-03-10T15:09:42Z")

</div>

Hi @webbm1,

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

[Interactive demo](https://trinket.io/processing/445438cc8d)

```auto
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][0] = not D[k][0]
        

def controls():
    
    for k in D:
        b, dir = D[k] #boolean (is it activated ?), direction
        if b:
            p.add(dir)

```

---

<div class="post-metadata">

**Author:** ![webbm1](https://avatars.discourse-cdn.com/v4/letter/w/97f17d/32.png) [@webbm1](https://discourse.processing.org/u/webbm1)\
**Post date:** [March 10, 2020, 11:54pm UTC](https://discourse.processing.org/t/issues-with-smooth-2d-movement-in-python/18556/3 "2020-03-10T23:54:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [March 11, 2020, 6:26am UTC](https://discourse.processing.org/t/issues-with-smooth-2d-movement-in-python/18556/4 "2020-03-11T06:26:30Z")

</div>

> [@webbm1](#):
>
> I’m aiming for something like this:

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: 🕶

https://www.openprocessing.org/sketch/718355/embed/https://www.openprocessing.org/sketch/717760/embed/

And now I’ve decided to do a Python Mode version as well. Check it out: 🐍

> **[Put Python and ProcessingJS Anywhere on the Web](https://trinket.io/processing/62fab051ed)**
>
> Python in the browser. No installation required.

> **[Players Movement by gotoloop](https://gotoloop.trinket.io/sites/players-movement)**
>
> Players Movement by gotoloop - powered by trinket.io

https://trinket.io/embed/python/62fab051ed?toggleCode=true&runOption=run&start=result

```auto
#!/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

Reddit.com/r/processing/comments/bshqa8/
how_would_i_make_a_keypressed_function_support/eor5epo/

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

Studio.ProcessingTogether.com/sp/pad/export/ro.91tcpPtI9LrXp
"""

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()

def keyPressed(): keysDown.add(keyCode)

def keyReleased(): keysDown.discard(keyCode)

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()

```

---

<div class="post-metadata">

**Author:** ![solub](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/solub/32/333_2.png) [@solub](https://discourse.processing.org/u/solub)\
**Post date:** [March 11, 2020, 3:15pm UTC](https://discourse.processing.org/t/issues-with-smooth-2d-movement-in-python/18556/5 "2020-03-11T15:15:58Z")

</div>

(me when I see GoToLoop posting in Python: … 🕵️‍♂️‍♂ 🤔 😮❕✍🏻 👨🏻‍💻 👌🏻 😏 🤫…)

Joke aside:

```auto
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.
