|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中: T0 r" {' t7 t5 a) c/ i& [. r
public void spreadMoney(int money){3 J2 U" R! s0 ?* N4 @# f
// Randomly place money in moneySpace
$ U- b" W% [7 T9 d for(int i = 0; i < money; i++){
; y2 I* o# N9 G. B3 H8 F' v; d* W) j' J3 O( }" m8 t# b5 ^$ r+ p
// Choose coordinates
+ M9 U; g* |$ ]$ z int x = (int)(Math.random()*(moneySpace.getSizeX()));
1 f) L6 e6 W6 j; s" M int y = (int)(Math.random()*(moneySpace.getSizeY()));2 M: ~+ b# e" h
" h) u8 n6 c. m% r/ h
// Get the value of the object at those coordinates( v {3 X' ]* N8 s r' _+ W; P. e
int I;
3 j8 r. x1 S! [ if(moneySpace.getObjectAt(x,y)!= null){ T) p1 _# l$ u6 j
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
9 f& s% g8 ^: ~ }
2 `8 A) P% e5 ?; C8 f. k6 K& I% d else{
$ E" S9 @( ?1 \8 u2 x" q& b( @. l I = 0; \) l$ j' W0 K" L5 R7 H2 n) L' V6 F
}
2 M2 j5 j/ ]+ o, E$ n9 U0 F // Replace the Integer object with another one with the new value
& o0 b/ ?# _7 l moneySpace.putObjectAt(x,y,new Integer(I + 1));8 ^: e# R. R n. c0 s
}& H+ d$ J! P# L, K2 Q, X7 o! j
这里面if(moneySpace.getObjectAt(x,y)!= null){' p# M3 B* w. V' W* }: Z& L, {. ]
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();6 K# T- I3 c! z' y; `
}7 ]8 r2 A2 K; Y. [
else{
. _. y7 ?" T. ~7 v1 O I = 0;
7 H3 C- k' o6 C' j. Z是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?+ Y3 g; r5 l! t; e5 L' q( @6 r7 y
初学者,还请见谅! |
|