⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gamedisplay.java

📁 一款手机游戏
💻 JAVA
📖 第 1 页 / 共 2 页
字号:


import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.game.TiledLayer;

public class GameDisplay extends Canvas implements Runnable
{
	public  Menu menu;
	public  Scene scene;   //场景
	public  TiledLayer foreground;
    public  Nikuman parent;
    private mainMenu mainmenu;
    private TiledLayer background;
    private TiledLayer backWall;
    private GameManager gameManager;
    
    public static  Sprite BallRebound;//试验子弹反弹精灵
    public static  Sprite BallDown;
    public static  Sprite PinaMiss;
    public  Sprite shooter;   //发射器
    private Sprite[] nikuman;//肉包子
    private Sprite[] toge;//齿轮
    private Sprite[] bullet;//子弹
    private Sprite life;  //1UP
    private Sprite pina;  //气球
    private Sprite crashGround;
    
    private Image bgGround;
    private Image star;
    private Image frGround;
    private Image shoot;
    private Image wall;
    private Image ball;
    private Image niku;
    private Image up;
    private Image pi;
    private Image ballre;
    private Image ballrd;
    private Image pinamiss;
    private Image alert1;
    private Image alert2;
    private Image alert3;
    private Image alert;
    private Sprite get;
    private Sprite next;
    private Sprite miss;
    private Image littleBall;
    private Image hand;
    
    private static boolean trans=false;//控制调用子弹精灵
    private static boolean isdown=false;
    private boolean isLeft = false; //向左移动
    private boolean isRight = false; //向右移动
    private boolean isDown=true;//主循环是否向下加速pina
    public boolean is =true;	//控制暂停后气球动与不动
    boolean isStop=false;
    boolean gameover=false;
    boolean onlyone=true;//保证后面的某个语句只执行一次
    boolean isgameover=false;
    
    public static int timecount=-1;//控制子弹反弹是否出现
    public static int misscount=-1;
    public int continue3=0;//用以判断是否游戏该结束,进入下一关
    public int nikuNumber=0;  //已获得的包子数量
    private int count=0; //屏幕上有的子弹数
    private int horiz=0;//水平速度
    private int horre=0;//竖直速度
    private int getniku=0;
    private int PX=0;
    private int PY=0;
    private int note=0;
    int getlife=0;//用以判断是否该显示得到生命
    int XY=170;
    int handposition=0;
    int fantannum=1;
    
    public GameDisplay(Nikuman p,mainMenu m)
    {
        parent = p;
        mainmenu=m; 
        setFullScreenMode(true);
        try
        {
            bgGround = Image.createImage("/image/Background.png");
            frGround = Image.createImage("/image/Foreground.png");
            wall = Image.createImage("/image/Wall.png");
            star = Image.createImage("/image/Toge.png");
            shoot = Image.createImage("/image/Shooter.png");
            ball = Image.createImage("/image/Ball.png");
            niku = Image.createImage("/image/Niku.png");
            up = Image.createImage("/image/UP.png");
            pi = Image.createImage("/image/Pina.png");
            pinamiss=Image.createImage("/image/Miss.png");
            //子弹反弹图片
            //arrow = Image.createImage("/image/Arrow.png");
            ballre=Image.createImage("/image/BallOp.png");
            ballrd=Image.createImage(ballre, 12, 0, 200, 28, 0);
            littleBall=Image.createImage("/image/littleBall.png");
            hand=Image.createImage("/image/hand.png");	
        }
        catch(Exception e){}
        try{
        alert=Image.createImage("/image/Get.png");
        alert1=Image.createImage(alert,0,0,76,18,Sprite.TRANS_NONE);
        alert2=Image.createImage(alert,76,0,50,18,Sprite.TRANS_NONE);
        alert3=Image.createImage(alert,135,0,54,18,Sprite.TRANS_NONE);
        }
        catch(Exception e){}
        
        
        gameManager = new GameManager(this);
        scene = new Scene();
        life = new Sprite(up,25,20);
        pina = new Sprite(pi,65,36);
        BallRebound=new Sprite(ballre,28,28);
        BallDown = new Sprite(ballrd,25,28);
        PinaMiss = new Sprite(pinamiss,100,54);
        shooter = new Sprite(shoot);
        crashGround=new Sprite(littleBall);
        bullet = new Sprite[7];
        menu = new Menu(parent,this,mainmenu);
        Thread t = new Thread(this);
        t.start();
    }
    
    public void paint(Graphics g)
    {
        gameManager.paint(g,0,0);
        if(isLeft&&is)
        {
            if(shooter.getX()>1) //坐标不小于0
                shooter.move(-3, 0);
        }
        if(isRight&&is)
        {
            if(shooter.getX()<418) //坐标不大于场景
                shooter.move(3, 0);
        }
        g.drawString("第五届齐鲁软件设计大赛",XY,203,0);
        if(continue3==3){//绘制进入下一关时前的选择
        	g.drawString("下一关", 100, 50, 0);
        	g.drawString("主菜单", 100, 70, 0);
        	g.drawImage(hand,80,50+handposition,20);
        }
        if(isgameover){
        	g.drawString("退出游戏", 100, 50, 0);
        	g.drawString("主菜单", 100, 70, 0);
        	g.drawImage(hand,80,50+handposition,20);
        }
    }
    
