java貪喫蛇三

一:貪喫蛇細節實現----數據對象實現

1,GameDto.class數據對象 實現二維數組map,分數,等級,snake對象,Food對象,線程開始暫停狀態標誌位start


package com.fupeng.dto;

import com.fupeng.entity.Food;
import com.fupeng.snake.Snake;
import com.fupeng.ui.JPanelGame;

public class GameDto {
	private  boolean[][] gameMap;
	
	public static  int score = 0;
	
	public static int level = 0;
	
	public   JPanelGame jPanelGame = new JPanelGame();
	
	private Snake snake;
	
	private boolean start;
	
	private Food foodEntity;
	
	public GameDto() {
		gameMap=new boolean[30][20];
		snake = new Snake(this);
	}
	public void setSnake(Snake snake) {
		this.snake = snake;
	}
	public Snake getSnake() {
		return snake;
	}
	public boolean[][] getGameMap() {
		return gameMap;
	}
	public boolean isStart() {
		return start;
	}
	public void setStart(boolean start) {
		this.start = start;
	}
	public Food getFoodEntity() {
		return foodEntity;
	}
	public void setFoodEntity(Food foodEntity) {
		this.foodEntity = foodEntity;
	}
	
}

boolean型二維數組map作爲遊戲區背景,存放snake對象和食物方塊對象。
score變量存放遊戲分數。
level變量存放遊戲等級。
start變量作爲標誌位,玩家控制線程循環檢查該標誌位,當開始或者暫停時間觸發時,改變標誌位,做出相應的響應。

2,Snake.class對象
該對象初始化snake對象,snake的結構使用linkedList實現,鏈表中存放的是Entity對象。鏈表實現是爲了便於喫食物的時候增加節點,移動的時候刪除節點。
snake初始化狀態爲3個長度,隨機出現在map(1<x<16)上。
package com.fupeng.snake;

import java.util.LinkedList;
import java.util.Random;

import com.fupeng.dto.GameDto;
import com.fupeng.entity.Entity;


public class Snake {
	public  static Random random = new Random();
	
	private  LinkedList<Entity> snake ;
	
	private GameDto gameDto;
	
	public Snake(GameDto gameDto){
		this.gameDto=gameDto;
		snake = new LinkedList<Entity>();
		snakeHead();
	}
	
	/*
	 * 初始化狀態蛇頭包括三個實體,且出現位置爲1<x<16
	 */
	private void snakeHead(){
		int y=random.nextInt(gameDto.getGameMap()[0].length-1);//0-19
		int x=random.nextInt(gameDto.getGameMap().length-15);//0-29
		for(int i=0;i<3;i++){
			Entity head = new Entity();
			int [] position = {x+2-i,y};
			head.setPosition(position);
			snake.add(head);
		}
	}

	public   LinkedList<Entity> getSnakeList() {
		return snake;
	}

}

3,Food.class
Food實現Runable接口實現多線程。一個Food小方塊就是一個Entity對象。在run方法中隨機生成食物小方塊的座標,並放入到map地圖上
timeFx()函數實現小方塊出現時間的控制
package com.fupeng.entity;

import java.awt.Component;

import com.fupeng.dto.GameDto;
import com.fupeng.snake.Snake;

public class Food extends Component implements Runnable{
	private static final long serialVersionUID = 1L;
	
	private GameDto gameDto;
	
	public Food(GameDto gameDto) {
		super();
		this.gameDto=gameDto;
	}
	
	@Override
	public void run() {
		while(true){
			int y=Snake.random.nextInt(gameDto.getGameMap()[0].length);//0-19
			int x=Snake.random.nextInt(gameDto.getGameMap().length);//0-29
			if(this.gameDto.isStart()){
				try {
					if(gameDto.getGameMap()[x][y])
						continue;
					gameDto.getGameMap()[x][y]=true;
					gameDto.jPanelGame.repaint();
					gameDto.jPanelGame.setVisible(true);
					//修改小方塊出現的時間time,出現時間跟level相關 time=f(level)
					Thread.sleep(timeFx(this.gameDto.level));
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
	/*
	 * 小方塊出現時間計算函數
	 * y=-40x+940  x<=20
	 * 		100			x>20
	 */
	private long timeFx(int level) {
		if(level<=20)
			return -40*level+940;
		else
			return 100;
	}
	
}

4,Entity.class小方塊的實體類
小方塊實體類,類成員變量包含一個int數組存放在map中的座標位置,並重寫equals方法
Food和snake鏈表中存放的是Entity對象。
package com.fupeng.entity;

import java.awt.Image;

import javax.swing.ImageIcon;


public class Entity {
	public  static final Image ENTITY = new ImageIcon("image/rect.png").getImage();
	
	public static final int DIMENSION = ENTITY.getHeight(null);
	
	private  int[] position = new int[2];
	public int[] getPosition() {
		return position;
	}
	public void setPosition(int[] position) {
		this.position = position;
	}
	
	@Override
	public boolean equals(Object obj) {
		if(this == obj)
			return true;
		if (obj instanceof Entity) {
			Entity objEntity = (Entity) obj;
			if(this.position[0] == objEntity.position[0] && this.position[1] == objEntity.position[1])
				return true;
		}
		return false;
	}
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章