package lightning;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
/*import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;*/
import java.util.EventListener;
import java.util.Vector;
import lightning.*;
public class MainPanel extends JPanel implements Runnable,MouseMotionListener,MouseListener{
Game a;
Vector BBv=new Vector();//子弹数组
Vector EEv=new Vector();//敌机数组
Vector BOMBv=new Vector();//爆炸数组
Vector EBv=new Vector();//敌机子弹数组
Vector DBv=new Vector();//大爆数组
int E_num;
int E_max;//敌机最大数
int E_vel;//敌机速度
int E_mov;//敌机横移机率
int E_hit;//敌机开火机率
int Sum;//击坠数
int hero_hp;//hero生命
Image back=null;
Image hero=null;
Image hero_l=null;
Image hero_r=null;
Image fire=null;
Image fire_l=null;
Image el_0=null;
Image el_0_l=null;
Image el_0_r=null;
Image el_bb=null;
Image el_bb_0=null;
Image bomb_0=null;
Image bomb_1=null;
Image bomb_2=null;
Image bomb_3=null;
Image db_1=null;
Image db_2=null; //大爆的图片
MediaTracker tracker=null;//媒体跟踪器,用来监测图像的装载
Thread Game_star;
int y;//背景滚动变量
int hero_oldx,hero_oldy;//hero的旧x,y坐标
int hero_x,hero_y;//hero的新x,y坐标
int seq;//hero的动画变量
// int dir;//hero的左右方向变量
boolean isfire;//hero开火
boolean isblast;//是否发爆
int BBx,BBy;
int DBx,DBy; //大爆的横纵坐标
int blastnum; //大爆的数量
boolean blastc; //控制大爆
Font f=new Font("黑体",Font.BOLD,30);
Font ff=new Font("黑体",Font.BOLD,90);
Font S=new Font("幼圆",Font.BOLD,15);
Font SS=new Font("幼圆",Font.BOLD,25);
MainPanel(Game a) {
this.a=a;
tracker=new MediaTracker(this);
back=Toolkit.getDefaultToolkit().getImage(MainPanel.class.getResource("back3.gif"));
tracker.addImage(back,0);
hero=Toolkit.getDefaultToolkit().getImage(MainPanel.class.getResource("hero.gif"));
tracker.addImage(hero,1);
fire=Toolkit.getDefaultToolkit().getImage(MainPanel.class.getResource("herozidan.gif"));
tracker.addImage(fire,2);
fire_l=Toolkit.getDefaultToolkit().getImage(MainPanel.class.getResource("herozidan2.gif"));
tracker.addImage(fire_l,3);
el_0=Toolkit.getDefaultToolkit().getImage(MainPanel.class.getResource("diji.gif"));
tracker.addImage(el_0,4);
el_0_l=Toolkit.getDefaultToolkit().getImage(MainPanel.class.getResource("dijileft.gif"));
tracker.addImage(el_0_l,5);
el_0_r=Toolkit.getDefaultToolkit().getImage(MainPanel.class.getResource("dijiright.gif"));
tracker.addImage(el_0_r,6);
el_bb=Toolkit.getDefaultToolkit().getImage(MainPanel.class.getResource("dijizidan.gif"));
tracker.addImage(el_bb,7);
el_bb_0=Toolkit.getDefaultToolkit().getImage(MainPanel.class.getResource("el_bb_0.gif"));
tracker.addImage(el_bb_0,8);
bomb_0=Toolkit.getDefaultToolkit().getImage(MainPanel.class.getResource("bomb_0.gif"));
tracker.addImage(bomb_0,9);
bomb_1=Toolkit.getDefaultToolkit().getImage(MainPanel.class.getResource("bomb_1.gif"));
tracker.addImage(bomb_1,10);
bomb_2=Toolkit.getDefaultToolkit().getImage(MainPanel.class.getResource("bomb_2.gif"));
tracker.addImage(bomb_2,11);
bomb_3=Toolkit.getDefaultToolkit().getImage(MainPanel.class.getResource("bomb_3.gif"));
tracker.addImage(bomb_3,12);
db_1=Toolkit.getDefaultToolkit().getImage(MainPanel.class.getResource("db1.gif"));
tracker.addImage(db_1,13);
db_2=Toolkit.getDefaultToolkit().getImage(MainPanel.class.getResource("db2.gif"));
tracker.addImage(db_2,14);//将大爆的图片加到媒体跟踪器中
this.addMouseMotionListener(this);
this.addMouseListener(this);
Game_star=new Thread(this);
y=-(5*a.y);
hero_x=a.x/2-50;
hero_y=a.y-150;
hero_oldx=hero_x;
hero_oldy=hero_y;
hero_hp=-1;
seq=0;
// dir=0;
Sum=0;
E_num=0;
E_max=9;
E_vel=7;
E_mov=90;
E_hit=97;
isfire=false;
isblast=false;
blastnum=0;
blastc=false;
}
public void paint(Graphics g){
g.drawImage(back,0,y,a.x,a.y*6,this);
g.setColor(Color.RED);
g.setFont(S);
g.drawString("导弹:"+blastnum,a.x-400,25);//绘制大爆的数量
g.drawString("生",a.x-23,40);
g.drawString("命",a.x-23,55);
g.fillRect(a.x-20,65,10,hero_hp);
g.setColor(Color.BLACK);
g.drawRect(a.x-20,64,10,300);
if(seq==0){
g.setColor(Color.RED);
}
if(seq==1){
g.setColor(Color.BLACK);
}
g.setFont(S);
g.drawString("积分:",a.x-80,25);
g.drawString(String.valueOf(Sum),a.x-40,25);
g.setFont(f);
if(hero_hp==-1){
g.setFont(f);
g.setColor(Color.RED);
g.drawString("你能超过1000分吗?",a.x/2-a.x/3,a.y/2-a.y/16);
g.setFont(SS);
g.setColor(Color.RED);
g.drawString("请按下鼠标键开始",a.x/2-a.x/4,a.y/2+a.y/8);
g.dispose();
try{
Thread.sleep(50);
}catch(Exception e){
}
this.Game_star=null;
}
//如果游戏结束显示如下信息
else if(hero_hp<0){
g.setFont(ff);
g.setColor(Color.RED);
g.drawString("GAME", a.x / 2 - a.x / 4, a.y / 2 - a.y / 16);
g.drawString("OVER", a.x / 2 - a.x / 4, a.y / 2 + a.y / 16);
g.setFont(SS);
g.setColor(Color.BLACK);
g.drawString("请按下鼠标键重来", a.x / 2 - a.x / 4, a.y / 2 + a.y / 8);
g.dispose();
try {
Thread.sleep(50);
}
catch (Exception e) {
}
this.Game_star=null;
}
//显示信息
if(Math.abs(y)>(a.y)*5-50){
g.drawString("本游戏即将开始",a.x/2-a.x/4,a.y/2-a.y/8);
}
if(Math.abs(y)>(a.y)*4 && Math.abs(y)<(a.y)*4+150){
E_max=13;
E_vel=7;
E_mov=90;
E_hit=95;
g.drawString("第一关即将开始",a.x/2-a.x/4,a.y/2-a.y/8);
}
if(Math.abs(y)>(a.y)*3 && Math.abs(y)<(a.y)*3+150){
E_max=16;
E_vel=9;
E_mov=90;
E_hit=93;
g.drawString("第二关即将开始",a.x/2-a.x/4,a.y/2-a.y/8);
}
if(Math.abs(y)>(a.y)*2 && Math.abs(y)<(a.y)*2+150){
E_max=29;
E_vel=11;
E_mov=90;
E_hit=91;
g.drawString("第三关即将开始",a.x/2-a.x/4,a.y/2-a.y/8);
}
if(Math.abs(y)>(a.y) && Math.abs(y)<(a.y)+150){
E_max=32;
E_vel=13;
E_mov=90;
E_hit=89;
g.drawString("最后了祝你好运",a.x/2-a.x/4,a.y/2-a.y/8);
}
if(y==0){
E_max=40;
E_vel=18;
E_mov=90;
E_hit=86;
g.drawString("向上帝祈祷吧!",a.x/2-a.x/4,a.y/2-a.y/8);
}
//处理敌机子弹s
for(int i=0;i<EBv.size();i++){
EnemyBullet ebs;
ebs=(EnemyBullet)EBv.elementAt(i);
if(ebs.y>-50){
if(ebs.cont<6){
g.drawImage(el_bb,ebs.x,ebs.y,13,20,this);
}
if(ebs.cont>=6){
g.drawImage(el_bb_0,ebs.x,ebs.y,13,15,this);
}
}
}
//处理子弹s
for(int i=0;i<BBv.size();i++){
PlayerBullet bs;
bs=(PlayerBullet)BBv.elementAt(i);
if(bs.y>-50){
if(bs.cont<3){
g.drawImage(fire_l,bs.x,bs.y,30,30,this);
}
if(bs.cont>=3){
g.drawImage(fire,bs.x,bs.y,30,30,this);
}
}
}
//处理大爆
for(int i=0;i<DBv.size();i++){
Blast blast;
blast=(Blast)DBv.elementAt(i);
if(blast.islive){
if(blast.cont<10){
//System.out.println(""+blast.x+" "+blast.y);
g.drawImage(db_1,blast.x,blast.y,200,200,this);
}
if(blast.cont>10 && blast.cont<20){
g.drawImage(db_2,blast.x,blast.y,200,200,this);
}
}else{
DBv.remove(i);
}
}
//处理爆炸s
for(int i
- 1
- 2
- 3
前往页