|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中' _3 S& \) d, I) K7 B
public void spreadMoney(int money){
2 f; N5 E$ R1 u ]2 E // Randomly place money in moneySpace
1 W5 o6 d/ T% \' ? for(int i = 0; i < money; i++){
" y$ }" ?5 g: U9 }1 n3 c. B
" Q' r8 x5 L, _8 [* ]# P7 ]) w // Choose coordinates
8 Z( R) m; z, m, S7 ] int x = (int)(Math.random()*(moneySpace.getSizeX()));
4 U5 k: D1 S1 t) m int y = (int)(Math.random()*(moneySpace.getSizeY()));
# H8 b* O8 q6 t9 ^" I& X* _
, T8 q2 T# L8 `' {* j // Get the value of the object at those coordinates
/ y0 @9 \ ?# Z$ d/ H$ M6 W int I;/ T! v/ w0 R$ Q- \
if(moneySpace.getObjectAt(x,y)!= null){
4 T* x4 _8 R# q8 }0 K ^: R' Q8 K- f* l I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();% i7 O& V. b- K1 F3 r$ x
}
( @% q4 F8 H9 i1 A4 K else{
, ]" s7 f" ^. ?) y Z I = 0;% w2 ^ Z& w& o$ P; m8 E$ ?* o) ]8 T
}
5 j* r; `6 u2 {6 I% E // Replace the Integer object with another one with the new value, L- d& u# E/ Z, g [* I
moneySpace.putObjectAt(x,y,new Integer(I + 1));
% c" b& c$ j2 h1 f& v) { }
' ]6 M: a. _9 `% q" K这里面if(moneySpace.getObjectAt(x,y)!= null){) q8 J! f2 S5 k
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
( k" G! x& ]1 N$ I8 \ }$ o$ z, r; I7 L, [7 P: }& \! N
else{
+ q$ j! V9 | [+ M2 r( D2 R I = 0;
" d7 k+ \: W/ \) S5 X* Q是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?$ Y% H) |4 J* Z: S- ]7 S+ T$ Q
初学者,还请见谅! |
|