|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
1 X0 k% m1 N7 ?" Bpublic void spreadMoney(int money){
$ L# _, }4 A, f n // Randomly place money in moneySpace8 K/ W& ]1 d" {2 D: Q/ U) e, G! q
for(int i = 0; i < money; i++){" e/ t& Z4 m Q! h% a; z0 D
( j$ p" N E3 Q2 P( ~& n0 q // Choose coordinates; i2 Z. l/ y6 G h
int x = (int)(Math.random()*(moneySpace.getSizeX()));
( P: N. m1 v* I int y = (int)(Math.random()*(moneySpace.getSizeY()));
1 \0 X4 { E: P
" o0 B* \+ |' V+ I0 [8 F& o# G+ L+ B // Get the value of the object at those coordinates
}" {5 K7 m( X9 j2 w/ Q" P$ P; t int I;
: H* K" l* F; R9 t. M4 P; o if(moneySpace.getObjectAt(x,y)!= null){) S, ^2 P1 `) d! S$ C! Z
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();& b( d Z3 S* h t- W b
}" M$ L6 c/ ?: q* J0 p. w
else{8 X+ w7 V' x, q; A9 J( V& B
I = 0;
' J$ }' P) g7 z9 X+ B: x }; G G2 V+ o `* K( e
// Replace the Integer object with another one with the new value/ U8 V8 t( _7 q2 J5 u
moneySpace.putObjectAt(x,y,new Integer(I + 1));: v7 m, W8 w5 H; R, l# w, J" \
}( e2 f% z4 ~9 J3 e% A
这里面if(moneySpace.getObjectAt(x,y)!= null){; d$ a+ k& M9 C2 ~7 t
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
5 P6 p$ H. Y3 T9 p& ^ }2 M3 M, T" M! _4 q+ X
else{3 P2 b1 V2 y9 q5 O, A
I = 0;
- s# i4 M1 n( T' N+ W. F是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?+ Z6 \2 o5 {, k% g
初学者,还请见谅! |
|