gpt4 book ai didi

java - 矩形无法正常移动

转载 作者:行者123 更新时间:2023-12-01 13:28:57 24 4
gpt4 key购买 nike

我正在与 slick 合作制作一些基本游戏。目前我正在玩弄矩形,但我似乎无法通过输入来控制矩形。基本上我使用 x、y 参数,然后使用 size 来绘制其余部分。当我使用“D”键时,我希望矩形向右平移,但它的尺寸却增加了

if(d.equals(Direction.RIGHT))
{
boundingBox.setX(boundingBox.getX() + 1);
boundingBox.setWidth(boundingBox.getWidth() + 1);
}

然后结果是

My code :

package com.mra;

import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.geom.Rectangle;

public class Block
{

public enum Direction
{
LEFT,
RIGHT,
UP,
DOWN;
}

float size;
Color color;
Rectangle boundingBox;

public Block(int x, int y, float size, Color color)
{
this.size = size;
this.color = color;
boundingBox = new Rectangle(x, y, x + size, y + size);
}

public float getSize()
{
return size;
}

public Color getColor()
{
return color;
}

public void render(Graphics g)
{
g.drawRect(boundingBox.getX(), boundingBox.getY(), boundingBox.getWidth(), boundingBox.getHeight());
}

public void inputMove(Input input, int KEY, Direction d)
{
if(input.isKeyDown(KEY))
{
if(d.equals(Direction.LEFT))
{

}
if(d.equals(Direction.RIGHT))
{
boundingBox.setX(boundingBox.getX() + 1);
boundingBox.setWidth(boundingBox.getWidth() + 1);
}
if(d.equals(Direction.UP))
{

}
if(d.equals(Direction.DOWN))
{

}
}
}

}

最佳答案

您正在更改边界框的宽度。

如果您只想更改位置,只需更改 X 和 Y,无需更改其他内容。

if(d.equals(Direction.RIGHT))
{
boundingBox.setX(boundingBox.getX() + 1);
}

关于java - 矩形无法正常移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21663472/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com