吃豆子Java运动问题 [英] Pacman java movement issues

查看:103
本文介绍了吃豆子Java运动问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在进行的吃豆子运动遇到了一些小问题.

I have faced a little problem with my pacman movement that i'm working on right now.

我似乎找不到一种使这种运动类似于起源的吃豆人运动的方法.为了解释我希望它如何移动,我上传了这张照片.

I can't seem to find a way to make the movement similar to the originated pacman movement. In order to explain how i want it to move, i have uploaded this picture.

现在,我使黄色矩形和蓝色墙壁之间发生碰撞.问题是,当我如图所示向左移动并单击向上箭头时,它不应停止,而是继续移动,然后在有可用空间时向上移动.

As it stand now, i have made the collision working between the yellow rectangle and the blue walls. The problem is when i move to the left as in the picture and i click the up arrow, it should not stop, but continue to move and then go up when there is a free space.

现在,如果您按如下所示单击向上箭头,则黄色矩形将停止:

Right now, the yellow rectangle will stop if you click on the up arrow like this:

上传了我的pacman类,其中包含我的动作,以及检测到碰撞时来自我的木板类的代码.

Have uploaded my pacman class which contains my movement, and code from my board class when collision is detected.

pacman类

public class Pacman {

private String pacmanup = "pacmanup.png";
private String pacmandown = "pacmandown.png";
private String pacmanleft = "pacmanleft.png";
private String pacmanright = "pacmanright.png";

private int dx;
private int dy;
private int x;
private int y;
private int width;
private int height;
private boolean visible;

private Image imageup;
private Image imagedown;
private Image imageleft;
private Image imageright;

public Pacman() {

    Thread thread = new Thread();

    thread.start();

    ImageIcon i1 = new ImageIcon(this.getClass().getResource(pacmanup));
    imageup = i1.getImage();

    ImageIcon i2 = new ImageIcon(this.getClass().getResource(pacmandown));
    imagedown = i2.getImage();

    ImageIcon i3 = new ImageIcon(this.getClass().getResource(pacmanleft));
    imageleft = i3.getImage();

    ImageIcon i4 = new ImageIcon(this.getClass().getResource(pacmanright));
    imageright = i4.getImage();

    width = imageup.getWidth(null);
    height = imageup.getHeight(null);
    visible = true;
    x = 27;
    y = 27;

}

public int getDx() {
    return dx;
}

public void setDx(int dx) {
    this.dx = dx;
}

public int getDy() {
    return dy;
}

public void setDy(int dy) {
    this.dy = dy;
}

public int getX() {
    return x;
}

public int getY() {
    return y;
}

public void setX(int x) {
    this.x = x;
}

public void setY(int y) {
    this.y = y;
}

public Image getImageup() {
    return imageup;
}

public Image getImagedown() {
    return imagedown;
}

public Image getImageleft() {
    return imageleft;
}

public Image getImageright() {
    return imageright;
}

public void setVisible(boolean visible) {
    this.visible = visible;
}

public boolean isVisible() {
    return visible;
}

public Rectangle getBounds() {
    return new Rectangle(x, y, width, height);
}

public Rectangle getOffsetBounds() {
    return new Rectangle(x + dx, y + dy, width, height);
}

public void move() {

    x += dx;
    y += dy;
}

public void keyPressed(KeyEvent e) {

    int key = e.getKeyCode();

    if (key == KeyEvent.VK_LEFT) {
        dx = -1;
        dy = 0;
    }

    if (key == KeyEvent.VK_RIGHT) {
        dx = 1;
        dy = 0;
    }

    if (key == KeyEvent.VK_UP) {
        dx = 0;
        dy = -1;
    }

    if (key == KeyEvent.VK_DOWN) {
        dx = 0;
        dy = 1; 
    }
}

public void keyReleased(KeyEvent e) {

    int key = e.getKeyCode();

    if (key == KeyEvent.VK_LEFT) {

    }

    if (key == KeyEvent.VK_RIGHT) {

    }

    if (key == KeyEvent.VK_UP) {

    }

    if (key == KeyEvent.VK_DOWN) {

    }


}

董事会课程中的代码

Rectangle r5 = pacman.getOffsetBounds();

    for (int j = 0; j<barriers.size(); j++) {
        Barrier b = (Barrier) barriers.get(j);
        Rectangle r4 = b.getBounds();

        if (r5.intersects(r4)) {

            pacman.setDx(0);
            pacman.setDy(0);
            break;

        }

    }

    pacman.move();

}

在我的板子类中创建了一个名为direction的字符串,该字符串检测黄色矩形的移动方向.

Have made an String called direction inside my board class which detects what direction the yellow rectangle are moving.

左",上",右"或下"

"left" "up" "right" or "down"

已经尝试编写一些代码,但是没有用.这是我尝试过的:

Have already tried to write some code, but not working. Here is what i tried:

      // moving to the left and tried go up
            else if (pacman.getDx() == 0 && pacman.getDy() == -1 && direction.equals("left")) {
                pacman.setDy(0);
                pacman.setDx(-1);
                break;

好吧,当它向左移动并单击向上时,它将继续向左移动,但是它不会像它应该做的那样在可用空间上上升:)

