📄 game.java
字号:
}
}
}
if (bs.islive) { bs.move();}//未击中的子弹继续移动
}
else {BBv.remove(i);}//清除击中的子弹
}
//处理爆炸s
for (int i = 0 ; i < BOMBv.size() ; i++)
{
Bomb bombs;
bombs = (Bomb) BOMBv.elementAt(i);
if (bombs.islive){ bombs.grow();}
else { BOMBv.remove(i);}
}
//处理敌机子弹是否击中
for (int i = 0 ; i < EBv.size() ; i++)
{
EB ebs;
ebs = (EB) EBv.elementAt(i);
ebs.hit(hero_x,hero_y);
//消除无效子弹
if ( ebs.islive
&& ebs.y > -10 && ebs.y < a.y - 10
&& ebs.x > 0 && ebs.x < a.x - 10)
{ ebs.move();}
else if (!ebs.islive)
{ System.out.println("hit you"); EBv.remove(i);hero_hp -= 10;}
else
{ EBv.remove(i);}
}
cont++;
if (cont%3 == 0)
{
if (isfire){ BBmove(BBx,BBy);}
if (seq == 0) { seq = 1;}
else { seq = 0;}
cont = 0;
}
repaint();
try
{
Thread.sleep(20);
}
catch(Exception E){ }
if(y == 0) {y = 0;}
else {y++;}
}
}
public void BBmove(int x, int y)
{
BB bs;
bs = new BB (x,y);
//保证不出届
if (BBx >= (a.x - 75)){ BBx = a.x - 75;}
if (BBy >= (a.y - 150)){ BBy = a.y - 150;}
BBv.add(bs);
}
public void mouseDragged(MouseEvent e)
{
isfire = true;
BBx = hero_x + 10;
BBy = hero_y;
hero_x = e.getX() - 35;
hero_y = e.getY() - 35;
//保证不出届
if (BBx >= (a.x - 75)){ BBx = a.x - 75;}
if (BBy >= (a.y - 150)){ BBy = a.y - 150;}
if (BBx < 25){ BBx = 25;}
if (BBy < 25){ BBy = 25;}
//限速
if ((hero_x - hero_oldx) > 15 ){ hero_x = hero_oldx + 15;};
if ((hero_x - hero_oldx) < -15 ){ hero_x = hero_oldx - 15;};
if ((hero_y - hero_oldy) > 15 ){ hero_y = hero_oldy + 15;};
if ((hero_y - hero_oldy) < -15 ){ hero_y = hero_oldy - 15;};
//保证不出届
if (hero_x >= (a.x - 70)){ hero_x = a.x - 70;}
if (hero_y >= (a.y - 120)){ hero_y = a.y - 120;}
if (hero_x < 0){ hero_x = 0;}
if (hero_y < 0){ hero_y = 0;}
//if ((hero_oldx - hero_x) > 3){dir = 1;}
//else if ((hero_x - hero_oldx) > 3){dir = 2;}
//else {dir = 0;}
hero_oldx = hero_x;
hero_oldy = hero_y;
}
public void mouseMoved(MouseEvent e)
{
hero_x = e.getX() - 35;
hero_y = e.getY() - 35;
//保证不出届
if (hero_x >= (a.x - 70)){ hero_x = a.x - 70;}
if (hero_y >= (a.y - 120)){ hero_y = a.y - 120;}
if (hero_x < 0){ hero_x = 0;}
if (hero_y < 0){ hero_y = 0;}
//限速
if ((hero_x - hero_oldx) > 15 ){ hero_x = hero_oldx + 15;};
if ((hero_x - hero_oldx) < -15 ){ hero_x = hero_oldx - 15;};
if ((hero_y - hero_oldy) > 15 ){ hero_y = hero_oldy + 15;};
if ((hero_y - hero_oldy) < -15 ){ hero_y = hero_oldy - 15;};
//判断方向
//if ((hero_oldx - hero_x) > 10){dir = 1;}
//else if ((hero_x - hero_oldx) > 10){dir = 2;}
//else {dir = 0;}
//改值
hero_oldx = hero_x;
hero_oldy = hero_y;
}
public void mouseClicked(MouseEvent e)
{ }
public void mousePressed(MouseEvent e)
{
/////////////////////////////////////////////////////////
//如果GAMEOVER可以重新开始
if (hero_hp < 0)
{
if (this.Game_star != null)
{
this.Game_star.stop();
this.Game_star = null;
}
this.Game_star = new Thread(this);
Game_star.start();
Game_star.setPriority(7);
y = - (5*a.y);
hero_x = a.x/2 - 35;
hero_y = a.y - 85;
hero_oldx = hero_x;
hero_oldy = hero_y;
hero_hp = 200;
seq = 0;
dir = 0;
Sum = 0;
E_num = 0;
E_max = 9;
E_vel = 7;
E_mov = 90;
E_hit = 97;
isfire = false;
EEv.removeAllElements();
BBv.removeAllElements();
EBv.removeAllElements();
BOMBv.removeAllElements();
}
///////////////////////////////////////////////////////////////////////
else
{
isfire = true;
BBx = hero_x+10;
BBy = hero_y;
//保证不出届
//保证不出届
if (BBx >= (a.x - 75)){ BBx = a.x - 75;}
if (BBy >= (a.y - 150)){ BBy = a.y - 150;}
if (BBx < 25){ BBx = 25;}
if (BBy < 25){ BBy = 25;}
}
}
public void mouseReleased(MouseEvent e)
{
isfire = false;
}
public void mouseEntered(MouseEvent e)
{
// TODO: Add your code here
}
public void mouseExited(MouseEvent e)
{
// TODO: Add your code here
}
}
//物体类
class BB //子弹类
{
int x;
int y;
int v;//速度
int cont;//生命周期
boolean islive = true ;
BB(int x,int y)
{
this.x = x;
this.y = y;
this.v = 10;
this.cont = 0;
}
public void move()
{
if (this.cont >= 7)
{ this.cont = 0 ;}
this.y -= this.v;
this.cont++;
}
public void hit(int a,int b)
{
if ( Math.abs((x + 25) - (a + 25)) < 26 && Math.abs((y + 25) - (b + 25)) < 26)
{this.islive = false;}
else {this.islive = true;}
}
}
class EE //敌兵类
{
int x;
int y;
int r;//几率
int f;//开火几率
int vx;//速度
int vy;//速度
int cont;//生命周期
boolean islive = true ;
boolean toleft = false ;
boolean toright = false ;
EE(int x,int vy,int r,int f)
{
this.x = x;
this.y = -50;
this.vx = 0;
this.vy = vy;
this.r = r;
this.f = f;
this.cont = 0;
}
// 敌机移动
public void move(int a)
{
// 一定几率横向移动
if(Math.random()*100 > r)
{
if (a - this.x > 50 && cont == 0)
{
this.vx = 3;
this.toright = true;
this.toleft = false;
}
else if (this.x - a > 50 && cont == 0)
{
this.vx = -3;
this.toright = false;
this.toleft = true;
}
this.cont++;
}
if (this.cont > 2 )
{
this.vx = 0;
this.toright = false;
this.toleft = false;
this.cont = 0;
}
this.x += this.vx;
this.y += this.vy;
}
// 敌机开火
public boolean tofire()
{
// 一定几率开火
if(Math.random()*100 > f){ return true; }
else { return false;}
}
//碰撞检查
public void hit(int a,int b)
{
if ( Math.abs((x + 25) - (a + 25)) < 28 && Math.abs((y + 25) - (b + 25)) < 28)
{this.islive = false;}
else {this.islive = true;}
}
}
class EB //敌兵子弹类
{
int x;
int y;
int vx;
int cont;//生命周期
int vy;//速度
boolean islive = true ;
EB(int x,int y,int a,int b)
{
this.x = x;
this.y = y;
this.vy = 15;
this.cont = 0;
if (this.x < a)
{
this.vx = 2;
}
else
{
this.vx = -2;
}
if (this.y < b)
{
this.vy = 7;
}
else
{
this.vy = -7;
}
}
public void move()
{
if (this.cont >= 20){ this.cont = 0;}
this.cont++;
this.y += this.vy;
this.x += this.vx;
}
public void hit(int a,int b)
{
if ( Math.abs((x + 10) - (a + 25)) < 28 && Math.abs((y + 10) - (b + 5)) < 28)
{this.islive = false;}
else {this.islive = true;}
}
}
class Bomb //敌兵爆炸类
{
int x;
int y;
int cont;
boolean islive = true ;
Bomb(int x,int y)
{
this.x = x;
this.y = y;
this.cont = 0;
}
public void grow()
{
if (cont > 61) {this.islive = false;}
this.cont ++;
this.y ++ ;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -