如何通过按特定键使对象“移动”? - 处理

时间:2015-07-01 20:39:12

标签: java processing

我是编程新手,我正在构建一个简单的pong应用程序的问题。想法是玩家1控制左侧和顶部的桨,而玩家2控制右侧和底部。该程序尚未完成,但我遇到了一个问题。现在使用我的代码,桨叶要么根本不移动要么在对角线上移动而不是从左到右移动两个桨叶。非常感谢帮助,任何关于如何修复或更好地组织我的程序的指示将不胜感激。感谢您的时间和帮助。

package xpong;

import processing.core.PApplet;
import processing.core.PFont;
import processing.core.PImage;

public class XPong extends PApplet {
    PFont f;      
    public boolean sideMoveLeft = false;
    public boolean sideMoveRight = false;
    public boolean topMoveLeft = false;
    public boolean topMoveRight = false;
    public float speed = 5;

    public float x = 0;
    public float y = 0;
    public float a = 0;
    public float b = 0;
    public boolean sideMoving = false;
    public boolean topMoving = false;



    public void setup() {
        size(800, 700);
        background(255, 255, 255);
        f = createFont("Arial",16,true);
        smooth();
    }

    public void draw() {
        background(255, 255, 255);
        textFont(f,16);                 
        fill(0);

        text("Press Spacebar to Begin!",300, 350);

            //leftBar();
            //topBar();
            //rightBar();
            //bottomBar();


        //Checks to see if if players keys are pressed to start playing and draws he bar
                moveLeftAndRight();
                translate(x, y);
                leftBar();          
                rightBar();

            /*if(key == 'q' || key == 'w'){
                moveTopAndBottom();
                translate(x, y);

                topBar();
            }*/
    }   
//Draws the bars
    public void leftBar() {

    fill(0, 0, 0);
    rect(40, 260, 10, 200);

    }
    public void rightBar() {

        fill(0, 0, 0);
        rect(730, 260, 10, 200);        
    }

    public void topBar() {

        fill(0, 0, 0);
        rect(300, 40, 200, 10);     
    }
    public void bottomBar() {

        fill(0, 0, 0);
        rect(300, 650, 200, 10);        
    }
    // Checks for key press and tells the program user wants to move
    public void keyPressed(){
        if(key == 'q')
        {
            sideMoveLeft = true;
            topMoveLeft = true;
            sideMoving = true;
            topMoving = true;

        }
        if(key == 'w')
        {
            sideMoveRight = true;
            topMoveRight = true;
            sideMoving = true;
            topMoving = true;

        }
        if(key == 'o'){
            sideMoveLeft = true;
            sideMoving = true;

        }
        if(key == 'p'){
            sideMoveRight = true;
            sideMoving = true;

        }
    }
    //checks if the player has released the key to stop movement, and everything is set back to false
    public void keyReleased(){
        if(key == 'q')
        {
            sideMoveLeft = false;
            topMoveLeft = false;
            sideMoving = false;
            topMoving = false;
        }
        if(key == 'w')
        {
            sideMoveRight = false;
            topMoveRight = false;
            sideMoving = false;
            topMoving = false;
        }
        if(key == 'o'){
            sideMoveLeft = false;
            sideMoving = false;

        }
        if(key == 'p'){
            sideMoveRight = false;
            sideMoving = false;

        }
    }
    //checks to see if users is pressing key and adds 5(speed) to y
    public void moveLeftAndRight()
    {
        if(sideMoveRight)
        {
            y += speed;

        }
        if(sideMoveLeft)
        {
            y -= speed;

        }

 }
    //checks to see if users is pressing key and adds 5(speed) to x
    public void moveTopAndBottom()
    {
        if(topMoveRight)
        {
            x += speed;

        }
        if(topMoveLeft)
        {
            x -= speed;

        }
    }
}

2 个答案:

答案 0 :(得分:0)

我认为问题出在您的if个案例中:

if (key == 'q' || key == 'w') {
    moveLeftAndRight();
    translate(x, y);
    leftBar();
}
if (key == 'q' || key == 'w') {
    moveTopAndBottom();
    translate(a, b);
    topBar();
}

两者都是相同的,这意味着:当且仅当您输入第二个if时,才输入第一个if。这可能导致对角线平移。我想你想把第二个if更改为:

if (key == 'w' || key == 's') {
    ...
}

答案 1 :(得分:0)

修复并更新代码并忽略评论代码:

package xpong;


import processing.core.PApplet;
import processing.core.PFont;
import processing.core.PImage;
public class XPong extends PApplet {
//  PFont f;
    //public boolean alwaysTrue = true;

    public boolean p1SideMoveUp = false;
    public boolean p1SideMoveDown = false;
    public boolean p2SideMoveUp = false;
    public boolean p2SideMoveDown = false;
    public boolean p1moving = false;
    public boolean p2moving = false;

    public float speed = 5;
//  int ballSpeed = 10;

    public float p1X = 0;
    public float p1Y = 0;
    public float p2A = 0;
    public float p2B = 0;
  //  int ballX = 400;

  //  float theta;
  //  String message = "thelra;kdjfasdfadsf adfa";

    public void setup() {

    //  size(800, 700);
    //  f = createFont("Arial",16,true);
        smooth();
    }

    public void draw() {
        background(255, 255, 255);
        //screenDivider();

        //textFont(f,16);
        //fill(0);
      //  text("Hello Strings!",10,100);

        moveRight();
        moveLeft(); 
    //ballX = ballX + ballSpeed;

    /*if ((ballX > width) || (ballX < 0)) {

        ballSpeed = ballSpeed * -1;
    }*/
      //stroke(0);
      //fill(175);
     // ellipse(ballX,350,32,32);
}   
    public void moveLeft(){
        moveP1Paddle();
        p1BarDraw();
    }
    public void moveRight(){
        moveP2Paddle();
        p2BarDraw();
    }

//Draws the bars
    public void p1BarDraw() {

    fill(0, 0, 0);
    rect(p1X + 40, p1Y + 260, 10, 200);

    }
    public void p2BarDraw() {

        fill(0, 0, 0);
        rect(p2A + 730, p2B + 260, 10, 200);        
    }
    /*public void screenDivider() {
        line(400, 10000, 400, 0);
    }*/

    // Checks for key press and tells the program user wants to move
    public void keyPressed(){
        if(key == 'q')
        {
            p1SideMoveUp = true;
        }
        if(key == 'w')
        {
            p1SideMoveDown = true;
        }
        if(key == 'o'){
            p2SideMoveUp = true;
        }
        if(key == 'p'){
            p2SideMoveDown = true;
        }
    }
    //checks if the player has released the key to stop movement, and everything is set back to false
    public void keyReleased(){
        if(key == 'q')
        {
            p1SideMoveUp = false;
        }
        if(key == 'w')
        {
            p1SideMoveDown = false;
        }
        if(key == 'o'){
            p2SideMoveUp = false;
        }
        if(key == 'p'){
            p2SideMoveDown = false;
        }
    }
    //checks to see if users is pressing key and adds speed to y
    public void moveP1Paddle()
    {
        if(p1SideMoveDown){
            p1Y += speed;
        }
        if(p1SideMoveUp){
            p1Y -= speed; 
        }

 }
    public void moveP2Paddle()
    {
        if(p2SideMoveDown){
            p2B += speed;
        }
        if(p2SideMoveUp){
            p2B -= speed;
        }
    }
}