Java练手项目--小游戏:飞机大战

本文记录了一周前开始学习Java基础时,通过尚学堂高老师的指导制作的一个飞机大战小游戏项目。游戏包括飞机的上下左右移动、边界限制、炮弹碰撞飞机导致飞机爆炸等元素。文章介绍了游戏的主要组成部分,如GameObject父类、GameUtil图像处理类、Constan常量类,以及BackGround、Plane、Shell、Explode和主框架类MyGameFrame的详细设计。游戏采用动态重画窗口,设置定时器以40ms刷新,并实现了键盘监听以控制飞机移动。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

飞机大战

一周前开始学习java基础,看了尚学堂高老师的视频,跟着做了一个飞机大战游戏的小项目,现在整理总结一下。

问题分析

模拟游戏,飞机可以由上下左右方向键控制,飞机被限制在window边界内,不能越过边界。炮弹碰到飞机,飞机死亡爆炸。

飞机大战中所需要的对象有:背景,飞机,炮弹,和爆炸后的动作。
背景、飞机、炮弹,都需要的属性有:图像,绘图坐标X,Y。飞机和炮弹需要运动速度,炮弹是单独绘制,需要Height和Width。都需要用到的方法就是:绘图drawImage。
所以,可以新建一个公共的父类,把所有对象需要的属性、方法放进去,被其他类继承。属性用private修饰,留出接口。

GameObject.java 类:

package com.qin.game;

import java.awt.*;

public class GameObject {
    private Image img;
    private double x,y;
    private int  speed;
    private int width, height;
    //重载构造器
    public GameObject() {
    }

    public GameObject(Image img, double x, double y, int speed, int width, int height) {
        this.img = img;
        this.x = x;
        this.y = y;
        this.speed = speed;
        this.width = width;
        this.height = height;
    }

    public GameObject(Image img, double x, double y, int speed) {
        this.img = img;
        this.x = x;
        this.y = y;
        this.speed = speed;
    }

    public GameObject(Image img, double x, double y){
        this.img = img;
        this.x = x;
        this.y = y;
    }

    //接口
    public void setImg(Image img) {
        this.img = img;
    }

    public Image getImg() {
        return img;
    }

    public void setX(double x) {
        this.x = x;
    }

    public double getX() {
        return x;
    }

    public void setY(double y) {
        this.y = y;
    }

    public double getY() {
        return y;
    }

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

    public int getSpeed() {
        return speed;
    }

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

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

    public int getHeight() {
        return height;
    }

    // 绘制物体程序
    public void drawImage(Graphics g){
        g.drawImage(img,(int)x,(int)y,null);
    }

    //物体的矩形
    public Rectangle getRect(){
        return new Rectangle((int)x,(int)y,width,height);
    }
}

在GameObject类中,drawImage方法需要用到img图像,所以我们还需要一个类可以读取图片以供drawImage使用。

GameUtil.java

package com.qin.game;

import java.awt.*;
import java.awt
public abstract class FlyingObject { public static final int LIFE = 0; //活着呢 public static final int DEAD = 1; //死了的(先爆破) public static final int REMOVE = 2; //删除了(爆破后删除) protected int state = LIFE; //当前状态(默认活着) protected int width; //宽 protected int height; //高 protected int x; //x坐标 protected int y; //y坐标 /** 无参构造方法 */ public FlyingObject(){ } /**专门给小敌机、大敌机、小蜜蜂提供的构造方法 */ public FlyingObject(int width,int height){ this.width = width; this.height = height; Random rand = new Random(); x = rand.nextInt(World.WIDTH-width); //x:0到(窗口-小敌机的宽)之间的随机数 y = -height; //y:负的小敌机的高 } /** 专门给英雄机、子弹、天空提供的构造方法 */ public FlyingObject(int width,int height,int x,int y){ this.width = width; this.height = height; this.x = x; this.y = y; } /** 读取图片 */ public static BufferedImage loadImage(String fileName){ try{ BufferedImage img = ImageIO.read(FlyingObject.class.getResource(fileName)); return img; }catch(Exception e){ e.printStackTrace(); throw new RuntimeException(); } } /** 飞行物移动了 */ public abstract void step(); public abstract BufferedImage getImage(); /** 判断是否活着 */ public boolean isLife(){ return state==LIFE; } /** 判断是否死了的 */ public boolean isDead(){ return state==DEAD; } /** 判断是否删除的 */ public boolean isRemove(){ return state==REMOVE; } /** 画对象 g:画笔 */ public void paintObject(Graphics g){ g.drawImage(getImage(), x, y, null); } /** 检测飞行物是否越界 */ public abstract boolean outOfBounds(); /** 敌人与子弹/英雄机的碰撞 this:敌人 other:子弹或英雄机 */ public boolean hit(FlyingObject other){ int x1 = this.x-other.width; //x1:敌人的x int x2 = this.x+this.width; //x2:敌人的x int y1 = this.y-other.height; //y1:敌人的y int y2 = this.y+this.height; //y2:敌人的y int x = other.x; //x:子弹的x int y = other.y; //y:子弹的y return x>=x1 && x=y1 && y<=y2; } public void goDead(){ state = DEAD; //修改当前状态为死了的 } }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值