java微信飞机大战,简单实现

import java.awt.*;import java.awt.event.*;import java.awt.image.BufferedImage;import java.io.*;import java.util.*;import java.util.Timer;import javax.imageio.ImageIO;import javax.sound.sampled.LineUnavailableException;import javax.sound.sampled.UnsupportedAudioFileException;import javax.swing.*;public class Hoofan extends JFrame{HuPanel hp;public static void main(String[] args) {new Hoofan();}public Hoofan(){hp=new HuPanel();this.addMouseMotionListener(hp);this.add(hp);this.setTitle("飞机大战");this.setSize(400, 600);this.setLocation(500, 100);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setVisible(true);}}class HuPanel extends JPanel implements Runnable,MouseMotionListener{Player player;//玩家static Vector<Enemy> enemys;//敌人飞机集合static Vector<Bullet> bullets;//子弹集合static Vector<Bomb> bombs;//炸弹集合BufferedImage bground,logo,shoot,bullet,small;BufferedImage smallBomb1,smallBomb2,smallBomb3;//炸弹图片SoundPlayer fire;public HuPanel(){player=new Player();enemys=new Vector<Enemy>();bullets=new Vector<Bullet>();bombs=new Vector<Bomb>();new Enemy();//启动敌人飞机try {bground=ImageIO.read(new File("images/shoot_background.png"));logo=ImageIO.read(new File("images/logo.png"));shoot=ImageIO.read(new File("images/shoot.png"));bullet=shoot.getSubimage(Bullet.imgX, Bullet.imgY, Bullet.imgW, Bullet.imgH);small=shoot.getSubimage(Enemy.imgX, Enemy.imgY, Enemy.imgW, Enemy.imgH);smallBomb1=shoot.getSubimage(Bomb.small1X, Bomb.small1Y, Bomb.smallW, Bomb.smallH);smallBomb2=shoot.getSubimage(Bomb.small2X, Bomb.small2Y, Bomb.smallW, Bomb.smallH);smallBomb3=shoot.getSubimage(Bomb.small3X, Bomb.small3Y, Bomb.smallW, Bomb.smallH);//fire=new SoundPlayer(Bullet.sound);//fire.loop();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}new Thread(this).start();}public void paint(Graphics g){super.paint(g);//画背景和玩家g.drawImage(bground, 0, -75,this);g.drawImage(logo, player.x, player.y,60,70,this);//画子弹for(int i=0;i<bullets.size();i++){Bullet b=bullets.get(i);g.drawImage(bullet, b.x, b.y,this);}//画敌人飞机for(int i=0;i<enemys.size();i++){Enemy e=enemys.get(i);g.drawImage(small, e.x, e.y,this);}//画炸弹for(int i=0;i<bombs.size();i++){Bomb b=bombs.get(i);if(b.life>12){g.drawImage(smallBomb1, b.x, b.y, this);}else if(b.life>5){g.drawImage(smallBomb2, b.x, b.y, this);}else{g.drawImage(smallBomb3, b.x, b.y, this);}//让b的生命值减小b.lifeDown();//如果炸弹生命值为0,就把该炸弹重bombs向量去掉if(b.life==0){bombs.remove(b);}}}@Overridepublic void run() {while(true){try {Thread.sleep(10);this.repaint();//刷新} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}@Overridepublic void mouseDragged(MouseEvent e) {}@Overridepublic void mouseMoved(MouseEvent e) {// 玩家的坐标跟随鼠标的坐标player.x=e.getX()-30;player.y=e.getY()-50;}}//飞机类class Plane{int x;int y;int speed;public Plane() {}public Plane(int x, int y,int speed) {this.x = x;this.y = y;this.speed=speed;}}//玩家类class Player extends Plane{int x=100;int y=450;Timer timer;Bullet bullet;public Player(){timer=new Timer();//定时器不断生成子弹timer.schedule(new PlayerTask(), 10);}class PlayerTask extends TimerTask{@Overridepublic void run() {// TODO Auto-generated method stubwhile(true){bullet=new Bullet(x+25,y);HuPanel.bullets.add(bullet);new Thread(bullet).start();try {Thread.sleep(100);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}}//敌人类class Enemy extends Plane implements Runnable{static int imgX=539;//图片x坐标static int imgY=617;static int imgW=50;//图片的宽度static int imgH=34;boolean isLife=true;//生命Timer timer;Random r;Enemy enemy;public Enemy() {r=new Random();timer=new Timer();//定时器不断生成敌人timer.schedule(new EnemyTask(), 100);}public Enemy(int x,int y,int speed) {super(x,y,speed);}class EnemyTask extends TimerTask{@Overridepublic void run() {// TODO Auto-generated method stubwhile(true){int result = 4 + (int)(Math.random() * ((8 – 4) + 1));//速度的值enemy=new Enemy(r.nextInt(350),0,result);HuPanel.enemys.add(enemy);new Thread(enemy).start();try {Thread.sleep(500);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}@Overridepublic void run() {// TODO Auto-generated method stubwhile(true){y+=speed;if(y>600){//超过边界,就从集合中删除HuPanel.enemys.remove(this);break;}try {Thread.sleep(50);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}class Bullet implements Runnable{int x;int y;static int imgX=71;static int imgY=78;static int imgW=7;static int imgH=21;int speed=8;//static String sound="sound/fire_bullet.wav";public Bullet(int x, int y) {super();this.x = x;this.y = y;}@Overridepublic void run() {// TODO Auto-generated method stubwhile(true){y-=speed;//碰撞检测for(int i=0;i<HuPanel.enemys.size();i++){Enemy e=HuPanel.enemys.get(i);if(x>=e.x&&x<=e.x+e.imgW&&y>=e.y&&y<=e.y+e.imgH){HuPanel.enemys.remove(e);Bomb bomb=new Bomb(e.x, e.y);HuPanel.bombs.add(bomb);try {//new SoundPlayer(bomb.sound).play();} catch (Exception e1) {e1.printStackTrace();}}}if(y<0){HuPanel.bullets.remove(this);break;}try {Thread.sleep(20);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}class Bomb{static int small1X = 272;//爆炸效果的第一个图片x坐标static int small1Y = 356;static int small2X = 878;static int small2Y = 704;static int small3X = 935;static int small3Y = 706;static int smallW = 50;static int smallH = 40;int x;int y;int life=18;//String sound="sound/small_plane_killed.wav";public Bomb(int x, int y) {this.x = x;this.y = y;}//减少生命值public void lifeDown(){if(life>0){life–;}}}

版权声明:本文为博主原创文章,未经博主允许不得转载。

,学习会使你永远立于不败之地。

java微信飞机大战,简单实现

相关文章:

你感兴趣的文章:

标签云: