📄 gamescreen.java
字号:
import javax.microedition.lcdui.*;
//import com.nokia.mid.ui.*;
import java.util.*;
public class gameScreen extends Canvas implements Runnable{//FullCanvas
public Image bufferImg; //离屏画布 ---缓冲区图片
public Graphics buffer_g; //离屏画笔 ---缓冲区画笔
public int screenW;//屏幕宽度,屏幕高度
public int screenH;
public final static int GAME=1;
public final static int WIN=2;
public final static int LOST=3;
public final static int LEVELINFO=0;
public final static int GAMEEND=4;
public final static int PAUSE=4;
public static int gameState=LEVELINFO;//初始状态为游戏
public Image imLeft[];
public Image imRight[];
public Image imWin;
public Image imLost;
public Image imFruit[];
public Vector allFruit;//保存所有水果
public Random random;
public int px; //绳子的x坐标值
public int py; //绳子的y坐标值
public final static int STOP=-1;
public final static int PLEFT=0;
public final static int PRIGHT=1;
public int dir=STOP;
public int oldDir=PRIGHT;
public int nowFrame=0;//卡卡的图片的侦数
public long lastTime;
public boolean fire=false; //是否放出了绳子
public int length=0; //绳子长度
public boolean zoom=true;//没有超出屏幕
public int count=0; //frame 计数器
public static int countFruit=1; //水果出现的计数器(计时器)
public int score;
public int levellastTime;
//苹果=0 西瓜=1 加分道具=2 加时间道具=3 多水果道具=4 加减速道具=5
public int levelTypes[][]={//每关中允许出现的NPC类型
{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,5},
{0,1,2,3,4,5}, {0,1,2,3,4,5}, {0,1,2,3,4,5},
{0,1,2,3,4,5}, {2,2,2,3,4,3}, {0,1,2,3,4,5},
{0,1,2,3,4,5}, {0,1,2,3,4,5}, {0,1,2,3,4,5},
{0,1,2,3,4,5}, {2,3,2,3,2,3}, {0,1,2,3,4,5},
{0,1,2,3,4,5}, {0,1,2,3,4,5}
};
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 int levelTime[]=
{0,100,100, 100,100,100, 100,100,100,
100,100,100, 100,90,80, 70,60,50,
50,40,30,20
};
public int level;
private int levelCount=20;
public long levelStartTime;
public static long drawtime; //剩余时间
public long waitTime;
public static int FruitSpeed=3;
public static int snum=0;//获取道具数量
public static int tnum=0;
public static boolean addFlag=false;//
public gameScreen()
{ screenW=this.getWidth(); //获取屏幕宽度
screenH=this.getHeight();//获取屏幕高度
bufferImg=Image.createImage(screenW,screenH);
buffer_g=bufferImg.getGraphics();
imLeft=new Image[2];
imLeft[0]=gameStart.loadImage("/panda_l1.png");
imLeft[1]=gameStart.loadImage("/panda_l2.png");
imRight=new Image[2];
imRight[0]=gameStart.loadImage("/panda_r1.png");
imRight[1]=gameStart.loadImage("/panda_r2.png");
imFruit=new Image[6];
imFruit[0]=gameStart.loadImage("/f1.png");//苹果=0
imFruit[1]=gameStart.loadImage("/f2.png");//西瓜=1
imFruit[2]=gameStart.loadImage("/d1.png");//加分道具=2
imFruit[3]=gameStart.loadImage("/d2.png");//加时间道具=3
imFruit[4]=gameStart.loadImage("/d3.png");//多水果道具=4
imFruit[5]=gameStart.loadImage("/d4.png");//加减速道具=5
imWin=gameStart.loadImage("/win.png");
imLost=gameStart.loadImage("/over.png");
allFruit=new Vector();
random=new Random();
px=screenW/2-20;
py=screenH-40;
level=1;
initLevel(level);//加载关卡信息
Thread t=new Thread(this);
t.start();
}
public void initLevel(int n)
{
level=n;
this.levelStartTime=System.currentTimeMillis();
this.levellastTime=this.levelTime[n];
this.score=0;
this.gameState=LEVELINFO;
FruitSpeed=3;
allFruit.removeAllElements();
if(n==1)//如果是第一关初始化道具
{
tnum=snum=0;
}
}
public void drawBack() //画背景
{ buffer_g.setColor(32,156,0);
buffer_g.fillRect(0,0,screenW,screenH);
buffer_g.setColor(255,255,255);
buffer_g.drawString("分数: "+score+" 剩余时间: "+drawtime,5,5,Graphics.LEFT|Graphics.TOP);
buffer_g.drawString("菜单",5,screenH-Font.getDefaultFont().getHeight(),0);
buffer_g.setColor(40,200,20);
buffer_g.drawString("第 "+this.level+" 关 ",50,screenH/2,Graphics.LEFT|Graphics.TOP);
buffer_g.setColor(200,200,200);
if(snum>0)
buffer_g.drawString("S: "+snum,130,screenH/2-Font.getDefaultFont().getHeight(),Graphics.LEFT|Graphics.TOP);
if(tnum>0)
buffer_g.drawString("T: "+tnum,130,screenH/2+Font.getDefaultFont().getHeight(),Graphics.LEFT|Graphics.TOP);
}
public void drawFruit()//画水果
{ for(int i=0;i<allFruit.size();i++)
{int tmp[]=(int [])allFruit.elementAt(i);
buffer_g.drawImage(imFruit[tmp[0]],tmp[1],tmp[2],0);
}
}
public void drawPanda()//画卡卡
{ switch(dir)
{case STOP:
switch(oldDir)
{case PLEFT:
buffer_g.drawImage(imLeft[nowFrame],px,py,0);
break;
case PRIGHT:
buffer_g.drawImage(imRight[nowFrame],px,py,0);
break;
}
break;
case PLEFT:
buffer_g.drawImage(imLeft[nowFrame],px,py,0);
break;
case PRIGHT:
buffer_g.drawImage(imRight[nowFrame],px,py,0);
break;
}
}
public void drawWin() //画过关
{
buffer_g.setColor(0);
buffer_g.fillRect(0,0,screenW,screenH);
buffer_g.setColor(255,255,255);
buffer_g.drawString("游戏胜利",80,30,0);
buffer_g.drawImage(this.imWin,30,30,0);
count++;
if(count>=40)
{
count=0;
level++;
if(level>levelCount)
{
gameState=GAMEEND;
}else
{
this.initLevel(level);
}
}
}
public void drawLost() //画失败
{
buffer_g.setColor(0);
buffer_g.fillRect(0,0,screenW,screenH);
buffer_g.setColor(255,255,255);
buffer_g.drawString("游戏失败",80,30,0);
buffer_g.drawImage(this.imLost,30,30,0);
count++;
if(count>=40)
{
count=0;
this.gotoMenu();
}
}
public void drawLine()
{ if(zoom)
{
length += 5;
////碰撞
for(int i=0;i<allFruit.size();i++)
{ int tmp[]=(int [])allFruit.elementAt(i);
//if (((py - length)>=tmp[2])&&((py - length)<=(tmp[2])+25)&&(px>=tmp[1])&&(px<=tmp[1]+25))
int x=px+20;
int y=py-length;
if((x<tmp[1])||
(x>(tmp[1]+25))||
(y<tmp[2])||
(y>(tmp[2]+25)))
{ //没有碰撞collision
}
else
{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<10;i++)
{addFlag=true;
makeFruit();
addFlag=false;
try{ Thread.sleep(100);}catch(Exception e){}
}
}};
ts.start();
}
else if(tmp[0]==5) {
int stmp=Math.abs(random.nextInt()%2);
if(stmp==0){FruitSpeed++;}
if(stmp==1){FruitSpeed--;}
}
else{
tmp[3] = 1; //被绳索抓取
}
if(tmp[0]>1){allFruit.removeElementAt(i);}
zoom=false;
}//end collision
}
}else
{
length-=5;
if(length<=0){
fire=false;
zoom=true;
}
}
if(length>py){
zoom=false;
}
buffer_g.setColor(255,255,255);
buffer_g.drawLine(px+20,py-length,px+20,py);
}
public void drawInfo()
{buffer_g.setColor(0);
buffer_g.fillRect(0,0,screenW,screenH);
buffer_g.setColor(255,255,255);
buffer_g.drawString("第"+level+"关",80,30,0);
buffer_g.drawString("倒计时(秒) "+this.levelTime[level],30,50,0);
buffer_g.drawString("通关分数 "+this.levelScore[level],30,70,0);
count++;
if(count>=30)
{
count=0;
this.gameState=GAME;
}
}
public void drawEnd()
{buffer_g.setColor(0);
buffer_g.fillRect(0,0,screenW,screenH);
buffer_g.setColor(255,255,255);
buffer_g.drawString("恭喜",80,30,0);
buffer_g.drawString("游戏通关",30,50,0);
count++;
if(count>=30)
{
count=0;
this.gameState=PAUSE;
gameStart.midlet.display.setCurrent(gameStart.midlet.menu);
}
}
public void paint(Graphics g) {
switch(gameState)
{
case LEVELINFO://关卡提示
drawInfo();//绘制提示信息
break;
case GAME://游戏状态
drawBack(); //画背景
drawFruit();//画水果
drawPanda();//画卡卡
if(fire)drawLine();
break;
case WIN: //过关
drawBack(); //画背景
drawWin();
break;
case LOST://失败
drawBack(); //画背景
drawLost();
break;
case GAMEEND://通关
drawEnd();
break;
}
g.drawImage(bufferImg,0,0,Graphics.LEFT|Graphics.TOP);
}
public synchronized void makeFruit()
{ countFruit++;//定时300次时随机出现一种水果
// System.out.println("ok"+ random.nextInt());
if(countFruit>300||addFlag)
{ countFruit=1;
int tmp[]=new int[4];
int ftype=Math.abs(random.nextInt()%6);
tmp[0]=this.levelTypes[level-1][ftype];//NPC的值 0表示苹果 1 表示西瓜
System.out.println("Level ="+level+"\t NPC的值 :"+tmp[0]);
tmp[1]=-40; //NPC的初始x轴坐标
//tmp[2] ==== //NPC的初始x轴坐标
switch(ftype)//NPC不同出现的位置不同
{ case 0:
tmp[2]=Math.abs((random.nextInt()%(screenH/2-50)))+(screenH/2);
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;
allFruit.addElement(tmp);
}
if(countFruit%FruitSpeed==0)
{
//移动水果
for(int i=0;i<allFruit.size();i++)
{int tmp[]=(int [])allFruit.elementAt(i);
if(tmp[3]==0){
tmp[1]+=8;
if(tmp[1]>screenW)
{allFruit.removeElementAt(i);}
}else
{ tmp[1]=px;
tmp[2]=py-length;
if(length<=0)
{if(tmp[0]==0) score+=100;
if(tmp[0]==1) score+=200;
allFruit.removeElementAt(i);
if(score>=levelScore[level]){
this.gameState=WIN;
allFruit.removeAllElements();
System.gc();
}
}
}
}}
}
public void run()
{ while(true)
{ if(this.gameState==GAME)
{//改坐标
if (dir >= 0) {
nowFrame++;
if ( (nowFrame > 1)) {
nowFrame = 0;
}
}else
{nowFrame=0;
}
switch (dir) {
case PLEFT:
if (px > 0)
px -= 8;
break;
case PRIGHT:
if (px < screenW - 40)
px += 8;
break;
}
//产生水果
makeFruit();
////倒计时
drawtime=this.levellastTime-(lastTime-this.levelStartTime)/1000;
if(drawtime<=0){this.gameState=LOST;}
}
//重绘
this.repaint();
this.serviceRepaints();
long currTime = System.currentTimeMillis();
long delay = 100 - (currTime - lastTime);
lastTime = currTime;
try { if (delay <= 0)
delay = 1;
Thread.sleep(delay);
} catch (Exception exception) { }
System.out.println(" state"+gameState);
}
}
public void gotoMenu()
{ if(gameState!=LOST)
{
gameStart.midlet.menu.insert(0,"返回游戏",null);
}
gameState=PAUSE;
gameStart.midlet.display.setCurrent(gameStart.midlet.menu);
waitTime=System.currentTimeMillis();
}
public void keyPressed(int n) {
if(n==-6)
{
gotoMenu();
}
if(n==Canvas.KEY_NUM1){
if(snum>0)
{this.score+=200;
snum--;
}
}
if(n==Canvas.KEY_NUM3)
{if(tnum>0)
{this.levellastTime+=50;
tnum--;
}
}
if(!fire)
{
if (n == Canvas.KEY_NUM4 || n == -3) {
dir = PLEFT;
}
if (n == Canvas.KEY_NUM6 || n == -4) {
dir = PRIGHT;
}
if(n==Canvas.KEY_NUM5||n==-5)
{ fire=true;
}
}
if(dir>=0) oldDir=dir;
}
public void keyReleased(int n)
{
dir=STOP;
}
public void hideNotify() {
if(gameState == GAME)
{gameState = PAUSE;}
}
public void showNotify() {//来电之后自动调用
if (gameState == PAUSE) {
gameState = GAME;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -