指针JAVA培训day_10/day_11/day_12
飞机大战的需求:
? 1.游戏可以产生: 小敌机/大敌机/小蜜蜂 -> 随机(概率)产生 ? 2.跟鼠标相关的操作: ? a.自己的飞机移动 ? b.点击开始游戏 ? c.鼠标移出窗口,游戏暂停 ? d.鼠标进入窗口,游戏继续 ? 3.子弹打中小敌机: 分数 + 5 ? 4.子弹打中大敌机: 分数 + 50,奖励(双倍火力,生命值加成) - 接口 ? 5.子弹打中小蜜蜂: 奖励(双倍火力,生命值加成)
API(application programming Interface): 应用程序接口 帮助文档
Scanner System -> java.lang Arrays -> java.utils Math -> java.lang 构造方法 方法
1.java.awt javax.swing - 窗口化界面
? Frame -> JFrame : 窗口类
DO_NOTHING_ON_CLOSE:关闭时什么都不做
HIDE_ON_CLOSE:关闭时隐藏窗口(默认效果)
DISPOSE_ON_CLOSE:关闭时注销窗口
EXIT_ON_CLOSE:关闭时退出程序
什么时候使用静态常量:
如果是只判断两种情况: boolean
如果是一开始就固定好几种状态: final
一般会使用int类型修饰,因为浮点型不能判断两个值是否相等
2.画板类:
? Panel -> JPanel -> 解决闪屏问题
原生的 JPanel 是不能自定义绘画的
只能画 按钮,输入框...等的一些组件
功能增强 -> 自定义类继承JPanel
因为子类更能一定比父类更强大
3.加载图片
? 1)图片是存在磁盘中,要将此文件加载到JVM内存中 ? I0 -> 输入/输出流
new File("相对路径") - 工程下
文件必须存在:和类同包下
ImageDemo.class -> 获取类的字节码文件对象
ImageDemo.class.getResource()
ImageDemo.class.getResourceAsStream() - 获取资源文件流
文件必须存在src下:
ImageDemo.class.getClassLoader()
4.程序设计 - 类的设计
? 对象: 小飞机/大飞机/小蜜蜂/英雄机/子弹
父类(基类):抽象类
成员变量:
int blood int x int y 图片
图片宽/ 高
成员方法:
move() -> 抽象方法 -> 中文
对象对应的类:
子类 extends 父类:
小飞机:
成员变量:
int blood int score int x int y 图片
图片宽/ 高 int speed
成员方法:
move()
大飞机
成员变量:
int blood int score int x int y 图片
图片宽/ 高 int speed
成员方法:
move()
小蜜蜂
成员变量:
int blood int x int y 图片
图片宽/ 高 int speed
成员方法:
move()
英雄机
成员变量:
int blood int x int y 图片
图片宽/ 高 子弹
成员方法:
move()
子弹
成员变量:
int blood int x int y 图片
图片宽/ 高 int speed
成员方法:
move()
构造方法:
5.存很多数据 -> 数组:
? 1.数组长度不可变的,如果想给数组扩容,就必须使用到数组的复制 ? 方法的复制: ? 1).最原始的方式 ? 2).Arrays.copyOf() ? 3).System.arrayCopy()
2.数组只能存放同种类型的数据
基本数据类型:
int[] = {1,2,3,4}
引用数据类型
Student[] = new Student[5];
ArrayList: 可变长数组 1.只能存放引用数据类型数组 int -> Integer (包装类) char -> Character byte -> Byte long -> Long
2.可以存所有类型,但是建议存同一种类型,因为有可能出现类型转换异常
泛型:<E> -> E表示Object,所有类型
3.构造方法:
空参构造方法: 没有内容的集合
ArrayList list = new ArrayList();
常用方法API:
void add(Object obj)
void add(int index, Object obj)
Object get(int index)
Obejct remove(int index)
具体步骤:
1.创建窗口对象 2.创建画板对象 3.类的设计 - 确定类中目前需要的成员变量和成员方法 4.成员方法目前只有一个 - move(), 研究不同类的移动方式 5.创建类需要不同的构造方法 - 初始化成员变量 a.加载(读取)图片 -> static{} -> 静态代码块,需要静态成员变量 b.获取父类中的成员变量 1.如果父类中的成员变量是protected修饰的 那么子类可以直接获取 2.如果父类中的成员变量是private修饰的 通过调用父类的有参构造方法super()
6.主程序: 添加定时器 a.生成所有飞行物 b.所有飞行物的移动 c.生成很多子弹 d.所有子弹的移动 e.判断所有飞行物 和子弹的越界 f.判断子弹和飞行物的碰撞 g.判断飞行物和英雄机的碰撞 h.英雄的移动 - 鼠标监听 MouseListener - 接口 MouseMotionListener - 接口 鼠标适配器 -> new MouseAdapter i.判断游戏是否结束 -> blood == 0
7.添加鼠标监听 - 接口
8.游戏状态的判断 -> 结合鼠标适配器完成 //静态常量 -> 全大写
开始状态 -> START
运行状态 -> RUNNING
暂停状态 -> PAUSE
死亡状态 -> GAME_OVER
//当前状态 - 会改变 -> 变量
int start = START;
START -> 单击 -> RUNNING
RUNNING -> 移出 -> PAUSE
getBlood == 0 -> GAME_OVER
PAUSE -> 进入 -> RUNNING
GAME_OVER -> 单击START
如果只要两个状态 -> Boolean
使用int类型存储 -> 判断值是否相等
9.碰撞 判断子弹和飞行物的碰撞 小蜜蜂/大敌机/小敌机 -> 向上造型fyings 判断飞行物和英雄机的碰撞
10.添加奖励 [双倍火力/ 生命值加成]
11.判断游戏结束 -> hero.blood == 0
12.灰烬 -> 和父类是不同的类
Window类:实现主方法
package planewar;
import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.Timer;
import java.util.TimerTask;
public class Window {
public static void main(String[] args) {
JFrame win = new JFrame();
win.setSize(400,600);
win.setLocationRelativeTo(null);
win.setTitle("飞机大战");
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Draw draw = new Draw();
win.add(draw);
draw.action();
win.setVisible(true);
}
}
Fly类:所有飞行物的父类
package planewar;
import java.awt.image.BufferedImage;
public abstract class Fly {
protected int blood;
protected int x;
protected int y;
protected BufferedImage image;
protected int imageWidth;
protected int imageHeight;
protected boolean remove;
Fly(){
}
Fly(int x,int y, BufferedImage image,int blood){
this.x=x;
this.y=y;
this.image=image;
this.blood=blood;
}
abstract void move();
public int getBlood() {
return blood;
}
public void setBlood(int blood) {
this.blood = blood;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public BufferedImage getImage() {
return image;
}
public void setImage(BufferedImage image) {
this.image = image;
}
public int getImageWidth() {
return imageWidth;
}
public void setImageWidth(int imageWidth) {
this.imageWidth = imageWidth;
}
public int getImageHeight() {
return imageHeight;
}
public void setImageHeight(int imageHeight) {
this.imageHeight = imageHeight;
}
public boolean isRemove() {
return remove;
}
public void setRemove(boolean remove) {
this.remove = remove;
}
}
ImageFiler类:图片素材类
package planewar;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
public class ImageFiler {
public static BufferedImage heroplane;
public static BufferedImage heroplane1;
public static BufferedImage littleplane;
public static BufferedImage littleplane1;
public static BufferedImage littleplane2;
public static BufferedImage littleplane3;
public static BufferedImage littleplane4;
public static BufferedImage bigplane;
public static BufferedImage bigplane1;
public static BufferedImage bigplane2;
public static BufferedImage bigplane3;
public static BufferedImage bigplane4;
public static BufferedImage bee;
public static BufferedImage bee1;
public static BufferedImage bee2;
public static BufferedImage bee3;
public static BufferedImage bee4;
public static BufferedImage bullet;
public static BufferedImage start;
public static BufferedImage pause;
public static BufferedImage gameover;
static {
try {
heroplane = ImageIO.read(ImageFiler.class.getResourceAsStream("image/hero0.png"));
heroplane1 = ImageIO.read(ImageFiler.class.getResourceAsStream("image/hero1.png"));
littleplane = ImageIO.read(ImageFiler.class.getResourceAsStream("image/airplane.png"));
littleplane1 = ImageIO.read(ImageFiler.class.getResourceAsStream("image/airplane_ember0.png"));
littleplane2 = ImageIO.read(ImageFiler.class.getResourceAsStream("image/airplane_ember1.png"));
littleplane3 = ImageIO.read(ImageFiler.class.getResourceAsStream("image/airplane_ember2.png"));
littleplane4 = ImageIO.read(ImageFiler.class.getResourceAsStream("image/airplane_ember3.png"));
bigplane = ImageIO.read(ImageFiler.class.getResourceAsStream("image/bigplane.png"));
bigplane1 = ImageIO.read(ImageFiler.class.getResourceAsStream("image/bigplane_ember0.png"));
bigplane2 = ImageIO.read(ImageFiler.class.getResourceAsStream("image/bigplane_ember1.png"));
bigplane3 = ImageIO.read(ImageFiler.class.getResourceAsStream("image/bigplane_ember2.png"));
bigplane4 = ImageIO.read(ImageFiler.class.getResourceAsStream("image/bigplane_ember3.png"));
bee = ImageIO.read(ImageFiler.class.getResourceAsStream("image/bee.png"));
bee1 = ImageIO.read(ImageFiler.class.getResourceAsStream("image/bee_ember0.png"));
bee2 = ImageIO.read(ImageFiler.class.getResourceAsStream("image/bee_ember1.png"));
bee3 = ImageIO.read(ImageFiler.class.getResourceAsStream("image/bee_ember2.png"));
bee4 = ImageIO.read(ImageFiler.class.getResourceAsStream("image/bee_ember3.png"));
bullet = ImageIO.read(ImageFiler.class.getResourceAsStream("image/bullet.png"));
start = ImageIO.read(ImageFiler.class.getResourceAsStream("image/start.png"));
pause = ImageIO.read(ImageFiler.class.getResourceAsStream("image/pause.png"));
gameover = ImageIO.read(ImageFiler.class.getResourceAsStream("image/gameover.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
Draw类:画板类
package planewar;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.TimerTask;
public class Draw extends JPanel {
public static final int START = 0;
public static final int RUNNING = 1;
public static final int PAUSE = 2;
public static final int GAME_OVER = 3;
int start = START;
HeroPlane hero= new HeroPlane();
ArrayList<Fly> flyings = new ArrayList();
ArrayList<Bullet> bullets = new ArrayList();
java.util.Timer timer =new java.util.Timer();
public void action(){
timer.schedule(new TimerTask() {
@Override
public void run() {
if(start==RUNNING){
hero.move();
createArrayFly();
createbullet();
flymove();
bulletmove();
outOfBoundAction();
outOfBullet();
fight();
crash();
}
repaint();
}
},100,20);
MouseAdapter adapter = new MouseAdapter(){
@Override
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
if(start==START){
start=RUNNING;
flyings = new ArrayList<Fly>();
bullets = new ArrayList<Bullet>();
hero.blood=3;
}
if(start==GAME_OVER){
start=START;
}
}
@Override
public void mouseEntered(MouseEvent e) {
super.mouseEntered(e);
if(start==PAUSE){
start=RUNNING;
}
}
@Override
public void mouseExited(MouseEvent e) {
super.mouseExited(e);
if(start==RUNNING){
start=PAUSE;
}
}
@Override
public void mouseMoved(MouseEvent e) {
super.mouseMoved(e);
if(start==RUNNING){
hero.setX(e.getX()-hero.getImageWidth()/2);
hero.setY(e.getY()-hero.getImageHeight()/2);
repaint();
}
}
};
this.addMouseListener(adapter);
this.addMouseMotionListener(adapter);
}
private void crash(){
for (int i = 0; i < flyings.size(); i++) {
Fly fly = flyings.get(i);
if(hero.getX()< (fly.getX()+fly.getImageWidth()) && hero.getX()+hero.getImageWidth()> fly.getX() &&
hero.getY()+hero.getImageHeight() > fly.getY() && hero.getY()< (fly.getY()+fly.getImageHeight())&&fly.blood>0){
fly.blood -= 1;
hero.blood -= 1;
if(hero.blood<=0){
start=GAME_OVER;
}
}
}
}
private void fight() {
for (int i = 0; i < flyings.size(); i++) {
Fly fly = flyings.get(i);
for (int j = 0; j < bullets.size(); j++) {
Bullet bullet = bullets.get(j);
if(bullet.getX()< (fly.getX()+fly.getImageWidth()) && bullet.getX()+bullet.getImageWidth()> fly.getX() &&
bullet.getY()+bullet.getImageHeight() > fly.getY() && bullet.getY()< (fly.getY()+fly.getImageHeight())&&fly.blood>0 ){
fly.blood -= 1;
bullet.blood -= 1;
if(bullet.blood==0){
bullets.remove(bullet);
}
if(fly.getBlood()==0){
if(fly instanceof Enemy){
Enemy enemy =(Enemy)fly;
hero.setScore(hero.getScore()+enemy.getScore());
}
if(fly instanceof Award) {
Award add = (Bee) fly;
int n = (int) (Math.random() * 10);
if(n%3==0){
hero.setBlood(hero.getBlood()+add.addblood());
}
}
if(fly instanceof BigPlane){
BigPlane bigPlane =(BigPlane) fly;
if(bigPlane.getBlood()==0){
int n = (int) (Math.random()*10);
if(n%3==0){
hero.doubleFire();
}
}
}
}
}
}
}
}
private void outOfBullet() {
for (int i = 0; i < bullets.size(); i++) {
Bullet bullet = bullets.get(i);
if(bullet.getY()< 0){
bullets.remove(bullet);
}
}
}
private void outOfBoundAction() {
for (int i = 0; i < flyings.size(); i++) {
Fly fly = flyings.get(i);
if(fly.getY() > 600){
flyings.remove(fly);
hero.setScore(hero.getScore()-2);
}
}
}
private void bulletmove() {
for (int i = 0; i < bullets.size(); i++) {
Bullet bullet = bullets.get(i);
bullet.move();
}
}
private void flymove() {
for (int i = 0; i < flyings.size(); i++) {
Fly fly = flyings.get(i);
fly.move();
if(fly.remove){
flyings.remove(fly);
}
}
}
int i=0;
private void createbullet() {
i++;
Bullet bullet;
if(i%10==0){
Bullet [] bs = hero.shoot();
for(int i = 0 ; i < bs.length ; i++){
bullets.add(bs[i]);
}
}
}
int flyingIndex=0;
private void createArrayFly() {
flyingIndex++;
if(flyingIndex%10 == 0){
int ran = (int) (Math.random()*100);
Fly fly;
if(ran<=10){
fly = new Bee();
}else if(ran>10&&ran<=30){
fly = new BigPlane();
}else{
fly = new LittlePlane();
}
flyings.add(fly);
}
}
@Override
public void paint(Graphics g) {
super.paint(g);
BufferedImage bg = null;
try {
bg= ImageIO.read(Draw.class.getResourceAsStream("image/background.png"));
} catch (IOException e) {
e.printStackTrace();
}
g.drawImage(bg,0,0,null);
g.drawImage(hero.getImage(),hero.getX(),hero.getY(),null);
if(start==START){
g.drawImage(ImageFiler.start,0,0,null);
}else if(start==RUNNING){
paintflyings(g);
paintBullet(g);
if(hero.blood==0){
start=GAME_OVER;
}
}else if(start==PAUSE){
paintflyings(g);
paintBullet(g);
g.drawImage(ImageFiler.pause,0,0,null);
}else if(start==GAME_OVER){
g.drawImage(ImageFiler.gameover,0,0,null);
}
Font font = new Font("微软雅黑",Font.BOLD,20);
g.setFont(font);
Color color = new Color(0,0,0);
g.setColor(color);
g.drawString("Score:"+hero.score,0,30);
g.drawString("Life:"+hero.blood,0,50);
}
private void paintBullet(Graphics g) {
for (int i = 0; i < bullets.size(); i++) {
Bullet bullet = bullets.get(i);
g.drawImage(bullet.getImage(),bullet.getX(),bullet.getY(),null);
}
}
private void paintflyings(Graphics g) {
for (int i = 0; i < flyings.size(); i++) {
Fly fly = flyings.get(i);
g.drawImage(fly.getImage(),fly.getX(),fly.getY(),null);
}
}
}
HeroPlane类:英雄机类
package planewar;
public class HeroPlane extends Fly implements Enemy , Award {
int score = 0;
Bullet[] bs;
HeroPlane(){
x=150;
y=400;
image=ImageFiler.heroplane;
imageWidth=image.getWidth();
imageHeight=image.getHeight();
blood=3;
}
int count = 0;
@Override
void move() {
count++;
if(count%5==0){
setImage(ImageFiler.heroplane1);
}else{
setImage(ImageFiler.heroplane);
}
}
int doublefire = 0;
public void doubleFire(){
doublefire += 15;
}
public Bullet[] shoot(){
Bullet[] bs = new Bullet[0];
if(doublefire == 0){
bs = new Bullet[1];
bs[0]= new Bullet(this.getX(), this.getY());
}else{
bs = new Bullet[2];
bs[0]= new Bullet(this.getX()-getImageWidth()/4, this.getY());
bs[1]= new Bullet(this.getX()+getImageWidth()/4, this.getY());
doublefire--;
}
return bs;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
@Override
public int addblood() {
return blood;
}
}
Bullet类:子弹类
package planewar;
public class Bullet extends Fly{
int speed;
Bullet(int x , int y){
blood=1;
image=ImageFiler.bullet;
imageWidth=image.getWidth();
imageHeight=image.getHeight();
this.x=x+46;
this.y=y-imageHeight;
speed=3;
}
@Override
void move() {
y -=speed;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
}
LittlePlane类:小敌机类
package planewar;
import java.util.ArrayList;
public class LittlePlane extends Fly implements Enemy{
int score = 1;
int speed;
int count=0;
LittlePlane(){
image = ImageFiler.littleplane;
imageWidth = image.getWidth();
imageHeight = image.getHeight();
x= (int) (Math.random()*400);
y=-imageHeight;
blood=1;
speed=3;
}
@Override
void move() {
if(blood!=0){
y +=speed;
}else{
if(count==0){
setImage(ImageFiler.littleplane1);
}else if(count==4){
setImage(ImageFiler.littleplane2);
}else if(count==8){
setImage(ImageFiler.littleplane3);
}else if(count==12){
setImage(ImageFiler.littleplane4);
}else if(count == 16){
remove = true;
}
count++;
}
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
}
BigPlane类:大敌机类
package planewar;
public class BigPlane extends Fly implements Enemy{
int score =5;
int speed;
int count =0;
BigPlane(){
blood=5;
image = ImageFiler.bigplane;
imageWidth=image.getWidth();
imageHeight=image.getHeight();
x= (int) (Math.random()*400);
y=-imageHeight;
speed=1;
}
@Override
void move() {
if(blood!=0){
y +=speed;
}else{
if(count==0){
setImage(ImageFiler.bigplane1);
}else if(count==4){
setImage(ImageFiler.bigplane2);
}else if(count==8){
setImage(ImageFiler.bigplane3);
}else if(count==12){
setImage(ImageFiler.bigplane4);
}else if(count==16){
remove=true;
}
count++;
}
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
}
Bee类:小蜜蜂类
package planewar;
public class Bee extends Fly implements Award {
int speed;
boolean aBoolean=true;
int count=0;
Bee(){
blood=1;
image=ImageFiler.bee;
imageWidth=image.getWidth();
imageHeight=image.getHeight();
x= (int) (Math.random()*400);
y=-imageHeight;
speed=2;
}
@Override
void move() {
if(blood!=0){
if(aBoolean){
x +=speed*2;
y +=speed;
}else {
x -=speed*2;
y +=speed;
}
if(x>400-imageWidth){
x -=speed*2;
aBoolean=false;
}else if(x<0){
x +=speed*2;
aBoolean=true;
}
}else{
if(count==0){
setImage(ImageFiler.bee1);
}else if(count==4){
setImage(ImageFiler.bee2);
}else if(count==8){
setImage(ImageFiler.bee3);
}else if(count==12){
setImage(ImageFiler.bee4);
}else if(count == 16){
remove=true;
}
count++;
}
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
@Override
public int addblood() {
return 1;
}
}
Enemy接口:敌机得分接口
package planewar;
public interface Enemy {
int getScore();
}
Award接口:加血奖励接口
package planewar;
public interface Award {
int addblood();
}
运行结果: 在这三天边学边做的过程中收获非常多,理解了很多知识点,学会了自己查找问题。 全部代码都在上面,图片素材不知道怎么上传,需要的可以随时找我!
|