    public synchronized void run()
    {
        while(continue3!=4&&!gameover)//(!isStop)
        {
        	if(XY<=-150) XY=170;
        	else XY--;
        	//if(isDown==true)
        	note=(note+1)%13;
        	MovePina(horiz,horre);
            try
            {   
                gameManager.centerFresh();
                for(int i=0; i<scene.togeNumber[Other.currentLevel-1]; i++)
                    toge[i].nextFrame();
                for(int i=0; i<Scene.nikumanNumber[Other.currentLevel-1]; i++)
                    nikuman[i].nextFrame();
                life.nextFrame();
                pina.nextFrame();
                if(misscount==0){
                	misscount=-1;//避免重复执行,避免屏幕卡
                	PinaMiss.setVisible(false);
                }
                else{
                	PinaMiss.nextFrame();
                	misscount--;
                }
                if(pina.getY()<=0){
                	horre=0;
        		    pina.move(0, 1);
        		}
                if(pina.getY()<=144&&note==0&&isDown==true)	
        		{
        			horre+=1;
        		}
                if(timecount==0) {
                	timecount=-1;
                	if(!isdown)
                	BallRebound.setVisible(false);
                	else{
                	BallDown.setVisible(false);
                	isdown=false;
                	}
                	}
                else{
                	if(!isdown)
                    BallRebound.nextFrame();
                	else
                    BallDown.nextFrame();
                    timecount--;
                }
                moveBullet();
                if(is==true) 
                collides();//检测碰撞
                crashGround();//墙的碰撞
                repaint();
                Thread.sleep(15);
                System.gc();
            }
            catch(Exception e){}
        }
    }
    public static void Rebound(int x,int y ,int z){ 
    	if(mainMenu.issoundopen[1]==true)
    	try{
    	mainMenu.soundplayer[3].start();
    	}catch(Exception e){}
    	timecount=7;
    	if(!isdown) {
    	BallRebound.setVisible(true);
    	if(z==1){
    	if(trans==true) {
    	BallRebound.setTransform(Sprite.TRANS_NONE);
    	trans=false;
    	}
    	BallRebound.setPosition(x-23, y+5);
    	BallRebound.setFrame(0);
    	}
    	else if(z==2){
    	if(trans==false){
    	BallRebound.setTransform(Sprite.TRANS_MIRROR);
    	trans=true;
    	    	}
    	BallRebound.setPosition(x, y+5);
    	BallRebound.setFrame(0);
    	}
    	}
    	else {
    	BallRebound.setVisible(false);
    	BallDown.setVisible(true);
    	BallDown.setPosition(x-10, y+5);
    	BallDown.setFrame(0);
    	}
    }
     protected void keyPressed(int keyCode)
     {
        int action = getGameAction(keyCode);
        if(action == LEFT||action == GAME_A)
        {
            isLeft = true;
        }
        if(action == RIGHT||action == GAME_B)
        {
            isRight = true;
        }
        if(action == FIRE||keyCode==42)
        {
            count ++;
            updateBullet();
            bullet[0].setPosition(shooter.getX()+8,shooter.getY()-4);
        }
        if(!is&&action==UP){
        	if(handposition==20){
        		handposition=0;
        	}
        }
        if(!is&&action==DOWN){
        	if(handposition==0){
        		handposition=20;
        	}
        }
        if(!is&&action==FIRE){
        	if(handposition==20){
        		menu.stop=true;
    			Other.pinaLife=2;
    			mainMenu.isLevel=false;
    			parent.setDisplayable(mainmenu);
        	}
        	else{
        		continue3=4;
        		menu.stop=true;
    			mainMenu.count2++;
    			mainmenu.keyPressed(-5);
        	}
        }
        if(isgameover&&action==FIRE){
        	if(handposition==20){

            	gameover=true;
        		menu.stop=true;
    			Other.pinaLife=2;
    			mainMenu.isLevel=false;
    			parent.setDisplayable(mainmenu);
        	}
        	else{

            	gameover=true;
        		parent.Exit();
        	}
        }
        
        if(keyCode == -7)
        { 
        	is=false;
        	mainMenu.isLevel=false;
            parent.setDisplayable(menu);
        }
     }
     protected void keyReleased(int keyCode)
     {
         int action = getGameAction(keyCode);
         if(action == LEFT||action == GAME_A)
         {
            isLeft = false;
         }
         if(action == RIGHT||action == GAME_B)
         {
             isRight = false;
         }
     }
     /* 子弹的更新 */
     private void updateBullet()
    {
        for(int i=count; i>0; i--)
        {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -