|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
' L+ T' l0 a; e T6 Z C: qpublic void spreadMoney(int money){
) `3 _/ q! U( d2 r2 Y2 q n; [( l // Randomly place money in moneySpace
0 ^7 C A, c0 v0 [* P! ? for(int i = 0; i < money; i++){
( o7 U* B+ d- o @5 L5 m! P" b, n( y# }! \/ d& H
// Choose coordinates4 k5 A0 m* F& A' g4 V
int x = (int)(Math.random()*(moneySpace.getSizeX()));
E0 s% Z2 ]1 o$ l- ?- ^. L int y = (int)(Math.random()*(moneySpace.getSizeY()));
& R4 N! v, [" e7 e8 v
O- B* j" n, n' p0 e4 a2 `# K // Get the value of the object at those coordinates
7 Y" @7 L9 B1 g2 l6 F int I;
% p3 Q2 E& C3 S' W3 u, T if(moneySpace.getObjectAt(x,y)!= null){$ y7 r5 ?2 D' s4 t
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();4 X% B2 {* r/ X7 C: w; S
}2 u3 E% P" N5 D! R
else{& O# i" [' Q6 B' H, \7 E# u- k4 R
I = 0;
. {6 r7 Q, C; @" M; H% x/ N+ { }& Y, @8 T5 m% }. t, P
// Replace the Integer object with another one with the new value
2 y% L: m& h" j% R- d5 u, ^' o/ @8 P moneySpace.putObjectAt(x,y,new Integer(I + 1));
) h1 [- p" e$ m( b/ t }: X! {% v" [& [! f7 ^3 j$ h
这里面if(moneySpace.getObjectAt(x,y)!= null){
* |3 E& ^( V. L/ D/ a1 U- i7 y I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();' V" B1 x0 k& x
}
a* X$ `6 d. U- E( a/ _, q else{, t: G( j1 X6 `( @7 F8 q
I = 0;
, u& O- x" l& g+ Q是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?& u4 j. r# B- f
初学者,还请见谅! |
|