代码下载 http://download.csdn.net/detail/zwyjg/9826678
python版本 http://blog.csdn.net/zwyjg/article/details/70207495
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.*;
import java.util.Timer;
import java.util.concurrent.CopyOnWriteArrayList;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Hufan extends JFrame {
HuPanel hp;
public static void main(String[] args) {
new Hufan();
}
public Hufan() {
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 CopyOnWriteArrayList enemys;// 敌人飞机集合
static CopyOnWriteArrayList bullets;// 子弹集合
static CopyOnWriteArrayList bombs;// 炸弹集合
BufferedImage bground, logo, shoot, bullet, small;
BufferedImage smallBomb1, smallBomb2, smallBomb3;// 炸弹图片
public HuPanel() {
player = new Player();
enemys = new CopyOnWriteArrayList();
bullets = new CopyOnWriteArrayList();
bombs = new CopyOnWriteArrayList();
new Enemy();// 启动敌人飞机
try {
bground = ImageIO.read(new File("images/shoot_background.png"));
logo = ImageIO.read(new File("images/logo.png"));
bullet = ImageIO.read(new File("images/bullet.png"));
small = ImageIO.read(new File("images/enemy1.png"));
smallBomb1 = ImageIO.read(new File("images/enemy1_down1.png"));
smallBomb2 = ImageIO.read(new File("images/enemy1_down2.png"));
smallBomb3 = ImageIO.read(new File("images/enemy1_down3.png"));
} 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 < 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);
}
}
}
public void run() {
while (true) {
try {
Thread.sleep(10);
this.repaint();// 刷新
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void mouseDragged(MouseEvent e) {}
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();
}
}
}
}
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;
public Bullet(int x, int y) {
super();
this.x = x;
this.y = y;
}
public void run() {
// TODO Auto-generated method stub
while (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);
}
}
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;
public Bomb(int x, int y) {
this.x = x;
this.y = y;
}
// 减少生命值
public void lifeDown() {
if (life > 0) {
life--;
}
}
}