简体   繁体   English

如何独立移动2名球员的乒乓球垫?

[英]How do I independently move the pads of the ping pong for 2 players?

so I just started doing some java with processing and encounter the problem that I can't move the pads independently from one another. 所以我刚开始做一些Java处理,遇到了我无法独立移动打击垫的问题。

While I move pad1 and start pad2, pad1 just stops and the other way around, so that the two players interfere each others actions when the game gets faster. 当我移动pad1并启动pad2时,pad1只是停下来,反之亦然,这样当游戏变得更快时,两个玩家就会互相干扰。

I tried to reach a difference by turning the pads into objects, but I didn't accomplish any changes to the problem itself. 我试图通过将垫子变成物体来达到目的,但是我并没有对问题本身做任何改变。 the end for the code can be ignored for now cause I think I will fix this with a forLoop. 现在可以忽略代码的结尾,因为我想我会用forLoop修复它。

float player_1_x;
float player_1_y;
float player_2_x;
float player_2_y;
float ball_x;
float ball_y;
float ball_vector_x;
float ball_vector_y;

int round;

void setup() {
  player_1_x = 20;
  player_1_y = 60;
  player_2_x = 780;
  player_2_y = 60;
  ball_x = 400;
  ball_y = 300;
  ball_vector_x = -3;
  ball_vector_y = 0;
  round = 0;
  size(800, 600);
  rectMode(CENTER);
}

void draw() {
  background(0);
  rect(ball_x, ball_y, 10, 10);
  rect(player_2_x, player_2_y, 20, 100);
  if(keyPressed) {
    if(keyCode == DOWN && player_2_y < 550) {
      player_2_y += 5;
      }
    if(keyCode == UP && player_2_y > 50) {
      player_2_y -= 5;
      }
  }
  rect(player_1_x, player_1_y, 20, 100);
  if(keyPressed) {
    if(key == 's' && player_1_y < 550) {
      player_1_y += 5;
      }
    if(key == 'w' && player_1_y > 50) {
      player_1_y -= 5;
      }
  }
  ball_x += ball_vector_x;
  ball_y += ball_vector_y;
  if(ball_x < 30) {
    if(ball_y < (player_1_y + 55) && ball_y > (player_1_y - 55)) {
      ball_vector_x = (-ball_vector_x) + 0.5;
      ball_vector_y -= (player_1_y - ball_y) * 0.05;
    } else {
        ball_x = 400;
        ball_y = 300;
        ball_vector_x = -3;
        ball_vector_y = 0;
        round = 0;
    }
  }
  if(ball_x > 770) {
    if(ball_y < (player_2_y +55) && ball_y > (player_2_y - 55)) {
      ball_vector_x = (-ball_vector_x) - 0.5;
      ball_vector_y -= (player_2_y - ball_y) * 0.05;
    } else {
        ball_x = 400;
        ball_y = 300;
        ball_vector_x = 3;
        ball_vector_y = 0;
        round = 0;
    }
  }
  if(ball_y > 595 || ball_y < 5) {
    ball_vector_y = -ball_vector_y;
  }
  if(ball_vector_x == 4 || ball_vector_x == -4) {
    round = 1;
  } else if(ball_vector_x == 5 || ball_vector_x == -5) {
    round = 2;
  } else if(ball_vector_x == 6 || ball_vector_x == -6) {
    round = 3;
  } else if(ball_vector_x == 7 || ball_vector_x == -7) {
    round = 4;
  } else if(ball_vector_x == 8 || ball_vector_x == -8) {
    round = 5;
  } else if(ball_vector_x == 9 || ball_vector_x == -9) {
    round = 6;
  } else if(ball_vector_x == 10 || ball_vector_x == -10) {
    round = 7;
  }
  text("round: " + round, 380, 20);

}

If you want to accept multiple inputs at once without collision, use the keyPressed() and keyReleased() functions. 如果要一次接受多个输入而不会发生冲突,请使用keyPressed()keyReleased()函数。 No matter when you press a key, these functions will accept your input. 无论何时按下键,这些功能都会接受您的输入。 However, these only register when you press and release a key, and not when you are holding down a key. 但是,这些仅在按下和释放键时注册,而在按住键时不注册。 To correct for this, make boolean variables for up and down, and when they are true, move the paddles. 要对此进行更正,请向上和向下设置布尔变量,当它们为true时,移动拨片。 This allows you to move the paddles smoothly and at any time, without any issues or interference. 这使您可以随时顺畅地移动桨叶,而不会出现任何问题或干扰。 You can then remove the if-statements that check for the keys in the draw() loop. 然后,您可以删除在draw()循环中检查键的if语句。 In practice, it looks like this: 实际上,它看起来像这样:

boolean player_1_down;//Initialize all of these to false in setup.
boolean player_1_up;
boolean player_2_down;
boolean player_2_up ;

....


void keyPressed(){
   if(key == 's') {
      player_1_down = true;
   }
   if(key == 'w' ) {
     player_1_up = true;
   }
   if(keyCode == DOWN ) {
      player_2_down = true;
   }
   if(keyCode == UP ) {
      player_2_up = true;
   }
}

void keyReleased(){
   if(key == 's' ) {
      player_1_down = false;
   }
   if(key == 'w') {
     player_1_up = false;
   }
   if(keyCode == DOWN ) {
      player_2_down = false;
   }
   if(keyCode == UP) {
      player_2_up = false;
   }
}

Then, in your draw() loop, near the top, put this, to make your paddles move: 然后,在您的draw()循环中,在顶部附近,将其放置,以使您的桨叶移动:

if(player_1_down && player_1_y < 550){
    player_1_y += 5;
}else if(player_1_up && player_1_y > 50){
    player_1_y -= 5;
}
if(player_2_down && player_2_y < 550){
    player_2_y += 5;
}else if(player_2_up&& player_2_y > 50){
    player_2_y -= 5;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM