We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › multiple keypressed
Page Index Toggle Pages: 1
multiple keypressed (Read 744 times)
multiple keypressed
Jan 23rd, 2009, 8:36pm
 
Hi guys, im new to processing and im trying to make a pong game in processing. Its all done but there is one bug remaining. In my game, to move your pannel up and down i use normal keys such as 'a'. but, when i press a  key while im pressing an other one the other pannel stop moving... wich IS a problem. Is there a function or something i can do to be able to press multiple keys at a time without bug??? please answer me.

Thanks,
Odin
Re: multiple keypressed
Reply #1 - Jan 24th, 2009, 2:32am
 
First make a boolean array

Then in the keyPressed function, do this;
keys[keyCode] = true;

and then in the keyReleased function
keys[keyCode] = false;

then check that array of keys every frame to know which buttons are pressed

if(keys[LEFT]){
 x = max(0,x-1); // moves player towards the left until 0
}
Re: multiple keypressed
Reply #2 - Jan 24th, 2009, 5:41pm
 
Thanks for the answer but im not sure how to use boolean arrays...im making a new array;
int[] keys = ['q', 'a', 'o', 'l']

but when i type:
keys[0] = true;

its says: cannot convert from boolean to int ...

here goes my whole code without the array and see by yourself.

int x = 10;        //rect1 X
int y = 125;       //rect1 Y
int x2 = 390;      //rect2 X
int y2 = 125;      //rect2 Y
int bx = 200;      //ball X
int by = 125;      //ball Y
int c = -1;        //ball x movement
int v = 0;         //ball y movement
int p1 = 0;        //Left player points
int p2 = 0;        //right player points

void setup() {
 size(400, 250);
 rectMode(CENTER);
 ellipseMode(CENTER);
 smooth();
 strokeWeight(2);
 frameRate(130);
 PFont arial;
 arial = loadFont("ariblk.vlw");
 textFont(arial, 48);
}

void draw() {
 background(100);
 text(p1, 165, 35);           //This is the first player's points.
 text(p2, 203, 35);           //This is the second player's points.
 line(200, 0, 200, 250);
 
 
   ellipse(bx, by, 10, 10);    //The Pong Ball
   
   bx = bx + c;                //Default ball movements.
   by = by + v;
   
if(bx == 20 && by >= y-25 && by <= y+25) {     //Movements if ball touch left player.
 if(by < y) {
   c = +2;
   v = -2; }
 if(by > y) {
   c = +2;
   v = +2; }
 if(by == y) {
   c = +2;
   v = 0; }
 }

if(bx == 380 && by >= y2-25 && by <= y2+25) {    //Movements if ball touch right player.
 if(by < y2) {
   c = -2;
   v = -2; }
 if(by > y2) {
   c = -2;
   v = +2; }
 if(by == y2) {
   c = -2;
   v = 0; }
 }

 if(bx < 0) {       //If the ball pass left limit.
 bx = 200;
 by = 150;
 v = 0;
 c = -1;
 p2 = p2+1;
 }

 if(bx > 400) {     //If the ball pass right limit.
   bx = 200;
   by = 150;
   v = 0;
   c = +1;
   p1 = p1+1;
 }

 if(by > 250) {   //The ball touch the lower limit.
   v = -2;
 }
 
 if(by < 0) {     //The ball touch the upper limit.
   v = +2;
 }
   
   rect(x, y, 10, 50);       //This is the left pannel.
     
   rect(x2, y2, 10, 50);     //This is the right pannel.
   

if(keyPressed == true) {
 if(key == 'q') {            //Move the left player up.
   y = max(25, y-2);
   }
 
 if(key == 'a') {           //Move the rigth player down.
   y = min(225, y+2);
   }
 
 if(key == 'o') {           //Move the right player up.
   y2 = max(25, y2-2);
   }
 
 if(key == 'l') {           //Move the right player down.
   y2 = min(225, y2+2);
   }  
 }
 
}
Re: multiple keypressed
Reply #3 - Jan 26th, 2009, 8:42am
 
I edited your code to make it work with simultaneous key presses, but that made it too long to paste in. Below are the changes. My changes allow you to move the to paddles seemingly simultaneously. Also, if you hold down the up and the down keys for a single paddle at the same time, the paddle stops. That seemed like reasonable behavior.

Before the setup() method, add:

// Array for controller keys
boolean[] keys = new boolean[4];
final int Q = 0;
final int A = 1;
final int O = 2;
final int L = 3;

Then at the end of setup(), add:

 // Init all controller keys to "not pressed"
 for (int i = 0; i< 4; i++) {
   keys[i] = false;
 }

Then following the line in your code:

 rect(x2, y2, 10, 50); //This is the right pannel.

replace everything with:

 if(keys[Q] == true) {  //Move the left player up.
   y = max(25, y-2);  
 }
 if(keys[A] == true) {  //Move the left player down.
   y = min(225, y+2);
 }
 if(keys[O] == true) {  //Move the right player up.
   y2 = max(25, y2-2);
 }
 if(keys[L] == true) {  //Move the right player down.
   y2 = min(225, y2+2);
 }
}

void keyPressed() {
 if(key == 'q') {
   keys[Q] = true;
 } else if(key == 'a') {
   keys[A] = true;
 } else if(key == 'o') {
   keys[O] = true;
 } else if (key == 'l') {
   keys[L] = true;
 }
}

void keyReleased() {
 if(key == 'q') {
   keys[Q] = false;
 } else if(key == 'a') {
   keys[A] = false;
 } else if(key == 'o') {
   keys[O] = false;
 } else if (key == 'l') {
   keys[L] = false;
 }
}
Re: multiple keypressed
Reply #4 - Jan 26th, 2009, 6:29pm
 
Thanks you very much! it work great!
Page Index Toggle Pages: 1