【问题标题】:How to make the Graphics move ONLY inside the JFrame and not outside the bounds of the JFrame?如何使图形仅在 JFrame 内移动而不在 JFrame 范围之外移动?
【发布时间】:2021-10-27 07:01:21
【问题描述】:

我目前正在开发一个Catch the Falling Object 类型的游戏,但现在的问题是玩家(也就是抓住物体的人)。当我通过左侧移动并击中窗口的边界时,它会将玩家发送到x - 1,但是当我转到右侧时(即使我将代码设置为if (x > windowBoundRight){ x = x - 1 },尽管它仍在向外移动窗口边界。谁能帮助我?(注意:顺便说一句,没有错误)

这是代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Game extends JPanel implements ActionListener, KeyListener {

    public int x = 0, y = 0, velx = 0, vely = 0;
    public boolean hitAnObject = false;
    public Color c = Color.BLACK;
    Timer timer = new Timer(5, this);


    public Game() {
        timer.start();
        setBackground(Color.CYAN);
        addKeyListener(this);
        setFocusable(true);
        setFocusTraversalKeysEnabled(false);
    }

    @Override
    public void paint(Graphics g){
        super.paint(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.fillRect(x, y, 50, 50);
        g.drawString("X = " + x + "Y = " + y, 0, 0);
    }

    public void actionPerformed(ActionEvent ae) {
        repaint();
        x += velx;
        y += vely;
    }

    public void left() {
        velx = -5;
        vely = vely + 0;
    }

    public void right() {
        velx = 5;
        vely = vely + 0;
    }

    public void up() {
        vely = -5;
        velx = velx + 0;
    }

    public void down() {
        vely = 5;
        velx = velx + 0;
    }
    public void stopMovement_UP() {
        vely = 0;
        velx = velx + 0;
    }
    public void stopMovement_DOWN() {
        vely = 0;
        velx = velx + 0;
    }
    public void stopMovement_LEFT() {
        velx = 0;
        vely = vely + 0;
    }
    public void stopMovement_RIGHT() {
        velx = 0;
        vely = vely + 0;
    }

    @Override
    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();
        if (keyCode == KeyEvent.VK_LEFT || keyCode == KeyEvent.VK_A) {
            left();
            if (x < 0) {
                x = 0;
            }
        }
        else if (keyCode == KeyEvent.VK_RIGHT || keyCode == KeyEvent.VK_D) {
            right();
            //checck if the graphic aka player moves out of the boundary
        }
        else if (keyCode == KeyEvent.VK_DOWN || keyCode == KeyEvent.VK_S) {
            down();
        }
        else if (keyCode == KeyEvent.VK_UP || keyCode == KeyEvent.VK_W) {
            up();
            if (y < 0) {
                y = 0;
            }
        }
    }

    @Override
    public void keyTyped(KeyEvent e) { }

    @Override
    public void keyReleased(KeyEvent e) { 
        int keyCode = e.getKeyCode();
        if (keyCode == KeyEvent.VK_LEFT || keyCode == KeyEvent.VK_A) {
            stopMovement_LEFT();
            if (x < 0) {
                x = 0;
            }
        }
        else if (keyCode == KeyEvent.VK_RIGHT || keyCode == KeyEvent.VK_D) {
            stopMovement_RIGHT();
            if (x > 436) {
                x = x + 2;
            }
        }
        else if (keyCode == KeyEvent.VK_DOWN || keyCode == KeyEvent.VK_S) {
            stopMovement_DOWN();
        }
        else if (keyCode == KeyEvent.VK_UP || keyCode == KeyEvent.VK_W) {
            stopMovement_UP();
            if (y < 0) {
                y = 0;
            }
        }
        
    }

    public static void main(String[] args) {
        Game game = new Game();
        game.getBackground();
    }

}

【问题讨论】:

  • 另外这个脚本只是动作我还没做掉物体的脚本
  • 但是如何检查它是否在游戏面板中?

标签: java swing game-development


【解决方案1】:

在方法 actionPerformed 中,您正在更改 xy 的值,但您没有检查计算值是否在 Game 面板的范围内.

在方法paint(在类Game)中,xy 坐标在面板的坐标空间内,即GameGame 左边缘的 x 坐标为 0(零)。右边缘的 x 坐标是Game 的宽度,您可以通过方法getWidth 检索。

同样,上边缘(Game)的 y 坐标为零,而下边缘的 y 坐标为Game 的高度,您可以通过方法getHeight 检索。

因此,您将变量xy(在方法actionPerformed)的计算值与(Game)的宽度和高度进行比较,以查看它们是否超出Game 的范围。

public void actionPerformed(ActionEvent ae) {
    x += velx;
    if (x > getWidth() || x < 0) {
        // 'x' is outside of the bounds
    }
    y += vely;
    if (y > getHeight() || y < 0) {
        // 'y' is outside of the bounds
    }
    repaint();
}

我认为您应该在计算xy 的新值之后调用方法repaint (而不是之前,如您问题中的代码所示)。

我不知道您希望玩家在碰到(Game 的)边缘之一后如何移动,所以我只在上述代码中的if 条件中写了一条注释。

如果您还没有这样做,我建议您阅读 Oracle Java 教程的使用 Swing 创建 GUI 中的 Performing Custom Painting 课程。

【讨论】:

  • 在 cmets // 'x' is outside of bounds 我只需要做 x = x - 1?
  • @Athran123_ 如果您希望玩家在击中右边缘后向相反方向移动,那么是的,您可以使用x--。当玩家击中左边缘时,你会做x++
猜你喜欢
  • 2015-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多