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.
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;
}