|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
* h% a1 m; w. o5 ]public void spreadMoney(int money){8 l+ \8 j# \) e! e1 f$ o3 o
// Randomly place money in moneySpace
& z3 G8 z G, x: A( h for(int i = 0; i < money; i++){6 v* Y, F/ f' K2 I) X( r; T8 o' y! K" W z
' ]1 r% K( C5 q. l. ~5 W // Choose coordinates; ?+ F, B7 }' F+ Y1 i8 V' @
int x = (int)(Math.random()*(moneySpace.getSizeX()));! q7 v- N3 ]2 h6 Q3 B% `
int y = (int)(Math.random()*(moneySpace.getSizeY()));
$ N9 h' G- @# C$ J
% l9 S9 D% G7 e0 u // Get the value of the object at those coordinates
# O0 G( p9 b+ ^) | int I;8 C L7 s& u+ [" T5 o5 ^
if(moneySpace.getObjectAt(x,y)!= null){" S J I. T4 Y( w9 {. j, e8 W
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
+ o5 B3 v1 H a4 H }+ h- s8 L0 c. a; a% g/ R( n$ x
else{% Y; ~6 u* J; _$ P& q# N
I = 0;$ B" o8 z2 T5 V$ B1 g
}/ s' s M; N* r, Z
// Replace the Integer object with another one with the new value- l; m9 @! {0 E! v+ W! x) w4 J
moneySpace.putObjectAt(x,y,new Integer(I + 1));$ u9 m5 S4 Z& e( u$ e5 y/ e+ G# u4 C8 ?
}3 T& A% C2 L& ^
这里面if(moneySpace.getObjectAt(x,y)!= null){5 D" J# h7 i# m6 X h
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
- L C2 Q7 a) F; \ `! z: M }
" c/ k( B5 r8 z+ a else{, ]* z. n+ P& \+ }* j
I = 0;
3 O! [2 S9 e, l6 s" P* g是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
) n9 Y% J% o! Y: K+ S$ }初学者,还请见谅! |
|