|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
/ j/ L6 H9 L# _public void spreadMoney(int money){
* n7 t# G0 x4 H& J // Randomly place money in moneySpace. X1 f1 |) O4 L$ g: S
for(int i = 0; i < money; i++){6 R. s* @. `& L1 u- w y6 v" [
% S4 g9 q9 l b& g1 W \# R
// Choose coordinates
" X. @3 n# u( v5 E; {, I8 I int x = (int)(Math.random()*(moneySpace.getSizeX()));* h. l' H/ b, ?$ Y" s t! F
int y = (int)(Math.random()*(moneySpace.getSizeY()));( x6 S4 |9 F# G% q) ], b
; O9 [2 ]* p% g- b' Y M // Get the value of the object at those coordinates" n7 A' J- ]& {1 W6 `2 K
int I;# j) h, c& }1 Z1 Z
if(moneySpace.getObjectAt(x,y)!= null){" r' i4 l# a9 F
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();6 j, u$ q1 |/ }
}
8 ?! `4 ?" n {; M" n- |; {' M else{2 ~+ h" c' ?7 r5 _* I# B
I = 0;
. Z# O: R% m$ ]- Q9 I }
- u' t; v% k0 m+ H2 c // Replace the Integer object with another one with the new value1 @+ w/ ~: \* Q1 M, Y" J
moneySpace.putObjectAt(x,y,new Integer(I + 1));* ^ |8 A& r' `
}' }1 ~4 t/ T5 {& v3 n
这里面if(moneySpace.getObjectAt(x,y)!= null){
) c+ C: p6 }/ Y% h9 K k I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
) L" k! S: h0 P" K% O! A7 K }
7 L. t1 |2 w- R d& b& ` else{* y+ o$ c% j8 x# M' H$ K; x* C
I = 0;
6 M [$ {6 m; u+ @7 R; j是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?* T& X! ?, D2 G8 ]# W$ g; b! ]
初学者,还请见谅! |
|