|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
6 ~* q* w3 e, Qpublic void spreadMoney(int money){7 C8 Z; {$ T& t/ G. ~! j
// Randomly place money in moneySpace, W/ S9 \) y8 G4 \3 o- P# g7 X
for(int i = 0; i < money; i++){5 R4 a7 W; ?# I; t5 { C- X
, m: |" k5 j- }' Z5 A% Z4 ]: s
// Choose coordinates
6 c' g$ G& q! [5 C! G- k int x = (int)(Math.random()*(moneySpace.getSizeX()));
2 ?9 ]! C5 q# P int y = (int)(Math.random()*(moneySpace.getSizeY()));$ F7 v1 r0 p1 f( A F+ p
% y5 R- w$ U" s9 I8 }2 ]8 t // Get the value of the object at those coordinates6 Y; G0 Z. h' B1 I
int I;9 T+ L" k; _. P7 Y; ]* g e
if(moneySpace.getObjectAt(x,y)!= null){2 m9 e' {) W l9 q6 z4 S
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
+ c4 u* r8 x. Z }
2 {4 u# E6 f& `9 a' k5 f else{
: k. n6 C6 t5 T0 |+ E4 @) T I = 0;
0 y. S+ Y& s/ f9 ~) H }
( f1 {; y5 ^& S( B4 X // Replace the Integer object with another one with the new value
1 M* [5 G6 e% Q1 K moneySpace.putObjectAt(x,y,new Integer(I + 1));
( V% E! Q; I2 H- o, s+ D }
* T6 A4 P; v' M6 I, J* ]6 x这里面if(moneySpace.getObjectAt(x,y)!= null){8 b+ |* _. K% T: w- g* H
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();2 \$ Q) _) V* J, N
}3 Y: U+ Y+ {+ [6 W- z& E# r
else{" ^5 e5 Y: b+ d n& _/ u
I = 0;
- c2 q3 z( d( W7 w* D# j- [是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?7 x* U3 L2 N- ^4 t, l& r+ x
初学者,还请见谅! |
|