飞机大战第一版

前言

文章主要讲述的是:飞机大战这款游戏使用java语言究竟该如何编写,此版本为飞机大战第一版,做为第一版所以会给大家基础代码方便大家理清整体思路。

Bee类

package PlaneGame.plane;

public class Bee extends Father{
    private int speed;
    private int blood;

    public Bee() {
        this.blood = 1;
        this.speed = 3;
        img = Test.beepicture;
        x = 10;
        y = 120;
        width = img.getWidth();
        height = img.getHeight();
    }

    @Override
    public void move() {

    }
}

BigPlane类

package PlaneGame.plane;

public class BigPlane extends Father{
    private int speed;
    private int blood;
    private int score;

    public BigPlane() {
        this.blood = 2;
        this.score = 5;
        this.speed = 1;
       img = Test.bigpicture;
       x = 10;
       y = 20;
       height = img.getHeight();
       width = img.getWidth();
    }

    @Override
    public void move() {

    }
}

Bullet类

package PlaneGame.plane;

public class Bullet extends Father{
    int speed;

    public Bullet() {
        this.speed = 5;
        img = Test.bulletpicture;
        x = 100;
        y = 100;
        width = img.getWidth();
        height = img.getHeight();
    }

    @Override
    public void move() {

    }
}

##Father类:所有类的父类

package PlaneGame.plane;

import java.awt.image.BufferedImage;

public abstract class Father {
    protected int x;
    protected int y;
    protected int width;
    protected int height;
    protected BufferedImage img;
    public Father(){

    }
    public BufferedImage getImg() {
        return img;
    }

    public void setImg(BufferedImage img) {
        this.img = img;
    }
    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 int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public abstract void move();
}

Hero类

package PlaneGame.plane;

import java.time.Year;

public class Hero extends Father{
    private int score;
    private int blood;
    private Bullet bullet;

    public Hero() {
        this.blood = 3;
        this.score = 0;
        //img = Test.hero;
        img = Test.heropicture;
        x = 10;
        y = 220;
        width = img.getWidth();
        height = img.getHeight();
    }
    @Override
    public void move() {

    }
}

SmallPlane类

package PlaneGame.plane;

public class SmallPlane extends Father{
    int speed;
    int blood;

    public SmallPlane() {
        this.speed = 2;
        this.blood = 1;
        img = Test.smallpicture;
        x = 40;
        y = 350;
        width = img.getWidth();
        height = img.getHeight();
    }

    @Override
    public void move() {
    }
}

Test:测试类

package PlaneGame.plane;
import PlaneGame.JPanelImpl;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
public class Test extends JPanel{
    public static BufferedImage bg;
    public static BufferedImage bigpicture;
    public static BufferedImage beepicture;
    public static BufferedImage heropicture;
    public static BufferedImage smallpicture;
    public static BufferedImage bulletpicture;
    static {
        try {
            bg = ImageIO.read(Test.class.getResourceAsStream("image/background.png"));
            bigpicture = ImageIO.read(Test.class.getResourceAsStream("image/bigplane.png"));
            beepicture = ImageIO.read(Test.class.getResourceAsStream("image/bee.png"));
            heropicture = ImageIO.read(Test.class.getResourceAsStream("image/hero1.png"));
            smallpicture = ImageIO.read(Test.class.getResourceAsStream("image/airplane.png"));
            bulletpicture = ImageIO.read(Test.class.getResourceAsStream("image/bullet.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    BigPlane bigpalne = new BigPlane();
    Bee bee = new Bee();
    Hero hero = new Hero();
    SmallPlane smallplane = new SmallPlane();
    Bullet bullet = new Bullet();
    public void paint(Graphics g) {
        super.paint(g);
        g.drawImage(bg,0,0,this);
        g.drawImage(bigpalne.getImg(),bigpalne.getX(),bigpalne.getY(),this);
        g.drawImage(bee.getImg(),bee.getX(),bee.getY(),this);
        g.drawImage(hero.getImg(),hero.getX(),hero.getY(),this);
        g.drawImage(smallplane.getImg(),smallplane.getX(),smallplane.getY(),this);
        g.drawImage(bullet.getImg(),bullet.getX(),bullet.getY(),this);
    }
    public static void main(String[] args) {
        JFrame jFrame = new JFrame("飞机大战");
        Test test = new Test();
        jFrame.add(test);
        jFrame.setSize(400,600);
        jFrame.setLocationRelativeTo(jFrame.getOwner());
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.setVisible(true);
    }
}

注意要点:

最后需要说明的是,测试程序里的图片需要更换,且第一版只是把逻辑整理清楚,并把主要几个物体的图片放在窗口前而已。后续会慢慢把主要功能都进行完善,如战机跟随鼠标进行移动;敌机进行移动等等…

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值