|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中) D8 G6 K3 O ^! l8 q
public void spreadMoney(int money){
/ A7 j; J# `$ V- C // Randomly place money in moneySpace. Q/ O7 V( U% e0 \; F, ?* H
for(int i = 0; i < money; i++){: c6 z' d$ F8 f3 t* u7 N2 H7 R
# U0 \, J. S2 \$ @
// Choose coordinates5 k& A X4 Q3 ]' r* |! C' @! E; }
int x = (int)(Math.random()*(moneySpace.getSizeX()));
6 W2 S- z) y1 L" C7 i int y = (int)(Math.random()*(moneySpace.getSizeY()));9 T( S( i( I' H& n
/ y2 `: i+ `1 e( i. I' n3 d' N // Get the value of the object at those coordinates
( H0 q6 }8 M( ~. ` int I;
1 G3 X# \" O' d0 f if(moneySpace.getObjectAt(x,y)!= null){
+ C' z X+ i* ]' K) y I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();4 K: @0 J Q; I
}5 `+ n; C% n! ^! [9 W
else{
! E' l9 {3 l: r I = 0;
# w, G7 C) [' t4 e6 W% ~1 O }5 Y% H/ n4 [' O5 x, b
// Replace the Integer object with another one with the new value
+ t, m: a4 m3 h; ^' K moneySpace.putObjectAt(x,y,new Integer(I + 1));
/ @2 ~+ w, x1 C" H r+ c1 ` }8 Y2 l- @& U8 A) j! S9 ?* k+ x \
这里面if(moneySpace.getObjectAt(x,y)!= null){
~) @+ z4 L9 @( y+ L- Q8 V1 D I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();$ X/ B0 P9 \) a6 |9 y3 E
}+ n3 I4 K) z0 C3 J' w
else{
$ }. s. t, n% c7 o5 i! Z' O. k I = 0;) E5 |2 } F+ y2 a0 w& L8 @4 b1 n3 w
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?, w2 i& L* X0 C" ^0 a; L# z
初学者,还请见谅! |
|