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

📄 helloworld.java

📁 j2me撞砖游戏 j2me撞砖游戏 j2me撞砖游戏
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

import java.io.IOException;
import java.lang.Math;
import java.util.Random;
class Color
{
  public static final int BLACK =0;
  public static final int WHITE =0xffffff;
  public static final int RED   =0xff0000;
  public static final int GREEN =0xff00;
  public static final int BLUE  =0xff;
  public static final int YELLOW=0xffff00;
  public static final int PURPLE=0xff00ff;
}

class Ball
{
  private int brickId=0;
  private double x;
  private double y;
  private double R;
  private int color;
  private double xSpeed;
  private double ySpeed;
  
  Ball(){}
  Ball(double x,double y,double R,int color,double xSpeed,double ySpeed)
  {
	  this.x=x;
	  this.y=y;
	  this.R=R;
	  this.color=color;
	  this.xSpeed=xSpeed;
	  this.ySpeed=ySpeed;
  }
  
  Ball(Ball ball)
  {
	  this.x=ball.x;
	  this.y=ball.y;
	  this.R=ball.R;
	  this.color=ball.color;
	  this.xSpeed=ball.xSpeed;
	  this.ySpeed=ball.ySpeed;
  }
  
  public void setBrickId(int brickId)
  {
	  this.brickId=brickId;
  }
  
  public void setX(double x)
  {
	  this.x=x;
  }
  
  public void setY(double y)
  {
	  this.y=y;
  }
  
  public void setR(double R)
  {
	  this.R=R;
  }
  
  public void setXSpeed(double xSpeed)
  {
	  this.xSpeed=xSpeed;
  }
  
  public void setYSpeed(double ySpeed)
  {
	   this.ySpeed=ySpeed;
  }
  
  public int getBrickId()
  {
	  return brickId;
  }
  
  public double getX()
  {
	  return x;
  }
  
  public double getY()
  {
	  return y;
  }
  
  public double getR()
  {
	  return R;
  }
  
  public double getXSpeed()
  {
	  return xSpeed;
  }
  
  public double getYSpeed()
  {
	  return ySpeed;
  }
  
  public void draw(Graphics g)
  {
	  g.setColor(Color.WHITE);
	  g.fillArc((int)x,(int)y,(int)R,(int)R,0,360);
	  g.setColor(color);
	  g.fillArc((int)x+1,(int)y+1,(int)R-2,(int)R-2,0,360);
  }
}

class Brick
{
  private int brickId;
  private int x;
  private int y;
  private int width;
  private int height;
  private int color;
  private boolean isReDraw=false;
  
  Brick(){}
  Brick(int brickId,int x,int y,int width,int height,int color)
  {
	  this.brickId=brickId;
	  this.x=x;
	  this.y=y;
	  this.width=width;
	  this.height=height;
	  this.color=color;
  }
  
  public void setBrickId(int brickId)
  {
	  this.brickId=brickId;
  }
  
  public void setX(int x)
  {
	  this.x=x;  
  }
  
  public void setY(int y)
  {
	  this.y=y;
  }
  
  public void setWidth(int width)
  {
	  this.width=width;
  }
  
  public void setHeight(int height)
  {
	  this.height=height;
  }
  public void setIsReDraw(boolean isReDraw)
  {
	  this.isReDraw=isReDraw;
  }
  public int getBrickId()
  {
	  return brickId;
  }
  
  public int getX()
  {
	  return x;  
  }
  
  public int getY()
  {
	  return y;
  }
  
  public int getWidth()
  {
	  return width;
  }
  
  public int getHeight()
  {
	  return height;
  }
  
  public boolean getIsReDraw()
  {
	  return isReDraw;
  }
  
