|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中& c; t- J: ]0 m+ R. }% b
public void spreadMoney(int money){
6 _3 u# B% T. M. U, ^ // Randomly place money in moneySpace
7 T: F7 d( \9 w3 o* J4 f2 L Q3 p for(int i = 0; i < money; i++){
$ D; ]1 e. H+ N5 O' i( v4 z
8 X0 @/ L3 s! X4 g- d1 t. R // Choose coordinates
, w" ?1 f5 `4 q/ c, I" a) ~) G int x = (int)(Math.random()*(moneySpace.getSizeX()));
$ c- w; |3 n# l* X4 s9 e6 P int y = (int)(Math.random()*(moneySpace.getSizeY()));
& o* T/ M- ?/ S) A! N1 `
}) [) I4 {( q/ S; e- ? // Get the value of the object at those coordinates U1 q% T5 |7 B! k/ @+ L
int I;
: D" A3 ?: k: m( w( | if(moneySpace.getObjectAt(x,y)!= null){
+ N0 n. \- z6 z* `0 K+ V9 [ I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();# c S3 j+ x: j/ m: ]
}" z' v* k4 A- @, z9 W: D1 e
else{. u9 w& ^1 b+ q' b6 n7 g
I = 0;
0 m9 d* e2 W3 R8 k4 W" {$ h }
+ l9 H# B s! U+ g A% Q S" N // Replace the Integer object with another one with the new value
2 q/ l9 A+ r* y; R2 R: } moneySpace.putObjectAt(x,y,new Integer(I + 1));- {+ q% I2 g$ W4 a3 k+ B: N
}" m4 U- s. O, ]/ y
这里面if(moneySpace.getObjectAt(x,y)!= null){/ I3 _# c: y5 X" z7 p+ f
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();# f/ f+ x9 C. R) x4 Q/ @% S9 i; [
}
6 X3 [9 ]+ b0 ~0 B2 B& k else{) i% Q* @4 A ^* l8 N( C
I = 0;
, k4 x Q+ p& x s7 `# D是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
, C" X7 T2 R' X3 q `9 h" U6 w初学者,还请见谅! |
|