|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
; M g& i! @% Y- k k' p8 _. kpublic void spreadMoney(int money){8 r8 J6 u+ _+ `5 O
// Randomly place money in moneySpace
, t% F5 {+ q( g$ X9 L7 \8 ~1 } for(int i = 0; i < money; i++){. S" t8 T7 O6 z9 c6 H- R( C" N
6 b0 w; D: y7 r // Choose coordinates( Q* y# ]6 E1 a+ N
int x = (int)(Math.random()*(moneySpace.getSizeX()));
+ j* U' M# y: Q7 }0 W5 h1 X2 P& n2 P0 n int y = (int)(Math.random()*(moneySpace.getSizeY()));
' B6 X4 U! D) \) ^+ N d
0 m; o8 u: k( ^! t! S5 M // Get the value of the object at those coordinates; `9 a0 v0 |. r* h
int I;
; u k" z @0 d s# j5 @- R, h if(moneySpace.getObjectAt(x,y)!= null){$ p, T" a9 ?3 D$ W7 c
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
6 c9 y' R+ \( ?$ E. A F Q7 }6 d }
4 d! p w0 K9 d$ i% k! [ else{
- ^+ V: k8 |8 ? I = 0;# c" R* Q6 I' m3 b$ F" ~
}
$ \. ^+ J# b7 J- A // Replace the Integer object with another one with the new value
/ J; K3 N9 Y$ V' H moneySpace.putObjectAt(x,y,new Integer(I + 1));
$ o( X7 O; N P }2 _% o) j2 P* M
这里面if(moneySpace.getObjectAt(x,y)!= null){: |/ J4 c# p/ N- I# V
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();5 I ^9 m; j6 v4 Z( t0 e; a
}
( m3 h2 _' W6 _8 X else{( Z2 ]. a# u/ I& o J0 |
I = 0;. g7 {: @8 ?5 D+ v4 \# r4 n: D
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?% b* @8 r7 s( C9 t1 M! A
初学者,还请见谅! |
|