一:贪吃蛇细节实现----数据对象实现
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;
}
}