基于Java坦克大战小游戏设计(1)

还是采用上学期学习到的设计模式,抽点时间写下这个游戏,游戏逻辑部分先写简单一点的,主要想加深对java设计、模块化等方面的理解。

首先先写Element包下各个元素的类:包括子弹、坦克、树木、河流、家、墙、血包 以及这些元素的基类。

其中基类包含这些类的共同属性和方法:坐标x、y , 一组图片 , 大小width、height , 绘制方法draw() , 获取矩形方法getRec()【用来简单判断是否发生碰撞】等。

树木、河流、墙不用多加属性或者重写什么方法 , 家的话需要重写draw方法,画家的同时顺便把家周围的一圈墙也画进去。

子弹要新加入的属性、方法有:速度、方向、类型(敌人的或者自己的...待确定)、是否存在、以及加入方向与图片的映射集合map等。判断是否出界,移动,绘制等方法。

血包的话要加入移动方法move(),比较简便的做法是可以选定几个坐标,然后在这几个坐标中随机移动。

坦克类:待完成中......

其实移动方法应该写成接口......

代码:

package element;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;

public class BasicElement {
	public static int width;
	public static int height;
	int x,y;
	public static Image[] images = null;
	
	public BasicElement(int x,int y,Image[] images) {
		this.x = x;
		this.y = y;
		this.images  = images;
	}
	
	public void draw(Graphics g) {
		g.drawImage(images[0],x,y,null);
	}
	
	public Rectangle getRect() {
		return new Rectangle(x,y,width,height);
	}

	public static int getWidth() {
		return width;
	}

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

	public static int getHeight() {
		return height;
	}

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

	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 static Image[] getImages() {
		return images;
	}

	public static void setImages(Image[] images) {
		BasicElement.images = images;
	}
	
	
}
package element;

import java.awt.Graphics;
import java.awt.Image;
import java.util.Random;

public class BloodBag extends BasicElement {

	public BloodBag(int x, int y, Image[] images) {
		super(x, y, images);
		// TODO Auto-generated constructor stub
	}
	
	public int step = 0;
	public boolean isExist = false;
	public static Random r;
	public int[][] position = { { 155, 196 }, { 500, 58 }, { 80, 340 },
			{ 99, 199 }, { 345, 456 }, { 123, 321 }, { 258, 413 } };
	
	@Override
	public void draw(Graphics g) {
		// TODO Auto-generated method stub
		if(r.nextInt(100) > 98) {
			isExist = true;
			move();
		}
		if(!isExist) {
			return ;
		}
		g.drawImage(this.images[0], x, y, null);
	}

	private void move() {
		// TODO Auto-generated method stub
		step++;
		if(step==position.length) {
			step = 0;
		}
		this.x = position[step][0];
		this.y = position[step][1];
	}

	public boolean isExist() {
		return isExist;
	}

	public void setExist(boolean isExist) {
		this.isExist = isExist;
	}
	
	
	
	

}
package element;

import java.awt.Graphics;
import java.awt.Image;
import java.util.HashMap;
import java.util.Map;

public class Bullet extends BasicElement{

	public String dir;
	public static int speed;
	public boolean isExist = false;
	public String type;
	private static Map map = new HashMap();
	static {
		map.put("L", images[0]);
		map.put("U", images[1]);
		map.put("R", images[2]);
		map.put("D", images[3]);
	}
	
	public Bullet(int x, int y, Image[] images, String dir,String type) {
		super(x, y, images);
		// TODO Auto-generated constructor stub
		this.dir = dir;
		this.type = type;
	}

	public void move() {
		switch(dir) {
		case "L":
			x-=speed;
			break;
		case "U":
			y-=speed;
			break;
		case "R":
			x+=speed;
			break;
		case "D":
			y+=speed;
			break;
		default:
			break;
		
		}
	}
	
	@Override
	public void draw(Graphics g) {
		// TODO Auto-generated method stub
		g.drawImage(map.get(dir),x,y,null);
		move();
	}
	
	
	public String getDir() {
		return dir;
	}

	public void setDir(String dir) {
		this.dir = dir;
	}

	public static int getSpeed() {
		return speed;
	}

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

	public boolean isExist() {
		return isExist;
	}

	public void setExist(boolean isExist) {
		this.isExist = isExist;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}
	
	public boolean outOfBounds() {
		if(x<0 || y<0 || x>FrameWidth || y>FrameHeight)
			return true;
		return false;
	}
	
}
package element;

import java.awt.Image;

public class CommonWall extends BasicElement {

	public CommonWall(int x, int y, Image[] images) {
		super(x, y, images);
		// TODO Auto-generated constructor stub
	}

}
package element;

import java.awt.Graphics;
import java.awt.Image;

public class Home extends BasicElement {

	public Home(int x, int y, Image[] images) {
		super(x, y, images);
		// TODO Auto-generated constructor stub
	}
	
	public boolean isExist = false;
	
	@Override
	public void draw(Graphics g) {
		// TODO Auto-generated method stub
		
			g.drawImage(this.images[0],x,y,null);
			
			for(int i = 0;i
package element;

import java.awt.Image;

public class MetalWall extends BasicElement {

	public MetalWall(int x, int y, Image[] images) {
		super(x, y, images);
		// TODO Auto-generated constructor stub
	}

}
package element;

import java.awt.Image;

public class River extends BasicElement{

	public River(int x, int y, Image[] images) {
		super(x, y, images);
		// TODO Auto-generated constructor stub
	}

}
package element;

import java.awt.Image;

public class Tree extends BasicElement{
	public Tree(int x, int y, Image[] images) {
		super(x, y, images);
		// TODO Auto-generated constructor stub
	}
	
}

 

你可能感兴趣的:(基于Java坦克大战小游戏设计(1))