|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中# E% ?$ X+ I# a0 e, s
public void spreadMoney(int money){' n7 P2 _5 O6 B2 w/ X* M# J
// Randomly place money in moneySpace
0 s2 p0 h2 p- D' }- ? for(int i = 0; i < money; i++){
( ~# S5 w" G4 f7 _& p
: u/ ^1 L7 E( y2 o, F. @0 _! B // Choose coordinates) R( `2 T/ S* p) O' s7 C% p
int x = (int)(Math.random()*(moneySpace.getSizeX()));
* }) c/ T# D4 {6 ~ int y = (int)(Math.random()*(moneySpace.getSizeY()));8 q2 H" w1 A( {! ^& v% q! X
% m% I% c" K( J" q/ ]3 W
// Get the value of the object at those coordinates* m0 g5 p; Z" G3 c5 W
int I;
7 E3 H, T* H7 `4 o3 D, d# j' {, n if(moneySpace.getObjectAt(x,y)!= null){
+ H. @, J, ?& l- c! n3 X I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
, a4 c, E! p1 ?2 ^ }
$ \* q0 u2 e" w7 l* C2 W+ H else{
9 X" i& n& b: E% Z4 f I = 0;
; l% M! f: k. d" Z& t& W }& q& ~0 V/ W' n9 V4 \
// Replace the Integer object with another one with the new value1 L; i( ]& t* o; T. O
moneySpace.putObjectAt(x,y,new Integer(I + 1));. S9 I# I& s& Y/ E! `( T
}" f8 R7 J+ S/ @# m2 t
这里面if(moneySpace.getObjectAt(x,y)!= null){
: ^5 U. L, t8 ^6 c/ g& D n I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
4 p- z, e7 f- e. _ u }2 f2 R, I1 @; D+ R$ x9 C# D
else{
* ~; F, U6 S% |+ X$ h I = 0;
8 ?8 j4 e1 u! m. L8 v5 e% C" r是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?
& y" x+ Z# J% U; j0 A E4 V/ w9 |& E初学者,还请见谅! |
|