Slick2d / LWJGL - 制作一个简单的乒乓球游戏问题

时间:2014-04-29 18:55:24

标签: java logic lwjgl slick2d pong

这是我的班级:

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);
    }

}

出于一些奇怪的原因,我不能将球拍向上移动,Y小于0,而且我也无法将它们向下移动。这是其他课程(Ball - Guy)

public class Guy {

public int x,y;

}


public class Ball {

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

public Image i;
}

球在触碰左球时也会停止。它不会回去。 x + = ...代码行似乎存在问题。我添加了调试消息,消息打印但坐标没有得到更新! 我相信整个代码存在一些错误 - 整天工作,没有发现任何错误:/

1 个答案:

答案 0 :(得分:0)

问题可能在以下几行:

 b.x += delta * .1f; 

您正在使用浮点数并从整数中添加/减去它。 Java引擎可能会对float数据执行强制转换,将其转换为整数,这将导致数据丢失,并可能导致程序无响应。

此外,您可能希望将多次复制的过程代码移动到类中,以改进此程序的面向对象特性。