|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中1 g+ F: ^# a( ?; e
public void spreadMoney(int money){) ~2 c, R# @0 c
// Randomly place money in moneySpace0 R8 a. z H4 G6 ]# w
for(int i = 0; i < money; i++){
; C }# t; N5 D( J. I1 w2 u9 v; @2 ~( x7 X
// Choose coordinates
- i" E" Q& v( a8 V% d4 j' { int x = (int)(Math.random()*(moneySpace.getSizeX()));
+ Y: }' J# u- C$ a int y = (int)(Math.random()*(moneySpace.getSizeY()));& {1 [/ T$ v+ d- X5 U' t* a; }
7 O4 X. Z. Z: Q0 Q" u" {) ^3 F9 V/ x \
// Get the value of the object at those coordinates
' D6 ~' D) w6 e# w6 h3 I int I;$ [+ ^3 ~# |) x% Z) _
if(moneySpace.getObjectAt(x,y)!= null){
: {1 ]0 }" ^; W I = ((Integer)moneySpace.getObjectAt(x,y)).intValue(); S; H2 O3 C) f& U; F
}; g: Z/ q2 s- A4 s# d6 ^+ u5 F5 T6 B. |
else{' w6 _' f3 c7 r3 G7 U2 W
I = 0;
3 \" ]9 x# u @6 l }, |4 Q- } ]7 Q# R( Q; W- j
// Replace the Integer object with another one with the new value
) ~+ e! l# x# C- D( R ]+ w moneySpace.putObjectAt(x,y,new Integer(I + 1));! v# `% h7 u7 g {
}
2 L$ p! a0 G1 i8 ^这里面if(moneySpace.getObjectAt(x,y)!= null){# f* ^' F% I4 v: H% h T
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();4 J- z% v% } e; H H; v+ Q
}
3 q' _! i4 g) F" p! D6 _4 K9 m y else{
+ g8 B6 i; m: O* P I = 0;' J# q0 C( n: r0 O% o# z4 U
是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?, k4 C) {# ?+ `, C# `
初学者,还请见谅! |
|