弹跳矩形(图形 g)

Bouncing rectangle (Graphics g)

我正在研究 Java 中的图形。目前我有一个从左向右移动的矩形。我希望它在碰到 Canvas 的右侧时开始向左移动,当它碰到右侧时向左移动,我已经包含了一个游戏循环,因为这最终会变成我的第一个非常基本的游戏。谢谢

P.S - 我针对此代码的不同部分遵循了一些教程,因此它可能有点混乱,我正在研究它:)

主要Class:

public class Game extends JFrame implements Runnable {

    private Canvas canvas = new Canvas();
    private RenderHandler renderer;
    private boolean running = true;
    public static int WIDTH = 1200, HEIGHT = WIDTH / 12*9;
    public static int moveX =WIDTH/2;

    public Game() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(WIDTH, HEIGHT);
        setLocationRelativeTo(null);
        setLocationRelativeTo(null);
        add(canvas);
        setVisible(true);
        canvas.createBufferStrategy(3);
        renderer = new RenderHandler(getWidth(), getHeight());

    }


    public void update() {

    }


    public void render() {
            BufferStrategy bufferStrategy = canvas.getBufferStrategy();
            Graphics graphics = bufferStrategy.getDrawGraphics();
            super.paint(graphics);

            renderer.render(graphics);

            graphics.dispose();
            bufferStrategy.show();
    }

    public void run() {
        BufferStrategy bufferStrategy = canvas.getBufferStrategy();
        int FRAMES = 0;
        int TICKS = 0;
        long lastTime = System.nanoTime();
        double unprocessed = 0;
        double nsPerSecs = 1000000000 /60.0;
        long Timer = System.currentTimeMillis();
        while(running) {
            long now = System.nanoTime();
            unprocessed += (now - lastTime) / nsPerSecs;
            lastTime  = now;

            if(unprocessed >= 1) {
                TICKS ++;
                update();
                unprocessed -= 1;
            } 
            try 
            {
                Thread.sleep(3); 
            }catch (InterruptedException e) {
                e.printStackTrace();
            }
            FRAMES++;
            render();

            if(System.currentTimeMillis() - Timer > 1000) {
                System.out.println("Ticks: " + TICKS + " FPS: " + FRAMES);
                TICKS = 0;
                FRAMES = 0;
                Timer += 1000;
            }
        }
    }
    public static void main(String[] args) {
        Game game = new Game();
        Thread gameThread = new Thread(game);
        gameThread.start();
    }

}

Class绘制图形:

public class RenderHandler {
public RenderHandler(int width, int height) {

}

public void render(Graphics g) {
    g.setColor(Color.BLACK);
    g.fillRect(0, 0, Game.WIDTH, Game.HEIGHT);
    g.setColor(Color.RED);
    g.fillRect(Game.moveX, Game.HEIGHT/2, 50, 50);
    if (Game.moveX >= Game.WIDTH) {
        Game.moveX ++;
    } else if (Game.moveX <= 0) {
        Game.moveX --;
    }else { Game.moveX++;
    }
    }
}

将此添加到渲染处理程序而不是渲染中的当前 if 语句

bool rol = true; // initialize this outside the method


If(Game.movex + 50 >= Game.width)
rol = false;
Else if(Game.movex <= 0)
rol = true;
If(rol)
Game.movex++;
Else
Game.movex--;

您需要在某处存储当前的移动方向,因此将其添加到游戏 class:

public static int deltaX = 1;

并将 render() 中的条件替换为

if (Game.moveX >= Game.WIDTH-50) {
    Game.deltaX =-1;
} else if (Game.moveX <= 0) {
    Game.deltaX =1;
}
Game.moveX += Game.deltaX;

如果您知道如何在屏幕上绘图以及这些东西是如何工作的,我认为这更多的是关于简化逻辑。

我从你的问题中粗暴地撕下的这个代码片段就在渲染发生的地方旁边(这是一个问题,因为我认为它相当杂乱无章;我建议游戏逻辑和渲染在两个不同的函数中发生) .基本上是说如果超出屏幕右边就向右移动,如果不超出屏幕左边就向左移动,最后,如果不超出屏幕就向左移动。

if (Game.moveX >= Game.WIDTH) {
        Game.moveX ++;
    } else if (Game.moveX <= 0) {
        Game.moveX --;
    }else { Game.moveX++;
    }

如果你想让它弹跳,你必须使用布尔值来跟踪它的移动状态,或者,如果你想要更多功能,使用一对浮点数或双精度数(浮点数通常用于 Java 游戏设计)跟踪它的位置,另一个跟踪它的速度。我现在手头紧,我会 return.