|
|
大家都看过carrydrop的程序吧,里面设置moneyspce时将钱任意分布在空间中
5 L, k3 W! X. x6 e+ r% Q! r- R. tpublic void spreadMoney(int money){
) t$ @& e6 ], W5 O // Randomly place money in moneySpace
8 L) D" D: e7 e0 T for(int i = 0; i < money; i++){0 @. K: D/ W, V. s% b- g {
" [( B. l% F: q# d/ n' V
// Choose coordinates
! W0 j5 ^9 ^- |) k- ?3 U int x = (int)(Math.random()*(moneySpace.getSizeX()));" v$ F+ j, U7 J4 z7 o' U8 B
int y = (int)(Math.random()*(moneySpace.getSizeY()));+ D9 g2 a+ G+ q8 ?& U' Z U" p
' S- h5 _, ~ d, N5 [
// Get the value of the object at those coordinates
( y; O9 e3 ?+ C! O' `3 n int I;
3 M, Y: I+ c! S1 W* {; m+ U if(moneySpace.getObjectAt(x,y)!= null){
L7 p9 U. P2 U$ Q; W' h I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();% }# @$ J" l1 v/ m
}' Y( p/ W& R" [$ a4 R1 I
else{ X9 W) q5 @2 O, K& Q
I = 0;
C" W! D8 s9 K" L* r }# K, R9 i5 }) [" @$ l; N! x- Y5 }
// Replace the Integer object with another one with the new value
, g& ?1 h0 K* y0 t0 ~: W. e moneySpace.putObjectAt(x,y,new Integer(I + 1));
/ v6 Y1 `' G9 N }
- W- v; J% S D这里面if(moneySpace.getObjectAt(x,y)!= null){+ s$ P+ ?! d8 y* Z2 Z
I = ((Integer)moneySpace.getObjectAt(x,y)).intValue();
- y6 c! ^1 e/ z) F4 R% A+ H- u' { }0 @( L! p+ M/ B4 a& S4 t2 Z
else{3 ^0 j# Y. K, p
I = 0;
: `; X; J+ V8 W/ }$ c是在干什么啊?为什么后面的moneySpace.putObjectAt(x,y,new Integer(I + 1));要用new Integer(I + 1),这都在原来钱的基础上加一啊,我怎么看不出来是随意放置钱财的呢?' q* e' w6 u& F0 k3 u
初学者,还请见谅! |
|