|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
; f; X* Z3 [7 w& C# l) S" gpublic void spreadMoney(int money){
7 N& o( e8 [' U4 R. z0 ~ // Randomly place money in moneySpace
% W1 r0 g, K1 Q" G6 f for(int i = 0; i < money; i++){ m9 t$ r* B! n7 y& {
' H* N, g8 V$ f8 ~ N$ F$ m
// Choose coordinates6 i) M4 j$ K6 U7 ~
int x = (int)(Math.random()*(moneySpace.getSizeX()));
9 R* g: M: [! F: H5 {& a int y = (int)(Math.random()*(moneySpace.getSizeY()));
2 M% \; x8 k1 B6 F6 {7 J4 P5 n1 O' Z9 F/ p4 I8 g: U
// Get the value of the object at those coordinates
6 `" ]4 M! Z4 w& h6 d+ k int I;& z; X- [9 {* M$ J7 N* W
if(moneySpace.getObjectAt(x,y)!= null){
% n7 j3 J0 s" s7 a+ T# C- L I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();* l+ `3 P- V% j' L/ k* j
}4 _* M( H; Q$ c2 s
else{* b% F! q; D( q' `4 _
I = 0;4 U4 x6 m) P+ S+ ?0 c5 L4 O
}
; E2 k6 d: i7 P# G! M // Replace the Integer object with another one with the new value
( F4 ]6 W) m# } moneySpace.putObjectAt(x,y,new Integer(I + 1));2 q5 d) O3 l/ Z- b: _9 ]
}
u* Y/ d* E" \# M. C% D这里面if(moneySpace.getObjectAt(x,y)!= null){- L! [ \. x4 _+ {* G$ j2 w
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();$ q7 Y, w& r- V4 c
}
, p6 X( B' Z7 K( b$ ~ else{7 J2 s2 C$ n# g7 ]' j9 I
I = 0;
& M% z# e4 v; |& {8 q4 p是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
]: o0 @ i) Y! \" o初学者,还请见谅! |
|