|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中$ ]. h3 s L3 p8 g, a
public void spreadMoney(int money){
# T) B, U' u! x5 Y9 m2 M- }# q# l // Randomly place money in moneySpace, [8 ^: x8 x% @1 t/ @( v; v
for(int i = 0; i < money; i++){
( ]+ I+ o6 u7 \, Z$ @ ?* ?0 Q8 ^# v' M8 L) M# I$ T2 s
// Choose coordinates" _: A! C: I4 C3 ^9 P
int x = (int)(Math.random()*(moneySpace.getSizeX()));) B- ]* \/ E: H$ x( p. R
int y = (int)(Math.random()*(moneySpace.getSizeY()));
! n. |9 Y# u6 ^2 @4 w3 j3 |9 a1 A* N: a
// Get the value of the object at those coordinates7 O. [4 ~8 M5 R4 ^6 b
int I;% O; d/ w. a0 G6 h8 ]3 b
if(moneySpace.getObjectAt(x,y)!= null){" Z4 G3 ~5 |4 s( U% H. p. `: D
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();) n+ q* V, p1 l* r% D8 J! u$ F
}# h; L. M- ]. L9 G+ A1 B" p
else{8 ?) S' o/ w1 C2 B* z& `! o1 `; w
I = 0;
8 `, T' b! Y$ A$ R. v; F- O }
% l3 o8 Q/ n- h t: F$ q5 s // Replace the Integer object with another one with the new value
% H& V& X+ e5 r* f moneySpace.putObjectAt(x,y,new Integer(I + 1));& u! G2 F0 C3 w6 W. C* H2 t
}
* d- X! H M2 M J1 _这里面if(moneySpace.getObjectAt(x,y)!= null){' X4 Z1 b: |1 |% E. n, p
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();7 C5 k. q0 b; l' |! T% Y0 G' I4 S4 J, `
}
9 S$ R$ e) h6 R8 _ else{
( S' R7 k; B( b* K% u I = 0;
* r. n. ]/ i. ^9 p1 a是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
+ i/ b* d6 R# \- A" J) T初学者,还请见谅! |
|