|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
) r0 L/ N6 e/ h9 r% Cpublic void spreadMoney(int money){
" \* H4 a; Y: X // Randomly place money in moneySpace
5 G8 `8 j% t; s* h6 l% ]8 w0 R1 P) B for(int i = 0; i < money; i++){
6 R4 g2 R1 p, m$ D; a1 A* T4 l, R5 e5 z: D2 [/ @/ E. B) s
// Choose coordinates
; D' p* w( P- n: \; w int x = (int)(Math.random()*(moneySpace.getSizeX()));
& W1 [9 h T" l$ h' j int y = (int)(Math.random()*(moneySpace.getSizeY()));
7 l) v3 ^0 N& U& O6 `6 C. m
j) ]8 w$ `4 l, b( _ // Get the value of the object at those coordinates% d$ j4 l5 c ?( a5 w
int I;; O3 L! V( P$ I' [
if(moneySpace.getObjectAt(x,y)!= null){4 [+ W3 Z( @" E! W0 t- H* r0 G
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
6 c" u2 q/ c4 g7 F }5 a% ~$ |$ r5 E) P* @
else{" S2 G, ]# b) L I5 ?
I = 0;+ ^3 Z$ Y* x0 y9 o' ^# J
}. B4 P p7 b; D6 E# |! u
// Replace the Integer object with another one with the new value' c3 p, g- L' z
moneySpace.putObjectAt(x,y,new Integer(I + 1));
% y4 e! h: Y! l) p K- n }" A: Q; k- e# a/ ^9 L6 u5 R0 l
这里面if(moneySpace.getObjectAt(x,y)!= null){5 x+ b3 E5 X! H/ v9 D8 q
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue(); U8 M0 h( C2 l% ?/ R1 r, J8 |
}
( q% m0 O( M4 f2 R. t else{
1 v0 i) O+ l" ~3 a! O. t I = 0;
3 b# f5 f6 h6 h是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?$ ?. z1 d) w( G- n/ X
初学者,还请见谅! |
|