Anroid
gameStart()
package
game.game;
import
java.util.Random;
import
java.util.Vector;
import
android.content.Context;
import
android.content.res.Resources;
import
android.graphics.Bitmap;
import
android.graphics.BitmapFactory;
import
android.graphics.Canvas;
import
android.graphics.Matrix;
import
android.graphics.Paint;
import
android.graphics.Paint.Style;
import
android.media.MediaPlayer;
import
android.util.Log;
public
class
gameStart{
public
gameMain
father
;
public
Bitmap
bk
;
public
Bitmap
Player
;
public
int
px
,
py
;
public
int
speed
=10;
public
Bitmap
imFruit
[];
public
int
speedtime
;
public
Bitmap
winbk
;
public
int
screenW
,
screenH
;
public
Vector
fruit
;
public
int
level
=1;
public
int
pframe
=0;
private
long
levelStartTime
;
private
int
score
;
public
int
tnum
;
public
int
snum
;
public
static
int
FruitSpeed
=2;
public
static
long
drawtime
;
public
long
levellastTime
;
public
boolean
dir
=
false
;
public
boolean
zoom
=
true
;
public
Bitmap
meirenyu
;
public
Bitmap
sudu
;
public
Bitmap
shijian
;
private
int
levelCount
=20;
public
int
count
=0;
public
int
cout
=0;
public
Bitmap
lost
;
public
int
defen
,
fenx
,
feny
;
public
int
jineng
=0;
private
int
a
=0;
public
int
ji
=0;
public
int
Zframe
=0;
public
int
levelTime
[]=
{
0,200,198,196,194,192,190,188,186,184,182,180,
178,176,174,172,170,168,166,164,162,160
};
public
int
levelTypes
[][]={
{0,0,0,0,0,0,},
{0,0,0,1,1,1},
{0,0,1,1,2,2},
{0,0,1,1,2,3},
{0,0,1,2,3,4},
{0,1,2,3,4,0},
{0,1,2,3,4,1},
{0,1,2,3,4,1},
{0,1,2,3,4,1},
{0,1,2,3,4,1},
{0,1,2,3,4,1},
{0,1,2,3,4,0},
{0,1,2,3,1,1},
{0,1,2,3,1,0},
{0,1,2,3,1,1},
{0,1,2,3,4,0},
{1,0,2,3,2,1},
{0,1,2,3,1,1},
{0,1,2,3,1,0},
{0,1,2,3,1,1}
};
private
int
fruittime
[]=
{
30,32,34,36,38,40,42,44,46,48,50,51,52,53,54,55,56,57,58,59
};
private
int
levelScore
[]=
{
0,500,600,700,800,900,1000,1200,1400,1600,1800,2000,
1000,2000,2000,2000,2000,1000,2000,2000,2000,2000
};
private
long
lastTime
;
private
int
countFruit
=1;
public
Random
random
;
private
MediaPlayer
win
;
public
MediaPlayer
pengzhuang
,
jinengl
;
private
Bitmap
fanhui
;
public
int
xuanze
=1;
public
gameStart(gameMain father){
this
.
father
=father;
random
=
new
Random();
imFruit
=
new
Bitmap[5];
fruit
=
new
Vector();
screenW
=father.
screenWidth
;
screenH
=father.
screenHeight
;
px
=
screenW
/2;
py
=
screenH
-50;
initBitmap(father);
initLevel(
level
);
}
private
void
initBitmap(Context context) {
//
TODO
Auto-generated method stub
//Log.i("2", "ok");
Resources r=context.getResources();
try
{
bk
=BitmapFactory.decodeResource(r, R.drawable.
stbk
);
Player
=BitmapFactory.decodeResource(r, R.drawable.
player
);
imFruit
[0]=BitmapFactory.decodeResource(r, R.drawable.
caomei
);
imFruit
[1]=BitmapFactory.decodeResource(r, R.drawable.
huanggua
);
imFruit
[2]=BitmapFactory.decodeResource(r, R.drawable.
xihongshi
);
imFruit
[3]=BitmapFactory.decodeResource(r, R.drawable.
xiangjiao
);
imFruit
[4]=BitmapFactory.decodeResource(r, R.drawable.
beizi
);
lost
=BitmapFactory.decodeResource(r, R.drawable.
lost
);
winbk
=BitmapFactory.decodeResource(r, R.drawable.
winbk
);
meirenyu
=BitmapFactory.decodeResource(r, R.drawable.
meirenyu
);
sudu
=BitmapFactory.decodeResource(r, R.drawable.
sudu
);
shijian
=BitmapFactory.decodeResource(r, R.drawable.
shijian
);
fanhui
=BitmapFactory.decodeResource(r, R.drawable.
fanhui
);
win
=MediaPlayer.create(context, R.drawable.
win
);
pengzhuang
=MediaPlayer.create(context, R.drawable.
pengzhuang
);
jinengl
=MediaPlayer.create(context, R.drawable.
jineng
);
jinengl
.setVolume(4, 7);
}
catch
(Exception e){e.printStackTrace();}
}
public
static
Bitmap rotate(Bitmap b,
int
degrees) {
if
(degrees != 0 && b !=
null
) {
Matrix m =
new
Matrix();
m.setRotate(degrees,
(
float
) b.getWidth() / 2, (
float
) b.getHeight() / 2);
try
{
Bitmap b2 = Bitmap.createBitmap(
b, 0, 0, b.getWidth(), b.getHeight(), m,
true
);
if
(b != b2) {
b.recycle();
//Bitmap
操作完应该显示的释放
b = b2;
}
}
catch
(OutOfMemoryError ex) {
//
如何出现了内存不足异常,最好
return
原始的
bitmap
对象。
.
}
}
return
b;
}
public
void
doDraw(Canvas canvas){
switch
(gameMain.
gm
.
gameState
)
{
case
States.
GM_ACTION
:
// Log.i("1","ok");
drawBack(canvas);
drawPlayer(canvas);
makefruit();
drawfruit(canvas);
drawscore(canvas);
break
;
case
States.
GM_WIN
:
drawWin(canvas);
drawInfo(canvas);
break
;
case
States.
GM_LOST
:
drawLost(canvas);
break
;
case
States.
GM_JINENG
:
drawBack(canvas);
drawPlayer(canvas);
drawfruit(canvas);
drawjineng(canvas);
break
;
case
States.
GM_PAUSE
:
drawBack(canvas);
drawPlayer(canvas);
drawfruit(canvas);
drawpause(canvas);
break
;
}
}
private
void
drawpause(Canvas canvas) {
//
TODO
Auto-generated method stub
Paint paint=
new
Paint();
paint.setStyle(Style.
STROKE
);
paint.setARGB(255, 0, 150, 200);
switch
(
xuanze
)
{
case
1:
canvas.drawBitmap(
fanhui
, 40,20,
null
);
canvas.drawRect(185,
screenH
/2,215,
screenH
/2+33,paint);
break
;
case
2:
canvas.drawBitmap(
fanhui
, 40, 20,
null
);
canvas.drawRect(305,
screenH
/2, 335,
screenH
/2+33,paint);
break
;
}
}
public
int
drawjineng(Canvas canvas){
switch
(
ji
)
{
case
1:
jineng
+=5;
if
(
jineng
>=
screenH
/2-10)
{
gameMain.
gm
.
gameState
=States.
GM_ACTION
;
jineng
=0;
ji
=0;
return
0;
}
canvas.drawBitmap(
sudu
,
screenW
/2-60,
jineng
,
null
);
break
;
case
2:
jineng
+=5;
if
(
jineng
>=
screenH
/2-10)
{
gameMain.
gm
.
gameState
=States.
GM_ACTION
;
jineng
=0;
ji
=0;
return
0;
}
canvas.drawBitmap(
shijian
,
screenW
/2-60,
jineng
,
null
);
}
return
1;
}
private
void
drawInfo(Canvas canvas) {
//
TODO
Auto-generated method stub
Paint paint=
new
Paint();
paint.setARGB(255,0,0,255);
canvas.drawText(
"
第
"
+
level
+
"
关
"
,
screenW
/2-40,
screenH
/2+20,paint);
canvas.drawText(
"
倒计时(秒)
"
+
this
.
levelTime
[
level
],
screenW
/2-40,
screenH
/2+40,paint);
canvas.drawText(
"
通关分数
"
+
this
.
levelScore
[
level
+1],
screenW
/2-40,
screenH
/2+60,paint);
}
private
void
drawLost(Canvas canvas){
Paint paint=
new
Paint();
paint.setARGB(255,0, 0, 0);
canvas.drawRect(0, 0,
screenW
,
screenH
,paint);
paint.setARGB(255,0, 100, 255);
canvas.drawBitmap(
lost
,
screenW
/2-40,
screenH
/2-40,
null
);
canvas.drawText(
"
您已经通过
"
+
level
+
"
关
"
,
screenW
/2-25,
screenH
/2+10, paint);
cout
++;
if
(
cout
>40)
{
gameMain.
gm
.
gc
.
load
.
process
=0;
gameMain.
gm
.
gameState
=States.
GM_MENU
;
level
=1;
}
}
public
void
zhangyunext()
{
if
(
Zframe
>1)
{
Zframe
=0;
}
else
{
Zframe
++;
}
}
private
void
drawWin(Canvas canvas) {
//
TODO
Auto-generated method stub
canvas.drawBitmap(
winbk
, 0, 0,
null
);
zhangyunext();
count
++;
if
(
count
>=40)
{
count
=0;
try
{
gameMain.
gm
.
gc
.
back
.seekTo(0);
gameMain.
gm
.
gc
.
back
.start();
}
catch
(Exception e) {
//
TODO
Auto-generated catch block
e.printStackTrace();
}
if
(
level
>
levelCount
)
{
gameMain.
gm
.
gameState
=States.
GM_LOST
;
}
else
{
initLevel(
level
);
gameMain.
gm
.
gameState
=States.
GM_ACTION
;
}
}
canvas.clipRect(
screenW
/2-40, 50,
screenW
/2-40+120, 170);
canvas.drawBitmap(
meirenyu
,
screenW
/2-40-120*
Zframe
, 50,
null
);
canvas.restore();
}
public
void
drawfruit(Canvas canvas)
//
画水果
{
for
(
int
i=0;i<
fruit
.size();i++)
{
int
tmp[]=(
int
[])
fruit
.elementAt(i);
canvas.drawBitmap(
imFruit
[tmp[0]],tmp[1],tmp[2],
null
);
}
}
public
int
drawscore(Canvas canvas)
{
Paint paint=
new
Paint();
paint.setARGB(255,255, 255, 255);
switch
(
defen
)
{
case
1:
a
+=2;
if
(
a
>=15)
{
a
=0;
defen
=0;
return
0;
}
canvas.drawText(
"+100"
,
fenx
,
feny
-
a
, paint);
break
;
case
2:
a
+=2;
if
(
a
>=15)
{
a
=0;
defen
=0;
return
0;
}
canvas.drawText(
"+200"
,
fenx
,
feny
-
a
, paint);
break
;
}
return
1;
}
public
synchronized
void
makefruit()
{
countFruit
++;
if
(
countFruit
>
fruittime
[
level
-1])
{
countFruit
=1;
int
tmp[]=
new
int
[4];
int
ftype=Math.abs(
random
.nextInt()%6);
tmp[0]=
this
.
levelTypes
[
level
-1][ftype];
tmp[1]=0;
switch
(ftype)
{
case
0:
tmp[2]=Math.abs((
random
.nextInt()%(
screenH
/2-50)));
break
;
case
1:
tmp[2]=Math.abs((
random
.nextInt()%(
screenH
/2+40)));
break
;
case
2:
tmp[2]=Math.abs((
random
.nextInt()%(
screenH
/2+40)));
break
;
case
3:
tmp[2]=Math.abs((
random
.nextInt()%(
screenH
/2+40)));
break
;
case
4:
tmp[2]=Math.abs((
random
.nextInt()%(
screenH
/2+40)));
break
;
case
5:
tmp[2]=Math.abs((
random
.nextInt()%(
screenH
/2+40)));
break
;
}
tmp[3]=0;
fruit
.addElement(tmp)
;
}
if
(
countFruit
%
FruitSpeed
==0)
{
for
(
int
i=0;i<
fruit
.size();i++)
{
int
tmp[]=(
int
[])
fruit
.elementAt(i);
if
(tmp[3]==0)
{
tmp[1]+=10+
level
*2;
if
(tmp[1]>
screenW
)
{
fruit
.removeElementAt(i);
}
System.gc();
}
else
{
if
(tmp[0]==0){
score
+=100;
defen
=1;
fenx
=tmp[1]+12;
feny
=tmp[2];}
if
(tmp[0]==1){
score
+=200;
defen
=2;
fenx
=tmp[1]+12;
feny
=tmp[2];}
fruit
.removeElementAt(i);
System.gc();
}
}
}
}
public
void
drawPlayer(Canvas canvas)
{
if
(
dir
)
{
if
(
zoom
)
{
if
(
py
>0)
{
py
-=
speed
;
for
(
int
i=0;i<
fruit
.size();i++)
{
int
tmp[]=(
int
[])
fruit
.elementAt(i);
int
x=
px
;
int
y=
py
;
if
(x<tmp[1]-30||x>tmp[1]+4||y<tmp[2]||y>tmp[2]+25)
{
}
else
{
try
{
pengzhuang
.start();
}
catch
(Exception e1) {
//
TODO
Auto-generated catch block
e1.printStackTrace();
}
if
(tmp[0]==2){
snum
++;}
else
if
(tmp[0]==3){
tnum
++;}
else
if
(tmp[0]==4){
//
增加水果
Thread ts=
new
Thread()
{
public
void
run()
{
for
(
int
i=0;i<15;i++)
{
makefruit();
try
{ Thread.sleep(100);}
catch
(Exception e){}
}
}
};
ts.start();
}
else
{
tmp[3]=1;
}
if
(tmp[0]>1)
fruit
.removeElementAt(i);
zoom
=
false
;
Player
=rotate(
Player
,180);
}
}
}
else
{
zoom
=
false
;
Player
=rotate(
Player
,180);
}
}
else
{
if
(
py
<
screenH
-50)
{
py
+=
speed
;
}
else
{
py
=
screenH
-50;
Player
=rotate(
Player
,180);
zoom
=
true
;;
dir
=
false
;
if
(
score
>=
levelScore
[
level
])
{
level
++;
gameMain.
gm
.
gameState
=States.
GM_WIN
;
try
{
gameMain.
gm
.
gc
.
back
.pause();
win
.seekTo(0);
win
.start();
}
catch
(Exception e1) {
//
TODO
Auto-generated catch block
e1.printStackTrace();
}
}
}
}
Pframe();
}
canvas.clipRect(
px
,
py
,
px
+64,
py
+64);
canvas.drawBitmap(
Player
,
px
-64*
pframe
,
py
,
null
);
canvas.restore();
}
public
void
time()
//
计算时间
{
long
currTime = System.currentTimeMillis();
lastTime
= currTime;
drawtime
=
this
.
levellastTime
-(
lastTime
-
this
.
levelStartTime
)/1000;
if
(
drawtime
<=0){gameMain.
gm
.
gameState
=States.
GM_LOST
;
try
{
gameMain.
gm
.
gc
.
back
.pause();
}
catch
(Exception e) {
//
TODO
Auto-generated catch block
e.printStackTrace();
}}
}
public
void
drawBack(Canvas canvas)
//
画背景
{
Paint paint=
new
Paint();
canvas.drawBitmap(
bk
, 0, 0,
null
);
paint.setARGB(255,255,255,255);
canvas.drawText(
"
分数:
"
+
score
+
"
剩余时间:
"
+
drawtime
,5,10,paint);
paint.setARGB(255,40,200,20);
canvas.drawText(
"
第
"
+
this
.
level
+
"
关
"
,
screenW
-50,10,paint);
paint.setARGB(255,200,200,200);
if
(
snum
>0)
canvas.drawText(
"S: "
+
snum
,6,25,paint);
if
(
tnum
>0)
canvas.drawText(
"T: "
+
tnum
,6,40,paint);
}
public
void
initLevel(
int
n)
{
level
=n;
speedtime
=
levelTime
[
level
];
this
.
levelStartTime
=System.currentTimeMillis();
this
.
levellastTime
=
this
.
levelTime
[n];
this
.
score
=0;
if
(gameMain.
gm
.
gameState
==States.
GM_WIN
)
{
gameMain.
gm
.
gameState
=States.
GM_ACTION
;
}
FruitSpeed
=3;
fruit
.removeAllElements();
if
(n==1)
//
如果是第一关初始化道具
{
tnum
=
snum
=10;
}
gameMain.
gm
.
gc
.
back
.seekTo(0);
gameMain.
gm
.
gc
.
back
.start();
System.gc();
}
public
void
Pframe()
{
if
(
pframe
>1)
{
pframe
=0;
}
else
{
pframe
++;
}
}
public
void
move(
int
direction)
{
switch
(direction)
{
case
Direction.
D_LEFT
:
if
(
px
>=0)
{
px
-=
speed
;
Pframe();
}
break
;
case
Direction.
D_REIGHT
:
if
(
px
<
screenW
-50)
{
px
+=
speed
;
Pframe();
}
break
;
}
}
}
J2M2:
gameStart()
import
java.io.InputStream;
public
class
gameStart{
public
Image
bk
;
public
Image
Player
;
public
int
px
,
py
;
public
int
speed
=8;
public
Image
imFruit
[];
public
int
speedtime
;
public
Image
winbk
;
public
Graphics
offG
;
public
Sprite
player
;
public
LayerManager
lm
;
public
int
screenW
,
screenH
;
public
Vector
fruit
;
public
int
level
=1;
private
long
levelStartTime
;
private
int
score
;
public
int
tnum
;
public
int
snum
;
public
static
int
FruitSpeed
=2;
public
static
long
drawtime
;
public
long
levellastTime
;
public
boolean
dir
=
false
;
public
boolean
zoom
=
true
;
public
Image
zhangyu
;
public
Image
sudu
;
public
Image
shijian
;
public
Sprite
Zhangyu
;
private
int
levelCount
=20;
public
int
count
=0;
public
int
cout
=0;
public
Image
lost
;
public
int
defen
,
fenx
,
feny
;
public
int
jineng
=0;
private
int
a
=0;
public
int
ji
=0;
public
int
levelTime
[]=
public
int
levelTypes
[][]={
private
int
fruittime
[]=
private
int
levelScore
[]=
private
long
lastTime
;
private
int
countFruit
=1;
public
Random
random
;
private
Player
win
;
public
Player
pengzhuang
,
jinengl
;
private
Image
fanhui
;
public
int
xuanze
=1;
public
gameStart(){
try
{
random
=
new
Random();
bk
=loadImage(
"/gmbk.png"
);
Player
=loadImage(
"/player.png"
);
InputStream is=
this
.getClass().getResourceAsStream(
"/win.wav"
);
win
=Manager.createPlayer(is,
"audio/x-wav"
);
is=
this
.getClass().getResourceAsStream(
"/pengzhuang.wav"
);
pengzhuang
=Manager.createPlayer(is,
"audio/x-wav"
);
is=
this
.getClass().getResourceAsStream(
"/jineng.wav"
);
jinengl
=Manager.createPlayer(is,
"audio/x-wav"
);
imFruit
=
new
Image[5];
imFruit
[0]=loadImage(
"/caomei.png"
);
imFruit
[1]=loadImage(
"/huanggua.png"
);
imFruit
[2]=loadImage(
"/xihongshi.png"
);
imFruit
[3]=loadImage(
"/xiangjiao.png"
);
imFruit
[4]=loadImage(
"/beizi.png"
);
lost
=loadImage(
"/lost.png"
);
winbk
=loadImage(
"/winbk.png"
);
zhangyu
=loadImage(
"/zhangyu.png"
);
sudu
=loadImage(
"/sudu.png"
);
shijian
=loadImage(
"/shijian.png"
);
fanhui
=loadImage(
"/fanhui.png"
);
}
catch
(Exception e){}
fruit
=
new
Vector();
lm
=
new
LayerManager();
player
=
new
Sprite(
Player
,32,32);
Zhangyu
=
new
Sprite(
zhangyu
,32,32);
lm
.append(
player
);
offG
=gameMain.
gm
.
gc
.
off
;
screenW
=
bk
.getWidth();
screenH
=
bk
.getHeight();
px
=
screenW
/2;
py
=
screenH
-
Player
.getHeight()-32;
player
.setPosition(
px
,
py
);
initLevel(
level
);
}
public
static
Image loadImage(String fname)
{
Image im=
null
;
try
{
im =Image.createImage(fname);
}
catch
(Exception e){
System.
out
.println(
"pic err fname="
+fname);
}
return
im;
}
public
void
Paint(){
switch
(gameMain.
gm
.
gameState
)
{
case
States.
GM_ACTION
:
drawBack();
drawPlayer();
makefruit();
drawfruit();
drawscore();
break
;
case
States.
GM_WIN
:
drawWin();
drawInfo();
break
;
case
States.
GM_LOST
:
drawLost();
break
;
case
States.
GM_JINENG
:
drawBack();
drawPlayer();
drawfruit();
drawjineng();
break
;
case
States.
GM_PAUSE
:
drawBack();
drawPlayer();
drawfruit();
drawpause();
break
;
}
lm
.paint(
offG
, 0, 0);
}
private
void
drawpause() {
//
TODO
Auto-generated method stub
switch
(
xuanze
)
{
case
1:
offG
.drawImage(
fanhui
, 40,
screenH
/2-20, 0);
offG
.setColor(0, 150, 200);
offG
.drawRect(58,
screenH
/2+15,50, 18);
break
;
case
2:
offG
.drawImage(
fanhui
, 40,
screenH
/2-20, 0);
offG
.setColor(0, 150, 200);
offG
.drawRect(140,
screenH
/2+15, 46, 18);
break
;
}
}
public
int
drawjineng(){
switch
(
ji
)
{
case
1:
jineng
+=5;
if
(
jineng
>=
screenH
/2-10)
{
gameMain.
gm
.
gameState
=States.
GM_ACTION
;
jineng
=0;
ji
=0;
return
0;
}
offG
.drawImage(
sudu
, 60,
jineng
, 0);
break
;
case
2:
jineng
+=5;
if
(
jineng
>=
screenH
/2-10)
{
gameMain.
gm
.
gameState
=States.
GM_ACTION
;
jineng
=0;
ji
=0;
return
0;
}
offG
.drawImage(
shijian
, 60,
jineng
, 0);
}
return
1;
}
private
void
drawInfo() {
//
TODO
Auto-generated method stub
offG
.setColor(0,0,255);
offG
.drawString(
"
第
"
+
level
+
"
关
"
,80,130,0);
offG
.drawString(
"
倒计时(秒)
"
+
this
.
levelTime
[
level
],80,150,0);
offG
.drawString(
"
通关分数
"
+
this
.
levelScore
[
level
+1],80,170,0);
}
private
void
drawLost(){
if
(
lm
!=
null
)
{
lm
.remove(
player
);
}
offG
.setColor(0, 0, 0);
offG
.fillRect(0, 0,
screenW
,
screenH
);
offG
.setColor(0, 100, 255);
offG
.drawImage(
lost
, 60,
screenH
/2-40, 0);
offG
.drawString(
"
您已经通过
"
+
level
+
"
关
"
, 80,
screenH
/2+10, 0);
cout
++;
if
(
cout
>40)
{
gameMain.
gm
.
gc
.
load
.
process
=0;
gameMain.
gm
.
gameState
=States.
GM_MENU
;
level
=1;
try
{
gameMain.
gm
.
gc
.
back
.setMediaTime(0);
gameMain.
gm
.
gc
.
back
.start();
}
catch
(MediaException e) {
//
TODO
Auto-generated catch block
e.printStackTrace();
}
}
}
private
void
drawWin() {
//
TODO
Auto-generated method stub
lm
.remove(
player
);
offG
.drawImage(
winbk
, 0, 0, 0);
lm
.append(
Zhangyu
);
Zhangyu
.nextFrame();
Zhangyu
.setPosition(
screenW
/2-20,
screenH
/2-60);
count
++;
if
(
count
>=30)
{
count
=0;
try
{
gameMain.
gm
.
gc
.
back
.setMediaTime(0);
gameMain.
gm
.
gc
.
back
.start();
}
catch
(MediaException e) {
//
TODO
Auto-generated catch block
e.printStackTrace();
}
if
(
level
>
levelCount
)
{
gameMain.
gm
.
gameState
=States.
GM_LOST
;
}
else
{
initLevel(
level
);
gameMain.
gm
.
gameState
=States.
GM_ACTION
;
}
}
}
public
void
drawfruit()
//
画水果
{
for
(
int
i=0;i<
fruit
.size();i++)
{
int
tmp[]=(
int
[])
fruit
.elementAt(i);
offG
.drawImage(
imFruit
[tmp[0]],tmp[1],tmp[2],0);
}
}
public
int
drawscore()
{
offG
.setColor(255, 255, 255);
switch
(
defen
)
{
case
1:
a
+=2;
if
(
a
>=15)
{
a
=0;
defen
=0;
return
0;
}
offG
.drawString(
"+100"
,
fenx
,
feny
-
a
, 0);
break
;
case
2:
a
+=2;
if
(
a
>=15)
{
a
=0;
defen
=0;
return
0;
}
offG
.drawString(
"+200"
,
fenx
,
feny
-
a
, 0);
break
;
}
return
1;
}
public
synchronized
void
makefruit()
{
countFruit
++;
if
(
countFruit
>
fruittime
[
level
-1])
{
countFruit
=1;
int
tmp[]=
new
int
[4];
int
ftype=Math.abs(
random
.nextInt()%6);
tmp[0]=
this
.
levelTypes
[
level
-1][ftype];
tmp[1]=0;
switch
(ftype)
{
case
0:
tmp[2]=Math.abs((
random
.nextInt()%(
screenH
/2-50)));
break
;
case
1:
tmp[2]=Math.abs((
random
.nextInt()%(
screenH
/2+40)));
break
;
case
2:
tmp[2]=Math.abs((
random
.nextInt()%(
screenH
/2+40)));
break
;
case
3:
tmp[2]=Math.abs((
random
.nextInt()%(
screenH
/2+40)));
break
;
case
4:
tmp[2]=Math.abs((
random
.nextInt()%(
screenH
/2+40)));
break
;
case
5:
tmp[2]=Math.abs((
random
.nextInt()%(
screenH
/2+40)));
break
;
}
tmp[3]=0;
fruit
.addElement(tmp);
}
if
(
countFruit
%
FruitSpeed
==0)
{
for
(
int
i=0;i<
fruit
.size();i++)
{
int
tmp[]=(
int
[])
fruit
.elementAt(i);
if
(tmp[3]==0)
{
tmp[1]+=3+
level
;
if
(tmp[1]>
screenW
)
{
fruit
.removeElementAt(i);
}
System.gc();
}
else
{
if
(tmp[0]==0){
score
+=100;
defen
=1;
fenx
=tmp[1]+12;
feny
=tmp[2];}
if
(tmp[0]==1){
score
+=200;
defen
=2;
fenx
=tmp[1]+12;
feny
=tmp[2];}
fruit
.removeElementAt(i);
System.gc();
}
}
}
}
public
void
drawPlayer()
{
if
(
dir
)
{
if
(
zoom
)
{
if
(
py
>0)
{
py
-=
speed
;
for
(
int
i=0;i<
fruit
.size();i++)
{
int
tmp[]=(
int
[])
fruit
.elementAt(i);
int
x=
px
;
int
y=
py
;
if
(x<tmp[1]-10||x>tmp[1]+16||y<tmp[2]||y>tmp[2]+25)
{
}
else
{
try
{
pengzhuang
.start();
}
catch
(MediaException e1) {
//
TODO
Auto-generated catch block
e1.printStackTrace();
}
if
(tmp[0]==2){
snum
++;}
else
if
(tmp[0]==3){
tnum
++;}
else
if
(tmp[0]==4){
//
增加水果
Thread ts=
new
Thread()
{
public
void
run()
{
for
(
int
i=0;i<15;i++)
{
makefruit();
try
{ Thread.sleep(100);}
catch
(Exception e){}
}
}
};
ts.start();
}
else
{
tmp[3]=1;
}
if
(tmp[0]>1)
fruit
.removeElementAt(i);
zoom
=
false
;
player
.setTransform(Sprite.
TRANS_ROT180
);
}
}
}
else
{
zoom
=
false
;
player
.setTransform(Sprite.
TRANS_ROT180
);
}
}
else
{
if
(
py
<
screenH
-
Player
.getHeight()-32)
{
py
+=
speed
;
}
else
{
py
=
screenH
-
Player
.getHeight()-32;
player
.setTransform(Sprite.
TRANS_NONE
);
zoom
=
true
;;
dir
=
false
;
if
(
score
>=
levelScore
[
level
])
{
level
++;
gameMain.
gm
.
gameState
=States.
GM_WIN
;
try
{
gameMain.
gm
.
gc
.
back
.stop();
// win.setMediaTime(0);
win
.start();
}
catch
(MediaException e1) {
//
TODO
Auto-generated catch block
e1.printStackTrace();
}
}
}
}
player
.nextFrame();
}
player
.setPosition(
px
,
py
);
}
public
void
time()
//
计算时间
{
long
currTime = System.currentTimeMillis();
lastTime
= currTime;
drawtime
=
this
.
levellastTime
-(
lastTime
-
this
.
levelStartTime
)/1000;
if
(
drawtime
<=0){gameMain.
gm
.
gameState
=States.
GM_LOST
;
try
{
gameMain.
gm
.
gc
.
back
.stop();
}
catch
(MediaException e) {
//
TODO
Auto-generated catch block
e.printStackTrace();
}}
}
public
void
drawBack()
//
画背景
{
offG
.drawImage(
bk
, 0, 0, 0);
offG
.setColor(255,255,255);
offG
.drawString(
"
分数:
"
+
score
+
"
剩余时间:
"
+
drawtime
,5,5,Graphics.
LEFT
|Graphics.
TOP
);
offG
.setColor(0, 0, 0);
offG
.drawString(
"
菜单
"
,5,
screenH
-Font.getDefaultFont().getHeight(),0);
offG
.setColor(40,200,20);
offG
.drawString(
"
第
"
+
this
.
level
+
"
关
"
,190,5,Graphics.
LEFT
|Graphics.
TOP
);
offG
.setColor(200,200,200);
if
(
snum
>0)
offG
.drawString(
"S: "
+
snum
,6,25,Graphics.
LEFT
|Graphics.
TOP
);
if
(
tnum
>0)
offG
.drawString(
"T: "
+
tnum
,6,40,Graphics.
LEFT
|Graphics.
TOP
);
}
public
void
initLevel(
int
n)
{
level
=n;
speedtime
=
levelTime
[
level
];
lm
.append(
player
);
lm
.remove(
Zhangyu
);
this
.
levelStartTime
=System.currentTimeMillis();
this
.
levellastTime
=
this
.
levelTime
[n];
this
.
score
=0;
if
(gameMain.
gm
.
gameState
==States.
GM_WIN
)
{
gameMain.
gm
.
gameState
=States.
GM_ACTION
;
}
FruitSpeed
=3;
fruit
.removeAllElements();
if
(n==1)
//
如果是第一关初始化道具
{
tnum
=
snum
=0;
}
System.gc();
}
public
void
move(
int
direction)
{
switch
(direction)
{
case
Direction.
D_LEFT
:
if
(
px
>=0)
{
px
-=
speed
;
player
.nextFrame();
player
.setPosition(
px
,
py
);
}
break
;
case
Direction.
D_REIGHT
:
if
(
px
<
screenW
-
player
.getHeight())
{
px
+=
speed
;
player
.nextFrame();
player
.setPosition(
px
+
speed
,
py
);
}
break
;
}
}
}
QQ283413152 吃草莓的小逸