|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
' r- G5 a! P$ w* H; C: g# spublic void spreadMoney(int money){
% x. K6 \) k3 i) }. s // Randomly place money in moneySpace- v3 X; X9 C8 L9 f; i, h5 {
for(int i = 0; i < money; i++){+ K5 H0 @! @+ I' [ s
# w' p! n+ h1 Z. @6 u# \
// Choose coordinates4 j! k0 z: I0 i8 ^/ T) j6 i
int x = (int)(Math.random()*(moneySpace.getSizeX()));
# ~( z, f5 j" j `; e6 a+ W# H8 A int y = (int)(Math.random()*(moneySpace.getSizeY()));
6 J( Y- l% ~ F0 Z2 e3 i( e: @; E8 n0 l7 O4 L0 N( c& \4 G: F
// Get the value of the object at those coordinates8 b, o+ |# [( P( k
int I;
5 l( C. C6 R/ T" [. z' h9 i$ L if(moneySpace.getObjectAt(x,y)!= null){1 o5 i, k" c& d7 w$ J* d
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
( h G! M4 l- g6 s$ ~ }
% I1 P! s- A V: [4 ]+ d7 s else{
+ v( X) e* E) S! m+ _3 c1 y I = 0;
7 @# A/ t6 v; K% W/ { }
9 a7 u, X! M3 X: b- j& ? // Replace the Integer object with another one with the new value
8 w0 c6 W8 N5 c" Q moneySpace.putObjectAt(x,y,new Integer(I + 1));
7 G- c9 k* m; V# ~8 g" m7 A }9 I8 B0 e) n6 M: ^# q! F
这里面if(moneySpace.getObjectAt(x,y)!= null){* h1 i& y% ?* r, `$ I& J4 f& @+ L
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();* X3 \2 ^# Q% |2 ?0 o* F2 W2 \0 P
}
* i2 s4 U" @' z' j. M( S/ l6 N else{, d" P1 T% z) Y. O, g
I = 0;
& j) K; W- S9 Z+ q1 w9 I' j" ^; {是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?& f; f) p% @$ f5 I _% G
初学者,还请见谅! |
|