|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中5 @# i8 N8 G7 v) P( z6 w
public void spreadMoney(int money){
# N' q: T/ R' g: h. V# ]3 u) K+ }- b; l // Randomly place money in moneySpace
7 u, Y* z t. a! I for(int i = 0; i < money; i++){, ^" W' D' b) S+ o0 {, O1 J) k- b, ~. k
: I" B9 y- I- x; Q% E4 B0 O4 L
// Choose coordinates5 A7 Z% \7 ~( O, h$ D
int x = (int)(Math.random()*(moneySpace.getSizeX()));8 O5 e8 w: T1 ~9 [
int y = (int)(Math.random()*(moneySpace.getSizeY()));
: s8 t; q; ^% |" y- I6 G& k6 p0 ?2 {4 p9 ^+ V' k
// Get the value of the object at those coordinates
* O F) E4 _) e& u& |$ D int I;; v+ l* w- U: [* | T0 z
if(moneySpace.getObjectAt(x,y)!= null){' @2 \; i5 V$ e, x
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
3 C R+ Q2 b M) q* ] }" Q0 J6 J7 |! Z. z" ]( K1 R
else{
! a1 r5 H* y/ p I = 0;
4 Z* I6 x' K/ i }
7 v0 [: C" [+ b1 L+ I3 \! k; g, f // Replace the Integer object with another one with the new value
. }& x* V9 ^, A, K/ B moneySpace.putObjectAt(x,y,new Integer(I + 1));
' f1 T1 E5 Z( C8 \7 v, {5 }5 H u7 M }; g V, A+ Q# D9 `$ X
这里面if(moneySpace.getObjectAt(x,y)!= null){5 y0 X0 {& z* ^* i- _9 ?/ i9 K% W
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();2 C6 n0 A* j `" b4 H- Y# g
}
0 P" o, Y2 a% }+ s- ?; V! M# R else{8 f8 f; {$ u/ U* d
I = 0;
/ l/ f) R- o0 o0 u; f& ~是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
' q8 b# a$ r& {4 D6 o初学者,还请见谅! |
|