Java多线程实现简单雷霆战机(只有一关,没有击落奖励)

import java.awt.*;
import java.awt.image.BufferedImage;

public abstract class FlyObject {
    protected int x;
    protected int y;
    protected int height;
    protected int width;
    protected int speed;
    protected BufferedImage image;
    public void draw(Graphics g){
        g.drawImage(image,x,y,null);
    }
    public abstract void move();
    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 getHeight() {
        return height;
    }

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

    public int getWidth() {
        return width;
    }

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

    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }

    public BufferedImage getImage() {
        return image;
    }

    public void setImage(BufferedImage image) {
        this.image = image;
    }
}
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;

public class Airplane extends FlyObject implements Enemy,Award{
    private int speed = 10;
    private int x;
    private int y;
    private int height;
    private int width;
    private boolean alive;
    private BufferedImage image;
    private static Random random = new Random(47);
    public Airplane(BufferedImage image,int speed){
        this(image,random.nextInt(FlyFrame.WIDTH),0,50,50,speed);
    }
    public Airplane(BufferedImage image,int x,int y,int height,int width,int speed){
        this.image = image;
        this.height = height;
        this.width = width;
        this.x = x;//敌机初始化横坐标是0-(窗口宽度-敌机图片宽度) 不会出界
        this.y = y;//敌机初始化纵坐标是负的敌机高度
        this.speed = speed;
        this.alive = true;
    }
    public int getAward(){
        return (int)Math.random()*3+1;
    }
    public int score(){
        return 50;
    }
    public void clear(){
        setHeight(0);
        setWidth(0);
        setX(0);
        setY(0);
    }
    public  void move() {
        int direction = (int) (Math.random() * 10);
        /*if (direction < 2) {

            this.x = this.x - 50;
            if (this.x < 0) {
                this.x = 50;
            }

        }*/
        if (direction > 4 && direction < 7) {
            this.y = y + 10;
            if (this.y > FlyFrame.HEIGHT) {
                this.y = FlyFrame.HEIGHT - 10;
            }

        }
        if (direction > 7) {
            this.y = y + 10;
            if (this.y > FlyFrame.HEIGHT) {
                this.y = FlyFrame.HEIGHT - 10;
            }


        }

        /*if (direction < 4 && direction > 2) {


            this.x = this.x + 30;
            if (this.x > FlyFrame.WIDTH - 30) {
                this.x = FlyFrame.WIDTH;
            }

        }*/
    }
    static class AirplaneFactory{
        public static Airplane createAirplane(BufferedImage image,int speed){
            return new Airplane(image,speed);
        }
    }

    public boolean isAlive() {
        return alive;
    }

    public void setAlive(boolean alive) {
        this.alive = alive;
    }

    @Override
    public int getSpeed() {
        return speed;
    }

    @Override
    public void setSpeed(int speed) {
        this.speed = speed;
    }

    @Override
    public int getX() {
        return x;
    }

    @Override
    public void setX(int x) {
        this.x = x;
    }

    @Override
    public int getY() {
        return y;
    }

    @Override
    public void setY(int y) {
        this.y = y;
    }

    @Override
    public int getHeight() {
        return height;
    }

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

    @Override
    public int getWidth() {
        return width;
    }

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

    @Override
    public BufferedImage getImage() {
        return image;
    }

    @Override
    public void setImage(BufferedImage image) {
        this.image = image;
    }

    public Rectangle getRectangle(){
        return new Rectangle(x,y,width,height);
    }
    static class AirPlaneFactory{
        public static Airplane createAirplane(BufferedImage image,int x,int y,int height,int width,int speed){
            return new Airplane(image,x,y,height,width,speed);
        }
    }
}

import java.awt.*;
import java.awt.image.BufferedImage;

public class Bullet extends FlyObject{
    private double degree;
    private int x;
    private int y;
    private int height;
    private int width;
    private int speed;
    private BufferedImage image;
    private boolean alive;
    public Bullet(BufferedImage image,int x, int y,int speed){
        this(image,x,y,50,50,speed);
    }
    public Bullet(BufferedImage image,int x, int y,int height,int width,int speed){
        this.x = x;//子弹横坐标随飞机
        this.y = y;//子弹纵坐标随飞机
        this.image = image;
        this.height = height;
        this.width = width;
        this.speed = speed;
        this.degree = Math.PI*Math.random();//随机角度
        this.alive = true;
    }
    public void clear(){
        setHeight(0);
        setWidth(0);
        setX(0);
        setY(0);
    }
    public void move(){
        //this.x += speed*Math.cos(degree);
     
  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值