|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
" b& @; Y, D" Cpublic void spreadMoney(int money){* o( y5 v& g3 O- ?
// Randomly place money in moneySpace1 a4 V3 i! e/ k" M9 L; p
for(int i = 0; i < money; i++){" x0 @" b. z- c# B
! |: {5 V/ _" O // Choose coordinates" S4 _; _- s! T n) I' W9 j
int x = (int)(Math.random()*(moneySpace.getSizeX()));
$ O7 I3 V3 g& K/ g% d int y = (int)(Math.random()*(moneySpace.getSizeY()));
& `1 U! O. V( a6 G% H
2 Q7 Y* T) o/ O# Q" E" L // Get the value of the object at those coordinates
1 j& P1 ^$ P# M) Z. V( P int I;
/ G# C) L& j" |, u7 `, E$ k0 t6 O if(moneySpace.getObjectAt(x,y)!= null){
5 b6 J; V6 W2 r2 [1 B% m I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();! R5 N/ b/ \+ |
}& X2 g( W$ C/ o9 R, D
else{
6 \& u5 b( e& ]% I I = 0;( a) B5 z% j0 ` l
}
. a- o, W+ j/ q8 ^7 q6 u5 \ // Replace the Integer object with another one with the new value/ M0 h3 V ~; M$ a0 p8 I4 _; J
moneySpace.putObjectAt(x,y,new Integer(I + 1));
3 ]4 D0 U, }/ W. Z }
. ~6 _! C) B+ S' M这里面if(moneySpace.getObjectAt(x,y)!= null){7 O+ v% L% {- j5 q
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
' U$ [8 m$ k6 d+ ^. V! {2 @ }
( q' `5 i8 y" t; H! {( t else{
3 l: K% r) P, u. h, k1 m I = 0;- l- b9 S( \( i3 z2 }* ~' B& Y
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
( f/ D- q$ h6 x8 X8 M8 z初学者,还请见谅! |
|