|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中5 p- ^; h& W7 T* L# I* d
public void spreadMoney(int money){2 N; D& H" r3 \- G
// Randomly place money in moneySpace
5 ]0 z$ e/ t% C. l, g for(int i = 0; i < money; i++){
2 P; i2 ~7 w, t, \: |1 o" v
$ g J/ p; \8 ?, G# Z! g // Choose coordinates* x9 M9 w: L! f6 ? W: Y
int x = (int)(Math.random()*(moneySpace.getSizeX()));
4 F* Z7 @8 L, Q6 [5 {7 Q/ C int y = (int)(Math.random()*(moneySpace.getSizeY()));
2 G/ P4 p Y2 ]7 z. C; B2 r5 ^
& P) S$ j0 X8 u, L // Get the value of the object at those coordinates6 F7 E& \* M) H- f) k
int I;
- X- V5 H+ V7 {4 ?, [* q: G8 h) ~ if(moneySpace.getObjectAt(x,y)!= null){
3 c: l% l& j6 @1 T. a I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
' l2 D* }+ A0 `, \% X }8 ]2 l8 A: L" n5 |9 }4 L3 v) A! }6 ?
else{
* c& ?& x) n9 X. `7 g$ S& T$ b5 e I = 0;. |, }6 n. o8 f; s. Z% M t; I
}9 e, ?6 ]5 P; \# h
// Replace the Integer object with another one with the new value
* `) T9 J* D# U( \8 ]- ? moneySpace.putObjectAt(x,y,new Integer(I + 1));
3 Y7 D% \0 y1 {( @0 w! m; Y0 ~& P }# ]7 S8 C$ z) H% |, x
这里面if(moneySpace.getObjectAt(x,y)!= null){
) y7 v6 l& t0 H' t# i I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();5 `' m% K; C4 | K- q0 U. ^
}
- Q/ o0 B4 m( e& O4 X" t/ J else{
1 O, n" o2 D- k1 z& ], h I = 0;
' c6 y1 ^: A& H是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?( t3 e2 @+ X6 M, V( x1 T+ z- W$ j5 X
初学者,还请见谅! |
|