|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中# r1 c* C/ ]5 u2 `$ N, i3 O0 V
public void spreadMoney(int money){5 Z- @/ L. `8 t
// Randomly place money in moneySpace
. i6 k8 n9 k8 p9 n1 C8 o" w for(int i = 0; i < money; i++){
0 s1 B8 F5 C9 ~: M# b4 O& E' [. t+ @2 v: h/ P5 r' @
// Choose coordinates) Z$ _* d& ? f: c
int x = (int)(Math.random()*(moneySpace.getSizeX()));
' l( O; H, x( l. i! e2 k int y = (int)(Math.random()*(moneySpace.getSizeY()));
" w# [! p+ p$ w& C! Q" d. |; V3 q* h8 \; [
// Get the value of the object at those coordinates
$ S+ {, N9 ~) j: Z& g7 w% G int I;! K$ t+ D E4 t
if(moneySpace.getObjectAt(x,y)!= null){7 \1 i( k4 J: [" B2 H/ k
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();" e* J/ H/ G7 C
}; [+ Y& F' p! i% y3 j! B
else{- E+ Y' H4 `- A" E, \
I = 0;
2 q K$ |4 z( d0 m* ` }$ W* F1 L1 c; {& ?
// Replace the Integer object with another one with the new value6 i! s, p( W/ R3 P; d4 p) P
moneySpace.putObjectAt(x,y,new Integer(I + 1));, `1 b0 h; G9 P, L- J% ?
}1 U; V3 K4 s* Q3 X
这里面if(moneySpace.getObjectAt(x,y)!= null){ u" R9 W+ m: c! R5 f! Z
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();; Y3 h/ a0 |9 U9 f6 Z8 E+ o
}( K6 X D. r6 |3 |6 W
else{
' g6 E& A7 `- Y8 J I = 0;# i( v2 D0 E( F# ^( D6 ~
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?8 ?0 d7 [. P0 y* T( j* Y9 M
初学者,还请见谅! |
|