|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
! T' w b7 k. f( E9 Epublic void spreadMoney(int money){
* J/ `" O6 O: j4 @% c, k // Randomly place money in moneySpace
: A1 q3 D! y7 O! Z r$ q) d for(int i = 0; i < money; i++){
2 C) f1 a4 k. @& A
; d- D) v( L5 e& d6 V // Choose coordinates
! |' I v* S& U* }; V Y int x = (int)(Math.random()*(moneySpace.getSizeX()));3 S$ ~( x1 B, ]$ d1 D. b- l
int y = (int)(Math.random()*(moneySpace.getSizeY()));9 C: K* X# o( _: @
p; o. d1 @' d; b( r+ U // Get the value of the object at those coordinates
' B7 x# d* \6 Z9 _: N" J int I;
?! a- O' i* ^/ U5 O$ Z- \5 F if(moneySpace.getObjectAt(x,y)!= null){
, ?" O& x% |: O" W, F- |8 ^( L I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
* ]4 E. h& X/ \9 w" |. E }
6 [0 Y& L# S) R+ o else{
7 x5 }0 f+ h. s; N I = 0;
; U0 v8 o4 P \" M" V/ r0 e }/ q$ d4 _4 i/ s( w& ?7 ]. p. ?
// Replace the Integer object with another one with the new value
0 d- J5 {) C$ @, F9 n moneySpace.putObjectAt(x,y,new Integer(I + 1));; w3 X* S5 n" B6 h- V, Z
}9 {0 D8 ~( t+ b" e5 b# O0 J
这里面if(moneySpace.getObjectAt(x,y)!= null){
% k [3 ~8 m5 a! c! t4 a5 ?( \ I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
' V: m. u' [' ?2 o5 Q+ _% H3 p$ j }# M' b* E& B1 [0 y( M+ V. ?
else{; m! u9 f4 n' b6 h+ N+ h; G
I = 0;
7 ?0 V, x4 w' ^; P0 M$ N2 g( w/ H8 Q是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?" ^1 t& n0 R* a8 [1 M8 _! x
初学者,还请见谅! |
|