在这里,我为你做了一个小测试!它不是一个完全实现的物理引擎,但我想它已经足够接近了......
int numberOdominos = 60;
Domino [] myDominos = new Domino[numberOdominos];
int currentDropDomino = -1;
void setup() {
size(600, 100);
myDominos[0] = new Domino(null);
for (int i = 1; i < myDominos.length; i++) {
myDominos[i] = new Domino(myDominos[i-1]);
}
}
void draw() {
background(0);
String s = getDominoState();
fill(255,0,0);
text("press any button from 0 to " + myDominos.length + " to drop that domino and all subsequent ones!!!! (r to reset)",20,10,width,height);
fill(255);
text(s, 5, 40, width, 100);
}
String getDominoState() {
String result = "";
for (int i = 1; i < myDominos.length; i++) {
if(currentDropDomino != -1 && i == currentDropDomino) {
myDominos[i].push();
}
result += myDominos[i].state;
}
if(currentDropDomino != -1) currentDropDomino += 1;
if(currentDropDomino > myDominos.length)
currentDropDomino = -1;
return result;
}
class Domino {
Domino previous;
char state = '|';
Domino(Domino previous) {
this.previous = previous;
}
void reset() {
state = '|';
}
char checkPrevious() {
if (previous.state == '/') push();
return state;
}
void push() {
state = '/';
}
}
void reset() {
for(int i = 0; i < myDominos.length; i++)
myDominos[i].reset();
}
void keyPressed() {
if(key == 'r' || key == 'R') {
reset();
return;
}
int pushDomino = 0;
try {
pushDomino = Integer.parseInt(""+key);
}
catch(NumberFormatException e) {
println("thats not a number...");
return;
}
if(pushDomino < myDominos.length) currentDropDomino = pushDomino;
}
您只需要使用您选择的一种来更改 keyPressed() 方法;就像是throwProjectileAt(int position) {}