实验内容
1)实现贪吃蛇游戏基本功能,屏幕上随机出现一个“食物”,称为豆子。玩家能利用上下左右键控制“蛇”的移动,“蛇”吃到“豆子”后“蛇”身体加长一节,得分增加,“蛇”碰到边界或蛇头与蛇身相撞,“蛇”死亡,游戏结束。
2)进行交互界面的设计,要有开始键、暂停键和停止退出的选项,能够控制游戏进程。对蛇吃到豆子进行分值计算,可以设置游戏速度,游戏音乐等拓展元素。
实现效果
实现过程
food类
package com.zjxu.snake.game;
import com.zjxu.snake.core.WConstant;
import com.zjxu.snake.core.WObject;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
public class Food extends WObject{
Color foodColor;
private double initX,initY;
int timer=0;
public Food(double x,double y,double width,double height) {
super(x, y, width, height);
this.initX = x;
this.initY =y;
init();
}
@Override
public void init() {
setX(initX);
setY(initY);
setVisable(true);
this.foodColor = Color.BLUE;
createFood();
}
@Override
public void draw(GraphicsContext gc) {
gc.setFill(foodColor);
gc.fillOval(getX(), getY(), getWidth(), getHeight());
}
@Override
public void update() {
if(! isvisable()) {
createFood();
setVisable(true);
timer=0;
}
if(WConstant.level=="hard")
{
if(timer==1000) {
createFood();
timer=0;
}
timer++;
}
}
/* getWidth/2 <x< GAME_WIDTH - getWidth
getHeight/2 <y< GAME_HEIGHT- getHeight */
private void createFood() {
double randomX = (WConstant.GAME_WIDTH - 3*getWidth()/2 + 1)* Math.random() + getWidth()/2;
double randomY = (WConstant.GAME_HEIGHT - 3*getWidth()/2+ 1)* Math.random() + getHeight()/2;
setX(randomX);
setY(randomY);
}
}
GameApplication类
package com.zjxu.snake.game;
import com.zjxu.snake.core.WApplication;
import com.zjxu.snake.core.WConstant;
import javafx.scene.paint.Color;
public class GameApplication extends WApplication{
@Override
protected void loadBefore() {
setSize(800, 600,20,20);
}
@Override
protected void load() {
super.setTitle("JAVA课程设计--贪吃蛇");
GameScreen gameScreen = new GameScreen(WConstant.GAME_WIDTH,WConstant.GAME_HEIGHT);
getRoot().getChildren().add(gameScreen);
gameScreen.start();
gameScreen.initEvents();
getScene().setFill(Color.RED);
}
}
GameScreen类:
package com.zjxu.snake.game;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.zjxu.snake.core.WConstant;
import com.zjxu.snake.core.WScreen;
import com.zjxu.snake.others.Db;
import com.zjxu.snake.others.FileController;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
public class GameScreen extends WScreen{
private SnakeHead snakeHead;
private SnakeBody snakeBody;
private Food food;
private Info info;
public Boolean quit=false;
SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //游戏日期的格式
public FileController fileController = new FileController();
public GameScreen(double width, double height) {
super(width, height);
snakeHead = new SnakeHead(0, 0, 20, 20);
snakeBody = new SnakeBody(snakeHead);
food = new Food(0, 0, 20, 20);
info = new Info();
addObject(snakeBody);
addObject(snakeHead);
addObject(food);
addObject(info);
gameState = GameState.GAME_START;
}
@Override
public void update() {
if (snakeHead.death==true) {
if(gameState!=GameState.GAME_PAUSE) {
//死亡后保存数据到文件与数据库,处理相应数据参数
String name = WConstant.name;
int score = info.getScore();
Date time = new Date(System.currentTimeMillis());
String format_time = formatter.format(time); //String 类型写入固定格式
fileController.write(WConstant.name, info.getScore(),WConstant.level,format_time );//写入文件
//String sql = " INSERT INTO easy(`name`,`score`,`time`) VALUES ('test',2,'test'); ";
String VALUES = "('" + name +