0

我想在处理中按一个按钮来添加/删除一个椭圆。我使用 void keyPressed() 和 void keyReleased() 作为键。现在我可以添加更多,并删除一些省略号。例如,如果我有一个,我可以按一个按钮,然后再有一个。或按一个按钮删除一个。但现在我想为每个椭圆使用不同的键集来移动这些椭圆(最多 4 个)。所以我做了4个不同的集合,但现在我不能让它们同时移动。我怎样才能做到这一点?请帮我。下面是我的代码。

final static int N1= 1;                           //players can move at the same time
final static int S1 = 2;                           
final static int E1 = 4;                           
final static int W1 = 8;                           // Speed limit. Round out edges for field (silhouette)
                                              // if time Collision detection

final static int N2 = 16;
final static int S2 = 32;
final static int E2 = 64;
final static int W2 = 128;


final static int N3 = 256;
final static int S3 = 512;
final static int E3 = 1024;
final static int W3 = 2048;

final static int N4 = 4096;
final static int S4 = 8192;
final static int E4 = 16384;
final static int W4 = 32768;

int result,k;                                  //store key input and initial number of players on field
float x1,x2,x3,x4,y1,y2,y3,y4,n,r;             //Decleare some useful variables


boolean sketchFullScreen() {                        //Make full screen
return true;
}



void setup() {                                      //Basic setup
size(displayWidth, displayHeight);                //Window size maximum
noCursor();                                       //Hide mouse cursor
frameRate(60);                                   //set the frame rate, can lower the rate for lower performent pc
result = 0;
k = 1;
n = 8;                                            //initial movement speed
r = 15;                                           //initial radius of bubble

x1 = 2*width/3;                                    
y1 = height/3; 
x2 = 2*width/3;                                    //light dot initial position
y2 = 2*height/3; 
x3 = width/3;                                  
y3 = height/3; 
x4 = width/3;                                    
y4 = 2*height/3;
}



void draw() {                                        //Start drawing
background(0);                                     //Black background(COMPLETELY BLACK)
switch(result){   
case N1: y1=y1-n; break;
case E1: x1=x1+n; break;
case S1: y1=y1+n; break;
case W1: x1=x1-n; break;                            //create movement to the dot by reassigning the coordinates
case N1|E1: y1=y1-n; x1=x1+n; break;                    
case N1|W1: y1=y1-n; x1=x1-n; break;
case S1|E1: y1=y1+n; x1=x1+n; break;
case S1|W1: y1=y1+n; x1=x1-n; break;

case N2: y2=y2-n; break;
case E2: x2=x2+n; break;
case S2: y2=y2+n; break;
case W2: x2=x2-n; break;                            //create movement to the dot by reassigning the coordinates
case N2|E2: y2=y2-n; x2=x2+n; break;                    
case N2|W2: y2=y2-n; x2=x2-n; break;
case S2|E2: y2=y2+n; x2=x2+n; break;
case S2|W2: y2=y2+n; x2=x2-n; break;

case N3: y3=y3-n; break;
case E3: x3=x3+n; break;
case S3: y3=y3+n; break;
case W3: x3=x3-n; break;                            //create movement to the dot by reassigning the coordinates
case N3|E3: y3=y3-n; x3=x3+n; break;                    
case N3|W3: y3=y3-n; x3=x3-n; break;
case S3|E3: y3=y3+n; x3=x3+n; break;
case S3|W3: y3=y3+n; x3=x3-n; break;

case N4: y4=y4-n; break;
case E4: x4=x4+n; break;
case S4: y4=y4+n; break;
case W4: x4=x4-n; break;                            //create movement to the dot by reassigning the coordinates
case N4|E4: y4=y4-n; x4=x4+n; break;                    
case N4|W4: y4=y4-n; x4=x4-n; break;
case S4|E4: y4=y4+n; x4=x4+n; break;
case S4|W4: y4=y4+n; x4=x4-n; break;
}

if(k>=5){k=4;}                                  //max bumber of players is 4
if(k<0){k=0;}                                   //least amount of players is 0


if(x1<0){x1=r/2;}
else if(x1>displayWidth){x1=displayWidth-r/2;}
if(x2<0){x2=r/2;}
else if(x2>displayWidth){x2=displayWidth-r/2;}
if(x3<0){x3=r/2;}
else if(x3>displayWidth){x3=displayWidth-r/2;}
if(x4<0){x4=r/2;}
else if(x4>displayWidth){x4=displayWidth-r/2;}

if(y1<0){y1=r/2;}
else if(y1>displayHeight){y1=displayHeight-r/2;}
if(y2<0){y2=r/2;}
else if(y2>displayHeight){y2=displayHeight-r/2;}
if(y3<0){y3=r/2;}
else if(y3>displayHeight){y3=displayHeight-r/2;}
if(y4<0){y4=r/2;}
else if(y4>displayHeight){y4=displayHeight-r/2;}


stroke(255);                                        //White light stroke
if(k==1){ellipse(x1,y1,r,r);}                       //Draw an ellipse at location (x,y) xwith radius of 15 pixels
if(k==2){
ellipse(x1,y1,r,r); 
ellipse(x2,y2,r,r);
    }

if(k==3){
ellipse(x1,y1,r,r); 
ellipse(x2,y2,r,r);
ellipse(x3,y3,r,r);
    }

if(k==4){
ellipse(x1,y1,r,r); 
ellipse(x2,y2,r,r);
ellipse(x3,y3,r,r);
ellipse(x4,y4,r,r);
     }
}         


