|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
* D" P% x9 m$ S2 ]public void spreadMoney(int money){
4 T. V3 z3 w' ~7 ^ // Randomly place money in moneySpace# `: Y+ q+ \; N5 h
for(int i = 0; i < money; i++){) }# r& F$ D3 A5 c
9 C. S+ S1 m! ]3 N
// Choose coordinates% k/ y. p o/ s! |
int x = (int)(Math.random()*(moneySpace.getSizeX()));
7 L& d1 Y2 G3 A/ t* q3 Y" c# }- T int y = (int)(Math.random()*(moneySpace.getSizeY()));8 c" j/ P( {+ a2 ^
( l+ H( e5 C) w" c
// Get the value of the object at those coordinates4 G) @) n+ S" G* N! ?3 M( g
int I;
( y8 {' h1 e' a" l& a, O if(moneySpace.getObjectAt(x,y)!= null){
( R' J5 i1 Y% L6 J) `. r I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();6 s) u0 C# C$ `/ `/ y
}
* X, z9 r2 L& r0 l/ M: w else{2 G! k, ^2 C* _( Z2 J+ p7 b; U# F1 q
I = 0;
. w- _. h2 J0 v% @3 b4 S }/ ?8 N9 T5 I5 i) `5 l6 p) b
// Replace the Integer object with another one with the new value8 X2 h5 `+ H' J4 F* e" I) b" E! E
moneySpace.putObjectAt(x,y,new Integer(I + 1));( j' t" H+ p/ W) M) w! o
}0 l. j; \" m. n+ }0 I1 v8 Z
这里面if(moneySpace.getObjectAt(x,y)!= null){
$ x( ^& h8 k5 Z I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
1 |3 k1 f* g0 a9 u9 y0 Z! N# ] }
0 u; x1 \: c# l3 |; T. W6 | else{
/ j6 ^- P) X8 S- C# { I = 0;$ Y% L( {3 P: {! y# d7 r- ~& m
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?( E" c8 f' _/ [9 W, F
初学者,还请见谅! |
|