|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
$ P! }0 E7 d& ipublic void spreadMoney(int money){
1 W7 A; F$ |- ^& |2 O& D // Randomly place money in moneySpace/ \- D" g' E& F' O/ V8 J& h6 b
for(int i = 0; i < money; i++){- x, u0 G& t, l
9 N8 a* Z8 p) i% U; o6 c* b. ?
// Choose coordinates7 E) j/ `1 {. @
int x = (int)(Math.random()*(moneySpace.getSizeX()));1 F! p3 d, U3 T. u& t3 }0 ^/ H
int y = (int)(Math.random()*(moneySpace.getSizeY()));
# s4 Z3 U+ ]4 Z( W
Z& L) T7 V4 g9 S0 h5 I6 G // Get the value of the object at those coordinates
L ~+ ?% q; {6 {+ p; X int I;
) z6 O+ u0 C+ g& Y% Z$ j0 y if(moneySpace.getObjectAt(x,y)!= null){9 {% |& | {+ k. N0 o2 G& m3 ^
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
% z: E* I4 x& n3 t+ i }
: H; V2 k2 J, l* Z& s else{$ l* m4 Z, d0 L
I = 0;9 s9 T" G- `- _8 c3 ]/ W: i
}9 C; b1 i. j9 h* v
// Replace the Integer object with another one with the new value# S8 r" _- i2 k4 E
moneySpace.putObjectAt(x,y,new Integer(I + 1));! [1 F" a# j0 i5 ]" g8 p2 q8 S
}
! o" U7 d9 _8 s" |2 f% k/ `5 s这里面if(moneySpace.getObjectAt(x,y)!= null){" a1 U! j& d6 U& \/ O5 f
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();# r8 W! W2 p; P- ~6 l( f4 X; G
}9 O6 e0 p+ @4 Z, R0 ~- F6 T! P
else{' e! i4 @, z9 b1 P
I = 0;
! x# m; e3 N2 k- ?. W4 q是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
* z* Q6 Y1 z4 n+ x$ ?" e* I初学者,还请见谅! |
|