|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
% P6 o* [$ V" T- h, Npublic void spreadMoney(int money){
( m/ T% E2 }0 J0 k, e: { // Randomly place money in moneySpace
% C7 K6 s$ e M; a S% ?6 J0 Y1 p for(int i = 0; i < money; i++){
1 Q1 p/ H: |6 w: Y; |6 F& \/ h- f* c' h, k+ ?
// Choose coordinates7 H# G( p& H$ i# t$ w6 i5 \! q
int x = (int)(Math.random()*(moneySpace.getSizeX()));
$ y4 b/ n% z* E$ A' T int y = (int)(Math.random()*(moneySpace.getSizeY()));3 ]& j# ~ [2 x5 B) r9 ` s$ q/ Y
7 W! k' b- o) S: _2 h8 @6 ? // Get the value of the object at those coordinates
$ q7 K; u) ?( P% g7 O int I;! w! Z9 F, }2 R2 e5 F3 U) G! A- _
if(moneySpace.getObjectAt(x,y)!= null){' b, ~, d8 ^6 \2 l2 x
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
- z1 T( e/ H p2 s }
" h% f+ i# n$ p- U1 N else{& b7 f6 b& t1 a1 y
I = 0;9 x* o7 s" K) s3 ~
}
& {8 m/ r9 f/ R6 E2 b H // Replace the Integer object with another one with the new value
& r. s: z3 C# L: l1 o moneySpace.putObjectAt(x,y,new Integer(I + 1));
5 _+ A9 ]% {" X! | }; Y1 @0 Q4 t6 F7 Q' w
这里面if(moneySpace.getObjectAt(x,y)!= null){
9 J2 G% ]1 H4 H8 q/ i/ u2 t I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();) {# G% Y3 \1 t0 N/ d6 `# E/ ^* v |
}
4 q9 T M( U5 E else{
0 K2 I7 P/ @& ^3 @. {+ C I = 0;
5 r! R* d/ U+ { y m3 {' |是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?* }6 o9 ^3 R3 |
初学者,还请见谅! |
|