|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
3 k, d) g. Q3 ]1 Wpublic void spreadMoney(int money){
! A3 S' M2 ^- @3 l9 g5 w* G // Randomly place money in moneySpace7 [4 s) s' {, Q+ Q% k' { V& f
for(int i = 0; i < money; i++){
1 @; j, x5 [2 N7 Q Q1 u N. {! O/ d) k: ^3 `" _
// Choose coordinates
) E% ~ I" r+ d$ ~: o h, ?; ? int x = (int)(Math.random()*(moneySpace.getSizeX()));& K3 @( i* H$ @5 z- j
int y = (int)(Math.random()*(moneySpace.getSizeY()));# }) S# p( \$ X" V/ z$ x
8 P+ L0 }4 v0 A" U
// Get the value of the object at those coordinates" D( a- p& L9 \2 q9 r5 g
int I;+ X8 {, G) O" m# M
if(moneySpace.getObjectAt(x,y)!= null){6 w4 f' g0 u) y
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
1 c2 b+ y0 S2 l: s! ?& w }
" R1 a& @* m: o3 |# b else{2 c3 T! Y8 ?2 v- r
I = 0;
- D$ q9 k" z: G! y) d }1 P5 u6 S. J, {( S
// Replace the Integer object with another one with the new value
9 @; p2 P. E# j+ w& V moneySpace.putObjectAt(x,y,new Integer(I + 1));
1 j' |3 L9 I8 o& T' w+ h }6 L5 t. N9 z, k4 `
这里面if(moneySpace.getObjectAt(x,y)!= null){% Y' v: ?; a$ S
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
, `' ~# [2 @# j. \/ ]2 M }* H2 L4 p2 }; i9 L* _- q9 B. T1 ]
else{) [! `5 A ]; @. R# h5 Q. _. A% P
I = 0;" K+ C+ p! H/ l3 Q V- Q" M
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
- V: m- _# q2 Z' E初学者,还请见谅! |
|