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

📄 game.java

📁 J2ME的手机小游戏,自己的毕业设计的程序
💻 JAVA
字号:
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
/*
 * 主要类 ,负责启程序画面等一系列工作
 * */
public class Game extends Canvas implements CommandListener {

	public static Image image;
	public int xspeed = 0, yspeed = 0;
	private Command menucmd;
	private Command pausecmd;
	private Command continuecmd;
	public Ball ball;
	public Father father;
	public Mapfirst gfirst;
	public Mapsecond gsecond;
	public Mapthird gthird;
	public boolean stay = false;
	public boolean status = true;
	public int time = 0;
	public int totaltime = 3000;
	public Display display;
	public static int score = 0;
	String message = "continues";
	Thread thread;
	Graphics gh;

	public Game() 
	{
		score=0;
		menucmd = new Command("menu", Command.BACK, 1);
		pausecmd = new Command("pause", 4, 1);
		continuecmd = new Command("continue", 4, 1);
		image = Image.createImage(super.getHeight(), super.getWidth());//做出图片 
		gh = image.getGraphics();
		ball = new Ball(39, 40);//初始化内部类
		this.addCommand(menucmd);
		this.addCommand(pausecmd);//Canvas添加command按钮
		setCommandListener(this);//监听器
		gfirst = new Mapfirst();
		gsecond = new Mapsecond();
		gthird = new Mapthird();
		//初始化地图
		thread = new Thread(new BallThread());
		thread.start();//启动用于循环画图的线程 
		isLevel();//根据难度设定得分

	}

	protected void paint(Graphics g) 
	{

		hitTests();
		//System.out.println("after hit test");
		gh.setColor(0XFFFFFF);//白色画笔
		gh.fillRect(0, 0, super.getWidth(), super.getHeight());
		gh.setColor(0X000000);//黑色
		gh.drawString("time: " + time / 1000, 18, 75, 0x10 | 0x1);
		gh.drawString("score: " + score, 75, 75, 0x10 | 0x1);
		gh.drawString("level: " + Level.level, 23, 85, 0x10 | 0x1);//在图片上写字
		ball.paint();
		if (message.equals("continues")) 
		{
			gfirst.paint();//第一张地图
		}
		if (message.equals("ok")) 
		{
			gsecond.paint();//2
		}
		if (message.equals("third"))
		{
			gthird.paint();//3
		}
		g.drawImage(image, 0, 0, 20);//画出图片
	}

	public void hitGate() //碰撞检测——是否碰到出口
	{

		int x = 0;
		int y = 0;
		int w = 0;
		int h = 0;
		boolean bfirst = false;
		boolean bsecond = false;
		boolean bthird = false;
		if (message.equals("continues"))//如果是第一副地图
		{
			x = gfirst.gate.getX();
			y = gfirst.gate.getY();
			w = gfirst.gate.getW();
			h = gfirst.gate.getH();
			bfirst = (hitTest(x, y, w, h, ball.getPotionx(), ball.getPotiony(),
					ball.getR() * 2, ball.getR() * 2));//碰到了返回true,没碰到返回false
		} else if (message.equals("ok")) //2
		{
			x = gsecond.gate.getX();
			y = gsecond.gate.getY();
			w = gsecond.gate.getW();
			h = gsecond.gate.getH();
			bsecond = (hitTest(x, y, w, h, ball.getPotionx(),
					ball.getPotiony(), ball.getR() * 2, ball.getR() * 2));
		} else if (message.equals("third")) //3
		{
			x = gthird.gate.getX();
			y = gthird.gate.getY();
			w = gthird.gate.getW();
			h = gthird.gate.getH();
			bthird = (hitTest(x, y, w, h, ball.getPotionx(), ball.getPotiony(),
					ball.getR() * 2, ball.getR() * 2));
		}

		if (bfirst && (message.equals("continues")))
		{
			message = "ok";//地图变换时候的状态,指示该换哪张图
			ball.setPX(20);//在新的地图上重新设定运动小球的坐标
			ball.setPY(2);
			xspeed = yspeed = 0;//同时让小球在新的地图上停下来
			System.out.println("hit first gate");
			sumScore();//计算上张图的得分
		}

		if (bsecond && (message.equals("ok"))) 
		{
			message = "third";
			ball.setPX(2);
			ball.setPY(2);
			xspeed = yspeed = 0;
			System.out.println("hit second gate");
			sumScore();
		}
		
		if (bthird && (message.equals("third"))) 
		{
			//完成整个游戏后
			JballGame.success();
		}

		

	}
	
