JAVA飞机大战游戏
有十款机型,还要声音
package com.eduask.view;
import com.eduask.model.Enemy001;
import com.eduask.model.Enemy002;
import com.eduask.model.Enemy003;
import com.eduask.model.Enemy004;
import com.eduask.model.Enemy005;
import com.eduask.model.Enemy006;
import com.eduask.model.Enemy007;
import com.eduask.model.Enemy008;
import com.eduask.model.Enemy009;
import com.eduask.model.Enemy010;
// 1:创建一个MainFrame 继承 BaseFrame
public class MainFrame extends BaseFrame {
public MainFrame(){
// 3:创建玩家
createPlayer();
// 4: 监听玩家手势
setTouchListener();
// 5: 设置玩家火力等级 (1,2,3)
setPlayerPowerLevel(1);
// 6: 添加敌人的类型
addEnemyType(Enemy001.class);
addEnemyType(Enemy002.class);
addEnemyType(Enemy003.class);
addEnemyType(Enemy004.class);
addEnemyType(Enemy005.class);
addEnemyType(Enemy006.class);
addEnemyType(Enemy007.class);
addEnemyType(Enemy008.class);
addEnemyType(Enemy009.class);
addEnemyType(Enemy010.class);
// 7:设置敌人携带道具
setEnemyHasItem();
// 8:统计得分
setCount();
}
}
package com.eduask.view;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.LayoutManager;
import java.awt.Toolkit;
import java.util.ArrayList;
import javax.swing.JPanel;
import com.eduask.model.Bullet;
import com.eduask.model.Enemy;
import com.eduask.model.Enemy001;
import com.eduask.model.Enemy002;
import com.eduask.model.Enemy003;
import com.eduask.model.Enemy004;
import com.eduask.model.Enemy005;
import com.eduask.model.Enemy006;
import com.eduask.model.Enemy007;
import com.eduask.model.Enemy008;
import com.eduask.model.Enemy009;
import com.eduask.model.Enemy010;
import com.eduask.model.Item;
import com.eduask.model.Player;
import com.eduask.thread.BulletThread;
import com.eduask.thread.DrawableThread;
public class MyPanel extends JPanel{
private static final long serialVersionUID = 1L;
public DrawableThread thread;
public Player player;
public ArrayList<Bullet> bullets = new ArrayList<Bullet>();
public ArrayList<Enemy> enemies = new ArrayList<Enemy>();
public ArrayList<Item> items = new ArrayList<Item>();
public MyPanel(){
this.bg = Toolkit.getDefaultToolkit().getImage("images/bg01.jpg");
this.player = new Player(this);
this.thread = new DrawableThread(this);
this.thread.start();
}
```
```java
public int timer = 0;
public Image bg;
public int top = 0;
public int enemyFlag;
public void paintComponent(Graphics g){
super.paintComponent(g);
timer++;
if( timer >= 10000 ){
timer = 0;
}
// 画背景
g.drawImage(bg, 0, top-bg.getHeight(this), bg.getWidth(this), bg.getHeight(this), null);
g.drawImage(bg, 0, top, bg.getWidth(this), bg.getHeight(this), null);
if( timer % 10 == 0 )
{
this.top+= 1;
if( top > bg.getHeight(this) ){
top = 0;
}
}
// 画飞机
if(BaseFrame.hasPlayer)
player.drawSelf(g);
// 创建新子弹
if( timer % 100 == 0 ){
if( player.attactMode == 1 ){
// 射出子弹
Bullet b = new Bullet(this);
b.x = player.x + player.width / 2 - b.width/2;
b.y = player.y;
bullets.add(b);
}
else if( player.attactMode == 2 ){
Bullet b1 = new Bullet(this);
b1.x = player.x + player.width / 2 - b1.width/2;
b1.y = player.y - 15;
bullets.add(b1);
Bullet b2 = new Bullet(this);
b2.x = player.x + player.width / 2 - b2.width/2 - b2.width - 5;
b2.y = player.y;
bullets.add(b2);
Bullet b3 = new Bullet(this);
b3.x = player.x + player.width / 2 - b3.width/2 + b3.width + 5;
b3.y = player.y;
bullets.add(b3);
}
else if( player.attactMode == 3 )
{
Bullet b1 = new Bullet(this);
b1.x = player.x + player.width / 2 - b1.width/2;
b1.y = player.y - 30;
bullets.add(b1);
Bullet b2 = new Bullet(this);
b2.x = player.x + player.width / 2 - b2.width/2 - b2.width - 5;
b2.y = player.y - 15;
bullets.add(b2);
Bullet b3 = new Bullet(this);
b3.x = player.x + player.width / 2 - b3.width/2 + b3.width + 5;
b3.y = player.y - 15;
bullets.add(b3);
Bullet b4 = new Bullet(this);
b4.x = player.x + player.width / 2 - b4.width/2 - 2*b4.width - 10;
b4.y = player.y;
bullets.add(b4);
Bullet b5 = new Bullet(this);
b5.x = player.x + player.width / 2 - b5.width/2 + 2*b5.width +10;
b5.y = player.y;
bullets.add(b5);
}
new BulletThread("video/bullet.wav").start();
}
// 画所有子弹
for( int i = 0 ; i <= bullets.size() - 1; i++ ){
bullets.get(i).drawSelf(g);
}
// 创建敌人
if( timer % 200 == 0 ){
// 创建敌人
Enemy enemy;
if( BaseFrame.enemiesType.size() != 0 ){
enemyFlag = (int)(Math.random() * BaseFrame.enemiesType.size());
try{
enemy =(Enemy) BaseFrame.enemiesType.get(enemyFlag).getConstructors()[0].newInstance(new Object[]{this});
enemies.add(enemy);
}catch(Exception e){
e.printStackTrace();
}
}
}
// 画所有敌人
for( int i = 0 ; i <= enemies.size() - 1; i++ ){
enemies.get(i).drawSelf(g);
}
// 画所有道具
if( BaseFrame.hasItem )
for( int i = 0 ; i <= items.size() - 1; i++ ){
items.get(i).drawSelf(g);
}
// 画分数
if( BaseFrame.hasCount ){
g.setColor(Color.WHITE);
g.drawString(""+player.pointer, BaseFrame.FrameWidth - 140, 15);
}
}
}
启动类
package com.eduask.view;
import com.eduask.thread.BulletThread;
import com.eduask.thread.MusicThread;
public class Main {
public static void main(String[] args) {
// 2:主函数 实例化 MainFrame对象
new MainFrame();
new MusicThread("video/bjmusic.wav").start();
}
}
package com.eduask.view;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import java.util.ArrayList;
import javax.swing.JFrame;
import com.eduask.listener.FrameMouseListener;
import com.eduask.listener.FrameMouseMotionListener;
import com.eduask.thread.MusicThread;
public class BaseFrame extends JFrame{
public static boolean hasPlayer = false;
public static boolean hasItem = false;
public static boolean hasCount = false;
public static boolean hasListener = false;
public static ArrayList<Class> enemiesType = new ArrayList<Class>();
public void setTouchListener(){
hasListener = true;
if( hasListener ){
mouselistener = new FrameMouseListener();
mouselistener.mainframe = this;
this.addMouseListener(mouselistener);
mousemotionlistener = new FrameMouseMotionListener();
mousemotionlistener.mainframe = this;
this.addMouseMotionListener(mousemotionlistener);
}
}
public void addEnemyType(Class c){
enemiesType.add(c);
}
public void createPlayer(){
hasPlayer = true;
}
public void setCount(){
hasCount = true;
}
public void setEnemyHasItem(){
hasItem = true;
}
public void setPlayerPowerLevel(int level){
this.panel.player.attactMode = level;
}
private static final long serialVersionUID = 1L;
public static int FrameWidth = 512;
public static int FrameHeight = 768;
public BaseFrame(){
super("学生作品飞机大战");
Dimension screenSize =Toolkit.getDefaultToolkit().getScreenSize();
setBounds( ((int)screenSize.getWidth() - FrameWidth) / 2, 0, FrameWidth, FrameHeight);
setLayout(null);
this.panel = new MyPanel();
this.panel.setBounds(0, 0, this.getWidth(), this.getHeight());
this.add(this.panel);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public MyPanel panel;
public FrameMouseListener mouselistener;
public FrameMouseMotionListener mousemotionlistener;
}
package com.eduask.thread;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;
public class BulletThread extends Thread {
private String fileName;
private AudioFormat format;
private byte[] samples;
public BulletThread(String fileName){
this.fileName = fileName;
reverseMusic();
}
public void reverseMusic(){
try {
// open the audio input stream
AudioInputStream stream =AudioSystem.getAudioInputStream(new File(fileName));
format = stream.getFormat();
// get the audio samples
samples = getSamples(stream);
}
catch (UnsupportedAudioFileException ex) {
ex.printStackTrace();
}
catch (IOException ex) {
ex.printStackTrace();
}
}
public byte[] getSamples(AudioInputStream audioStream) {
int length = (int)(audioStream.getFrameLength() * format.getFrameSize());
byte[] samples = new byte[length];
DataInputStream is = new DataInputStream(audioStream);
try {
is.readFully(samples);
}catch (IOException ex) {
ex.printStackTrace();
}
return samples;
}
public void play(InputStream source) {
int bufferSize = format.getFrameSize() *
Math.round(format.getSampleRate() / 10);
byte[] buffer = new byte[bufferSize];
SourceDataLine line;
try {
DataLine.Info info =
new DataLine.Info(SourceDataLine.class, format);
line = (SourceDataLine)AudioSystem.getLine(info);
line.open(format, bufferSize);
}
catch (LineUnavailableException ex) {
ex.printStackTrace();
return;
}
line.start();
try {
int numBytesRead = 0;
while (numBytesRead != -1) {
numBytesRead =
source.read(buffer, 0, buffer.length);
if (numBytesRead != -1) {
line.write(buffer, 0, numBytesRead);
}
}
}
catch (IOException ex) {
ex.printStackTrace();
}
line.drain();
line.close();
}
public void run(){
InputStream stream =new ByteArrayInputStream(samples);
play(stream);
}
}
package com.eduask.thread;
import com.eduask.view.MyPanel;
public class DrawableThread extends Thread{
public MyPanel mypanel;
public DrawableThread(MyPanel mypanel){
this.mypanel = mypanel;
}
public void run(){
while(true){
mypanel.repaint();
try {
Thread.currentThread().sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
package com.eduask.thread;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;
public class MusicThread extends Thread {
private String fileName;
private AudioFormat format;
private byte[] samples;
private boolean flag;
public MusicThread(String fileName){
this.fileName = fileName;
reverseMusic();
}
public void reverseMusic(){
try {
// open the audio input stream
AudioInputStream stream =AudioSystem.getAudioInputStream(new File(fileName));
format = stream.getFormat();
// get the audio samples
samples = getSamples(stream);
}
catch (UnsupportedAudioFileException ex) {
ex.printStackTrace();
}
catch (IOException ex) {
ex.printStackTrace();
}
}
public byte[] getSamples(AudioInputStream audioStream) {
int length = (int)(audioStream.getFrameLength() * format.getFrameSize());
byte[] samples = new byte[length];
DataInputStream is = new DataInputStream(audioStream);
try {
is.readFully(samples);
}catch (IOException ex) {
ex.printStackTrace();
}
return samples;
}
public void play(InputStream source) {
int bufferSize = format.getFrameSize() *
Math.round(format.getSampleRate() / 10);
byte[] buffer = new byte[bufferSize];
SourceDataLine line;
try {
DataLine.Info info =
new DataLine.Info(SourceDataLine.class, format);
line = (SourceDataLine)AudioSystem.getLine(info);
line.open(format, bufferSize);
}
catch (LineUnavailableException ex) {
ex.printStackTrace();
return;
}
line.start();
try {
int numBytesRead = 0;
while (numBytesRead != -1) {
flag = false;
numBytesRead =
source.read(buffer, 0, buffer.length);
if (numBytesRead != -1) {
line.write(buffer, 0, numBytesRead);
}
}
flag = true;
}
catch (IOException ex) {
ex.printStackTrace();
}
line.drain();
line.close();
}
public void run(){
flag = true;
while(true){
if(flag){
InputStream stream =new ByteArrayInputStream(samples);
play(stream);
}
}
}
}
package com.eduask.listener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import com.eduask.view.BaseFrame;
public class FrameMouseListener implements MouseListener{
public BaseFrame mainframe;
@Override
public void mouseClicked(MouseEvent e) {
mainframe.panel.player.x = e.getX() - mainframe.panel.player.width/2;
mainframe.panel.player.y = e.getY() - mainframe.panel.player.height / 2 - 10;
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stu
}
}
package com.eduask.listener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import com.eduask.view.BaseFrame;
public class FrameMouseMotionListener implements MouseMotionListener {
public BaseFrame mainframe
@Override
public void mouseDragged(MouseEvent e) {
mainframe.panel.player.x = e.getX() - mainframe.panel.player.width/2;
mainframe.panel.player.y = e.getY() - mainframe.panel.player.height / 2 - 10;
}
@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
}
}
package com.eduask.model;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import com.eduask.thread.BulletThread;
import com.eduask.view.BaseFrame;
import com.eduask.view.MyPanel;
public class Bullet {
public int width;
public int height;
public int x;
public int y;
public Image[] images = new Image[]{
Toolkit.getDefaultToolkit().getImage("images/bullet01.png"),
Toolkit.getDefaultToolkit().getImage("images/bullet02.png"),
Toolkit.getDefaultToolkit().getImage("images/bullet03.png"),
Toolkit.getDefaultToolkit().getImage("images/bullet04.png"),
Toolkit.getDefaultToolkit().getImage("images/bullet05.png"),
Toolkit.getDefaultToolkit().getImage("images/bullet06.png")
};
public int imageIndex = 0;
public MyPanel mypanel;
public Bullet(MyPanel mypanel){
this.mypanel = mypanel;
width = 8;
height = 8;
}
public void drawSelf(Graphics g){
g.drawImage(images[imageIndex], x, y, width, height, null);
if( mypanel.timer % 1 == 0 )
{
imageIndex++;
y--;
if( imageIndex >= images.length ){
imageIndex = 0;
}
if(y<0){
mypanel.bullets.remove(this);
}
}
// 判断子弹是否击中敌人
for( int i = 0 ; i <= mypanel.enemies.size() - 1; i++ )
{
Enemy e = mypanel.enemies.get(i);
if( e.hp <= 0 ){continue;}
if( x >= e.x - width && x <= e.x + e.width && y >= e.y - height && y <= e.y + e.height ){
mypanel.bullets.remove(this);
e.underAttact();
}
}
}
}
package com.eduask.model;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import com.eduask.thread.BulletThread;
import com.eduask.view.BaseFrame;
import com.eduask.view.MyPanel;
public abstract class Enemy {
public int width;
public int height;
public int x;
public int y;
public Image image;
public MyPanel mypanel;
public int speed;
public int hp;
public int pointer = 0;
public Image[] images = new Image[]{
Toolkit.getDefaultToolkit().getImage("images/die01.png"),
Toolkit.getDefaultToolkit().getImage("images/die02.png"),
Toolkit.getDefaultToolkit().getImage("images/die03.png"),
Toolkit.getDefaultToolkit().getImage("images/die04.png")
};
public int imageIndex = 0;
public Item[] items = null
public Enemy(MyPanel mypanel){
this.mypanel = mypanel;
}
public void drawSelf(Graphics g){
if( hp > 0 )
{
g.drawImage(image, x, y, width, height, null);
}
else
{
x = x + width/2 - 22;
y = y + height / 2 - 22;
width = 45;
height = 45;
g.drawImage(images[imageIndex], x, y, width, height, null);
if( mypanel.timer % 10 == 0 )
{
imageIndex++;
if( imageIndex >= images.length ){
// 被打死了
killed();
new BulletThread("video/boom.wav").start();
}
}
}
if( mypanel.timer % speed == 0 ){
y++;
if( y > BaseFrame.FrameHeight ){
mypanel.enemies.remove(this);
}
}
}
public void killed(){
mypanel.player.pointer += this.pointer;
if( items != null && items.length > 0 )
{
for( int i = 0 ; i <= items.length - 1; i++ )
{
Item item = items[i];
item.y = this.y;
if( i == 0 ){
item.x = this.x + this.width / 2 - item.width/2;
}else if(i == 1){
item.x = this.x + this.width / 2 - item.width/2 - item.width - 10;
}else if( i == 3){
item.x = this.x + this.width / 2 - item.width/2 - item.width * 2 - 20;
}else if(i == 2){
item.x = this.x + this.width / 2 - item.width/2 + item.width + 10;
} else if( i == 4 ){
item.x = this.x + this.width / 2 - item.width/2+ item.width * 2 + 20;
}
mypanel.items.add(item);
}
}
mypanel.enemies.remove(this);
}
public void underAttact(){
if( hp > 0 ){hp--;
}
}
package com.eduask.mode;
import java.awt.Graphics;
import java.awt.Toolkit
import com.eduask.view.BaseFrame;
import com.eduask.view.MyPanel;
public class Enemy001 extends Enemy
public Enemy001(MyPanel mypanel) {
super(mypanel);
width = 60;
height = 40;
x = (int)(Math.random() * (BaseFrame.FrameWidth - width) );
y = 0 - height;
image = Toolkit.getDefaultToolkit().getImage("images/enemy01.png");
speed = 3;
hp = 3;
pointer = 100;
this.items = new Item[]{
new Item001(mypanel),
new Item001(mypanel),
new Item001(mypanel)
};
}
}
package com.eduask.model;
import java.awt.Graphics;
import java.awt.Image
import com.eduask.view.BaseFrame;
import com.eduask.view.MyPanel
public abstract class Item
public int width;
public int height;
public int x;
public int y;
public Image[] images;
public int imageIndex = 0;
public MyPanel mypanel;
public int pointer;
public int attactMode = 1;
public int powerTime = 0;
public int speed;
public int imageSpeed
public Item(MyPanel mypanel){
this.mypanel = mypanel;
imageSpeed = (int)(Math.random() * 20 + 20);
}
public void drawSelf(Graphics g){
g.drawImage(images[imageIndex], x, y, width, height, null);
if( mypanel.timer % imageSpeed == 0 )
{
imageIndex++;
if( imageIndex >= images.length ){
imageIndex = 0;
}
}
if( mypanel.timer % speed == 0 ){
y++;
if( y > BaseFrame.FrameHeight ){
mypanel.enemies.remove(this);
}
}
}
public void eated()
mypanel.player.pointer += this.pointer;
if( this.attactMode >= mypanel.player.attactMode ){
mypanel.player.attactMode = this.attactMode;
mypanel.player.powerTime = this.powerTime;
}
mypanel.items.remove(this);
System.gc();
}
}
package com.eduask.model;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import com.eduask.view.MyPanel;
public class Item001 extends Item
public Item001(MyPanel mypanel) {
super(mypanel);
// TODO Auto-generated constructor stub
width = 15;
height = 15;
images = new Image[]{
Toolkit.getDefaultToolkit().getImage("images/star01.png"),
Toolkit.getDefaultToolkit().getImage("images/star02.png"),
Toolkit.getDefaultToolkit().getImage("images/star03.png"),
Toolkit.getDefaultToolkit().getImage("images/star04.png"),
Toolkit.getDefaultToolkit().getImage("images/star05.png"),
Toolkit.getDefaultToolkit().getImage("images/star06.png"),
Toolkit.getDefaultToolkit().getImage("images/star07.png"),
Toolkit.getDefaultToolkit().getImage("images/star08.png"),
Toolkit.getDefaultToolkit().getImage("images/star09.png")
};
pointer = 100;
attactMode = 1;
powerTime = 0;
speed =(int)( Math.random() * 3 + 3);
}
}
package com.eduask.model;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import com.eduask.view.BaseFrame;
import com.eduask.view.MyPanel
public class Player {
public int width;
public int height;
public int x;
public int y;
public Image[] images = new Image[]{
Toolkit.getDefaultToolkit().getImage("images/player01.png"),
Toolkit.getDefaultToolkit().getImage("images/player02.png"),
Toolkit.getDefaultToolkit().getImage("images/player04.png"),
Toolkit.getDefaultToolkit().getImage("images/player05.png"),
Toolkit.getDefaultToolkit().getImage("images/player06.png"),
Toolkit.getDefaultToolkit().getImage("images/player08.png"),
Toolkit.getDefaultToolkit().getImage("images/player09.png")
};
public int imageIndex = 0;
public MyPanel mypanel;
public int pointer = 0;
public int attactMode = 0;
public int powerTime = 0;
public Player(MyPanel mypanel){
this.mypanel = mypanel;
width = 100;
height = 100;
x = ( BaseFrame.FrameWidth - width ) / 2;
y = BaseFrame.FrameHeight - height * 2;
}
public void drawSelf(Graphics g){
g.drawImage(images[imageIndex], x, y, width, height, null)
if( mypanel.timer % 50 == 0 )
{
imageIndex++;
if( imageIndex >= images.length ){
imageIndex = 0;
}
}
// 攻击时间
if( this.attactMode > 1 ){
if( this.powerTime > 0 ){
this.powerTime--;
if( this.powerTime <= 0 ){
this.attactMode = 1;
}
}
}
// 判断是否吃到道具
for( int i = 0 ; i <= mypanel.items.size() - 1; i++ )
{
Item item = mypanel.items.get(i);
if( this.x >= item.x - this.width && this.x <= item.x + item.width && this.y >= item.y - this.height && this.y <= item.y + item.height )
{
item.eated();
}
}
}
}