|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中 y) V$ C7 P3 M0 y3 D! D, r4 m. J
public void spreadMoney(int money){9 _8 M. P- ?4 f# n
// Randomly place money in moneySpace% _ I9 o3 G' P
for(int i = 0; i < money; i++){4 X$ q. R- W! H* M, `1 L5 K! w8 w6 b
, b$ ^& P9 C/ [ l3 J! `' D( M // Choose coordinates8 I T0 w6 O8 o$ V* c" R7 g
int x = (int)(Math.random()*(moneySpace.getSizeX()));
) b4 M5 c7 h) p6 O1 ?8 ^3 K, { int y = (int)(Math.random()*(moneySpace.getSizeY()));& a( V a1 z0 e2 ~+ K; Y
3 \0 C/ o; r9 h* A, p // Get the value of the object at those coordinates
+ u) Z+ R$ v- I7 A int I;
# ?$ V, h) o* _" P8 V* g, M if(moneySpace.getObjectAt(x,y)!= null){
8 E: Y5 r: ~ u+ N6 v" E- g' _; |* S I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();( @1 k9 ~1 P& }( T5 U! P
}- ~% I* _! {- W
else{& u& j* n g; ^) b* W
I = 0;
# A2 d3 V/ o( K/ \ }
& K$ @* g$ _9 j- V; ~ // Replace the Integer object with another one with the new value
2 J+ E' J0 I$ J moneySpace.putObjectAt(x,y,new Integer(I + 1));
. x0 P1 ^, r# ^' ~+ E h4 G }6 I& f0 ?7 P/ q. R5 a) @
这里面if(moneySpace.getObjectAt(x,y)!= null){
0 P& I& m3 }! H# \: w0 u( x' _5 d9 w I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
+ N5 C! s& l5 b }1 r9 B) F' b' B/ _5 z7 E
else{
0 W9 Q6 K _) I$ O I = 0;
8 b, a4 Y6 a) v g; f: x" s" r是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?" S3 v# i" V% E/ U
初学者,还请见谅! |
|