void keyPressed() {
switch(key) {
case('w'):case('W'):result |=N1;break;
case('d'):case('D'):result |=E1;break;
case('s'):case('S'):result |=S1;break;              //classify the cases for the key pressed
case('a'):case('A'):result |=W1;break;}
switch(key) { 
case('8'):result |=N2;break;
case('6'):result |=E2;break;
case('5'):result |=S2;break;              
case('4'):result |=W2;break;}
switch(key) {
case('t'):case('T'):result |=N3;break;
case('h'):case('H'):result |=E3;break;
case('g'):case('G'):result |=S3;break;              
case('f'):case('F'):result |=W3;break;}
switch(key) {
case('i'):case('I'):result |=N4;break;
case('l'):case('L'):result |=E4;break;
case('k'):case('K'):result |=S4;break;              
case('j'):case('J'):result |=W4;break;}
switch(key) {
case('+'):case('='): n++;break;
case('_'):case('-'): n--;break;                    //change movement speed
case('['):case('{'): r=r-1;break;                  //change radius of bubble
case(']'):case('}'): r=r+1;break;
case(':'): k--;break;                            //add and take away players
case('"'): k++;break;
}
}

void keyReleased(){  
switch(key) {                                      
case('w'):case('W'):result ^=N1;break;
case('d'):case('D'):result ^=E1;break;
case('s'):case('S'):result ^=S1;break;              //classify the cases for the key pressed
case('a'):case('A'):result ^=W1;break;}
switch(key) {  
case('8'):result ^=N2;break;
case('6'):result ^=E2;break;
case('5'):result ^=S2;break;              
case('4'):result ^=W2;break;}
switch(key) {
case('t'):case('T'):result ^=N3;break;
case('h'):case('H'):result ^=E3;break;
case('g'):case('G'):result ^=S3;break;              
case('f'):case('F'):result ^=W3;break;}
switch(key) {
case('i'):case('I'):result ^=N4;break;
case('l'):case('L'):result ^=E4;break;
case('k'):case('K'):result ^=S4;break;              
case('j'):case('J'):result ^=W4;break;} 
switch(key) {    
case('+'):case('='): n++;break;
case('_'):case('-'): n--;break;
case('['):case('{'): r=r-1;break;
case(']'):case('}'): r=r+1;break;
}
}
4

1 回答 1

1

我不愿意涉足你所有的常量和 switch 语句,但解决这个问题的方法是为每种可能性设置变量。就像是:

//direction variables: 1 is up/right, 0 is none, -1 is left/down
int y1 = 0, x1 = 0, y2 = 0, x2 = 0...;

void keyPressed(){
  if(key == 'w' || key == 'W') y1 = 1;
  if(key == 's' || key == 'S') y1 = -1;
  if(key == 'd' || key == 'D') x1 = 1;
  ... 
}
void keyReleased(){
  if(key == 'w' || key == 'W' || key == 's' || key == 'S') y1 = 0;
  ...
}
void draw(){
  if(y1 == 1) moveOneUp();
  if(y1 == -1) moveOneDown();
  if(y2 == 1) moveTwoUp();
  ...
}

为所有各种可能性(x1,x2,x3,x4,y1,y2,y3,y4)执行此操作。

于 2013-10-17T16:57:13.227 回答