|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
) Y9 X5 C0 V6 P0 Npublic void spreadMoney(int money){
4 H6 Y. e+ c" e# Q( I- s* ]2 Q$ } // Randomly place money in moneySpace; \% f9 L0 p- p$ G q
for(int i = 0; i < money; i++){
# r& m. N6 @2 d6 D. o4 d2 y/ C: u; J# W1 s1 Y
// Choose coordinates
$ y/ V% l! m( Q int x = (int)(Math.random()*(moneySpace.getSizeX()));1 R9 G/ R9 j; r9 J
int y = (int)(Math.random()*(moneySpace.getSizeY()));- K& D" k+ ^' a7 ?4 m" B8 ~
) I& w( f/ n6 }
// Get the value of the object at those coordinates
, L$ F4 J, n7 C" Z% H. M int I;
) r' g* {) T* ]8 H2 s" u& O if(moneySpace.getObjectAt(x,y)!= null){6 {& z! ], T4 q% v- E3 P/ g
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
4 x+ n3 {5 k. I, ^! { }
( R& x& c3 J5 l2 y! f else{* j. S/ U1 ~& x" g+ v( L; R
I = 0;
9 |, e0 I* j! C7 l }
9 L% e9 s! q- Z) E // Replace the Integer object with another one with the new value
8 ~2 Y. c; F1 Q) K( D% u: c moneySpace.putObjectAt(x,y,new Integer(I + 1));! c. k) w' |1 ]
}
8 y! f! C; q: n" t/ L4 P" H这里面if(moneySpace.getObjectAt(x,y)!= null){& `4 B; [0 R, `! ?0 C) E
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();! X* Z4 r+ e" l- S: M6 E l
}0 h- G& v$ A& _3 a! E3 V
else{- ~- A" Z" w2 L+ T; ~
I = 0;
. h5 e) a# _! L3 Q O( o4 n: Q9 y, K是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?" N; f. i' V( F2 L( C
初学者,还请见谅! |
|