|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中; r/ r7 K( |0 ?# J0 }( O
public void spreadMoney(int money){
# p1 B$ C9 r2 N4 @* V3 a! a // Randomly place money in moneySpace2 R0 N, n- K: P6 I
for(int i = 0; i < money; i++){. _6 y }$ I. H+ N
6 t2 T: _* v3 O9 G // Choose coordinates9 N' y2 ~' B; G0 S
int x = (int)(Math.random()*(moneySpace.getSizeX()));1 V8 O/ j+ p" W8 q. l8 i9 {2 ~
int y = (int)(Math.random()*(moneySpace.getSizeY()));6 t! w x$ g# }$ q+ t$ c
) S3 E+ m6 e2 H) z( W( a // Get the value of the object at those coordinates2 D/ @& K/ \* `( j7 P( S
int I;5 p2 K z. ^. U4 T
if(moneySpace.getObjectAt(x,y)!= null){/ S* P; @% C9 _8 P* L0 S8 u
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();7 w& b" a- n1 R9 Y& C0 j! q
}
0 V3 A9 w9 f4 Y/ B8 T9 }& }9 W- s& @ else{4 ?8 o, S! I% e3 ~
I = 0;
* M$ r0 p6 B2 ?; \1 u" b( F- ^ }0 I' A! f4 C0 P' |& U9 d
// Replace the Integer object with another one with the new value o. k, a4 U# {* s/ J
moneySpace.putObjectAt(x,y,new Integer(I + 1));
* {4 r2 ]' |% K- K }6 ?1 O/ N0 A! Y$ C5 m( |
这里面if(moneySpace.getObjectAt(x,y)!= null){
8 N7 |# Y) i- [+ e: r I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
" D' v3 q$ n/ E- | }
: M7 g$ a2 q0 O5 u& f. }# q6 A! a else{
3 G+ N! d+ }! z! ^3 n, s I = 0;( w' m0 G& X1 k8 ^& N
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?. c' T/ \9 O. W) l% `& W
初学者,还请见谅! |
|