0

我正在尝试制作一个 java 沙子游戏,但一点也过不去。我已经制作了在 mouseX 和 mouseY 处绘制矩形的方法,并且我已经对其进行了设置,因此它会更新每一帧,因此它会不断地跟随鼠标。

我假设我会使用一个数组来创建每个矩形,然后从那里使用预定义的算法漂浮到地面上,我对此很满意,我只是不明白如何“钩住我的方法'到一个数组。

这是我用来绘制矩形的方法(在它自己的名为方法的类中)

import org.newdawn.slick.Graphics;

public class Methods {

public Graphics g = new Graphics();

public int sizeX = 4;
public int sizeY = 4;

public void drawParticle(float x, float y){
    g.drawRect(x, y, sizeX, sizeY);
}
}

这是我的主要课程

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

public class Control extends BasicGameState {
public static final int ID = 1;

public Methods m = new Methods();

int mouseX;
int mouseY;

public void init(GameContainer container, StateBasedGame game) throws SlickException{
}

public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException {
    m.drawParticle(mouseX, mouseY);
}

public void update(GameContainer container, StateBasedGame game, int delta) {
}

public void mouseReleased(int button, int x, int y){
    mouseX = 0;
    mouseY = 0;
}

public void mouseDragged(int oldx, int oldy, int newx, int newy) {
    mouseX = newx;
    mouseY = newy;
}

public int getID() {
    return ID;
}

}

但是当我点击时,只有一个矩形跟随鼠标,而不是在鼠标处创建很多:L

4

1 回答 1

0

公共变量:

Rectangle boxes[] = new Rectangle[maxnum];
int boxnum = 0;

鼠标移动时:

boxes[boxnum] = new Rectangle[e.getX(), e.getY(), sizeX, sizeY);
boxnum = boxnum + 1;

绘制粒子时:

counter = 0;
do
{
   g.drawRect(boxes[counter].x, boxes[counter].y, sizeX, sizeY);
   counter = counter + 1;
} while (counter < maxnum);

Where maxnum is the maximum number of boxes you will have. This way you can store multiple rectangles in your array and go through the array and draw them when you update the screen. Hope this helps.

于 2012-10-08T02:57:41.567 回答