|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中' S e: {, U; t7 a& N+ z
public void spreadMoney(int money){9 _2 e' C: U& R1 M- V& e7 P
// Randomly place money in moneySpace0 b- _) C& U1 z x: X' `9 Q
for(int i = 0; i < money; i++){
6 q# c6 R) s9 ^6 t/ t/ L* |" b- k) l+ P( G3 T' J7 ~# k
// Choose coordinates
% ~( A8 Q: l6 f* K8 J; y6 Y int x = (int)(Math.random()*(moneySpace.getSizeX()));
, _/ d4 e7 {, r4 j int y = (int)(Math.random()*(moneySpace.getSizeY()));8 j; d8 s+ b# {8 O. R* H
* v* F2 @1 U2 p8 b* o
// Get the value of the object at those coordinates
" I3 e! \: c2 T int I;
: i: M- J, F0 V# {* I" C if(moneySpace.getObjectAt(x,y)!= null){
: Z. a" P! D- J I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();( H4 L4 y- b9 Z
}. o$ E3 I8 v. g; L/ _" x% Z
else{
5 W9 |8 [: X5 R8 O2 ]0 c2 P1 D I = 0;: V: L& P3 k& ]! R
}) o$ ` Z4 H1 t6 Y6 k
// Replace the Integer object with another one with the new value9 B- ]! A, X3 l. _, N% {
moneySpace.putObjectAt(x,y,new Integer(I + 1));
- t' y0 v4 m+ n/ U }: s3 u% |6 c* M E5 n; {2 G
这里面if(moneySpace.getObjectAt(x,y)!= null){
9 f* w" f4 f0 e" Y$ A( f I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();3 j+ g7 I3 O- N# ?) I5 n
}
" B% A# B* j+ ?, {, u3 o else{9 v/ I2 q& O* [4 m1 p: ]$ ?
I = 0;
0 E$ R* K3 l. y6 i3 k# B是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?# g: @# Y; P6 A9 n O$ a
初学者,还请见谅! |
|