Keyboard controls for multiplayer games

I’m trying to make a variant of pong, as well as a tanks game, but I can’t find a good way to make multiplayer work. Advice? Current method in the pong variant is shown below, but it has trouble taking multiple inputs at the same time.

Code from main file:

void keyReleased(){
	if(keyCode==UP){
		g.p1.isMovingUp=false;
	}
	if(keyCode==DOWN){
		g.p1.isMovingDown=false;
	}
	if(key=='w'||key=='W'){
		g.p2.isMovingUp=false;
	}
	if(key=='s'||key=='S'){
		g.p2.isMovingDown=false;
	}
}

Code from paddle class:

public void moveUp(){
	if(isMovingUp&&y>0){
		y-=speed;
	}
}
public void moveDown(float h){
	if(isMovingDown&&y+pHeight<h){
		y+=speed;
	}
}

Code in game class:

if(keyPressed){
	if(keyCode==UP&&p1.getY()>0){
		p1.isMovingUp=true;
		p1.isMovingDown=false;
	}
	if(keyCode==DOWN&&p1.getY()+p1.getHeight()<gHeight){
		p1.isMovingDown=true;
		p1.isMovingUp=false;
	}
	if((key=='w'||key=='W')&&p2.getY()>0){
		p2.isMovingUp=true;
		p2.isMovingDown=false;
	}
	if((key=='s'||key=='S')&&p2.getY()+p2.getHeight()<gHeight){
		p2.isMovingDown=true;
		p2.isMovingUp=false;
	}
}

Full program: https://www.openprocessing.org/sketch/747012

1 Like

i made a runner what easy
now also works in 2 player(hand) / one keyboard / mode,
also the diagonal running works nice ( so up to 4 keys work parallel )

// 2 player / hand mode for 2 ( diagonal ) runner 

int[]     p1keysb={0,0,0,0};                     // pressed key memory, NOW INT SPEED
char[]    p1keyss={'w','s','a','d'};             // key list to be detected parallel UP DOWN LEFT RIGHT
int[]     p2keysb={0,0,0,0};                     // pressed key memory, NOW INT SPEED
char[]    p2keyss={38,40,37,39};                 // key CODE to be detected parallel UP DOWN LEFT RIGHT
int x1=0, y1, x2=0, y2, speed = 3, size = 10;

void setup() {
  size(400, 400);
  x2 = width;
  noStroke();
  print_keyinfo();
}

void draw() {
  background(255);
  move();
}

void print_keyinfo() {
  print("Player 1 use keys:");
  for ( int i=0;i<4;i++) print("["+p1keyss[i]+"] ");
  print("\nPlayer 2 use keys:");
  for ( int i=0;i<4;i++) print("["+p2keyss[i]+"] ");
  println("\nfor      UP DOWN LEFT RIGHT");
}

void move() {                           // and draw
    x1 = constrain(x1 += p1keysb[3]-p1keysb[2] , 0, width-size-1);
    y1 = constrain(y1 += p1keysb[1]-p1keysb[0] , 0, height-size-1);
    fill(200,0,0);
    square(x1, y1, size);
    x2 = constrain(x2 += p2keysb[3]-p2keysb[2] , 0, width-size-1);
    y2 = constrain(y2 += p2keysb[1]-p2keysb[0] , 0, height-size-1);
    fill(0,0,200);
    square(x2, y2, size);
}

void keyPressed() {
  //println("key "+key+" keyCode "+keyCode);
  for ( int i=0;i<4;i++) if ( key    ==p1keyss[i] ) p1keysb[i]=speed;
  for ( int i=0;i<4;i++) if ( keyCode==p2keyss[i] ) p2keysb[i]=speed;
}

void keyReleased() {
  for ( int i=0;i<4;i++) if ( key    ==p1keyss[i] ) p1keysb[i]=0;
  for ( int i=0;i<4;i++) if ( keyCode==p2keyss[i] ) p2keysb[i]=0;
}

hope that idea helps.

1 Like

This topic may be of interest:

I adapted for multiple key presses.

:slight_smile:

1 Like