|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中. L- E) ^# I1 _5 A8 Y/ g8 G
public void spreadMoney(int money){6 _# [! A% p; x5 D
// Randomly place money in moneySpace
; Q. P& k9 e. J* l% x for(int i = 0; i < money; i++){! G4 k; X$ H. ]7 F! x+ a% m
l$ @% K' y1 ] z' I // Choose coordinates6 V s& l! y8 i) z" A1 F d6 M
int x = (int)(Math.random()*(moneySpace.getSizeX()));% y: j0 L3 ~. S* b/ L; P
int y = (int)(Math.random()*(moneySpace.getSizeY()));
( k2 q+ w$ j- Q- l$ [; s @ m( r8 B4 g
// Get the value of the object at those coordinates
0 J0 j5 _7 x* @; T& b int I;5 p/ v! m5 @" M" D9 I
if(moneySpace.getObjectAt(x,y)!= null){
' K% o2 |+ n. l4 i# q! o I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();4 |1 H- b3 X6 d8 Q
}
0 @0 Z' X# ^- Z) a; d9 L else{! [9 h. q; O2 N8 ?
I = 0;
7 z5 t" B: R8 d$ z0 T, j }9 A3 `. g9 E! S
// Replace the Integer object with another one with the new value7 B0 ^ B5 |4 C. L4 h2 S
moneySpace.putObjectAt(x,y,new Integer(I + 1));
* J/ Y7 W2 }8 |+ M }- W% J% d- [3 x1 X
这里面if(moneySpace.getObjectAt(x,y)!= null){
) G9 C1 H/ X! w& R' d& ` I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
9 X$ Y2 }6 V4 p }
' H6 W% D3 J. i( I/ a7 N1 C else{
7 `% G, Q% e( F ^2 s2 @9 W3 d I = 0;
9 U/ j. ^7 \3 D/ ~0 J% t是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
6 X8 a' Z9 B9 p: k1 e初学者,还请见谅! |
|