|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中9 b$ z: N8 l% }! b$ q* q8 y" W
public void spreadMoney(int money){
- b7 {1 u% P2 `+ @2 W+ \0 I // Randomly place money in moneySpace
+ j+ Y: r0 k# D( ?0 p; ~ for(int i = 0; i < money; i++){2 i3 k6 ]. g6 ?; ^8 e Q, G
1 U5 E3 |8 Z7 X- L; s# z // Choose coordinates
4 s+ V" Q/ e6 J* e& h3 E# S6 ? int x = (int)(Math.random()*(moneySpace.getSizeX()));
# z( g+ _3 n% Y% `, c5 n* A int y = (int)(Math.random()*(moneySpace.getSizeY()));1 t5 ~& Q# _; \3 p" L: B6 x
2 v) ]0 C2 F4 m5 ^9 [
// Get the value of the object at those coordinates$ e: |. ?) h h! H, p) R' }
int I;
# u$ ^- K% c, \ if(moneySpace.getObjectAt(x,y)!= null){3 X. V! S; R/ t' ]
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
8 c& y0 M& L9 n/ `. f& g. q }( }/ Q2 t4 a, t8 {# R- b( \
else{% N9 @. f' Y; C2 R
I = 0;
3 m7 o# J9 J9 ]% l9 E9 v4 y }
& {9 ]6 k; W) g: G# b5 ~% } H // Replace the Integer object with another one with the new value
7 x* X/ k! z8 ?2 C moneySpace.putObjectAt(x,y,new Integer(I + 1));5 j# j) }: d( a# C. { r
}2 [# H C+ W4 u, j, e& e
这里面if(moneySpace.getObjectAt(x,y)!= null){
5 Z3 ?" @: E, O/ W5 X I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();9 c" t" D3 E" b. ~
}* `" `6 {, v: f: \0 o- L: S: B
else{7 C' m; P' Q) A
I = 0;
5 L! s8 Q+ z# ?# T7 m1 X; y3 N是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?" D+ I8 }5 J* \8 u' @ b; x
初学者,还请见谅! |
|