|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
: w2 R1 Z6 f h) Vpublic void spreadMoney(int money){& @$ p# \3 e3 A- t7 c0 R; A8 ^1 Y
// Randomly place money in moneySpace# g9 S) a% O* K
for(int i = 0; i < money; i++){; v1 z1 v7 V" p3 u) p3 D# R) g
9 i2 x- \' v7 V2 e+ W, d) ~ // Choose coordinates9 f) h# k6 |. n( I; n8 x
int x = (int)(Math.random()*(moneySpace.getSizeX()));
/ {0 |7 W# `' n$ O int y = (int)(Math.random()*(moneySpace.getSizeY()));
4 t( H0 Y" S) v- o2 l0 G, x6 P. n4 ?0 J1 ^: D' H
// Get the value of the object at those coordinates% G' p G8 ?/ g; p& J, p
int I;
E' m0 s, S& v if(moneySpace.getObjectAt(x,y)!= null){- K W# Y0 b2 A1 E
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();: P. X( Y# m$ `: S$ H
}
) H# p! o" D' }0 k/ Y5 ] else{
! A3 F. ]- ?2 C3 @! B$ D% a; ?6 b& \ I = 0;/ u" L B6 b0 e2 v0 j
}1 v+ v7 ?2 F* u$ e
// Replace the Integer object with another one with the new value5 q: E- ^7 O: u1 v
moneySpace.putObjectAt(x,y,new Integer(I + 1));
" p' o. m5 Q4 j! p }
( C$ @% Q! F' [/ i! ^这里面if(moneySpace.getObjectAt(x,y)!= null){: D# ^, J* j0 w; a' M0 y* d1 L" {
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
2 x' S! I* Y3 X6 ?3 z; }" ? }
$ V' d" | T/ k N; o else{
9 j; d r0 @( D& F I = 0;
+ L& x2 M3 `$ H+ _$ o# o是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
" ~- y4 ^7 N/ Q5 c初学者,还请见谅! |
|