这一版是利用三个线程同时进行的初级版飞机大战,还有一些小bug,比如会有闪动现象,子弹过多时会有bug现象。
图片需要自己寻找,我的图片只能在我这运行
先看看运行效果
主界面
package Planfight1;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
public class mainer {
private ArrayList<herobull> herobull=new ArrayList();
private ArrayList<enemy> enemylist=new ArrayList();
private ArrayList<enemybullet> enemybull=new ArrayList();
private int x,y;
public void drawer(){
JFrame frame=new JFrame();
JButton bu=new JButton("开始/暂停");
frame.add(bu);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//设置流式布局
frame.setLayout(new FlowLayout());
//界面可见
frame.setSize(1500,800);
frame.setVisible(true);
//创建画笔
Graphics g =frame.getGraphics();
//鼠标监听器
Mouse btnlist=new Mouse(frame,herobull,g,x,y);
frame.addMouseListener(btnlist);
frame.addMouseMotionListener(btnlist);
//创造前进线程
Movebullet move=new Movebullet( herobull,enemylist,enemybull);
move.start();
//创造敌机线程
Addbullet addbul=new Addbullet(enemylist, enemybull, g);
addbul.start();
//创建英雄
hero hero=new hero(x, y, g, btnlist);
//显示图像线程
DrawThread draw=new DrawThread(g,herobull,enemylist,enemybull,hero);
draw.start();
//开始暂停按钮
Btnlis bt =new Btnlis(move);
//为按钮增加动作监听器
bu.addActionListener(bt);
}
public static void main(String[] args){
mainer dra=new mainer();
dra.drawer();
}
}
父类,定义方法
package Planfight1;
public interface Istar {
//父类
public void bulmove();
public void fire();
}
敌机
package Planfight1;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
public class enemy implements Istar{
private Graphics g;
public enemy(int x,int y,int type,Graphics g){
this.x=x;
this.y=y;
this.type=type;
this.g=g;
}
public int x,y;//子弹坐标
public int r=20;
public int type;
ImageIcon img2=new ImageIcon("C:\\Users\\lenovo-M\\Desktop\\plane.png");
Image im2=img2.getImage();
public void bulmove() {
// TODO Auto-generated method stub
this.x=x-1;
}
public void fire() {
// TODO Auto-generated method stub
//显示出飞机
g.drawImage(im2, this.x, this.y, null);
}
}
敌机子弹
package Planfight1;
import java.awt.Color;
import java.awt.Graphics;
public class enemybullet implements Istar{
public int x,y,r=20;
private Graphics g;
public enemybullet(int x,int y,Graphics g){
this.x=x;
this.y=y;
this.g=g;
}
//子弹向前
public void bulmove() {
// TODO Auto-generated method stub
this.x=x-2;
}
public void fire() {
// TODO Auto-generated method stub
}
public void drawbull(){
//画出子弹
g.setColor(Color.black);
g.fillOval(this.x, this.y, this.r, this.r);
}
}
已方飞机
package Planfight1;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
public class hero implements Istar{
private Graphics g;
private Mouse mouse;
public hero(int x,int y,Graphics g,Mouse mouse){
this.x=x;
this.y=y;
this.g=g;
this.mouse=mouse;
}
public int x,y;//子弹坐标
public int type;
public void bulmove() {
// TODO Auto-generated method stub
}
public void fire() {
// TODO Auto-generated method stub
}
public void draw_hero(){
ImageIcon icon =new ImageIcon("C:\\Users\\lenovo-M\\Desktop\\myplane.png");
Image im=icon.getImage();
//将鼠标监听器移动的xy坐标传过来控制已方飞机坐标
x=mouse.x;
y=mouse.y;
g.drawImage(im, x-100, y-100, null);
}
}
已方子弹
package Planfight1;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
public class herobull implements Istar{
private Graphics g;
public herobull(int x,int y,int type,Graphics g){
this.x=x;
this.y=y;
this.type=type;
this.g=g;
}
public int x,y;//子弹坐标
public int r=20;
public int type;
public void bulmove() {
// TODO Auto-generated method stub
this.x=x+3;
}
public void fire() {
// TODO Auto-generated method stub
//画出子弹
g.setColor(Color.red);
g.fillOval(this.x, this.y,this.r,this.r);
}
}
移动飞机线程
package Planfight1;
import java.util.ArrayList;
public class Movebullet extends Thread {
private Boolean boo = true;
private ArrayList<herobull> herobull;
private ArrayList<enemy> enemylist;
private ArrayList<enemybullet>enemybull;
public Movebullet(ArrayList<herobull> herobull, ArrayList<enemy> enemylist,ArrayList<enemybullet>enemybull) {
this.herobull = herobull;
this.enemylist = enemylist;
this.enemybull=enemybull;
}
//建立开关,控制开始与暂停,每次调用一次方法则改变开关状态
public void change() {
boo = !boo;
}
public void run() {
while (true) {
if (boo) {
// 已方子弹前进
for (int i = 0; i < herobull.size(); i++) {
herobull bul = herobull.get(i);
bul.bulmove();
}
// 敌方前进
for (int i = 0; i < enemylist.size(); i++) {
enemy enem = enemylist.get(i);
enem.bulmove();
}
//敌方子弹前进
for(int i=0;i<enemybull.size();i++){
enemybullet enembull=enemybull.get(i);
enembull.bulmove();
}
for (int i = 0; i < enemylist.size(); i++) {
enemy enem = enemylist.get(i);
for (int j = 0; j < herobull.size(); j++) {
herobull her = herobull.get(j);
//检测碰撞消除
if ((Math.abs(her.x -enem.x)<enem.r )&& (Math.abs(her.y - enem.y) < 2*enem.r)) {
herobull.remove(j);
enemylist.remove(i);
}
//如果超过边界则移除多余的
if(her.x>1500)herobull.remove(j);
if(enem.x<0)enemylist.remove(i);
}
}
}
try {
Thread.sleep(10);
} catch (Exception ef) {
}
}
}
}
画飞机线程
package Planfight1;
import java.awt.Color;
import java.awt.Image;
import java.util.Random;
import java.awt.Graphics;
import java.util.ArrayList;
import javax.swing.ImageIcon;
public class DrawThread extends Thread{
private Graphics g;
private hero hero;
private ArrayList<herobull> herobull;
private ArrayList<enemy> enemylist;
private ArrayList<enemybullet>enemybull;
public DrawThread(Graphics g,ArrayList<herobull> herobull,ArrayList<enemy> enemylist,ArrayList<enemybullet>enemybull,hero hero) {
this.g=g;
this.herobull=herobull;
this.enemylist=enemylist;
this.enemybull=enemybull;
this.hero=hero;
}
public void run(){
ImageIcon img=new ImageIcon("C:\\Users\\lenovo-M\\Desktop\\back.jpg");
Image im=img.getImage();
//进入线程后死循环这段代码表示一直画
while(true){
//显示出图像
for(int i=0;i<herobull.size();i++){
herobull bul=herobull.get(i);
/*g.setColor(Color.red);
g.fillOval(bul.x,bul.y,bul.r,bul.r);*/
bul.fire();
}
for(int i=0;i<enemylist.size();i++)
{
enemy enem=enemylist.get(i);
enem.fire();
}
for(int i=0;i<enemybull.size();i++){
enemybullet enembull=enemybull.get(i);
enembull.drawbull();
}
hero.draw_hero();
try{
Thread.sleep(10);
}catch(Exception ef){}
//刷屏背景,每次画完所有的飞机子弹之后用一张图来刷背景
g.drawImage(im, 0, 100, null);
}
}
}
增加敌机和敌机子弹线程
package Planfight1;
import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.Random;
public class Addbullet extends Thread{
private Graphics g;
private ArrayList<enemy> enemylist;
private ArrayList<enemybullet>enemybull;
public Addbullet(ArrayList<enemy> enemylist,ArrayList<enemybullet>enemybull,Graphics g) {
this.enemylist=enemylist;
this.g=g;
this.enemybull=enemybull;
}
public void run(){
while(true){
//每过一段时间增加敌机和子弹
Random y=new Random();
int target=100+y.nextInt(500);
enemy enem=new enemy(1500, target,2,g);
enemylist.add(enem);
enemybullet enembull =new enemybullet(1500-60, target, g);
enemybull.add(enembull);
try{
Thread.sleep(800);
}catch(Exception ef){}
}
}
}
鼠标监听器
package Planfight1;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
public class Mouse implements MouseListener, MouseMotionListener{
public JFrame frame;
private ArrayList<herobull> herobull;
private Graphics g;
int x=300,y=300;
public Mouse(JFrame frame,ArrayList<herobull> herobull,Graphics g,int x,int y){
this.frame=frame;
this.herobull=herobull;
this.g=g;
this.x=x;
this.y=y;
}
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseReleased(MouseEvent e) {
if(MouseEvent.BUTTON1==1){
//按下鼠标左键出现子弹
herobull bul=new herobull(e.getX(),e.getY(),1,g);
this.herobull.add(bul);
}
if(MouseEvent.BUTTON3==e.getButton()){
//按下鼠标右键出现三个子弹
herobull bul1=new herobull(e.getX(),e.getY()+30,1,g);
herobull bul2=new herobull(e.getX(),e.getY()-30,1,g);
herobull bul3=new herobull(e.getX(),e.getY(),1,g);
this.herobull.add(bul1);
this.herobull.add(bul2);
this.herobull.add(bul3);
}
}
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
//获取鼠标位置即传给飞机,使得飞机跟着鼠标移动
x=e.getX();
y=e.getY();
}
}
动作监听器,控制开始与暂停
package Planfight1;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Btnlis implements ActionListener{
private Movebullet move;
public Btnlis(Movebullet move){
this.move=move;
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//每按一次按钮改变一次开关状态
move.change();
}
}