still struggling with bounding to screen and score board

so I asked a question earlier and I fixed the fact that both players were not able to move at the same time but now I cant get both players to stay in their own side of the game if anyone could explain how to do it I am little slow please and thank you If someone could also point me in a direction on how to implement a score board would be greatly appreciated

          Player Player1;
            Player Player2;
            float x = 300;
            float y = 200;
            float xspeed = 1;
            float yspeed = 3.3;
            float PlayerSpeed =10;
            float x0 = 200;// falling ball
            float y0 = 0;//^
            float y0_speed=5;//^
            int score=0;
            float xPos;
            float yPos;
            float speed;
            boolean moveLeft, moveRight, moveLeft1, moveRight1;

            void setup() {
            size(600, 400);
            background(55);
            Player1 = new Player(color(255), 137, 369, 2);
            Player2 = new Player(color(255), 452, 369, 2);
            speed = 8;
            moveLeft = moveRight = moveLeft1 = moveRight1 = false;
            }
            void draw() {
            println (mouseX +"," + mouseY);
            background(55);
            noStroke();
            fill(255);
            rect(300, 1, 0.1, 800);
            if(moveLeft) Player1.xPlayer -= speed;
            if(moveRight) Player1.xPlayer += speed;
            if(moveLeft1) Player2.xPlayer -= speed;
            if(moveRight1) Player2.xPlayer += speed;
            if (y0 > height+15) //15 is the radius of the ball
            {
            y0 = -15; //Move it back to just above the screen
            x0 = random(width); //start in randmon spot
            y0_speed = random(3, 7); //Pick a new random speed
            }
            //Move the ball
            y0 += y0_speed; //Increase the balls y value
            //Ball
            fill(255, 255, 255);
            ellipse(x0, y0, 30, 30);
            Player1.display();
            Player2.display();
            }
            class Player {// Trying out classes xD
            color c;
            float xPlayer;
            float yPlayer;
            float WPlayer;
            float Hplayer;
            float pspeed;
            Player(color tempC, float xpos, float ypos, float padspeed) {
            c = tempC;
            xPlayer = xpos;
            yPlayer = ypos;
            Hplayer = 5;
            WPlayer = 40;
            pspeed = padspeed;
            }
            void display() {
            stroke(0);
            fill(c);
            rect(xPlayer, yPlayer, WPlayer,Hplayer);
            }
            }
            void keyPressed() {// player movement
            if (keyCode == LEFT) {
            moveLeft = true;
            } else if(keyCode == RIGHT) {
            moveRight = true;
            } else if(key == 'a') {
            moveLeft1 = true;
            } else if(key == 'd') {
            moveRight1 = true;
            }
            }

            void keyReleased() {//Player movement
            if (keyCode == LEFT) {
            moveLeft = false;
            } else if(keyCode == RIGHT) {
            moveRight = false;
            } else if(key == 'a') {
            moveLeft1 = false;
            } else if(key == 'd') {
            moveRight1 = false;
            }
            }
Tagged:

Answers

  • Answer ✓

    You adjust the player's positions on lines 31 to 34. What constraints are there on those values? If you only want the players to be on the left side, the X position must be between 0 and width/2. What values should the X position be for players on the right side?

    You will need to check for these conditions after updating their positions, and make adjustments to the position if they are out of the valid range of values. constrain() might be a good function to use.

Sign In or Register to comment.