|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
- I2 k" e! B4 J" n6 a* }( P% ]2 Epublic void spreadMoney(int money){
# n/ m$ K$ b/ d& q) ^6 }# A, t |2 u // Randomly place money in moneySpace& ]; x8 l2 m2 y! i
for(int i = 0; i < money; i++){" m/ X' Z% Z4 L1 r+ ]
5 @% I* t' q7 ?+ d6 w2 r& x6 ? // Choose coordinates
( `6 m* q" b) i int x = (int)(Math.random()*(moneySpace.getSizeX()));
( [+ D- P4 S% f& r& Q: }9 A }+ g int y = (int)(Math.random()*(moneySpace.getSizeY()));
2 r; |, G, u6 i* F/ f j7 j- o# Q
// Get the value of the object at those coordinates* g( X; S$ k8 N9 n6 W
int I;3 W: c% R8 P( _# S" k3 S6 c
if(moneySpace.getObjectAt(x,y)!= null){
3 N4 S9 X, ^$ ?9 f I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();6 }8 u! x: [/ s! P6 S2 C- a% S( f
}
3 D* ]9 j( I- z9 V; h. Q else{) Z+ ?* q/ K! N- ^0 P9 w' W
I = 0;! l) ] R; E n! U9 c; s8 Q
}
9 U- u* B( t2 ~0 \7 ^+ U7 \ // Replace the Integer object with another one with the new value2 f" M6 G& e0 B' Q# x3 R2 G
moneySpace.putObjectAt(x,y,new Integer(I + 1));$ n V6 l0 V3 M1 Q6 w( n% M! g
}
9 J/ W' D' S" n这里面if(moneySpace.getObjectAt(x,y)!= null){
6 V- B- c- M+ u" W; m I = ((Integer)moneySpace.getObjectAt(x,y)).intValue(); D( {( b. p- D
}: d; }: D4 Z. }' R$ n- n
else{
1 S0 P4 d& p/ S/ R# z5 T: C I = 0;
( J" R7 k' u! \8 r/ w2 W是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
& u! a* u/ f9 l9 l初学者,还请见谅! |
|