|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
- x0 ^( w1 M" J$ I' ^public void spreadMoney(int money){
# Q8 c( [0 u# H+ @9 I2 s4 I1 ` // Randomly place money in moneySpace9 e1 n/ t. M( K' b/ @* x
for(int i = 0; i < money; i++){0 o8 b4 H% G9 U' i) T: p0 n `; g
Z: C! i$ f, G2 g, ]/ E
// Choose coordinates1 [/ ^2 x, d8 o3 A" Q! x, P
int x = (int)(Math.random()*(moneySpace.getSizeX()));
9 [7 E' y% v0 s int y = (int)(Math.random()*(moneySpace.getSizeY()));
* Y2 v' r" w* Q% m( k- `4 ~( v1 W; ^7 Q- M4 ?5 y
// Get the value of the object at those coordinates Z. _. V' t* u5 Y' C5 y6 H
int I;
: F- L, o$ }. S$ [+ b1 Z% Z if(moneySpace.getObjectAt(x,y)!= null){
- |4 W3 Q0 y3 V& [9 ~ I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
7 \( z/ D( ]1 M. d }
/ {) R9 M. Z# w! Y" H7 ~% i4 C else{
; `$ p2 P. ]. e I = 0;
, q3 o0 Q+ B4 v6 W( y+ h; m; d" G2 g }
! L+ @4 J! a: K% z2 \ c // Replace the Integer object with another one with the new value2 ~. Y; i X6 q4 p8 B
moneySpace.putObjectAt(x,y,new Integer(I + 1));$ D4 R" V, s% M, L
}! {" ?2 y1 d" }
这里面if(moneySpace.getObjectAt(x,y)!= null){
; J/ K2 Y9 |8 n; C" u( E. {; B2 l I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
8 ~0 a! J7 b6 p9 l! B }& q" f) x; ~6 ?/ V2 Q i) A* S
else{- |& C* u, D! R/ N' d, Q9 z' [
I = 0;
9 x8 R. a: Q7 ?$ T是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
4 `$ \; W5 J; ~8 h5 ?. r初学者,还请见谅! |
|