|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中% `5 {- @* D9 x! ~- Z& Z# _
public void spreadMoney(int money){2 y" m* ` M9 Y; G- l! t
// Randomly place money in moneySpace
, U. H, G+ t, j; |. ` for(int i = 0; i < money; i++){
0 s0 w' ~" }; I. \! ^/ Y+ p
3 L. h2 H0 s0 L; O3 D! X // Choose coordinates! r8 P" @/ }! m
int x = (int)(Math.random()*(moneySpace.getSizeX()));, L+ M& }& k( k
int y = (int)(Math.random()*(moneySpace.getSizeY()));
. Z" o" d$ O' h& E0 F: H& v2 v
, C* F$ M4 G( R* c% ?7 Z! x // Get the value of the object at those coordinates
8 t% E9 O2 v: _9 B( t W int I;
/ S; }% {1 {6 m) w6 L F/ ^' z) y6 U if(moneySpace.getObjectAt(x,y)!= null){ [1 _1 ~$ k# F# n$ p
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
3 T$ x7 [. Q7 t( k! l }! B; V$ {' ?" ~4 q" i
else{ d8 _! v6 j7 @! E0 k
I = 0;
: E3 b6 b0 S" u& \9 Y }
9 Y. R* z* @9 K6 P+ g6 n" b // Replace the Integer object with another one with the new value
% Y1 U4 q1 D( p$ P moneySpace.putObjectAt(x,y,new Integer(I + 1));/ k( j% h! Q, e1 H6 m* K6 D
}
# n6 k* Q7 F3 w$ _7 c这里面if(moneySpace.getObjectAt(x,y)!= null){
9 c c' m7 ^2 u0 l* E } I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();5 t% n4 K" C* u$ w* ?, S& w( c
}
% M( r5 B2 b. R* J7 | else{
/ V1 H% I3 F4 n I = 0;
2 n, I: X; g$ ?$ Z# j是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?4 O1 l/ e, f% q; r: }$ z; D
初学者,还请见谅! |
|