JAVA 开发《贪吃蛇》02-模型类
一、蛇类
package com.snake.model;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import static com.snake.constant.Constant.*;
public class Snake {
private int lenght;
private int[] snakeX;
private int[] snakeY;
private char fx;
private BufferedImage[] headIm;
private BufferedImage headImR;
private BufferedImage bodyIm;
private int fxNum;
private boolean isStart;
private boolean isFail;
Random random = new Random();
public Snake() {
init();
this.fxNum = 3;
headIm = new BufferedImage[4];
for (int i = 0; i < 4; i++) {
File file = new File("static/snake_"+i+".png");
try {
headIm[i] = ImageIO.read(file);
} catch (IOException e) {
e.printStackTrace();
}
}
File file01 = new File("static/body.png");
try {
bodyIm = ImageIO.read(file01);
} catch (IOException e) {
e.printStackTrace();
}
}
private void init(){
this.isFail = false;
this.lenght=3;
this.fx='R';
this.snakeX = new int[800];
this.snakeY = new int[800];
this.snakeX[0] =SIZE*(random.nextInt(8)+3);
this.snakeY[0] =SIZE*(random.nextInt(7)+4);
this.snakeX[1] = this.snakeX[0] - SIZE;
this.snakeY[1] = this.snakeY[0];
this.snakeX[2] = this.snakeX[1] - SIZE;
this.snakeY[2] = this.snakeY[1];
}
public void snakeInit(){
this.setLenght(3);
this.setFx('R');
this.setFxNum(3);
this.setSnakeX(0,SIZE*(random.nextInt(8)+3));
this.setSnakeY(0,SIZE*(random.nextInt(7)+4));
this.setSnakeX(1,this.SnakeX(0) - SIZE);
this.setSnakeY(1, this.SnakeY(0));
this.setSnakeX(2,this.SnakeX(1) - SIZE);
this.setSnakeY(2,this.SnakeY(1));
}
public void snakeMove(){
this.headImR = headIm[this.fxNum];
if(this.isStart == true && this.isFail == false) {
for (int i = lenght - 1; i > 0; i--) {
this.snakeX[i] = snakeX[i - 1];
this.snakeY[i] = snakeY[i - 1];
}
if (fx == 'R') {
this.snakeX[0] += SIZE;
if (this.snakeX[0] >= SIZE * MY_WIDTH) {
this.snakeX[0] = 0;
}
}
if (fx == 'U') {
this.snakeY[0] -= SIZE;
if (this.snakeY[0] < SIZE * 4) {
this.snakeY[0] = SIZE * MY_HEIGHT-SIZE;
}
}
if (fx == 'D') {
this.snakeY[0] += SIZE;
if (this.snakeY[0] >= SIZE * MY_HEIGHT) {
this.snakeY[0] = SIZE * 4;
}
}
if (fx == 'L') {
this.snakeX[0] -= SIZE;
if (this.snakeX[0] < 0) {
this.snakeX[0] = SIZE * MY_WIDTH-SIZE;
}
}
}
}
public int getLenght() {
return lenght;
}
public void setLenght(int lenght) {
this.lenght = lenght;
}
public int getSnakeX(int i) {
return snakeX[i];
}
public void setSnakeX(int i,int x) {
this.snakeX[i] = x;
}
public int getSnakeY(int i) {
return snakeY[i];
}
public void setSnakeY(int i,int y) {
this.snakeY[i] = y;
}
public char getFx() {
return fx;
}
public void setFx(char fx) {
this.fx = fx;
}
public BufferedImage getBodyIm() {
return bodyIm;
}
public void setBodyIm(BufferedImage bodyIm) {
this.bodyIm = bodyIm;
}
public BufferedImage getHeadImR() {
return headImR;
}
public void setHeadImR(BufferedImage headImR) {
this.headImR = headImR;
}
public int getFxNum() {
return fxNum;
}
public void setFxNum(int fxNum) {
this.fxNum = fxNum;
}
public boolean isStart() {
return isStart;
}
public void setStart(boolean start) {
isStart = start;
}
public boolean isFail() {
return isFail;
}
public void setFail(boolean fail) {
isFail = fail;
}
}
二、食物类
package com.snake.model;
import java.util.Random;
import static com.snake.constant.Constant.*;
public class Food {
private int foodX;
private int foodY;
Random random = new Random();
public Food(){
init();
}
private void init(){
this.foodX = (random.nextInt(25)+1)*SIZE;
this.foodY = (random.nextInt(20)+5)*SIZE;
}
public void foodInit(){
this.foodX = (random.nextInt(25)+1)*SIZE;
this.foodY = (random.nextInt(20)+5)*SIZE;
}
public int getFoodX() {
return foodX;
}
public void setFoodX(int foodX) {
this.foodX = foodX;
}
public int getFoodY() {
return foodY;
}
public void setFoodY(int foodY) {
this.foodY = foodY;
}
}
三、积分系统类
package com.snake.model;
import static com.snake.constant.Constant.*;
public class ScoreSystem {
private String scoreName;
private String lengthName;
private int scoreNum;
private int lengthNum;
private int X;
private int Y;
public ScoreSystem(){
init();
}
private void init(){
this.scoreName = "积分:";
this.lengthName = "长度:";
this.scoreNum = 0;
this.lengthNum = 3;
this.X = SIZE*25;
this.Y = SIZE+15;
}
public void scoreAugment(){
this.scoreNum += 10;
this.lengthNum ++;
}
public void scoreInit(){
this.scoreNum = 0;
this.lengthNum = 3;
}
public String getScoreName() {
return scoreName;
}
public void setScoreName(String scoreName) {
this.scoreName = scoreName;
}
public String getLengthName() {
return lengthName;
}
public void setLengthName(String lengthName) {
this.lengthName = lengthName;
}
public int getScoreNum() {
return scoreNum;
}
public void setScoreNum(int scoreNum) {
this.scoreNum = scoreNum;
}
public int getLengthNum() {
return lengthNum;
}
public void setLengthNum(int lengthNum) {
this.lengthNum = lengthNum;
}
public int getX() {
return X;
}
public void setX(int x) {
X = x;
}
public int getY() {
return Y;
}
public void setY(int y) {
Y = y;
}
}
四、死亡标题
package com.snake.model;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import static com.snake.constant.Constant.*;
public class DieTitle {
private BufferedImage dieImage;
private int dieTitleImX;
private int dieTitleImY;
public DieTitle(){
init();
File file = new File("static/fail.png");
try {
dieImage = ImageIO.read(file);
} catch (IOException e) {
e.printStackTrace();
}
}
private void init(){
this.dieTitleImX = (SIZE-1) * 7;
this.dieTitleImY = SIZE * 13;
}
public BufferedImage getDieImage() {
return dieImage;
}
public void setDieImage(BufferedImage dieImage) {
this.dieImage = dieImage;
}
public int getDieTitleImX() {
return dieTitleImX;
}
public void setDieTitleImX(int dieTitleImX) {
this.dieTitleImX = dieTitleImX;
}
public int getDieTitleImY() {
return dieTitleImY;
}
public void setDieTitleImY(int dieTitleImY) {
this.dieTitleImY = dieTitleImY;
}
}