|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
. ]4 d% |( X' w# @5 v, tpublic void spreadMoney(int money){, l2 C- r- t. q
// Randomly place money in moneySpace
; _) l E2 x1 b4 \ for(int i = 0; i < money; i++){
8 _5 d1 C$ M% J- R( H* X' s" d' a5 i% s+ o8 a
// Choose coordinates: x" B2 A$ h& J+ _2 m" b7 }
int x = (int)(Math.random()*(moneySpace.getSizeX()));" U, g* k) X& p6 \
int y = (int)(Math.random()*(moneySpace.getSizeY()));
[& `8 T& a) H( S8 X
* h. ?2 t( b% d) c% _ // Get the value of the object at those coordinates# A8 J, w3 \3 _; B3 @
int I;. R2 P- D/ Z/ W3 J
if(moneySpace.getObjectAt(x,y)!= null){- Y: }% a3 U: \' t8 K
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
/ _6 c6 a+ s4 D+ { }6 z0 E) a q4 U' _; I
else{
# q& F& W2 Q+ l# G0 W7 c I = 0;/ e) o) U" q7 C4 ]1 v
}
1 ~4 N' E/ j7 d) Z) G6 T( [ // Replace the Integer object with another one with the new value+ K+ Z1 l* M/ E! q6 C
moneySpace.putObjectAt(x,y,new Integer(I + 1));
" O5 q& `2 p8 R1 P7 k0 q+ l' b }" O0 l& j* T8 ?# T2 g! x0 `( ~
这里面if(moneySpace.getObjectAt(x,y)!= null){( ~) v2 K' q1 a; N D4 Y
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
0 i3 r3 Y9 T9 J; Z" y1 K; N# w }! }# z6 b; q) j+ F+ }
else{
9 e( b: x6 S& n- \ I = 0;
; W3 U" K% k! v' e/ [! C: d是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
0 J! c, C2 a" X i初学者,还请见谅! |
|