	public boolean hitBall(int x1,int y1,int r1,int x2,int y2,int r2 )
	{
		int x=x1+r1-x2-r2;
		int y=y1+r1-y2-r2;
		int r=r1+r2;
		int len=(int )Math.sqrt((x*x+y*y));
		if(len>=r)
			return false;
		
		return true;
	}
	

	public void hitTests() 
	{

		hitGate();
		int bx = 0, by = 0, bw = 0, bh = 0;
		int ax = ball.getPotionx();
		int ay = ball.getPotiony();
		int aw = ball.getR() * 2;
		int ah = aw;
		
		if (message.equals("continues")) 
		{
			for (int i = 0; i < gfirst.rect.length; i++) 
			{
				//使用循环检测地图上的每个物体是否与运动的小球有碰撞
				bx = gfirst.rect[i].getX();
				by = gfirst.rect[i].getY();
				bw = gfirst.rect[i].getW();
				bh = gfirst.rect[i].getH();
				//stay = hitTest(ax, ay, ah, aw, bx, by, bw, bh);
				stay = hitTest(bx, by, bh, bw, ax, ay, aw, ah);
				if (stay||out()) {
					//碰撞或者小球运动出界后的处理
					System.out.println(stay + ", i=" + i);
					xspeed = yspeed = 0;
					break;
					
				}
			}
			
			for(int i=0; i<gfirst.sball.length;i++)//碰到别的球
			{
				
				System.out.println("ball test");
				bx=gfirst.sball[i].getXo();
				by=gfirst.sball[i].getYo();
				bw=gfirst.sball[i].getR();
				if(hitBall(bx,by,bw,ax,ay,aw/2))
				{
					ball.setPX(39);
					ball.setPY(40);
					xspeed=0;
					yspeed=0;
					break;
				}
				
			}
		}
		
		if (message.equals("ok")) 
		{
			
			for (int i = 0; i < gsecond.rect.length; i++) 
			{
				bx = gsecond.rect[i].getX();
				by = gsecond.rect[i].getY();
				bw = gsecond.rect[i].getW();
				bh = gsecond.rect[i].getH();


				stay = hitTest(bx, by, bh, bw, ax, ay, aw, ah);
				if (stay||out()) 
				{
					System.out.println(stay + ", i=" + i);

					xspeed = yspeed = 0;
					break;
					
				}
			}
			
			for(int i=0; i<gsecond.sball.length;i++)//碰到别的球
			{
				System.out.println("ball test");
				bx=gsecond.sball[i].getXo();
				by=gsecond.sball[i].getYo();
				bw=gsecond.sball[i].getR();
				if(hitBall(bx,by,bw,ax,ay,aw/2))
				{
					ball.setPX(20);
					ball.setPY(2);
					xspeed=0;
					yspeed=0;
					break;
				}
				
			}
		}
		
		if (message.equals("third")) 
		{
			for (int i = 0; i < gthird.rect.length; i++) 
			{
				bx = gthird.rect[i].getX();
				by = gthird.rect[i].getY();
				bw = gthird.rect[i].getW();
				bh = gthird.rect[i].getH();

				stay = hitTest(bx, by, bh, bw, ax, ay, aw, ah);
				
				if (stay||out()) 
				{
					System.out.println(stay + ", i=" + i);
					xspeed = yspeed = 0;
					break;
					
				}
			}
			
			for(int i=0; i<gthird.sball.length;i++)//碰到别的球
			{
				System.out.println("ball test");
				bx=gthird.sball[i].getXo();
				by=gthird.sball[i].getYo();
				bw=gthird.sball[i].getR();
				if(hitBall(bx,by,bw,ax,ay,aw/2))
				{
					ball.setPX(2);
					ball.setPY(2);
					xspeed=0;
					yspeed=0;
					break;
				}
				
			}
		}
	}
	
	public void isLevel()
	{
		//根据难度设定分数
		if (Level.level.equals("easy")) 
		{
			totaltime = 3000;
		}
		if (Level.level.equals("medium")) 
		{
			totaltime = 2000;
		}
		if (Level.level.equals("hard"))
		{
			totaltime = 1500;
		}
	}

	public void sumScore() 
	{

		// System.out.println("successfully pass");
		score = score + (1-time/(time+50)) * totaltime;//分数的计算方法

	}
	
