📄 accv.java
字号:
import java.io.IOException;
import java.util.Random;
import java.util.Vector;
import javax.microedition.lcdui.*;
public class AcCV extends Canvas implements Runnable {
Thread t=new Thread(this);
int gamex=56,gamex1=147,gamey=33;
int width=30,height=32;
int fps=2,x,y;
int count;
static int ji=400;
int[] boss={10,20,30,40,50};
int[] monstery={20,30,40,50,60};
int[] monsterx={20,40,60,80,100,120,140,160,180,200,20,40,60,80,100,120,140,160,180,200};
int []monsterx2={20,40,60,80,100,120,140,160,180,200,20,40,60,80,100,120,140,160,180,200};
int []monsterx3={20,40,60,80,100,120,140,160,180,200,20,40,60,80,100,120,140,160,180,200};
int []monsterx4={20,40,60,80,100,120,140,160,180,200,20,40,60,80,100,120,140,160,180,200};
int []monsterx5={20,40,60,80,100,120,140,160,180,200,20,40,60,80,100,120,140,160,180,200};
int[] heighty={-250,-240,-230,-210,-200};
static int num=0, tankkey=6;
static int level=1;
Random ran=new Random();
Back back=null;
Image backGround=null;
Image Ground=null;
Menu showMenu=null;
About showAbout=null;
Help showHelp=null;
Bullet bullet=null;
Game game=null;
Image pressure=null;
Logo showLogo=null;
gameOver GameOver=null;
Graphics bufferG;
Image bufferImg=null;
Image brickImg=null;
int bigMap;
Vector vector;
AaMID midlet;
AaMID gameExit;//退出游戏方法
static boolean bRun=false;
static boolean aRun=false;//控制坦克方向
static boolean cRun=false;//控制退出
static boolean dRun=false;//控制计数器
static boolean eRun=false;//控制menu游戏从新开始
static boolean fRun=false;//控制怪碰撞和显示
static int state=0;//游戏状态0:LOGO状态 1、菜单界面 2、关于界面 3、帮助状态 4、暂停状态 5、游戏中状态6、过关状态 7、游戏失败状态
public AcCV(AaMID gameExit)
{ super();
this.gameExit=gameExit;
try {
brickImg=Image.createImage("/11-1.png");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
setFullScreenMode(true);
bufferImg=Image.createImage(getWidth(),getHeight());
bufferG=bufferImg.getGraphics();
bRun=true;
game=new Game(this);
back=new Back(this);
vector =new Vector();
new Thread(this).start();
}
public void addBullet(Bullet bullet){
vector.addElement(bullet);
}
public void paint(Graphics g)
{
bufferG.setClip(0, 0, getWidth(), getHeight());
bufferG.setColor(255, 255, 255);
bufferG.fillRect(0, 0, getWidth(), getHeight());
switch(state)
{
case 0://LOGO状态
if(showLogo==null)
System.out.println("showLogo");
if(showLogo!=null)
showLogo.paint(bufferG);
break;
case 1://菜单界面
if(showMenu==null)
System.out.println("showMenu");
if(showMenu!=null)
showMenu.paint(bufferG);
break;
case 2://关于界面
if(showAbout==null)
System.out.println("showAbout");
if(showAbout!=null)
showAbout.paint(bufferG);
break;
case 3://帮助状态
if(showHelp==null)
System.out.println("showHelp");
if(showHelp!=null)
showHelp.paint(bufferG);
break;
case 4://暂停状态
break;
case 5://游戏中状态
if(back!=null){
// back.paint(bufferG);
bufferG.drawImage(brickImg, bigMap, 0, 0);
bufferG.drawImage(brickImg, bigMap+brickImg.getWidth(), 0,0);
switch (tankkey) {
case 6:
bufferG.setClip(back.X, back.Y+262, 25, 32);
bufferG.drawImage(back.tank, back.X,back.Y+230, Graphics.LEFT|Graphics.TOP);
break;
case 4:
bufferG.setClip(back.X, back.Y+262, 25, 32);
bufferG.drawImage(back.tank, back.X,back.Y+166, Graphics.LEFT|Graphics.TOP);
break;
case 5:
break;
}
game.paint(bufferG);
bufferG.setClip(0, 0, getWidth(),getHeight());
bufferG.setColor(255,255,0);
bufferG.drawString("分数:"+num, 0,0,0);
bufferG.drawString("关卡数:"+level, 160,0,0);
if(level==1){
bufferG.drawString("记时器:"+ji, 70,0,0);
}
else if(level==2){
bufferG.drawString("记时器:"+ji, 70,0,0);
}
else if(level==3){
bufferG.drawString("记时器:"+ji, 70,0,0);
}
else if(level==4){
bufferG.drawString("记时器:"+ji, 70,0,0);
}
bufferG.setClip(0, 0, getWidth(),getHeight());
if(Bullet.isDisplay){
for(int i=0;i<vector.size();i++){
Bullet bullet=(Bullet)vector.elementAt(i);
bullet.paint(bufferG);
}
}
}
break;
case 6://转圈状态
case 7:
if(GameOver==null)
System.out.println("GameOver");
if(GameOver!=null)
GameOver.paint(bufferG);
break;
case 8:
break;
}
g.drawImage(bufferImg, 0, 0, 0);
}
public void run()
{
while(bRun==true)
{
long startIime=System.currentTimeMillis();
gameLogic();
mapMove();
time();
repaint();
if(aRun==true){
back.move();
}
long takenTime=System.currentTimeMillis()-startIime;
if(takenTime<150/fps){
try {
Thread.sleep(150/fps-takenTime);
}
catch(Exception e)
{
}
}
}
}
public void mapMove(){
bigMap-=4;
if (bigMap<-brickImg.getWidth())
{ bigMap=0;
}
}
public void time(){
if(dRun==true){
ji--;
}
}
public void gameLogic()
{
switch(state)
{
case 0://LOGO状态
if(showLogo==null)
{
showLogo=new Logo(this);
}
break;
case 1://菜单界面
if(showAbout!=null)
{
showAbout.recycleRes();
showAbout=null;
}
if(showHelp!=null)
{
showHelp.recycleRes();
showHelp=null;
}
if(showLogo!=null)
{
showLogo.recycleRes();
showLogo=null;
}
if(showMenu==null)
{
showMenu=new Menu(this);
}
break;
case 2://关于界面
if(showMenu!=null)
{
showMenu.recycleRes();
showMenu=null;
}
if(showAbout==null)
{
showAbout=new About(this);
}
break;
case 3://帮助状态
if(showMenu!=null)
{
showMenu.recycleRes();
showMenu=null;
}
if(showLogo!=null)
{
showLogo.recycleRes();
showLogo=null;
}
if(showHelp==null)
{
showHelp=new Help(this);
}
break;
case 4://暂停状态
break;
case 5://游戏中状态
if(showMenu!=null)
{
showMenu.recycleRes();
showMenu=null;
}
if(Bullet.isDisplay){
for(int i=0;i<vector.size();i++){
Bullet bullet=(Bullet)vector.elementAt(i);
bullet.move();
}
if(level==1){
peng();
if(ji==0){
state=7;
dRun=false;
ji=600;
vector.removeAllElements();
}
else if(monsterx[0]==-30&&monsterx[1]==-30&&monsterx[3]==-30&&monsterx[4]==-30&&monsterx[5]==-30&&monsterx[6]==-30&&monsterx[7]==-30&&monsterx[8]==-30&&monsterx[9]==-30
&&monsterx2[0]==-30&&monsterx2[1]==-30&&monsterx2[3]==-30&&monsterx2[4]==-30&&monsterx2[5]==-30&&monsterx2[6]==-30&&monsterx2[7]==-30&&monsterx2[8]==-30&&monsterx2[9]==-30
&&monsterx3[0]==-30&&monsterx3[1]==-30&&monsterx3[3]==-30&&monsterx3[4]==-30&&monsterx3[5]==-30&&monsterx3[6]==-30&&monsterx3[7]==-30&&monsterx3[8]==-30&&monsterx3[9]==-30
&&monsterx4[0]==-30&&monsterx4[1]==-30&&monsterx4[3]==-30&&monsterx4[4]==-30&&monsterx4[5]==-30&&monsterx4[6]==-30&&monsterx4[7]==-30&&monsterx4[8]==-30&&monsterx4[9]==-30
&&monsterx5[0]==-30&&monsterx5[1]==-30&&monsterx5[3]==-30&&monsterx5[4]==-30&&monsterx5[5]==-30&&monsterx5[6]==-30&&monsterx5[7]==-30&&monsterx5[8]==-30&&monsterx5[9]==-30
){
vector.removeAllElements();
level++;
dRun=false;
ji=600;
}
}
if(level==2){
if(ji==0){
level++;
dRun=false;
ji=360;
vector.removeAllElements();
}
else if(
game.monster1x[0]==-30&&game.monster1x[1]==-30&&game.monster1x[3]==-30&&game.monster1x[4]==-30&&game.monster1x[5]==-30&&game.monster1x[6]==-30&&game.monster1x[7]==-30&&game.monster1x[8]==-30&&game.monster1x[9]==-30
&&game.monster2x[0]==-30&&game.monster2x[1]==-30&&game.monster2x[3]==-30&&game.monster2x[4]==-30&&game.monster2x[5]==-30&&game.monster2x[6]==-30&&game.monster2x[7]==-30&&game.monster2x[8]==-30&&game.monster2x[9]==-30
&&game.monster3x[0]==-30&&game.monster3x[1]==-30&&game.monster3x[3]==-30&&game.monster3x[4]==-30&&game.monster3x[5]==-30&&game.monster3x[6]==-30&&game.monster3x[7]==-30&&game.monster3x[8]==-30&&game.monster3x[9]==-30
&&game.monster4x[0]==-30&&game.monster4x[1]==-30&&game.monster4x[3]==-30&&game.monster4x[4]==-30&&game.monster4x[5]==-30&&game.monster4x[6]==-30&&game.monster4x[7]==-30&&game.monster4x[8]==-30&&game.monster4x[9]==-30
&&game.monster5x[0]==-30&&game.monster5x[1]==-30&&game.monster5x[3]==-30&&game.monster5x[4]==-30&&game.monster5x[5]==-30&&game.monster5x[6]==-30&&game.monster5x[7]==-30&&game.monster5x[8]==-30&&game.monster5x[9]==-30
){
vector.removeAllElements();
level++;
dRun=false;
ji=360;
}
pengTwo();
}
if(level==3){
if(ji==0){
state=7;
}
else if(monsterx[10]==-30&&monsterx[11]==-30&&monsterx[13]==-30&&monsterx[14]==-30&&monsterx[15]==-30&&monsterx[16]==-30&&monsterx[17]==-30&&monsterx[18]==-30&&monsterx[19]==-30
&&monsterx2[10]==-30&&monsterx2[11]==-30&&monsterx2[13]==-30&&monsterx2[14]==-30&&monsterx2[15]==-30&&monsterx2[16]==-30&&monsterx2[17]==-30&&monsterx2[18]==-30&&monsterx2[19]==-30
&&monsterx3[10]==-30&&monsterx3[11]==-30&&monsterx3[13]==-30&&monsterx3[14]==-30&&monsterx3[15]==-30&&monsterx3[16]==-30&&monsterx3[17]==-30&&monsterx3[18]==-30&&monsterx3[19]==-30
&&monsterx4[10]==-30&&monsterx4[11]==-30&&monsterx4[13]==-30&&monsterx4[14]==-30&&monsterx4[15]==-30&&monsterx4[16]==-30&&monsterx4[17]==-30&&monsterx4[18]==-30&&monsterx4[19]==-30
&&monsterx5[10]==-30&&monsterx5[11]==-30&&monsterx5[13]==-30&&monsterx5[14]==-30&&monsterx5[15]==-30&&monsterx5[16]==-30&&monsterx5[17]==-30&&monsterx5[18]==-30&&monsterx5[19]==-30
&&num>890 ){
vector.removeAllElements();
dRun=false;
level++;
ji=110;
}
pengThree();
}
if(level==4){
if(ji==0){
state=7;
}
pengBoss();
}
}
if(cRun==true){
state=7;
}
break;
case 6:
if(showMenu!=null)
{
showMenu.recycleRes();
showMenu=null;
}
if(game==null){
game=new Game(this);
}
break;
case 7:
if(GameOver==null)
GameOver=new gameOver(this);
break;
case 8:
break;
}
}
public void shuTwo(){
game.monster1x[0]=20;
game.monster1x[1]=40;
game.monster1x[2]=60;
game.monster1x[3]=80;
game.monster1x[4]=100;
game.monster1x[5]=120;
game.monster1x[6]=140;
game.monster1x[7]=160;
game.monster1x[8]=180;
game.monster1x[9]=200;
game.monster2x[0]=20;
game.monster2x[1]=40;
game.monster2x[2]=60;
game.monster2x[3]=80;
game.monster2x[4]=100;
game.monster2x[5]=120;
game.monster2x[6]=140;
game.monster2x[7]=160;
game.monster2x[8]=180;
game.monster2x[9]=200;
game.monster3x[0]=20;
game.monster3x[1]=40;
game. monster3x[2]=60;
game.monster3x[3]=80;
game.monster3x[4]=100;
game.monster3x[5]=120;
game.monster3x[6]=140;
game.monster3x[7]=160;
game.monster3x[8]=180;
game.monster3x[9]=200;
game.monster4x[0]=20;
game.monster4x[1]=40;
game.monster4x[2]=60;
game.monster4x[3]=80;
game.monster4x[4]=100;
game.monster4x[5]=120;
game.monster4x[6]=140;
game.monster4x[7]=160;
game.monster4x[8]=180;
game.monster4x[9]=200;
game.monster5x[0]=20;
game.monster5x[1]=40;
game.monster5x[2]=60;
game.monster5x[3]=80;
game.monster5x[4]=100;
game.monster5x[5]=120;
game.monster5x[6]=140;
game.monster5x[7]=160;
game.monster5x[8]=180;
game.monster5x[9]=200;
}
public void shu(){
monsterx[0]=20;
monsterx[1]=40;
monsterx[2]=60;
monsterx[3]=80;
monsterx[4]=100;
monsterx[5]=120;
monsterx[6]=140;
monsterx[7]=160;
monsterx[8]=180;
monsterx[9]=200;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -