|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中# L: S0 J7 c/ y0 K, K- Z% {! \
public void spreadMoney(int money){
9 P6 ~# l( G* A6 ]. Y // Randomly place money in moneySpace ~# X) U$ z1 {0 }/ J5 D( a
for(int i = 0; i < money; i++){* C" q9 ~- y: }
& U3 Z X/ U1 L# v9 M // Choose coordinates8 P# |4 { y# s. \
int x = (int)(Math.random()*(moneySpace.getSizeX()));
$ \: q! A* Q2 i' n int y = (int)(Math.random()*(moneySpace.getSizeY()));+ Z5 j! {. X' n* y. i
* B' D! D& c4 \3 b. v1 W: H
// Get the value of the object at those coordinates4 e* Y- b$ X) u% i- n
int I;
4 \ n! ^. |$ \0 w% s" s& s+ y if(moneySpace.getObjectAt(x,y)!= null){
# i6 O) _9 M3 m3 g9 r) m4 b5 Q9 C I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
6 J* h& ~4 c, l; K" V$ [0 q }6 s3 k) A2 H2 G- Y. j8 K
else{
8 f. O; D |. K) i. t/ B I = 0;
0 E3 X8 [, r. | }
/ z. i/ {7 W1 h8 J% z8 w1 R // Replace the Integer object with another one with the new value
4 K9 P6 c; f7 I' [+ ^; I: d moneySpace.putObjectAt(x,y,new Integer(I + 1));
( t: ]) g1 ^& S. F: ~9 D% V% X }
5 ~9 {. w! K- v- w这里面if(moneySpace.getObjectAt(x,y)!= null){
/ y8 y* p2 p$ @. W( E( V, J/ o- f% o I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
# }* D1 {- G% }7 W2 Z, p/ p2 k }
0 P4 _5 ]$ Y$ Q5 I9 T4 u. W else{) n9 ?1 N0 P$ Y1 }: x0 p
I = 0;
3 T0 X- U8 i( Y$ \# W# D D是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?8 I5 h# _3 f: N2 o# U
初学者,还请见谅! |
|