|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中* ?0 n" N% K" @1 m9 W) h! R
public void spreadMoney(int money){. J( _6 B! C! B( ^
// Randomly place money in moneySpace# R- z3 r I, z2 ?# G& e: i7 w' L
for(int i = 0; i < money; i++){+ A: Z( d. ?$ j4 t; c: Y
* V5 E/ r* K* C( U/ H5 C) P$ Z, [ // Choose coordinates
8 |% t9 w' @. r1 P int x = (int)(Math.random()*(moneySpace.getSizeX()));' D Q/ v4 ~ C0 ^) i0 z
int y = (int)(Math.random()*(moneySpace.getSizeY()));
% e \8 V/ ]9 A. q; H' Q4 x8 B7 h8 C' n8 e3 Q
// Get the value of the object at those coordinates
9 I& T/ E2 | |: p5 t int I;- {6 i' }0 p4 `& M# z: ~: t
if(moneySpace.getObjectAt(x,y)!= null){
- Y6 W* i- F8 k I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
$ ?& @- d9 a; \; N9 s }
0 h$ m6 x/ b) [0 s else{
, K( p. s& p9 F2 E, ^) M I = 0;& Q3 _, [9 S! |
}
- g) `3 g7 @! ~2 k' [ e1 m" [ // Replace the Integer object with another one with the new value# P* e8 W' D. L4 x# [2 D, L, [
moneySpace.putObjectAt(x,y,new Integer(I + 1));
; |. V- h* q# e, J/ E/ ]- O }7 q# `- n6 f: v' M4 `2 R
这里面if(moneySpace.getObjectAt(x,y)!= null){
" r6 K* k3 e* | T1 k) H8 G3 J I = ((Integer)moneySpace.getObjectAt(x,y)).intValue(); n. L7 h( E( L6 m, B3 A5 w u @
}& ~% j% p3 z% ~
else{
: b# |& o8 M$ J: ~7 Q I = 0;
) K, a6 s( ^4 b& w q$ ^是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?; Y2 S% y5 ~$ f* z( w
初学者,还请见谅! |
|