|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
0 l4 C6 A4 c$ g3 L1 d$ _' Ipublic void spreadMoney(int money){
1 ]; f4 |- p- a // Randomly place money in moneySpace; h$ h1 l4 o- u9 L, c( ]
for(int i = 0; i < money; i++){6 D: c# z2 J. c; q/ w; K
* G% [0 M- a2 y: i. H* V8 [ // Choose coordinates
6 P1 Z& P7 o7 e/ Y% `3 a" V int x = (int)(Math.random()*(moneySpace.getSizeX()));6 x; ` s) b, ]
int y = (int)(Math.random()*(moneySpace.getSizeY()));9 S8 E" F; E& W2 @8 `) a" n
/ g1 d" c d, {: m" Q // Get the value of the object at those coordinates
/ A6 _0 M- U" v' x int I;; _: |( a% J) s' e; m
if(moneySpace.getObjectAt(x,y)!= null){0 {, T/ A% @/ x: ~" _9 i1 V
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();7 w- w4 p1 e: V8 [, b& R1 I
}7 ~7 p# w8 M! F/ O0 @3 m/ A& v2 [
else{
) E: N+ ?2 ~ J' o+ d I = 0;
9 T2 `0 x3 p5 l2 v9 c }
5 o+ q3 X" h5 x5 s% q! z1 w3 p // Replace the Integer object with another one with the new value; B% G2 U& u& N2 ^. k+ N+ n
moneySpace.putObjectAt(x,y,new Integer(I + 1));' f8 K* Q5 @) x: R3 D
}
7 k* O z) c- e( Z0 M这里面if(moneySpace.getObjectAt(x,y)!= null){" k B( R, h) g6 H$ t
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();* T! h. f6 Q ?1 E
}
% E, N0 p2 X; F* u else{
) e% a, W5 f* h) o' ?- U& C I = 0;
) b8 F. V. X0 m) q6 E8 o3 L是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
; y) l# S/ s; w+ l- o& ]初学者,还请见谅! |
|