  public boolean checkCollision1(Ball ball)
  {
	  if(this.getBrickId()==ball.getBrickId()) return false;
	  
	  double ballR=ball.getR();
	  double ballCenterX=ball.getX()+ballR/2;
	  double ballCenterY=ball.getY()+ballR/2;
	  double brickCenterX=x+width/2;
	  double brickCenterY=y+height/2;
	  
	  if(this.getBrickId()==ball.getBrickId()) return false;
	  
	  if((ballCenterY >= y)&&(ballCenterY <= (y+height)))
	  {
		  if( ( ( ballCenterX < brickCenterX )&&( ( ball.getX() + ballR )>= x ) && ( ball.getXSpeed()>0 ) ) ||
		      ( ( ballCenterX > brickCenterX )&&( ball.getX()<= ( x+width ) )   && ( ball.getXSpeed()<0 ) ) )
	      {
		    ball.setXSpeed(-ball.getXSpeed());
		    ball.setBrickId(this.brickId);
		    return true;
	      }
	  }else
	  if((ballCenterX >= x)&&(ballCenterX <= (x+width)))
	  {
		  if( ( ( ballCenterY < brickCenterY )&&( ( ball.getY() + ballR ) >= y ) && ( ball.getYSpeed()>0 ) ) || 
		      ( ( ballCenterY > brickCenterY )&&( ball.getY() <= ( y+height ) )  && ( ball.getYSpeed()<0 ) ) )
	      {
		    ball.setYSpeed(-ball.getYSpeed());
		    ball.setBrickId(this.brickId);
		    return true;
	      }
	  }	  

	  
	  
	  return false;
  }
  public boolean checkCollision2(Ball ball)
  {
	  if(this.getBrickId()==ball.getBrickId()) return false;
	  
	  double ballR=ball.getR();
	  double ballCenterX=ball.getX()+ballR/2;
	  double ballCenterY=ball.getY()+ballR/2;
	  
	  double distanceX=0;
	  double distanceY=0;
	  double ballSpeedX=-ball.getXSpeed();
	  double ballSpeedY=-ball.getYSpeed();

      if(ballCenterX < x && ballCenterY < y)
	  {
	   	distanceX = ballCenterX-x;
	   	distanceY = ballCenterY-y;
	  }
	  else if(ballCenterX > ( x+width ) && ballCenterY < y)
	  {
	  	distanceX = ballCenterX-(x+width);
	  	distanceY = ballCenterY-y;
	  }
	  else if(ballCenterX < x && ballCenterY > ( y+height) )
	  {
	   	distanceX = ballCenterX-x;
	   	distanceY = ballCenterY-(y+height);
	  }
	  else if(ballCenterX > ( x+width ) && ballCenterY > ( y+height))
	  {
	   	distanceX = ballCenterX-(x+width);
	   	distanceY = ballCenterY-(y+height);
	  }
	  else
	  {
	   	return false;
	  }
	    
	  if(Math.abs(distanceX) <= (ballR/2+Math.abs(ballSpeedX)) && Math.abs(distanceY) <= (ballR/2+Math.abs(ballSpeedY)))
	  {
	    double distance = distanceX * distanceX + distanceY * distanceY;
	
	    if( distance <=  ballR*ballR/4 )
	    {
	      if(( ballSpeedX*distanceX+ballSpeedY*distanceY) <= 0) 
	      {
	    	  isReDraw=true;
	    	  return false;
	      }
	      
		  distance = Math.sqrt(distance);
				  
		  distanceX= -distanceX/distance;
		  distanceY=  distanceY/distance;
				  
		  distance=2*( distanceY * ballSpeedX + distanceX * ballSpeedY );
		
		  distanceX= distanceX * distance;
		  distanceY= distanceY * distance;
			 
		  ball.setXSpeed( ballSpeedX - distanceY );
		  ball.setYSpeed( ballSpeedY - distanceX );
		  ball.setBrickId(this.brickId);
		  return true;
		}
	   isReDraw=true;
	  }
	  return false;
	  
  }
  
  public void draw(Graphics g)
  {
	g.setColor(color);
	g.fillRect(x,y,width,height);
	
	g.setColor(Color.BLACK);
	g.drawLine(x+width  ,y,x+width  ,y+height);
	g.drawLine(x+width-1,y,x+width-1,y+height);
	
	g.drawLine(x,y+height  ,x+width,y+height  );
	g.drawLine(x,y+height-1,x+width,y+height-1);
	
	g.setColor(Color.WHITE/2);
	g.drawLine(x,y,x+width,y);
	g.drawLine(x,y+1,x+width-1,y+1);
	
	g.drawLine(x,y,x,y+height);
	g.drawLine(x+1,y,x+1,y+height-1);
  }
}

class GCanvas extends GameCanvas implements Runnable
{
	private int bricksRows=8;
	private int bricksCols=8;
	private int bricksViewWidth=200;
    private int bricksViewHeight=100;
    private int bricksWidth =bricksViewWidth/bricksCols;
    private int bricksHeight=bricksViewHeight/bricksRows;
    
	private Brick userBrick;
    private Brick[][] bricks=new Brick[bricksRows][bricksCols];
    private boolean[][] isShow=new boolean[bricksRows][bricksCols];
    
	private int viewWidth=220;
	private int viewHeight=240;
	private int viewLeft =(this.getWidth()-viewWidth)/2;
	private int viewRight=viewLeft+viewWidth;
	private int viewTop  =(this.getHeight()-viewHeight)/2;
	private int viewButtom=viewTop+viewHeight;
	
	private int userBrickWidth=40;
	private int userBrickHeight=10;
	private int userBrickLeftLimit=viewLeft+1;
	private int userBrickRightLimit=viewRight-userBrickWidth-1;
	
	private int userSpeed=0;
	private int userAcce=1;
	private int userSpeedLimit=10;
	
	private Ball ball;

⌨️ 快捷键说明

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