|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
H6 w' m2 y7 Y* Apublic void spreadMoney(int money){1 {. J/ r2 l7 f! L$ n% L: n
// Randomly place money in moneySpace6 d# b8 V9 [; s8 o0 X& E
for(int i = 0; i < money; i++){8 a- Z- H: ]4 C$ @& A. h
* c6 [) V) s9 T. n" @# k: K
// Choose coordinates$ E8 K& r- U6 l4 f3 h# x
int x = (int)(Math.random()*(moneySpace.getSizeX()));* \/ l# v* I5 l8 b: J! f. |' S
int y = (int)(Math.random()*(moneySpace.getSizeY()));
0 t( F$ {% R6 m
- d* w$ b5 n* c: D // Get the value of the object at those coordinates! s2 {. W m9 u: [( ~7 p8 B
int I;8 K) R* Y( c6 t6 y
if(moneySpace.getObjectAt(x,y)!= null){4 e/ g: Q' q R1 b% k
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();: w- f7 ^1 c; d: V! D
}7 r' _/ j' j" B& N! ^
else{
0 G/ c# C/ |% o. q( e I = 0;% f2 r- M9 D) P9 z1 V( b- u* f
}
: K2 G4 ^6 B$ y! d, D" a- U+ ~ // Replace the Integer object with another one with the new value
5 o- m3 W6 y0 Z8 b! @% ~: q3 m moneySpace.putObjectAt(x,y,new Integer(I + 1));
! P. I! }; N ]- b" S+ N# H }
- {2 V, k+ {3 ?, L这里面if(moneySpace.getObjectAt(x,y)!= null){- D, P$ C t/ U1 F( ^& A- J+ ~, N
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
* u+ c( H e6 y0 g) Z4 ]" H0 Z }# m, J0 a1 g( G% O6 y
else{- b+ @3 L' J* b1 j; }
I = 0;
/ N! x' `) P9 ], `是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
" T: ~, B j1 u3 Q初学者,还请见谅! |
|