|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
1 ]- f& ?, m# c+ N* ~, A) rpublic void spreadMoney(int money){) n" s# d: Q2 r
// Randomly place money in moneySpace) u! @% W* W9 \
for(int i = 0; i < money; i++){
4 ~0 a" k7 U/ f0 H' P* }+ s# @7 q, q5 n& c% Z, u @- L
// Choose coordinates& g9 S( G, }7 |* O1 y
int x = (int)(Math.random()*(moneySpace.getSizeX()));6 g7 R/ T9 J* {
int y = (int)(Math.random()*(moneySpace.getSizeY()));
# g% K0 c; |2 I$ x G2 y+ V4 Z; K6 n7 D" w. @
// Get the value of the object at those coordinates
* @; P8 [5 ]8 G. o# @& z int I;- v( g# W& @- V( O9 R+ {' z# w
if(moneySpace.getObjectAt(x,y)!= null){, u4 Z3 m- X6 K- P) E0 t+ L
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();* U6 K/ } Z4 _. @5 f
}1 ^' u7 `0 {( l! j# Y3 W9 d, d9 o
else{
: e8 V/ Q: Q$ s2 V$ r I = 0;: N1 A9 G/ M3 w1 ]. M) ]
}
$ U! I# }, d. g, s // Replace the Integer object with another one with the new value
% E. X5 T" d, W5 l- Z, S& k+ X moneySpace.putObjectAt(x,y,new Integer(I + 1));8 b# O% n, b, e" n: _+ U6 V" Y7 f
}1 f& _2 o, e; t6 {
这里面if(moneySpace.getObjectAt(x,y)!= null){: d% ]+ R4 M2 X& Y6 Z8 M
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();0 f. j. s, q# g8 q1 t& o: p
}3 `/ O% q4 `$ f7 |( e
else{
- t( U: o' L3 B1 d% B I = 0;
4 ^* P$ F( J+ t3 ~! t是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
b8 j2 p8 |5 K7 |- n" ]+ R初学者,还请见谅! |
|