|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中 f T) U0 B7 P* y
public void spreadMoney(int money){
, R: w4 Q& _' t% M, J // Randomly place money in moneySpace
. ]" _1 F1 i3 q2 }/ L3 x for(int i = 0; i < money; i++){; w; I3 `4 m8 q* _) W0 X z. g. x+ [5 C
& z9 }2 T. U3 ~: m$ |
// Choose coordinates+ e$ O' P3 k( B" _
int x = (int)(Math.random()*(moneySpace.getSizeX()));9 |% o: R7 Z6 p' w. j9 p7 ~; G1 w
int y = (int)(Math.random()*(moneySpace.getSizeY()));' [0 K' y: v4 g) |5 _
' X3 S; y& i6 \1 S% u. {* W
// Get the value of the object at those coordinates
+ _( }" B6 G! p" u. T int I;; B, [: U7 P% Q) j2 V8 X
if(moneySpace.getObjectAt(x,y)!= null){
( g* a6 ?) E6 H& ~# v I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
4 k( x2 I# p; ], ]# |% { }
0 U2 W' W& w; f' d4 ~- V else{4 }6 e; w' J' e) h( O6 q7 `
I = 0;, t# W" @4 H( S) E
}$ x" j8 Q* P! E6 o) e% B
// Replace the Integer object with another one with the new value
4 C. u: C. G* u moneySpace.putObjectAt(x,y,new Integer(I + 1));6 v) s* U2 I( D7 L0 D. e% _. O
}5 ? G4 E* @+ D F, p7 q
这里面if(moneySpace.getObjectAt(x,y)!= null){
; y( Q$ E/ U' p1 g. \ I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();9 ~ L7 G+ X/ P- ^$ p
}1 W& S1 B+ i: O" G3 U/ j) F; \
else{
6 t) d: z) C5 ^( e5 ` I = 0;0 K- V) D! D8 f! f" O
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
4 L7 K5 g, n7 [' v* H5 u) |0 N初学者,还请见谅! |
|