📄 hitbrick.java.bak
字号:
}
//检测球与NormalObject对象及其派生类的碰撞
public boolean CollisionWithObject(NormalObject ob)
{
float temp ;
collisioned = false ;
if((ob.x - this.x - this.cx) < 3 && (ob.x - this.x - this.cx) > -1)
{
if((this.y + this.cy >= ob.y) && (this.y <= ob.y+ob.cy))
{
if(this.vx - ob.vx > 0.0f)
{
collisioned = true ;
this.vx = -1.0f * (this.vx - ob.vx) ;
ob.vx = -0.4 * ob.vx ;
}
}
}
else
if((this.x - (ob.x + ob.cx)) < 3 && (this.x - (ob.x + ob.cx)) > -1)
{
if((this.y + this.cy >= ob.y) && (this.y <= ob.y+ob.cy))
{
if(ob.vx - this.vx > 0.0f)
{
collisioned = true ;
this.vx = 1.0f * (ob.vx - this.vx) ;
ob.vx = -0.4 * ob.vx ;
}
}
}
else
if((ob.y - this.y - this.cy) < 2 && (ob.y - this.y - this.cy) > 0)
{
if((this.x + this.cx >= ob.x) && (this.x <= ob.x + ob.cx))
{
if(this.vy - ob.vy > 0.0f)
{
collisioned = true ;
this.vy = -1.0f * (this.vy - ob.vy) ;
this.vx += ((float)(randomG.nextG(5) % 30 -15)) / 80.0f ;
this.vx += ob.vx * 0.6 ;
}
}
}
else
if((this.y - (ob.y + ob.cy)) < 2 && (this.y - (ob.y + ob.cy)) > 0)
{
if((this.x + this.cx >= ob.x) && (this.x < ob.x + ob.cx))
{
if(ob.vy - this.vy > 0.0f)
{
collisioned = true ;
this.vy = 1.0f * (ob.vy - this.vy) ;
this.vx += ((float)(randomG.nextG(5) % 30 -15)) / 80.0f ;
this.vx += ob.vx * 0.6 ;
}
}
}
return collisioned ;
}
//class Ball
//成员函数 END////////////////////////////////////////////////////////////////
}
//击球板类,由MoveObject派生
class Plate extends MoveObject
{
//成员变量 //////////////////////////////////////////////////////////////////
//class Plate
//静止界限
private float restLimit = 0.1f ;
//粘滞界限
private float stickykey = 1.0f ;
//衰减系数
private float attenuation = -0.7f ;
//摩擦系数
private float resistance = 0.03f ;
//正向加速度与逆向加速度
private float acceleration = 0.05f, antiAcceleration = 0.03f ;
//class Plate
//成员变量 END///////////////////////////////////////////////////////////////
//成员函数 //////////////////////////////////////////////////////////////////
//class Plate
public Plate(String path, int x, int y, float vx, float vy, boolean special)
{
super(path, x, y, vx, vy, special) ;
//if(suc)
//{
//}
}
//计算并移动击球板
public void move()
{
//根据参数计算击球板的速度
getV() ;
moveLimit() ;
if(!ball.CollisionWithObject(this))
{
//产生位移
if((vx - (int)vx) > 0.5f)
x += ((int) vx + 1);
else
x += (int)vx ;
}
}
//运动约束
private void moveLimit()
{
//边界约束
//击球板碰撞边界后反弹并带有速度衰减
if(vx < 0 && x <= 0)
vx *= attenuation ;
else if(x + cx >= width && vx > 0)
vx *= attenuation ;
//边界约束 END
}
//计算速度
private void getV(boolean left, boolean right)
{
//按下左右键时,
if(left)
{
//当速度降至静止界限下,就迫使击球板以一定初速运动
//其他情况按恒加速度运动计
if(vx < restLimit && vx > -restLimit)
{
vx = -1.0f ;
}
else if(vx < 0.0f)
{
vx -=antiAcceleration ;
}
else if(vx > 0.0f)
{
vx -= acceleration ;
}
}
if(right)
{
//当速度降至静止界限下,就迫使击球板以一定初速运动
//其他情况按恒加速度运动计
if(vx < restLimit && vx > -restLimit)
{
vx = 1.0f ;
}
else if(vx > 0.0f)
{
vx +=antiAcceleration ;
}
else if(vx < 0.0f)
{
vx +=acceleration ;
}
}
if(!left && !right)
{
//当移动键未按下,且速度降至粘滞界限下(1像素/帧以下),就迫使击球板停下来
//其他情况按恒加速度运动计
if((vx < stickykey && vx > 0.0f) || (vx > -stickykey && vx < 0.0f) )
vx = 0.0f ;
else if(vx > 0.0f)
{
vx -= resistance ;
}
else if(vx < 0.0f)
{
vx += resistance ;
}
}
}
private void getV()
{
//按下左右键时,
if(left)
{
//当速度降至静止界限下,就迫使击球板以一定初速运动
//其他情况按恒加速度运动计
if(vx < restLimit && vx > -restLimit)
{
vx = -1.0f ;
}
else if(vx < 0.0f)
{
vx -=antiAcceleration ;
}
else if(vx > 0.0f)
{
vx -= acceleration ;
}
}
if(right)
{
//当速度降至静止界限下,就迫使击球板以一定初速运动
//其他情况按恒加速度运动计
if(vx < restLimit && vx > -restLimit)
{
vx = 1.0f ;
}
else if(vx > 0.0f)
{
vx +=antiAcceleration ;
}
else if(vx < 0.0f)
{
vx +=acceleration ;
}
}
if(!left && !right)
{
//当移动键未按下,且速度降至粘滞界限下(1像素/帧以下),就迫使击球板停下来
//其他情况按恒加速度运动计
if((vx < stickykey && vx > 0.0f) || (vx > -stickykey && vx < 0.0f) )
vx = 0.0f ;
else if(vx > 0.0f)
{
vx -= resistance ;
}
else if(vx < 0.0f)
{
vx += resistance ;
}
}
}
//class Plate
//成员函数 END////////////////////////////////////////////////////////////////
}
}
//class MyCanvas
//嵌套类 END///////////////////////////////////////////////////////////////
//砖块类
class Brick extends NormalObject
{
public int count;
public int pictureNum ;
public Brick(int x , int y , int cx, int cy, int count , int strong, int pictureNum)
{
this.x = x ;
this.y = y ;
this.cx= cx ;
this.cy= cy ;
this.count = count ;
this.strong = strong ;
this.pictureNum = pictureNum ;
}
public void set(int count , int strong, int pictureNum)
{
this.count = count ;
this.strong = strong ;
this.pictureNum = pictureNum ;
}
}
//代表物体的基类
class NormalObject
{
//成员变量 //////////////////////////////////////////////////////////////////
//class NormalObject
public int x, y, cx, cy ;
public float vx = 0.0f, vy=0.0f ;
public int strong ;
//class NormalObject
//成员变量 END///////////////////////////////////////////////////////////////
//成员函数 //////////////////////////////////////////////////////////////////
//class NormalObject
public NormalObject(int x, int y,Image objectImage )
{
//成员初始化
this.x = x ;
this.y = y ;
this.cx = objectImage.getWidth() ;
this.cy = objectImage.getHeight() ;
}
public NormalObject()
{
}
//设置位置
public void setPos(int x, int y)
{
this.x = x ;
this.y = y ;
}
//绘制
public void paint(Graphics g,Image objectImage, boolean draw)
{
if(objectImage != null && draw)
{
g.drawImage(objectImage, x,y, Graphics.TOP | Graphics.LEFT) ;
}
}
public void paint(Graphics g)
{
}
//class NormalObject
//成员函数 END///////////////////////////////////////////////////////////////
}
//代表移动物体的基类
class MoveObject extends NormalObject
{
//成员变量 //////////////////////////////////////////////////////////////////
//class MoveObject
//public int x, y, cx, cy ;
//public float vx, vy ;
public boolean special, suc = true ;
public Image moveObject ;
//class MoveObject
//成员变量 END///////////////////////////////////////////////////////////////
//成员函数 //////////////////////////////////////////////////////////////////
//class MoveObject
public MoveObject(String path, int x, int y, float vx, float vy, boolean special)
{
super() ;
//图像载入几成员初始化
try
{
moveObject = Image.createImage(path) ;
this.x = x ;
this.y = y ;
this.cx = moveObject.getWidth() ;
this.cy = moveObject.getHeight() ;
this.vx = vx ;
this.vy = vy ;
}
catch(Exception error)
{
suc = false ;
}
}
//移动
public void move()
{
}
//设置位置
public void setPos(int x, int y)
{
this.x = x ;
this.y = y ;
}
//绘制
public void paint(Graphics graphics)
{
//super() ;
if(suc)
{
graphics.drawImage(moveObject, x,y, Graphics.TOP | Graphics.LEFT) ;
}
}
//class MoveObject
//成员函数 END///////////////////////////////////////////////////////////////
}
class RandomG extends Random
{
public RandomG()
{
super() ;
}
public int nextG(int bits)
{
return next(bits) ;
}
}
//动画线程类
class ImageMover implements Runnable
{
ImageMover()
{
}
public void run()
{
long ss =System.currentTimeMillis() ;
//帧定时
while (true)
{
if(canvas.dieLine <= 0)
break ;
long time = System.currentTimeMillis();
if((time - ss) > 10000)
{
ss = time ;
if(debug)
canvas.ticker.setString("dieline is " + canvas.dieLine) ;
}
//动画逻辑
{
canvas.plate.move() ;
canvas.ball.move() ;
//canvas.enemy.move(true) ;
}
time = System.currentTimeMillis() - time;
if (time >= 1) continue;
try
{
Thread.sleep(1 - time);
}
catch (Exception e){};
//重绘帧
canvas.repaint() ;
}
}
}
//主类
//嵌套类 END////////////////////////////////////////////////////////////////
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -