|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中% N0 H+ Y4 Z* e2 A# w- d- i
public void spreadMoney(int money){* U' H2 [" S$ o2 g+ X% Z
// Randomly place money in moneySpace
% j7 m+ h7 ~8 C& R* k for(int i = 0; i < money; i++){
. ?# l* n( l6 S: Y4 \5 o4 C& B. M; o4 \3 T: u# J+ N
// Choose coordinates
. Q; t4 L! l' t! a1 N int x = (int)(Math.random()*(moneySpace.getSizeX()));$ z* M) w( H8 O% O2 C3 K3 x
int y = (int)(Math.random()*(moneySpace.getSizeY()));
9 }# H" w9 w" O( C+ m: ~/ r @$ m( X7 T
// Get the value of the object at those coordinates) Q+ }& [: }, N. p. H1 \
int I;9 E/ Q! L$ C7 ^, r
if(moneySpace.getObjectAt(x,y)!= null){
7 {. F6 d5 b" v9 @+ \, j" b8 r I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
6 |4 Q# w# T+ u2 d! X }! r- i# Z# ]' s
else{" G; z" O! C" s3 M
I = 0;
0 `. @. Y. r; B0 M: F' ` }5 q1 H1 o: p! h& C# v% G( b0 P% c
// Replace the Integer object with another one with the new value2 h4 d5 H' h4 T
moneySpace.putObjectAt(x,y,new Integer(I + 1));
; F" q$ G: K) `) x& j% p, E/ | }3 d& ]8 S/ ~% C) D8 e
这里面if(moneySpace.getObjectAt(x,y)!= null){
: X! |, K6 `& W. Y( G I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();8 n% c H- Q+ d/ w
}2 l0 L# Y) T8 I: l
else{
1 S: [) f1 H: ^! x- m9 H* Z I = 0;
$ q% r! L' q. d: O: K是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
3 ^- F4 f, }! @6 G& ]5 _. h初学者,还请见谅! |
|