|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
: ?0 t) O6 R+ P4 Z( {public void spreadMoney(int money){
/ f5 r' L' Y `7 ^6 P3 n& X // Randomly place money in moneySpace
2 s* g! p% ?7 b$ E9 Q3 O for(int i = 0; i < money; i++){/ k% S) T. d# m! [
0 ^4 I& c/ r6 q- F
// Choose coordinates7 z1 k v. M/ {5 J
int x = (int)(Math.random()*(moneySpace.getSizeX()));
( f) l6 D& X7 f, k$ f1 L int y = (int)(Math.random()*(moneySpace.getSizeY()));) V) P" x, G3 o* |. a
. Z. r; O9 j5 G' |! t& Q6 T
// Get the value of the object at those coordinates
8 i( k$ u- h0 I+ c; X1 m0 r int I;$ N% l/ e8 V; F; A+ v0 b8 x+ e
if(moneySpace.getObjectAt(x,y)!= null){
* t( d+ s% I8 b' f1 K* }; k# T I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();' h( m$ C9 g! q" V7 _
}
; n3 y! w+ `- {$ P: u5 G: r0 ~ else{; |% y( v2 Q( b1 c3 t- g% u
I = 0;' y4 N( H Q+ ~+ n2 C- C J* h% d g" `
}& o) Z9 B1 I; v0 Y8 J8 |
// Replace the Integer object with another one with the new value
0 `, ^% M9 Z9 S+ q2 S: y moneySpace.putObjectAt(x,y,new Integer(I + 1));' F6 ?2 B5 D+ f0 i4 X4 s. _
}
$ O' W! m$ s9 }7 `: O1 r5 C这里面if(moneySpace.getObjectAt(x,y)!= null){& x% S3 a: P' g
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();/ u, N; G& f* x! G5 o- C
}
" T2 U( B- q0 V+ }* Z; Y% t3 ~1 v# Z else{0 d. \: Y4 ]7 q) S
I = 0;
; b% h$ D: d1 }是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
: _. c- k5 h0 J; r7 v% T初学者,还请见谅! |
|