|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中4 e& }9 J B; j: G1 y
public void spreadMoney(int money){
& o# [4 g M3 |/ e // Randomly place money in moneySpace
4 m, G1 a/ ^2 a+ F8 x7 m7 Z for(int i = 0; i < money; i++){
& z$ ]) c `9 z! ~ N# S8 ^! c4 R$ J V* Z2 r3 W2 I! w
// Choose coordinates
' h4 Q* N5 r/ `9 L$ _( u7 O- W int x = (int)(Math.random()*(moneySpace.getSizeX()));
+ Y" v; w5 H5 a% G- @ int y = (int)(Math.random()*(moneySpace.getSizeY()));
8 x* G8 Y& Z( h3 o0 _" B
( K1 J' Q4 _7 p5 Q- _' U f // Get the value of the object at those coordinates
( D2 R! i, O4 Y f0 S int I;7 _' ~2 O# O5 @6 L f1 O; U
if(moneySpace.getObjectAt(x,y)!= null){1 g( h" P5 Z( `% O0 Y# x
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();% _& ?2 I0 W) k5 }: |
}4 T, k& K0 ?- o
else{
) D. R8 C/ W3 G( c" A1 H I = 0;
1 `5 u3 k+ {8 l) Q }
% c* i$ e1 v3 K4 D O // Replace the Integer object with another one with the new value- ^' @ r# U* {
moneySpace.putObjectAt(x,y,new Integer(I + 1));: C$ E& H6 ]6 b5 o( `
}1 Y, s- q! O" g0 P. \" R8 o
这里面if(moneySpace.getObjectAt(x,y)!= null){
7 S5 w3 `" U5 r I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();; Q- p8 a, `; I
}, t1 `6 K4 L* ]0 n2 i0 f
else{* r; ~) e; I! `- u8 h2 D
I = 0;* o) c5 w3 E; |+ ~2 p. n
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?# `, K1 z( u, h* e: ^) [9 i
初学者,还请见谅! |
|