|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
, q0 o+ T d* m% F/ fpublic void spreadMoney(int money){
, `; T( u$ [+ A/ P // Randomly place money in moneySpace5 A5 ^7 A, g l& ?" A0 j
for(int i = 0; i < money; i++){
0 B1 J; k+ ?9 `5 `' T% N# G, |+ C
, V2 |) t7 Q2 @& d6 Y* e // Choose coordinates
. d2 L& M1 `* d: x" j/ L int x = (int)(Math.random()*(moneySpace.getSizeX()));
# G5 F% E8 S* e: R1 S7 p int y = (int)(Math.random()*(moneySpace.getSizeY()));
0 I. Y* _! @" L+ ~+ p$ O# l$ u( i# y- e$ ?
// Get the value of the object at those coordinates% O, t1 F, d$ Z- E. y
int I;
7 N3 X4 P+ L/ B/ s if(moneySpace.getObjectAt(x,y)!= null){ b* R1 q* ]1 v0 q1 m, R/ @
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();) V+ Q. C! g' g! ], l- S
}
\ ~) ~- E7 A3 \& E else{
+ Z! f9 `( _: k I = 0;5 z$ C! { @2 W4 Q
}; S/ n5 }1 P; J# I, z& F9 R" u
// Replace the Integer object with another one with the new value
) j: |* Z$ ~* V4 E moneySpace.putObjectAt(x,y,new Integer(I + 1));/ b( ] ~7 @9 a, u2 _7 E1 Z' L
}
, w0 u6 Y7 w# ^2 N6 N* T这里面if(moneySpace.getObjectAt(x,y)!= null){+ I1 @" `) v1 r( S# @4 \
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
7 d ?$ e% u9 G, s }( x6 e }' z5 c1 M* v
else{
; Z( J& k( `8 ~! N" O9 M+ t! i I = 0;
. F9 W5 S9 x$ w/ b' A; j0 t0 ~) M是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?5 G$ |( `- Q4 M6 s. E
初学者,还请见谅! |
|