|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
7 A$ M" m2 U: k: H& g- Rpublic void spreadMoney(int money){2 C0 H6 R6 Y# K) ^3 e& o' ?/ p8 h
// Randomly place money in moneySpace
) b6 W3 S) o( F2 u9 h: E0 R% h for(int i = 0; i < money; i++){9 T( a4 e; h+ s/ r
% \$ P2 A& |5 @2 E9 k
// Choose coordinates" G( q0 ]3 ^0 |* C5 _' d' M
int x = (int)(Math.random()*(moneySpace.getSizeX()));4 n0 o) P3 x8 N! `% U
int y = (int)(Math.random()*(moneySpace.getSizeY()));
3 \! N# u/ `% R/ i- R8 q0 H. {2 p! z9 ]: I$ E6 h0 ~
// Get the value of the object at those coordinates {/ S! Z8 w; ^* `3 x) v& x4 |
int I;
$ {: T, i1 i; U! h; [. W7 n% s if(moneySpace.getObjectAt(x,y)!= null){% D( b( e3 b4 N* O5 B3 w$ o
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();' i* j* j. v$ ^' O2 y0 L: {
}
$ N/ A* L6 L$ H B9 C% K: H else{
; I5 P9 J2 M' H& ^5 j C I = 0;' C3 C, b* n* ~- P
}1 O6 ^" o1 z; }( p3 H
// Replace the Integer object with another one with the new value
+ H- b+ B- M, o- X+ G moneySpace.putObjectAt(x,y,new Integer(I + 1));) {) Y1 e$ I2 T; d- A, U3 \3 u7 ?) A( n
}
* c5 C, X. H: ]这里面if(moneySpace.getObjectAt(x,y)!= null){+ E! W; X: J/ T/ f9 F$ t+ X2 y1 ]
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();2 {9 M& Q" Q# d* J
}3 S$ H+ i1 W a% s1 |
else{
' O2 a7 G1 v8 p5 ~" N) ? H( s I = 0; e2 P" f2 p, \. b- k7 d
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?$ s$ R% L5 c: x/ e6 Q8 `
初学者,还请见谅! |
|