|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中+ _# ~& A" K2 k1 c" {+ F, a" {) W# s- n
public void spreadMoney(int money){
, f0 r* |0 M; R& { // Randomly place money in moneySpace
5 e& T8 i5 G3 H: D) L for(int i = 0; i < money; i++){4 E" T, A) P6 ?5 u+ Q" D
0 }. x+ y) z6 W% y# D1 v // Choose coordinates1 B( r5 p2 }& E4 y1 g1 ~
int x = (int)(Math.random()*(moneySpace.getSizeX()));4 f7 d5 o2 e$ i0 b
int y = (int)(Math.random()*(moneySpace.getSizeY()));7 W- G9 Y0 d; \2 l) T( m' q
$ x$ V: c ?+ e% n, M+ N( ^4 F // Get the value of the object at those coordinates
. Z7 |8 a1 w9 T+ X- z& o# o3 `; z- j int I;
- t! ~" B7 S D; ]3 t0 N if(moneySpace.getObjectAt(x,y)!= null){; N. h, X+ V: k3 n# Q
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
0 y# g; s; l) h9 Q }5 B* {) s" ?% m2 l$ {5 a( p
else{: |2 W% Y# n$ n q+ G& `5 ?
I = 0;
: n+ ?% f$ t# I9 ?- Z( } }
" W8 y: Q- v% S% |. q! t2 s. C; { // Replace the Integer object with another one with the new value
4 T) F; Y0 S& i" Q moneySpace.putObjectAt(x,y,new Integer(I + 1));
$ A. K. j' P" Z- _" J } w2 w1 Z0 o5 \. g" o& N2 y
这里面if(moneySpace.getObjectAt(x,y)!= null){
( x. X k |/ w& y: E$ \ [ I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();7 U5 M" c( @" x" A" p$ w- m
}5 v0 n# S5 r+ w' f- c b
else{
9 B8 S# d! V5 G$ d4 E I = 0;
+ B3 y; F4 X1 C( G! Y( V5 S是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
! O/ v; v: b' ~5 ^$ I7 r2 h: M初学者,还请见谅! |
|