|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
% J3 U p a9 w4 `public void spreadMoney(int money){
`: _( V4 P! S" n3 `5 _ // Randomly place money in moneySpace: l e7 B8 W \5 s
for(int i = 0; i < money; i++){
4 Q- x2 _! G9 O6 w5 W2 a3 k+ b& |& D+ P( B' `; t% n5 @4 @2 p
// Choose coordinates
3 |6 [8 B# R, }! M- W* M; q8 Y int x = (int)(Math.random()*(moneySpace.getSizeX()));" a4 e; s6 s. v: ~% S
int y = (int)(Math.random()*(moneySpace.getSizeY()));
( ^+ U O5 x# z2 n1 k: ~. a$ a* L
// Get the value of the object at those coordinates
1 R# B+ a. t7 |& N- g int I;
5 S1 S3 B7 D2 r% x. f5 |" z$ @ if(moneySpace.getObjectAt(x,y)!= null){
0 s! u8 p1 [. s; b1 y I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
( d2 G5 a" J/ j8 D" ] }( w; G J3 H) S. [2 }. M
else{
/ J, W1 ?; j v3 {1 ?( N2 N' q1 y: U4 [ Q I = 0;) h4 y" K' Y1 U
}, o* G0 s3 E/ R( d, ^# Y
// Replace the Integer object with another one with the new value6 A6 n$ a# X' j* J: J
moneySpace.putObjectAt(x,y,new Integer(I + 1));/ X, f/ H. X/ U% w7 g
}
* U* k/ s+ R' a1 j$ [这里面if(moneySpace.getObjectAt(x,y)!= null){3 e5 p" Z9 G" x2 V
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
% Z# m9 G. `- A5 g4 T" t3 e }
8 Q; _2 Y, L% L- o" [6 `1 W' x2 a else{
) I5 x; ~! L$ Q6 y6 V* w# f. o I = 0;
; w; ?2 D! f% Y' g) ~是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
$ m7 K9 g: z+ K7 @# @初学者,还请见谅! |
|