|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中" B9 O# |+ }7 g' D. O/ _
public void spreadMoney(int money){# v t- ~+ W1 B9 x0 h7 L, _8 O- k
// Randomly place money in moneySpace
9 ]; I, c: S# c/ M/ N! m: A for(int i = 0; i < money; i++){( d; z! T0 z: ~/ _+ W1 n
) z# _. L7 L% V* j3 u3 a5 K6 B
// Choose coordinates8 {' e: C2 A$ r/ c* ^, e! ~
int x = (int)(Math.random()*(moneySpace.getSizeX()));* A" J2 H2 H( `1 s+ |9 k
int y = (int)(Math.random()*(moneySpace.getSizeY()));! s: k' a _) u
0 I2 {4 |7 }6 W9 h. @. l // Get the value of the object at those coordinates( W4 l7 ^& V! ?3 Z& D2 v9 }
int I;- |9 p( Q( w. f, t: C4 S
if(moneySpace.getObjectAt(x,y)!= null){
% Q8 q$ |1 Y [! N B I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
# H2 C* k ^) M1 D" Z# ^ }
T6 z8 n% o. l! B' g else{% l. z+ E! q7 V3 ^% L4 c" I9 G
I = 0;8 s( Y! q# l- Y J
}
# V/ O3 h% _. p4 _. l2 k7 U1 d // Replace the Integer object with another one with the new value5 ^) l. K" h* p k9 l
moneySpace.putObjectAt(x,y,new Integer(I + 1));+ @# O6 {7 {4 z
}8 x' E0 s9 |; V0 \
这里面if(moneySpace.getObjectAt(x,y)!= null){
& Z2 ^* Q' _+ I! C+ c; C: A- ^ I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();. P) V& R/ C' S+ U; @
}
# e$ l9 J1 c6 M* E else{. |7 W# I+ @0 U: x( S
I = 0;. x' o; Q- a; `: v- i7 P/ {" k u |8 K
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
* Y ]/ v' v5 i初学者,还请见谅! |
|