Well, when it moves to the left and you click up, it will continue to move left, but it wont go up on the free space as it should do :)

键盘键/按下键有问题吗?也许按下的键没有激活,或者当您单击时发生了什么?

Is it something with the keydapter / pressed key? maybe the pressed up key is not activated or something when u click up?

推荐答案

我的看法是,这里有两个问题.

(问题1)

如果要让pacman继续移动,请不断更改dx or dy的值,直到按下另一个键.无论如何,这就是经典的吃豆人的工作方式.它始终不停移动/尝试以保持朝最后一个keypressed的方向移动. (您可以在此处亲自查看实际的游戏机制- https://www.google .com/doodles/30th-anniversary-pac-man )

If you want pacman to keep moving, keep changing the dx or dy values until another key is pressed. That's how the classic pacman worked anyway. It always kept moving/tried to keep moving in the direction of the last keypressed. (See the game mechanic in action here yourself - https://www.google.com/doodles/30th-anniversary-of-pac-man)

(问题2)

如果您不打算实现经典的pacman(从您尝试执行的操作来看,看起来就好像不是在实现经典的pacman),并且如果您想更改控件的工作方式,例如根据按键历史智能切换方向,请考虑存储按键.问题是您要跟踪多少个按键方向?

If you're not trying to implement classic pacman (from what you're trying to do, it don't look like you're implementing classic pacman), and if you want to change the way the controls worked slightly, like intelligently switch direction based on history of key presses, consider storing the keypresses. The question is how many keypress directions do you want to track ?

假设您只想跟踪两个按键,则需要有一个大小为2的array,该大小在任何时候仅存储两对key:value.如果您可以沿方向(即键将为方向,而将为. > truefalse).

Assuming you only want to track two keypresses, you'd need to have an array of size two that will only store two key:value pairs at any time. The key will be direction and value will be if you can move in that direction of not (i.e boolean true or false).

以下是使用此array解决问题的方法:

Here is how you could use this array to solve your problem:

当按下方向键时,将其设置为array[0].key,然后检查是否可以朝该方向移动.如果可以,请将array[0].value的值设置为true,然后使用此值查看是否需要通过检查array[0].keyarray[0].value来更改其他函数中的dxdy变量的值.状态以决定您是否可以朝那个方向移动.实际move功能的详细信息将在后面说明.您已经实现了基本的move方法,所以很好,您只需添加一些条件并对其进行一些修改.

When a direction key is pressed set it into the array[0].key and check if you can move in that direction. if you can, set the value of array[0].value to true and use this value to see if you need to change the value of dx or dy variable in another function by checking the array[0].key and array[0].value state to decide if you can move in that direction or not. Details of the actual move function will be explained later. You already have basic the move method implemented, so that's good, you'll just need to add some conditions and modify it a bit.

现在,当按下第二个键(如up key)时,请执行以下操作:

Now, when second key (like up key) is pressed do the following:

  1. 设置temp = array[0]
  2. 设置array[0].key = 'up'
  3. set array[1].value =检查是否可以朝array[0].key方向移动,如果不能则设置为false.
  4. 设置array[1] = temp并检查array[1].valuetrue还是false
  1. set temp = array[0]
  2. set array[0].key = 'up'
  3. set array[1].value = check if you can move in the direction of array[0].key and set true if you can set false if you cannot.
  4. set array[1] = temp and check if array[1].value is true or false

请注意,您已将最早的按键操作移至array[1],最新的按键操作移至了array[0].

Notice that, you have moved the oldest keypressed action to array[1] and latest keypressed to array[0].

在这一点上,设置一个约定,您将总是首先尝试向array[0]方向移动,因为它存储了所按下的最新按键的方向值,然后才尝试向array[1]方向移动.

At this point, set a convention that you'll always try to move in the direction of array[0] first because it stores direction value of the latest key pressed, and only then you'll try to move in array[1].

在通过将它们的布尔值设置为array[0].valuearray[1].value来检查是否可以朝着array[0]array[1]的方向移动之后,您现在知道了可以移动的位置并准备好实际移动:

After checking if you can move in direction of array[0] and array[1] by setting their boolean values to array[0].value and array[1].value, you now know where you can move and are ready to move actually:

因此,在您的move方法中,执行以下操作,然后if(array[0].value==true):

So in your move method, do the following, if(array[0].value==true) then:

  1. array[0]方向移动并忽略array[1](即,如果您使用循环实现了移动逻辑,则会中断)
  1. Move in the direction of array[0] and ignore array[1] (i.e break in case you implement the move logic using a loop)

else if(array[1].value == true):

  1. array[1]

一旦您的move()函数返回,并且您使用的是新的(x,y),请返回到第一步,然后重新进行遍历.

Once your move() function returns, and you're at the new (x,y) go back to step one and do it all over again.

希望您需要实施的步骤很明确.

Hope the steps you'll need to implement are clear.

这篇关于吃豆子Java运动问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