|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
2 r' E# b' H% hpublic void spreadMoney(int money){
5 K! T& C. W& O; i; f4 @9 t d // Randomly place money in moneySpace0 U6 h. c/ q3 R. ^
for(int i = 0; i < money; i++){* o0 p- s. b9 D5 v+ n$ L
1 w w% X6 u. O' Z0 K7 _3 }, M
// Choose coordinates* B8 k9 Q" W2 V1 i) V
int x = (int)(Math.random()*(moneySpace.getSizeX()));
C$ ~) a, I" [ int y = (int)(Math.random()*(moneySpace.getSizeY()));
/ ?4 m: N4 U3 j; Z$ D, \! e/ Q
! @/ }7 m2 u, T3 o! X6 f4 A, h, P% S6 T // Get the value of the object at those coordinates
1 D8 A2 ~3 Q; v" T- A2 d int I;
/ S9 K+ j, o$ a: q- T if(moneySpace.getObjectAt(x,y)!= null){
0 f$ h: T% S- v k I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
$ G R$ |9 u+ K! s7 n$ _ }
) s/ g, x1 w# B- U else{
5 I; S, L1 O9 ]' l I = 0;8 D6 D( `2 W+ X6 }) x
}* a, M! y$ _$ u+ M
// Replace the Integer object with another one with the new value* T1 y. E: S- x- u
moneySpace.putObjectAt(x,y,new Integer(I + 1));3 Y/ J' M% k4 H3 }. l8 Y
}9 t1 i0 f B, A* H; T \) J
这里面if(moneySpace.getObjectAt(x,y)!= null){
5 m) R- d' K! t1 h& y, v I = ((Integer)moneySpace.getObjectAt(x,y)).intValue(); A3 w& A8 B) P ?/ S
}
0 A7 u9 F7 i( U1 b) ^ W else{& g- ]$ Y; C. w+ W* d2 O
I = 0;
3 Z" q. I- `' t, I是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
* v- E$ O! j. [# j9 \初学者,还请见谅! |
|