How to improve movement with keys?

Heya, i picked up a project again that i dropped a while ago, and it includes a playable figure that is steered with w, a, s and d. But i noticed how the player would first kinda spazz out and then change direction with a big delay, when holding in a direction and then switching to another. You can also go in a direction with the wrong key, by simultaneously pressing down to keys, then letting go of the key that is supposed to steer you in the direction that you are currently moving to, which leaves you pressing down the other key, but still going in the same direction.

(the following code blocks are snippets out of the previously mentioned project)

This is how i currently have it, the block below is how i tried it previously (it resulted in the exact same issues):

int camx = 0;
int camy = 0;

void setup() {
  size(1920, 1080);
  background(100);
}

void draw() {
  background(100);
  drawPlayer();
  inputPlayer();
  drawUI();
}

void drawPlayer() {
  fill(#4667AA);
  strokeWeight(1);
  stroke(0);
  rectMode(CENTER);
  rect(width/2, height/2, 100, 100);
  rectMode(CORNER);
}

void inputPlayer() {
  if (keyPressed == true) {
    if (key == 'w') {
      if (camy > -100000) {
        camy = camy-10;
      }
    } else {
      if (key == 'a') {
        if (camx > -100000) {
          camx = camx-10;
        }
      } else {
        if (key == 's') {
          if (camy < 100000) {
            camy = camy+10;
          }
        } else {
          if (key == 'd') {
            if (camx < 100000) {
              camx = camx+10;
            }
          }
        }
      }
    }
  }
}

void drawUI() {
  fill(255);
  textSize(20);
  text("X: " + (camx / 100) + "     Y: " + (0-(camy/100)), (width * 0.02604), (height*0.740740));
}
void inputPlayer() {
  if (keyPressed == true) {
    if (key == 'w') {
      if (camy > -100000) {
        camy = camy-10;
}
}
      if (key == 'a') {
        if (camx > -100000) {
          camx = camx-10;
}
}
        if (key == 's') {
          if (camy < 100000) {
            camy = camy+10;
}
}
          if (key == 'd') {
            if (camx < 100000) {
              camx = camx+10;
            }
          }
        }
      }

Can someone tell me what is going on and, perhaps, how to fix and/or improve it?

1 Like

There are a few suggestions;
Firstly, check the fullScreen() function.
Secondly, while you are testing the game, I suggest you reduce the grid size (so its not 100k) and even put it in a varaible.
Thirdly about the keys;
I do not recommend you check for key strokes in draw(). It only updates when the screen is drawn and cannot be forcefully updated.

Instead look into:
keyPressed(){} and keyReleased(){}
for basic programs only keyPressed is enough. However, if you have a more complex system where you need to check for several keys at the same time do something like this

boolean isPressed = false, isPressed2 = false;
char ky = ' ', ky2= 'a'; //space and LOWERCASE a, 'A' will not work
void setup() {
  size(600,600);
}
void draw() {
  update();
}
void update() {
  if(isPressed) {
    fill(255);
  } else {
    fill(0);
  }
  rect(0,0,width*0.5,height);
  if(isPressed2) {
    fill(255,0,0);
  } else {
    fill(0,0,255);
  }
  rect(width*0.5,0,width*0.5,height);
}
void keyPressed() {
  if(key == ky) isPressed = true;
  if(key == ky2) isPressed2 = true;
}
void keyReleased() {
  if(key == ky) isPressed = false;
  if(key == ky2) isPressed2 = false;
}

It is a program that allows you to monitor the state of two buttons. It should help you.

Just instead of background colors, use x++ or x --.

2 Likes

Heyyy okay thank you, the method with two simultaneous keys is really good, since im thinking about making the player be able to sprint with shift.

(i dont want fullscreen for two reasons: i prefer having a window that you can move around and close quickly, and i also want the resolution (effectively the window size) to be adjustable between 720p and 2k)

While I’m at it, is using Processing for an “open-world”-game like this acceptable? I’ve seen Processing struggle with some very basic code sometimes (which may be because said code was poorly written, but still), and i’m worried that the performance will drop when i start adding all of the entities sitting around in the world (of course i’m gonna limit the simulation distance though).

  1. Yeah, the solution to buggy keys in processing can, 99% of the time, be solved with the keyPressed() and keyReleased() functions. :slight_smile:

  2. In theory, you can make an open world game in here. Now, if you plan on making a 3D open world game, that might be a different story. You generally have to consider rendering distance, how many things you usually have on screen, not to mention graphics. This honestly applies to all 3D games, regardless of if they’re open world, and if what you’re looking for is 3D, you should probably ask someone more skilled with Processing P3D than me.

With that said, if it’s 2D rendering you’re looking for, you could definitely run an open world game on Processing. Of course, you might have an easier time with an actual game engine, like Unity, since those were actually built for making games, and have tons of libraries and assets made specifically with game development in mind. However, you could still do it on Processing, no doubt. The only requisite in terms of resources for all open world games is that you need to store a lot of data (not only game sprites and audio, but also world maps and terrain). And, really, if you can figure out how to load and unload files from the data folder, that shouldn’t be much of an issue.

Good luck!

2 Likes

Okay, thanks a lot!

I’m not interested in 3D anyways, since in my opinion, making 3D stuff look cool and polished is way harder than in 2D and just not worth the effort in some cases (like mine).
And I’m happy to hear that it is possible in Processing! That only leaves me with the “problem” that I don’t have talent for drawing, which will mean that the textures will be horrifying, but who cares :smiley:

@Braunstein also be aware with an (open-world) game that your goal for how it will be played is very important. Do you want it to be multiplayer in person or over a network? Do you want it to be playable on a website, or mobile, or downloadable from a site like itch.io as an application that others can run?

If any of these things are important to you, then before taking on a big project, I’d strongly suggest starting with something a tiny test – like Tic-Tac-Toe or simpler – and then go through the process of trying to publish / translate / transfer it in the way that you want. You may find that you prefer p5.js or something else depending on your needs, but we can answer your questions as they come up.

hey, thanks for the tips, but i haven’t event thought about publishing the project…i just wanted to make a fun game to send to my friends. regarding the multiplayer: i thought of local multiplayer, with everyone using the same keyboard (perhaps i can add flexible controller and additional keyboard compatibilty if i even finish this), which is also why i’m gonna try to squeeze the controls onto only a few keys and avoid forcing to use the mouse, so i have it easier if i eventually start working on local multiplayer features.

That’s exactly what I’m talking about. Is your goal is to have them install Processing and run it on their desktop / laptop computers that way, for example? So they’ll need to have Windows / Mac / Linux. Or do you want to use PDE application exporting. If they only play on phones, you’ll probably want to use p5.js, et cetera. I’m not trying to ruin the fun of a new project. I’m just saying – if your goal is to send it to friends – to first check that what you are spending time on is something that your friends will actually be able to run.

Well, most people who I’m thinking of have Processing installed anyways, since we all learned this language together, but I guess nothing speaks against exporting the java application? I appreciate the advice, and I will remember this, but as long as I’m not intending this to be playable on a website, a phone, etc., there is not much to worry about. I think. Um. Please correct me if I’m wrong

That’s right – if you are just trying to pass PDE projects to friends who have Processing, there should be nothing to worry about!