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

0818b9ca8b590ca3270a3433284dd417.png

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 enemys;//敌人飞机集合

static Vector bullets;//子弹集合

static Vector bombs;//炸弹集合

BufferedImage bground,logo,shoot,bullet,small;

BufferedImage smallBomb1,smallBomb2,smallBomb3;//炸弹图片

SoundPlayer fire;

public HuPanel(){

player=new Player();

enemys=new Vector();

bullets=new Vector();

bombs=new Vector();

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 block

e.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

Bullet b=bullets.get(i);

g.drawImage(bullet, b.x, b.y,this);

}

//画敌人飞机

for(int i=0;i

Enemy e=enemys.get(i);

g.drawImage(small, e.x, e.y,this);

}

//画炸弹

for(int i=0;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);

}

}

}

@Override

public void run() {

while(true){

try {

Thread.sleep(10);

this.repaint();//刷新

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

@Override

public void mouseDragged(MouseEvent e) {}

@Override

public 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{

@Override

public void run() {

// TODO Auto-generated method stub

while(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 block

e.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{

@Override

public void run() {

// TODO Auto-generated method stub

while(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 block

e.printStackTrace();

}

}

}

}

@Override

public void run() {

// TODO Auto-generated method stub

while(true){

y+=speed;

if(y>600){//超过边界,就从集合中删除

HuPanel.enemys.remove(this);

break;

}

try {

Thread.sleep(50);

} catch (Exception e) {

// TODO Auto-generated catch block

e.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;

}

@Override

public void run() {

// TODO Auto-generated method stub

while(true){

y-=speed;

//碰撞检测

for(int i=0;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 block

e.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--;

}

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值