飞机大战(Java)
学习笔记分享,引用请写明出处
Main类
package PlaneWar;
import javax.swing.*;
public class Main extends JPanel
{
static int weight = 600;
static int height = 800;
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setTitle("PlaneWar By Hsing");
frame.setSize(weight,height);
frame.setDefaultCloseOperation(3);
frame.setLocationRelativeTo(null);
//创建并添加面板对象;
GamePanel panel = new GamePanel();
frame.add(panel);
//添加监听器;
frame.addMouseMotionListener(panel);
frame.setVisible(true);
//初始化游戏;
panel.init();
}
}
GamePanel类
package PlaneWar;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.List;
import java.util.*;
public class GamePanel extends JPanel implements MouseMotionListener
{
//创建英雄机画像对象;
ImageIcon heroImageIcon = new ImageIcon("D:/JDK/PlaneWar/Domo/Hero.png");
//创建logo画像对象;
ImageIcon logo = new ImageIcon("D:/JDK/PlaneWar/Domo/logo.png");
//list存在于java.util.List,这里使用了默认包中的泛类;
//创建一个list,用于存放敌人对象;
List<Enemy> enemy = new ArrayList<Enemy>();
//创建一个list,用于存放子弹对象;
List<Butter> butter = new ArrayList<Butter>();
//创建一个list,用来存放爆炸物对象;
List<Bumb> bomb = new ArrayList<Bumb>();
//定义英雄机的初始坐标;
int herox = 300;
int heroy = 400;
//定义一个专用来记分的变量nub;
int nub = 0;
//绘图方,继承于JPanel,类Graphics存在于java.awt.*中;
public void paint(Graphics page)
{
super.paint(page);
//logo进行绘图,通过getImage()方法得到logo对象的图像,置于(400,20)处;
page.drawImage(logo.getImage(), 400, 20, null);
//在page中创建一个标签对象,蓝色,30号大小,字体使用默认字体;
page.setFont(new Font("",Color.blue.getBlue( ),30));
//在标签上写上内容,置于(10,30)的位置;
page.drawString("当前得分:"+nub, 10, 30);
//绘制英雄机,调用getImage()方法得到英雄机图像,绘制在(herox,heroy)坐标上;
page.drawImage(heroImageIcon.getImage(), herox, heroy, null);
//遍历敌人list,在list中得到对应的敌人对象;
for(int i=0;i<enemy.size();i++)
{
Enemy enemyone = enemy.get(i);
//调用敌人类的drawImage()方法,在page上绘制敌人;
enemyone.drawImage(page);
}
//遍历子弹list,在list中得到子弹对象;
for(int i=0;i<butter.size();i++)
{
Butter butterone = butter.get(i);
//调用子弹类中drawlist()方法,在page上绘制敌人;
butterone.drawImage(page);
}
//遍历爆炸list,在list中得到爆炸对象;
for(int i=0;i<bomb.size();i++)
{
Bumb bombone = bomb.get(i);
//调用爆炸类中的drawImage()方法,在page上绘制敌人;
bombone.drawImage(page);
}
}
//构建关于英雄机的监听器方法,监听鼠标事件;
public void mouseMoved(MouseEvent event)
{
//得到鼠标所在的坐标;
int x = event.getX();
int y = event.getY();
//将英雄机的坐标更改为鼠标所在位置并调整,使鼠标永远在英雄机中心;
herox = x - (heroImageIcon.getIconWidth()/2);
heroy = y - (heroImageIcon.getIconHeight()/2);
//重画英雄机;
repaint();
}
public void mouseDragged(MouseEvent event){ }//其他鼠标事件;
//游戏的主要部分(构造方法)
public GamePanel()
{
//在敌人list中加入10个新的敌人对象;
for(int i=0;i<10;i++)
{
enemy.add(new Enemy());
}
}
//构建初始化方法
public void init()
{
int flag=0;
//无限循环
for(;;)
{
flag++;
//每循环十五次;
if(flag%15==0)
{
//创建一个新的子弹对象;就在英雄机所在的位之上,并调整使在英雄机中间
Butter butterone = new Butter(herox+(heroImageIcon.getIconWidth()/2), heroy);
//将这个对象添加进子弹list;
butter.add(butterone);
}
//实现敌人移动;
for(int i=0;i<enemy.size();i++)
{
//从敌人list中的得到一个敌人对象;
Enemy enemyone = enemy.get(i);
//调用敌人类move()方法实现移动;
enemyone.move();
//当出现超过游戏界面时
if(enemyone.getY()>Main.height)
{
//敌人list删除这个对象
enemy.remove(enemyone);
//删除后在list中添加一个新的敌人对象
enemy.add(new Enemy());
}
}
//实现子弹移动;
for(int i=0;i<butter.size();i++)
{
//从子弹list中得到一个子弹对象;
Butter tempbutter = butter.get(i);
//调用子弹类move()方法实现子弹移动;
tempbutter.move();
//当子弹发射过界时,从子弹list中删除这个对象;
if(tempbutter.getY()<0)
{
butter.remove(tempbutter);
}
}
for(int i=0;i<enemy.size();i++)
{
//从敌人list中得到一个敌人对象;
Enemy enemyone = enemy.get(i);
for(int j=0;j<butter.size();j++)
{
//从子弹list中得到一个子弹对象;
Butter onebutter = butter.get(j);
//调用isHit()方法判断,如果出现子弹与敌人发生碰撞;
if(isHit(enemyone,onebutter))
{
//删除这个子弹;
butter.remove(onebutter);
//创建一个爆炸对象,并添加到爆炸list中去,将爆炸定位在碰撞后的敌机位置;
bomb.add(new Bumb(enemyone.getX(), enemyone.getY()));
//得分+10;
nub+=10;
//删除这个被碰撞的敌人;
enemy.remove(enemyone);
//在增加一个新的敌人对象;
enemy.add(new Enemy());
}
}
}
//删除爆炸
for(int i=0;i<bomb.size();i++)
{
//得到爆炸list中一个爆炸对象;
Bumb bombone = bomb.get(i);
//调用爆炸类中的move()方法计数
bombone.move();
//十次循环后删除这个对象;
if(bombone.getCount()>10)
{
bomb.remove(bombone);
}
}
//睡眠5s后,将重绘界面;
try
{
Thread.sleep(5);
}
catch(InterruptedException event)
{
event.printStackTrace();
}
repaint();
}
}
//创建判断碰撞的方法;
public boolean isHit(Enemy e, Butter b)
{
//子弹与敌人机的图片发生重合,即为击中敌人;
Rectangle rect = new Rectangle(e.getX(),e.getY(),b.getWidth(),b.getHight());
Point p = new Point(b.getX()+b.getWidth()/2,b.getY()+b.getHight());
return rect.contains(p);//包含方法;
}
}
Hero类
package PlaneWar;
import javax.swing.*;
public class Hero
{
private int width;
private int hight;
private int x;
private int y;
private ImageIcon heroImageIcon = new ImageIcon("D:/JDK/PlaneWar/Domo/Hero.png");
public Hero()
{
this.width = heroImageIcon.getIconWidth();
this.hight = heroImageIcon.getIconHeight();
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
public int getHight()
{
return hight;
}
public int getWidth()
{
return width;
}
}
敌人类
package PlaneWar;
import java.util.Random;
import java.awt.*;
import javax.swing.*;
public class Enemy
{
private int width;
private int hight;
private int x;
private int y;
private ImageIcon enemyIcon = new ImageIcon("D:/JDK/PlaneWar/Domo/enemy.png");
public Enemy()
{
width = enemyIcon.getIconWidth();
hight = enemyIcon.getIconHeight();
//设置敌机位置,在界面范位内随机;
Random random = new Random();
this.x = random.nextInt(Main.weight-(width/2));
this.y = -random.nextInt(Main.height-(hight/2));
}
//设置敌机移动方法,每一次向下增加4个单位长度;
public void move()
{
this.y += 4;
}
//敌人绘制方法
public void drawImage(Graphics page)
{
//在page画布上,(x,y)坐标上绘出敌人的画像;
page.drawImage(enemyIcon.getImage(), x, y, null);
}
public int getY()
{
return y;
}
public int getX()
{
return x;
}
}
子弹类
package PlaneWar;
import javax.swing.ImageIcon;
import java.awt.*;
public class Butter
{
private int x;
private int y;
private int hight;
private int width;
private ImageIcon butterIcon = new ImageIcon("D:/JDK/PlaneWar/Domo/buttle.png");
public Butter(int x,int y)
{
this.x = x;
this.y = y;
this.hight = butterIcon.getIconHeight();
this.width = butterIcon.getIconWidth();
}
//子弹的移动方法,每次向上移动四个单位长度;
public void move()
{
this.y -= 4;
}
public void drawImage(Graphics page)
{
page.drawImage(butterIcon.getImage(), x, y, null);
}
public int getY()
{
return y;
}
public int getX()
{
return x;
}
public int getHight()
{
int temp = hight;
return temp;
}
public int getWidth()
{
int temp = width;
return temp;
}
}
爆炸类
package PlaneWar;
import javax.swing.*;
import java.awt.*;
public class Bumb
{
private int x;
private int y;
private int width;
private int height;
private int count;
private ImageIcon bombIcon = new ImageIcon("D:/JDK/PlaneWar/Domo/Boon.png");
//将爆炸坐标定为碰撞后的敌机;
public Bumb(int x,int y)
{
this.x = x;
this.y = y;
this.width = bombIcon.getIconWidth();
this.height = bombIcon.getIconHeight();
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
public void drawImage(Graphics page)
{
page.drawImage(bombIcon.getImage(), x, y, null);
}
//计数方法;
public void move()
{
count++;
}
public int getCount()
{
return count;
}
public int getWidth()
{
return width;
}
public int getHight()
{
return height;
}
}
有些方法根本没用,是我自己强迫症忍不住写的
还没有评论,来说两句吧...