class MystartPanel extends JPanel implements Runnable{
int times=100;
//覆盖父类paint函数
public void paint(Graphics g){
super.paint(g);
//画出面板大小
g.fillRect(0, 0, 420, 300);
//间隔1秒
if(times%2==0){
//画笔颜色、字体
g.setColor(Color.yellow);
Font myfont= new Font("华文新魏",Font.BOLD,30);
//获取当前字体
g.setFont(myfont);
//画出字符串
g.drawString("Stage:1",150,150);
}
}
//线程,让开始面板闪烁
public void run() {
//循环
while(true){
try {
//休眠50毫秒
Thread.sleep(50);
} catch (Exception e) {
e.printStackTrace();
}
//重画
this.repaint();
times--;
}
}
}
//定义面板MyPanel
class MyPanel extends JPanel implements KeyListener,Runnable{
private int intsize=3;
//定义我的坦克、敌人坦克、我的子弹、敌人子弹
HeroTank hero;
EnemyTank enemytank;
Shot myshot;
Shot enemyshot;
//向量敌人坦克、我的子弹、炸弹
Vector<EnemyTank> enemy=new Vector<EnemyTank>();
Vector<Shot> heroshots=new Vector<Shot>();
Vector<Bomb> bombs=new Vector<Bomb>();
Vector<Node> nodes=new Vector<Node>();
//定义图片
Image image1=null;
Image image2=null;
Image image3=null;
// 构造函数
public MyPanel(String flag){
// 调用记录
Recorder.getRecording();
// 初始化我的坦克
hero=new HeroTank(190, 270,0, 0, 2,null);
// 初始化敌人坦克
if(flag.equals("newgame")){
for(int i=0;i<intsize;i++){
enemytank=new EnemyTank(i*195, 0, 1, 2, 2, null);
enemy.add(enemytank);
enemytank.setEts(enemy);
Thread t2=new Thread(enemytank);
t2.start();
// 初始化敌人子弹
enemyshot=new Shot(enemytank.x+10,enemytank.y+30, enemytank.direction, true);
enemytank.enemyshots.add(enemyshot);
Thread t4=new Thread(enemyshot);
t4.start();
}
}else{
nodes=new Recorder().getNodesAndEnNums();
for(int i=0;i<nodes.size();i++){
Node node=nodes.get(i);
enemytank=new EnemyTank(node.x, node.y, 1,node.direction, 2, null);
enemy.add(enemytank);
enemytank.setEts(enemy);
Thread t2=new Thread(enemytank);
t2.start();
// 初始化敌人子弹
enemyshot=new Shot(enemytank.x+10,enemytank.y+30, enemytank.direction, true);
enemytank.enemyshots.add(enemyshot);
Thread t4=new Thread(enemyshot);
t4.start();
}
}
// 初始化爆炸效果图片
image3=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/111.jpg"));
image2=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/222.jpg"));
image1=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/333.jpg"));
}
// 重写父类paint函数
public void paint(Graphics g){
super.paint(g);
// 面板大小
g.fillRect(0, 0, 420, 300);
// 画出面板记录
this.showInfo(g);
if(hero.isLive){
g.setColor(Color.CYAN);
this.drawTank(hero.getX(), hero.getY(), hero.getType(), hero.getDirection(),hero.getSpeed(), g);
}
// 画出炸弹
for(int i=0;i<bombs.size();i++){
Bomb bomb=bombs.get(i);
if(bomb.life>6){
g.drawImage(image1,bomb.x, bomb.y, 30,30, this);
}else if(bomb.life>3){
g.drawImage(image2,bomb.x, bomb.y, 30,30, this);
}else{
g.drawImage(image3,bomb.x, bomb.y, 30,30, this);
}
bomb.lifeDown();
if(bomb.life==0){
bombs.remove(bomb);
}
}
// 画出敌人坦克
for(int i=0;i<enemy.size();i++){
EnemyTank enemytank=enemy.get(i);
if(enemytank.isLive==true){
this.drawTank(enemytank.getX(),enemytank.getY(),enemytank.getType(),
enemytank.getDirection(),enemytank.getSpeed(), g);
for(int j=0;j<enemytank.enemyshots.size();j++){
Shot enemyshot=enemytank.enemyshots.get(j);
if(enemyshot.isLive==true){
g.fillOval(enemyshot.x, enemyshot.y, 1, 2);
}else{
enemytank.enemyshots.remove(enemyshot);
}
}
}
}
// 画出我的子弹
for(int i=0;i<heroshots.size();i++){
myshot=heroshots.get(i);
if(myshot!=null&&myshot.isLive==true){
g.setColor(Color.WHITE);
g.fillOval(myshot.x, myshot.y, 1, 2);
repaint();
}
if(myshot.isLive==false){
heroshots.remove(myshot);
}
}
}
// 画出面板记录函数
public void showInfo(Graphics g){
this.drawTank(70, 335, 0, 0, 0, g);
g.setColor(Color.black);
Font myfont= new Font("华文新魏",Font.BOLD,30);
g.setFont(myfont);
g.drawString(" X "+Recorder.getMyLife(), 100, 360);
this.drawTank(70, 375, 1, 0, 0, g);
g.setColor(Color.black);
Font myfont2= new Font("华文新魏",Font.BOLD,30);
g.setFont(myfont2);
g.drawString(" X "+Recorder.getEnNum(), 100, 400);
g.setColor(Color.black);
Font myfont3=new Font("华文新魏", Font.BOLD, 50);
g.drawString("您的总成绩", 525, 50);
this.drawTank(535,80, 1, 0, 0, g);
g.setColor(Color.BLACK);
g.drawString(" X "+Recorder.getAllenNum(), 580, 105);
}
// 画坦克函数
public void drawTank(int x, int y,int type, int direction, int speed,Graphics g){
switch(type){
case 0:
g.setColor(Color.YELLOW);
break;
case 1:
g.setColor(Color.GREEN);
break;
}
switch(direction){
// 坦克方向↑
case 0:
g.fill3DRect(x, y, 5, 30,false);
g.fill3DRect(x+15, y, 5, 30,false);
g.fill3DRect(x+5, y+5, 10, 20,false);
g.fillOval(x+5, y+10,10, 10);
g.drawLine(x+10,y, x+10, y+15);
break;
// 坦克方向→
case 1:
g.fill3DRect(x, y, 30, 5,false);
g.fill3DRect(x, y+15, 30, 5,false);
g.fill3DRect(x+5, y+5,20, 10,false);
g.fillOval(x+10, y+5,10, 10);
g.drawLine(x+15,y+10, x+30, y+10);
break;
// 坦克方向↓
case 2:
g.fill3DRect(x, y, 5, 30,false);
g.fill3DRect(x+15, y, 5, 30,false);
g.fill3DRect(x+5, y+5, 10, 20,false);
g.fillOval(x+5, y+10,10, 10);
g.drawLine(x+10,y+30, x+10, y+15);
break;
// 坦克方向←
case 3:
g.fill3DRect(x, y, 30, 5,false);
g.fill3DRect(x, y+15, 30, 5,false);
g.fill3DRect(x+5, y+5,20, 10,false);
g.fillOval(x+10, y+5,10, 10);
g.drawLine(x+15,y+10, x, y+10);
break;
}
}
// 创建我的子弹
public void shotEnemy(int x,int y,int direction,boolean isLive){
// 判断我的坦克方向
switch(hero.direction){
case 0:
myshot=new Shot(x+10,y,0, true);
heroshots.add(myshot);
break;
case 1:
myshot=new Shot(x+30,y+10,1,true);
heroshots.add(myshot);
break;
case 2:
myshot=new Shot(x+10,y+30,2,true);
heroshots.add(myshot);
break;
case 3:
myshot=new Shot(x,y+10,3, true);
heroshots.add(myshot);
break;
}
// 执行我的子弹线程
Thread t=new Thread(myshot);
t.start();
}
// 线程函数
public void run() {
while(true){
try {
Thread.sleep(100);
} catch (Exception e) {
e.printStackTrace();
}
this.hitEnemyTank();
this.hitHeroTank();
this.repaint();
}
}
// 事件处理
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_W){
this.hero.setDirection(0);
this.hero.moveUp();
repaint();
}
else if(e.getKeyCode()==KeyEvent.VK_S){
this.hero.setDirection(2);
this.hero.moveDown();
repaint();
}
else if(e.getKeyCode()==KeyEvent.VK_A){
this.hero.setDirection(3);
this.hero.moveLeft();
repaint();
}
else if(e.getKeyCode()==KeyEvent.VK_D){
this.hero.setDirection(1);
this.hero.moveRight();
repaint();
}
else if(e.getKeyCode()==KeyEvent.VK_J){
if(heroshots.size()<=4&&hero.isLive==true){
this.shotEnemy(hero.x, hero.y,hero.direction, true);
repaint();
}
}
}
// 击中敌人坦克
public void hitEnemyTank(){
for(int i=0;i<heroshots.size();i++){
myshot=heroshots.get(i);
if(myshot.isLive){
for(int j=0;j<enemy.size();j++){
enemytank=enemy.get(j);
if(enemytank.isLive){
if(this.hitTank(enemytank, myshot)){
Recorder.reduceEnEnm();
Recorder.addallenNum();
}
}
}
}
}
}
// 我的坦克被击中
public void hitHeroTank(){
for(int i=0;i<this.enemy.size();i++){
EnemyTank enemytank=enemy.get(i);
if(enemytank.isLive){
for(int j=0;j<enemytank.enemyshots.size();j++){
Shot enemyshot=enemytank.enemyshots.get(j);
if(hero.isLive){
if(this.hitTank(hero, enemyshot)){
Recorder.reducemyLife();
}
}
}
}
}
}
// 判断子弹是否击中坦克
public boolean hitTank(Tank enemytank,Shot shot){
boolean b=false;
switch(enemytank.direction){
case 0:
case 2:
if(shot.x>enemytank.x&&shot.x<enemytank.x+20&&shot.y>enemytank.y&&shot.y<enemytank.y+30){
shot.isLive=false;
enemytank.isLive=false;
Recorder rd=new Recorder();
b=true;
Bomb bomb=new Bomb(enemytank.x, enemytank.y);
bombs.add(bomb);
}break;
case 1:
case 3:
if(shot.x>enemytank.x&&shot.x<enemytank.x+30&&shot.y>enemytank.y&&shot.y<enemytank.y+20){
shot.isLive=false;
enemytank.isLive=false;
Recorder rd=new Recorder();
b=true;
Bomb bomb=new Bomb(enemytank.x, enemytank.y);
bombs.add(bomb);
}break;
}
return b;
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
} |