Question

This is my class:

public class Play extends BasicGameState{

    public int state;

    private Input in;

    private int rh = 100, rw = 10;//racket - Width , Height

    private boolean left;//trye if the ball is going left

    Ball b;//the ball

    Guy p1,p2;//the players

    public Play(int state)
    {
        this.state = state;
    }

    @Override
    public void init(GameContainer gc, StateBasedGame sbg)
                        throws SlickException 
    {
        Resources.init();

        b = new Ball();

        b.w = 20;
        b.h = 20;

        b.x = 800 / 2 - (b.w / 2);
        b.y = 600 / 2 - (b.h / 2);

        p1 = new Guy();
        p2 = new Guy();

        p1.x = 10;
        p2.x = 780;//idk why 790 did not work

        p1.y = 600 / 2 - (rh / 2);
        p2.y = p1.y;

        left = true;

        b.ny = 0;
    }

    @Override
    public void render(GameContainer gc, StateBasedGame sbg, Graphics g)
                        throws SlickException 
    {//just render

        g.fillRect(p1.x, p1.y, rw, rh);
        g.fillRect(p2.x, p2.y, rw, rh);

        g.fillRect(b.x, b.y, b.w, b.h);

        //g.drawImage(Resources.racket, p1.x, p1.y);
        //g.drawImage(Resources.racket, p2.x, p2.y);
        //g.drawImage(b.i , b.x , b.y);

        debug();
    }

    @Override
    public void update(GameContainer gc, StateBasedGame sbg, int delta)
                    throws SlickException 
    {
        in = gc.getInput();//update input

        if(Keyboard.isKeyDown(Keyboard.KEY_W))
        {//update racket coordinates if keys are down
            p1.y += delta * .1f;
        }
        if(Keyboard.isKeyDown(Keyboard.KEY_S))
        {
            p1.y += delta * .1f;
        }

        if(Keyboard.isKeyDown(Keyboard.KEY_UP))
        {
            p2.y -= delta * .1f;
        }
        if(Keyboard.isKeyDown(Keyboard.KEY_DOWN))
        {
            p2.y += delta * .1f;
        }


        if(left)
        {//collision detection (including setting angle to the ball if it doesnt hit the exact center of the racket)
            if(b.x <= p1.x + rw && b.y <= p1.y + rh && b.y >= p1.y)
            {
                if(b.y + b.h == p1.x + (rh / 2))
                {
                    b.ny = 0;
                }
                else if(b.y + b.h < p1.x + (rh / 2))
                {
                    int d = p1.y + (rh / 2) - b.y;
                    b.ny = - d / 500;
                }
                else if(b.y + b.h > p1.x + (rh / 2))
                {
                    int d = p1.y + rh - b.y;
                    b.ny = d / 500;
                }

                left = false;
            }
        }
        else
        {
            if(b.x >= p2.x + rw && b.y <= p2.y + rh && b.y >= p2.y)
            {
                if(b.y + b.h == p2.x + (rh / 2))
                {
                    b.ny = 0;
                }
                else if(b.y + b.h < p2.x + (rh / 2))
                {
                    int d = p2.y + (rh / 2) - b.y;
                    b.ny = - d / 500;
                }
                else if(b.y + b.h > p2.x + (rh / 2))
                {
                    int d = p2.y + rh - b.y;
                    b.ny = d / 500;
                }

                left = true;
            }
        }    

        if(left)
        {//set the images
            b.i = Resources.ball_move_left;
        }
        else
        {
            b.i = Resources.ball_move_right;
        }

        if(b.y <= 0 || b.y >= 600)
        {//add "walls" on floor and ceiling
            b.ny = - b.ny;
        }

        if(left)
        {//update ball X coordinate
            b.x -= delta * .1f;
        }
        else
        {
            b.x += delta * .1f; 
        }

        b.y += b.ny * delta * .1f;//update ball Y coordinate

    }

    @Override
    public int getID() 
    {
        return state;
    }

    private void debug()
    {
        System.out.println("P1 : X:" + p1.x + " Y:" + p1.y);
        System.out.println("P2 : X:" + p2.x + " Y:" + p2.y);
        System.out.println("BALL : X:" + b.x + " Y:" + b.y);
    }

}

For some weird reason I cant move the rackets up having a Y less than 0 and I also cant move them down at ALL. Here is the other classes (Ball - Guy)

public class Guy {

public int x,y;

}


public class Ball {

public int x,y,w,h,ny;

public Image i;
}

The ball also stops upon touching the left racket. It does not go back. There seems to be an issue with the x+= ... lines of code. I added debug messages , the messages print but the coordinates dont get an update! I believe there is some bug with the whole code - working on it a whole day , did not find any errors :/

Was it helpful?

Solution

The problem is potentially in lines like:

 b.x += delta * .1f; 

Where you are taking a float and adding/subtracting it from an integer. It is possible that the Java engine performs a cast on the float data to turn it into an integer, which will loose data, and could cause the program to be non-responsive.

Additionally, you may want to move some of that procedural code that you have duplicated many times into the classes to improve the Object Oriented nature of this program.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top