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

📄 homecanvas.java

📁 我做的手机游戏。有关宠物的
💻 JAVA
字号:
import java.io.IOException;

import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.game.TiledLayer;
import javax.microedition.midlet.*;

public class HomeCanvas extends GameCanvas implements CommandListener, Runnable {
	private Command backCommand;

	private PetMIDlet midlet;

	private Image home;

	private Display display;
	
	 private WormPit theGame;
	 private Thread myThread;
	 private TetrisGameCanvas theTertis;
	 private int drawX = 0;

		private int drawY = 0;
		
		private int dx=0,dy=0;

		private int i, j;
		private int x,y;


		private Sprite sprite;

		private Image titledImage, spriteImage;

		private Graphics g;

		private TiledLayer background;
		
		private LayerManager layManager;

		
		int a1=6;
	    int a2=0;
	    int a3=3;
	    int a4=9;



	private Command wanshua = new Command("玩耍", Command.OK, 2);

	private Command wanwanju = new Command("玩玩具", Command.OK, 2);

	private Command tanchishe = new Command("贪吃蛇", Command.OK, 2);

	private Command e = new Command("俄罗斯方块", Command.OK, 2);

	private Command sleep = new Command("睡觉", Command.OK, 2);

	private Command eating = new Command("喂食", Command.OK, 2);

	public HomeCanvas(PetMIDlet midlet) {
		super(true);
		this.midlet = midlet;

		
		

		backCommand = new Command("返回", Command.BACK, 1);
		addCommand(backCommand);
		addCommand(wanshua);
		addCommand(wanwanju);
		addCommand(tanchishe);
		addCommand(e);
		addCommand(sleep);
		addCommand(eating);
		setCommandListener(this);
		
		drawY = 79;

		try {
			spriteImage = Image.createImage("/man.png");
			titledImage = Image.createImage("/background.png");

		} catch (Exception e) {

		}
		//精灵单元宽32高48
		sprite = new Sprite(spriteImage, 32, 48);
	//	sprite.setFrameSequence(right_seq);

		g = this.getGraphics();
       //背景的初始化
		background = new TiledLayer(1, 1, titledImage, 753, 420);
		background.setCell(0,0,1);
		
	//	background.move(0,drawY - 50);
		
		layManager = new LayerManager();
		
		
		Thread thread = new Thread(this);
		thread.start();
		

	}
	
	
	
	
	
	

	public void run() {
		while (true) {
			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
	        input();
			//sprite.nextFrame();
			sprite.setPosition(drawX + dx , drawY+dy);
			
			layManager.append(sprite);
			layManager.append(background);	
			
			layManager.setViewWindow(dx,dy,getWidth() ,158);
			g.setColor(0, 0, 0);
			g.drawString("宠物的小窝",0,0,Graphics.TOP|Graphics.LEFT);
			g.drawLine(0,19,getWidth(),19);
			layManager.paint(g,0,20);
			flushGraphics();
		}
	}
                 //小人行走画面的坐标控制
	public  void input() {
		
		int keystates = getKeyStates();
	
		switch (keystates) {
		case UP_PRESSED:{
            if(dy-5>-drawY)
            	dy=dy-5;
            else dy=-drawY;
			
			if(a1+1<9)
				a1++;
			else a1=6;

		    sprite.setFrame(a1);
			break;
			}
		case DOWN_PRESSED:
			if(dy+5<300)
				dy+=5;
			else dy=300;

            if(a2+1<3)
            	a2++;
            else a2=0;
            sprite.setFrame(a2);
			break;
		case LEFT_PRESSED:
            if(dx-5>-drawX)
            	dx=dx-5;
            else dx=-drawX;
          
            if(a3+1<6)
            	a3++;
            else a3=3;
            sprite.setFrame(a3);
			break;
		case RIGHT_PRESSED:
           if(dx+5<(723-drawX))
        	   dx=dx+5;
           else dx=(723-drawX);
           
           if(a4+1<12)
        	   a4++;
           else a4=9;
           sprite.setFrame(a4);
			break;
		}
		x=dx;y=dy;
		if(x<0)
			x=0;
		else if(x>(753-getWidth()))
				x=753-getWidth();
		if(y<0)
			y=0;
		else if(y>260)
			y=260;
	}


	
	

	
	
	//按钮响应事件
	
	public void commandAction(Command c, Displayable d) {
		if (c == wanshua) {

		} else if (c == wanwanju) {
			// 进入玩玩具界面
		} 
		
		else if (c == tanchishe) {
			
			//添加贪吃蛇游戏
			theGame = new WormPit(midlet);
			display=midlet.getDisplay();
			display.setCurrent(theGame);
			 try {
		            // Start the game in its own thread
		            myThread = new Thread(theGame);
		            //ensure the game thread will work after pause
		            theGame.setDestroyed(false);
		            myThread.start();
		        } catch (Error e) {
		            
		        }// 进入游戏界面(贪吃蛇)
		  
		} 
		
		
		
		else if (c == e) {
			
			theTertis=new TetrisGameCanvas(midlet);
			Display.getDisplay(midlet).setCurrent(theTertis);
			
					
			// 进入游戏界面(俄罗斯方块)
			
		} else if (c == sleep) {
			// 显示睡觉的动作
		} else if (c == eating) {
			// 进入喂食界面
		}
		else if(c==backCommand){
			midlet.LookInfoBackmenu();
		}

		

	}

}

⌨️ 快捷键说明

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