|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
; z3 R# Y! }' r8 f4 G& w O- V& r8 ppublic void spreadMoney(int money){
: w6 u s, }9 k4 ~ // Randomly place money in moneySpace
$ @. W; T& I, N4 K2 v( a for(int i = 0; i < money; i++){
! W' w# x; B6 N o- H6 X$ S' g# I+ F% v" D+ ^: z
// Choose coordinates, y# f* Q v0 E/ q) ?. Y
int x = (int)(Math.random()*(moneySpace.getSizeX()));
' J/ O+ E; H. o) Y' N2 ? int y = (int)(Math.random()*(moneySpace.getSizeY()));
4 @1 d( d2 h3 }9 O8 x: k+ h' L& D) A0 G; C. ]2 g+ `0 ^4 n* y5 t
// Get the value of the object at those coordinates
: @; Y3 k5 ^8 L# M" X- g* ~: { int I; @2 F& q, X" s& B1 z* _3 e
if(moneySpace.getObjectAt(x,y)!= null){1 N+ b1 M% Q P, a5 ~# L
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();0 H1 k. O9 i- M5 R r( b B
}
. R& t- _+ @5 Q/ r' S- K) d else{
5 V6 l" ]+ ?+ c0 H9 p/ p I = 0; U0 l- N( r* X2 ~: N8 Y
}
' I1 v4 X! t, u! k a O& i# U // Replace the Integer object with another one with the new value
; I, e$ v) J# D" g* f moneySpace.putObjectAt(x,y,new Integer(I + 1));5 V/ d: }8 W4 K- D4 a- I/ e3 ~6 j
}
. y4 G4 m+ N! c1 \& P这里面if(moneySpace.getObjectAt(x,y)!= null){
8 o( q) _. [( {2 M# a# e, S% Q I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();+ p- D v7 K0 L7 P
}
7 k! e( }# C" E( d4 F else{
6 h) x$ ]" d* E. J2 q I = 0;( J0 Q+ m1 x6 y* S& \( p7 ~
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
$ q( t6 i- z5 H: m% q7 x3 y初学者,还请见谅! |
|