问题描述
我正在尝试制作一个java沙子游戏并且无法超越一点。我已经制作了我的方法,在mouseX和mouseY上绘制一个矩形,并且我已将其设置为每帧更新所以它不断跟随鼠标。
I'm trying to make a java sand game and can't get past one bit. i've made my method that draws a rectangle at mouseX and mouseY, and i have set it up so it updates every frame so it constantly follows the mouse.
我是什么假设是我会使用一个数组来创建每个矩形,并从那里使用预定义的算法浮动到地面,我都很好,我只是不明白如何勾住我的方法直到数组。
what i assume is that i would use an array to create each rectangle, and from there would use a pre-defined algorithm to float to the ground, I'm all good with that, i just don't understand how to 'hook my method' up to an array.
这是我用来绘制矩形的方法(在它自己的类中称为方法)
This is the method i am using to draw the rectangle (in it's own class called Methods)
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
but when i click, just one rectangle follows the mouse, instead of many being created AT the mouse :L
推荐答案
公共变量:
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);
其中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.
这篇关于绘制多个像素/矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!