|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
4 r" h5 T+ y0 ?; z7 ^8 c+ v; F, qpublic void spreadMoney(int money){0 x. w+ m" k9 K2 y+ F4 g! M
// Randomly place money in moneySpace
) N, W; X: ]# Q' ~! f/ J for(int i = 0; i < money; i++){* L& a" o9 K6 ^7 l+ l2 ?
/ c$ p( G4 ]5 _. q
// Choose coordinates; }5 z9 q0 t; B2 E* w6 b9 I
int x = (int)(Math.random()*(moneySpace.getSizeX()));
6 H- o! g' U; M1 G9 y, @ int y = (int)(Math.random()*(moneySpace.getSizeY()));
; R' z n) u( E" P! Y9 {6 o1 a$ X( D$ J7 t8 M
// Get the value of the object at those coordinates6 ~5 s( a, P% o$ Q# m8 S
int I;8 M7 ~, s& \ R5 P- m
if(moneySpace.getObjectAt(x,y)!= null){# y2 U7 M, |7 m ~3 @
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();/ s; X% ~% O5 ^
}
2 S0 ]9 v6 g6 R1 K9 B7 k I- \ else{& R, U3 z; p& c( p J
I = 0;
) X, |8 m& ]" M }; A2 y! e6 @/ S8 r- X7 {
// Replace the Integer object with another one with the new value, t& ~4 ]/ I# A5 t" K
moneySpace.putObjectAt(x,y,new Integer(I + 1));
j4 A7 b" N, ` }
8 J5 W& N3 ?6 @% p' }' U这里面if(moneySpace.getObjectAt(x,y)!= null){
6 D8 w5 O! ~& ~7 S I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
( j, _; j, L. b3 {. B" m }$ w) O5 Q& l6 V# i: h
else{$ C/ J* u) K+ w u9 V6 b, J% H
I = 0;
h2 f$ i1 z% A ? e$ t是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?5 n3 D7 ]0 G8 F$ w
初学者,还请见谅! |
|