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

📄 demoball.java

📁 手机小游戏弹球
💻 JAVA
字号:
package com.my;
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.*;
import java.util.*;
public class DemoBall extends MIDlet {
	Display display;
	Timer timer = new Timer();
	Ball  ball = new Ball();
	TimerTask mytask = new MyTask();
	public DemoBall() {
		// TODO Auto-generated constructor stub
		display = Display.getDisplay(this);
		display.setCurrent(ball);
	}
	protected void destroyApp(boolean arg0){
		// TODO Auto-generated method stub
	}
	protected void pauseApp() {
		// TODO Auto-generated method stub
	}
	protected void startApp() {
		// TODO Auto-generated method stub
		timer.schedule(mytask,0,10);
	}
	class MyTask extends TimerTask
	{
		public void run()
		{
			ball.run();
		}
	}
	class Ball extends Canvas
	{
		private int width;
		private int height;
		private int x;
		private int y;	
		private int step_x;
		private int step_y;	
		private int box_x;
		private int box_y;
		private int box_step;
		boolean Flag = true;
		Random gen = new Random();
		public Ball()
		{
			width = getWidth();
			height = getHeight();
			x = width / 2;
			y = height / 2 ;
			step_x = (gen.nextInt()%3+1);
			step_y = (gen.nextInt()%3+1);
			box_x = (width-80)/2;
			box_y = height-10;
			box_step = 0;
		}	
		protected void paint(Graphics g)
		{
			g.setColor(0,0,0);
			g.fillRect(0,0,width,height);

			if (Flag==false)
			{
				g.setColor(0,255,0);
				g.drawString("Game Over",width/2, height/2, 0);
			}
			else
			{
				g.setColor(255,255,255);
				g.fillArc(x, y, 10,10, 0,360);			
				g.setColor(0,255,0);
				g.fillRect(box_x,box_y,80,10);
			}
		}
		public void run()
		{
			// process data
			x = x + step_x;
			y = y + step_y;
			if (x+10>width-1)
			{
				x = width-1-10;
				step_x = -step_x;
			}
			if (x<0)
			{
				x = 0;
				step_x = -step_x;
			}		
			if ((y+10+10>height-1) && (box_x< x) && (box_x+80>x))
			{
				y = height-1-10-10;
				step_y = -step_y;			
			}			
			if (y+10>height-1)
			{
				Flag = false;				
			}
			if (y<0)
			{
				y = 0;
				step_y = -step_y;
			}
			box_x+=box_step;	
			if (box_x<0) box_x = 0;
			if (box_x>width-81) box_x = width-81;
			// draw
			repaint();			
		}		
		protected void keyPressed(int keyCode)
		{
			switch(getGameAction(keyCode))
			{
			case LEFT:
				if (box_step>0) box_step=0;
				box_step--;
				break;
			case RIGHT:
				if (box_step<0) box_step=0;
				box_step++;
				break;
			case UP:
			case DOWN:
				box_step=0;
				break;
			}
		}
		protected void keyRepeated(int keyCode)
		{
			keyPressed(keyCode);
		}
	}	
}

⌨️ 快捷键说明

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