|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
/ e n' T6 @% u" Fpublic void spreadMoney(int money){5 Y' \, x# }$ ~1 j0 G' ~* y8 ^
// Randomly place money in moneySpace
# @. Y& |5 {3 ^; G for(int i = 0; i < money; i++){$ v9 `- E) M5 H( C% ^
/ y8 M8 R" K, g! y: w' i4 n, {7 r0 K
// Choose coordinates0 t# ?# J1 e8 a9 F
int x = (int)(Math.random()*(moneySpace.getSizeX()));$ n" A: H- ]7 h. ^
int y = (int)(Math.random()*(moneySpace.getSizeY()));
$ Z# k/ p+ V* k6 _" x: Y% r
& f5 I6 o1 o; B // Get the value of the object at those coordinates" n: G1 w1 ~4 b! v8 P7 c4 a6 T
int I; P; m' Z& A/ K6 l( b1 W
if(moneySpace.getObjectAt(x,y)!= null){
: w* e& U, i, R9 _% P3 J4 m I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();2 @6 ~- R1 b7 L" M0 f, ?: H
}; R! j. G0 L2 V9 K9 W0 w
else{
" }9 G& Y+ [5 a' p1 O I = 0;1 k. R3 U) ~5 v+ Y* N; d
}" W4 x" Q- i/ ? M, g6 Q
// Replace the Integer object with another one with the new value. w- m2 J8 W* v7 U' a; y
moneySpace.putObjectAt(x,y,new Integer(I + 1));, _; n1 e! P2 [
}& @+ l0 b' l, n* t7 {6 C
这里面if(moneySpace.getObjectAt(x,y)!= null){6 {' j' W) ~$ [, G
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
; k( w; r& H* v6 d+ p }
3 R" P- o# P: x! ~/ ]) C% @' d else{# j3 T2 d1 x) w3 j
I = 0;% O& f7 p" d6 z* K$ z0 [
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
5 b$ R' [9 Q3 P$ h; u* x, j9 f3 a初学者,还请见谅! |
|