JAVA实现简单“伪植物大战僵尸“游戏

这篇博客分享了作者用两天时间用JAVA编写的一款简易植物大战僵尸游戏。程序存在较多BUG,未进行深度优化,主要包括游戏对象如植物、僵尸、背景的表示,以及音乐播放和简单的数据库操作。游戏中的移动对象、图片处理、主类逻辑和音乐播放等功能进行了简要介绍。尽管存在一些问题,如僵尸生命值和豌豆射手安放位置的判定不准确,但源代码已在GIT上公开供下载。
摘要由CSDN通过智能技术生成

用两天时间试着用JAVA模拟植物大战僵尸写的游戏

在这里插入图片描述
程序只用了2天时间完成,BUG较多,也没有做过多优化,功能也很简单,最后也没有想去改进

源代码免费提供,CSDN上的需要积分下载,可以去GIT上下载**

GIT地址:https://github.com/akh5/JAVA/tree/master/Plants
在这里插入图片描述
在这里插入图片描述
只有当左下角图标亮起时才能安放豌豆射手

主要代码

父类MovingObject,包含所有判断和图片读取相关代码

package Plants;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Random;


public abstract class MovingObject {
   
    public static final int LIFE = 0;
    public static final int DEAD = 1;
    public static final int REMOVE = 2;
    public static final int EAT = 3 ;
    public static final int HIT = 4;
    protected int state = LIFE ;//当前状态(默认为活着)



    protected int width; //宽
    protected int height; //高
    protected int x;  //横坐标
    protected int y; //纵坐标

    /*僵尸,植物的构造方法*/
    public MovingObject(int width,int height) {
   
        this.width = width;
        this.height = height;
    }


    public MovingObject(int width , int height , int x , int y){
   
        this.width = width;
        this.height = height;
        this.x = x;
        this.y = y;
    }

    public abstract void step();

    //获取对象的图片
    public abstract BufferedImage getImage();

    //判断对象是否是活着
    public boolean isLife() {
   
        return state == LIFE ; //当前状态为LIFE 表示对象活着
    }
    //判断对象是否是死着
    public boolean isDead() {
   
        return state == DEAD ; //当前状态为LIFE 表示对象活着
    }
    //判断对象是否删除
    public boolean isRemove() {
   
        return state == REMOVE ; //当前状态为LIFE 表示对象活着
    }

    public boolean isEat(){
   
        return state == EAT;
    }

    public boolean isHit(){
   
        return state == HIT ;
    }

    public boolean outofBounds() {
   
        return this.y>=World.HEIGHT;
    }

    public void paintObject(Graphics g) {
   
        g.drawImage(this.getImage(), this.x,this.y,null);
    }
//    /*判断敌人是否越界*/
//    public boolean outofBounds() {
   
//        //return this.y>=World.HEIGHT;
//    }

    /*碰撞检测   this是敌人   other是子弹*/
    public boolean  hit(MovingObject other) {
   
        int x1 = this.x-other.width; //
        int x2 = this.x+this.width; //
        int y1 = this.y-other.height;
        int y2 = this.y+this.height;
        int x = other.x;
        int y = other.y;
        //x在x1与x2之间,且y在y1与y2之间,即为撞上了
        return x>=x1&&x<=x2&&y>=y1&&y<=y2;
    }
    public void goDead() {
   
        state = DEAD; //将当前状态修改为DEAD
    }

    public void goEat(){
   
        state = EAT;
    }
    public void goHit(){
   
        state = HIT;
    }


}

图片Image类,存放图片路径
这里用切换图片的方法让图片动起来,植物和僵尸有十几张按帧截出来图,循环切换图片达到动图效果

package Plants;

import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.IOException;


public class Images {
   

    public static BufferedImage background; //背景图
    public static BufferedImage bullets; //子弹图
    public static BufferedImage[] sun; //太阳图
    public static BufferedImage[] plants; //植物切换
    public static BufferedImage[] zombies; //僵尸
    public static BufferedImage select0; //背景图
    public static BufferedImage select1;
    public static BufferedImage[] eat;
    public static BufferedImage[] die;
    public static BufferedImage start;
    public static BufferedImage gameover;
    public static BufferedImage sunback;

    static{
   
        background = readImage("background.png");
        bullets = readImage("bullet.png");
        sun = new BufferedImage[10];
        for(int i = 0; i< 10;i++){
   
            sun[i] = readImage("Sun"+i+".png");
        }

        select0 = readImage("select0.png");
        select1 = readImage("select1.png");

        sunback = readImage("SunBack.png");

        eat = new BufferedImage[4];
        for(int i = 0;i<4;i++){
   
            eat[i] = readImage("eat"+i+".png");
        }

        start = readImage("start.png");

        gameover = readImage("gameover.png");


        die = new BufferedImage[3];
        for (int i = 0; i<3;i++){
   
            die[i] = readImage("die"+i+".png");
        }


        plants = new BufferedImage[13];
        for(int i = 0;i<13;i++){
   
            plants[i] = readImage("Peashooter["+(i+1)+"].png");
        }


        zombies = new
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值