飞机大战--java

这篇博客记录了作者作为菜鸟在2月10日完成的Java编程案例——飞机大战。游戏中,玩家需操控飞机躲避炮弹,并展示游戏进行的时间。游戏包括了游戏物体根类、飞机类、常量类、炮弹类、爆炸类以及主程序等多个组件,经过努力全部开发完毕。
摘要由CSDN通过智能技术生成

菜鸟日记—2月10日

案例 : 飞机大战

要求: 玩家可控制飞机躲避炮弹,界面显示玩家游玩时间

效果:
在这里插入图片描述
在这里插入图片描述

1.游戏物体根类

(飞机,炮弹等类都继承于此类)

import java.awt.*;

/*
* 游戏物体的根类
* */

public class GameObject {
   

    Image img;      //图片
    double x,y;     //物体的坐标
    int speed;      //物体移动的速度
    int width,height;       //物体的宽度和高度

    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;

        this.width = img.getWidth(null);
        this.height = img.getHeight(null);
    }

    public GameObject(){
   }

    public void drawMyself(Graphics g){
   
        g.drawImage(img,(int)x,(int)y,width,height,null);
    }

    //碰撞检测器
    //所有的物体都是矩形。当你获得对应的矩形的时候,我们就可以做一些相关的判断的操作!
    public Rectangle  getRec(){
   
        return new Rectangle((int)x,(int)y,width,height);
    }

}

2.飞机类

import java.awt.*;
import java.awt.event.KeyEvent;

/*
* 飞机相关属性类
* */

public class Plane extends GameObject{
   

    //移动方向
    boolean up,down,left,right;

    boolean live = true;  //活着

    @Override
    public void drawMyself(Graphics g){
   

        //飞机飞行的算法,可自行设定
        //x += speed;

        //死了就不绘制飞机
        if (live){
   

            super.drawMyself(g);

            if (up){
   
                y -= speed;
            }
            if (down){
   
                y += speed;
            }
            if (left){
   
                x -= speed;
            }
            if (right){
   
                x += speed;
            }
            if (x >= 770){
   
                x = 760;
            }
            if (x <= 0){
   
                x = 10;
            }
            if (y 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值