|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中: T7 Q7 [; _- I2 G
public void spreadMoney(int money){/ i- N2 z+ G/ h1 ~
// Randomly place money in moneySpace
5 I/ J8 F0 K1 Z0 `% g0 W! A# J! d for(int i = 0; i < money; i++){9 q+ t4 [* D2 S5 h( d* d
8 f, d5 p- g5 P4 b // Choose coordinates5 P# C+ \ q1 E( t' G
int x = (int)(Math.random()*(moneySpace.getSizeX()));
; `# m5 a. ]' z: z# x+ {! k int y = (int)(Math.random()*(moneySpace.getSizeY()));
D- z9 f' ^ r. t; n8 }; M& c) ]% q m" i" m% W% j7 W. f' ~/ t
// Get the value of the object at those coordinates
9 i5 Z. m5 C9 p7 L( S int I;8 [1 {4 o% V6 h
if(moneySpace.getObjectAt(x,y)!= null){
) v) Y7 A" ]+ m/ V* @ I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
' D2 j4 z, S; b2 {% ^ }* j$ o$ F& o( F3 Z) Z4 _
else{, W: ~$ u l# J) W/ V
I = 0;
8 q. V5 f. o; u+ i+ e }
# ~* N1 o5 O" ]' l! x F& T( D // Replace the Integer object with another one with the new value6 q, k% b! y0 i: W
moneySpace.putObjectAt(x,y,new Integer(I + 1));
7 `+ ^/ B G/ B+ u# X w }0 T) [9 s% {, o# N% M
这里面if(moneySpace.getObjectAt(x,y)!= null){
! B6 E+ X! L A( F% B7 q I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();- B% b2 @! L$ l/ S, A
}
2 ~. r- D( _4 _( a+ b1 g1 c4 Q4 n else{+ f( x" H i; U, \7 X* M! f
I = 0;
" ^( ^- W+ q; t! s$ {' M e是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?6 a3 N" x& f( l; j
初学者,还请见谅! |
|