|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
2 B Q; Y! g( ^7 Dpublic void spreadMoney(int money){/ v" q# y- d+ p) ?' ?
// Randomly place money in moneySpace# c( V" `1 K9 I$ q3 [% q+ m
for(int i = 0; i < money; i++){7 S" x( f( O9 T( k9 R) R1 ]9 H. @' i
" M. E- L" b+ l; w: y
// Choose coordinates
- W' ~3 L) Q* E0 V int x = (int)(Math.random()*(moneySpace.getSizeX()));7 I+ j% R: s# E: J' |
int y = (int)(Math.random()*(moneySpace.getSizeY()));
/ h7 f0 G' H8 v1 y
. B5 R# I$ H) n: u* w% F7 v // Get the value of the object at those coordinates
' k/ `' D& N; B. h( F' K( k int I;
/ x/ `4 B+ r) @& D6 j5 j: M if(moneySpace.getObjectAt(x,y)!= null){; D& ?0 ?+ } q- W
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();7 t: V% }; P, X% k4 I2 u
}5 z& y3 m: }% I& L- n( B8 i
else{
9 p* l; K9 ~$ @ q. L& N I = 0;: k# @) |) W, a8 E
}+ v( X3 b) e3 K- M: {" u0 O3 `
// Replace the Integer object with another one with the new value' C# l( q" ~% j" o
moneySpace.putObjectAt(x,y,new Integer(I + 1));
- O. ?9 z4 @, n }
1 ~6 n/ E" i! `8 {- P) M这里面if(moneySpace.getObjectAt(x,y)!= null){
) [2 @2 @/ Y' a+ G' q9 _ I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();# ~7 r$ J0 U" Z- g7 w' x# E
}
; y1 K c5 K2 `/ V else{
' {6 `8 |+ |. b' c' u I = 0;! n5 Q( \" U- l( v7 u( D0 a6 Y
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?1 v3 I/ ]& O4 b0 i! S) ~
初学者,还请见谅! |
|