	public boolean out()
	{
		//小球运动出界检测
		if(ball.getPotionx()<=0||ball.getPotiony()<=0||(ball.getPotionx()+2*ball.getR())>=100||(ball.getPotiony()+2*ball.getR())>=72)
		return true;
		return false;
		
	}

	public boolean hitTest(int ax, int ay, int ah, int aw, int bx, int by,
			int bh, int bw) 
	
	{
		//小球与物体的碰撞检测
		if ((Math.abs((ax + aw / 2) - (bx + bw / 2)) < Math.abs((aw + bw) / 2))
				&& (Math.abs((ay + ah / 2) - (by + bh / 2)) < Math
						.abs((ah + bh) / 2)))
			/*
			 * x方向: | (x1 + w1 / 2) – (x2 + w2/2) | < |(w1 + w2) / 2|
			 * 
			 * y方向: | (y1 + h1 / 2) – (y2 + h2/2) | < |(h1 + h2) / 2|
			 */
			return true;
		else
			return false;
		
	}

	public int getTime()
	{
		//总的时间
		return time;
	}

	public void commandAction(Command c, Displayable arg1) 
	
	{
		if (c == menucmd) 
		{
			//游戏中的时候按到MENU command;
			JballGame.display();
		}
		if (c == pausecmd) 
		{
			//游戏中的暂停按钮
			status = false;
			this.removeCommand(pausecmd);
			addCommand(continuecmd);

		}
		
		if (c == continuecmd) 
		{
			//继续按键
			System.out.println("change status to true");
			status = true;			
			removeCommand(continuecmd);
			addCommand(pausecmd);
		}
	}

	protected void keyPressed(int keyCode)
	
	{
		//按方向键的处理
		//在相应的方向上增减速度
		int action = super.getGameAction(keyCode);
		
		if (status) 
		{
			System.out.println("key pressed");
			switch (action) {
			case LEFT:
				xspeed = xspeed - 1;
				break;
			case UP:
				yspeed = yspeed - 1;
				break;
			case RIGHT:
				xspeed = xspeed + 1;
				break;
			case DOWN:
				yspeed = yspeed + 1;
				break;
			}
		}
	}

	protected void keyReleased(int keyCode)
	{
		// TODO Auto-generated method stub
		super.keyReleased(keyCode);
	}

	protected void keyRepeated(int keyCode) 
	{
		// TODO Auto-generated method stub
		super.keyRepeated(keyCode);
	}

	public void run() 
	
	{
		while (true)
		{
			repaint();

		}

	}

	class Ball {

		Mapfirst first;
		public int potionx = 0, potiony = 0;
		public int oldx = 39, oldy = 36;
		public int r = 2;

		public Ball(int x, int y)
		{
			potionx = x;
			potiony = y;

		}

		public void stay() 
		{
			//碰撞后小球回到之前没碰的位置上
			potionx = oldx;
			potiony = oldy;
		}

		public void paint() {
			
			if (stay||out()) 
			{
				//如果发生了碰撞或者出界
				stay();
				System.out.println("hit");
			}
			oldx = potionx;
			oldy = potiony;
			setPotionx(xspeed);//
			setPotiony(yspeed);//设定x y方向上的速度 
			Graphics g = image.getGraphics();
			System.out.println("potionx=" + potionx + " ,potiony=" + potiony
					+ ",oldx=" + oldx);
			g.fillArc(potionx - r, potiony - r, 2 * r, 2 * r, 0, 360);

		}

		public void setPX(int x)
		
		{
			potionx = x;
		}

		public void setPY(int y) 
		
		{
			potiony = y;

		}

		public boolean isInterRect()
		{
			return false;
		}

		public void setPotionx(int x)
		{
			potionx = potionx + x;
		}

		public void setPotiony(int y) 
		{
			potiony = potiony + y;
		}

		public int getPotiony()
		{
			return potiony;
		}

		public int getPotionx()
		{
			return potionx;
		}

		public int getR() 
		{
			return r;
		}
	}

	class BallThread implements Runnable {
		
		int delay = 100;

		public void run() 
		{
			while(true)
			{
			//	System.out.println("thread running");
				while(status)
				{
					repaint();
					try {
						Thread.sleep(delay);
						time = time + delay;
					} catch (InterruptedException e) {
					e.printStackTrace();
					}
				}
			}
		}

	}

}

⌨️ 快捷键说明

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