用两天时间试着用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