import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.util.Duration;
import java.util.ArrayList;
import java.util.Random;
public class AI extends Player {
Timeline timer;
Food food;
ArrayList<Player> players;
//boolean foodIsUp, foodIsRight;
public AI(int stepSize, String colour, int playerNum, ArrayList<Player> players) {
super(stepSize, colour, playerNum);
this.players = players;
// randomAI();
}
public int findFoodDirectionY() {
SnakePiece head = getSnake().getFirst();
if(food.getPosY() != head.getPosY()) {
if(head.getPosY() - food.getPosY() < 0) {
// negative ; go down
return 1;
} else {
// positive; go up
return 0;
}
} else {
// return -1 means it is on the correct axis already
return -1;
}
}
public int findFoodDirectionX() {
SnakePiece head = getSnake().getFirst();
if(food.getPosX() != head.getPosX()) {
if(head.getPosX() - food.getPosX() < 0) {
// negative; go right
return 2;
} else {
// positive; go left
return 3;
}
} else {
// return -1 means it is on the correct axis already
return -1;
}
}
public void timerTick() {
if(!wallCollisionDetection()) {
// continue with whatever direction the wallCollisionDetection() set if it was true
if(!snakeCollisionDetection()) {
int directionX = findFoodDirectionX();
if(directionX == -1) {
turn(findFoodDirectionY());
} else {
turn(directionX);
}
}
}
}
public boolean snakeCollisionDetection() {
SnakePiece head = getSnake().getFirst();
int direction = getSnake().getDirection();
int[] result = expectedPosition(head.getPosX(), head.getPosY(), direction);
int newPosX = result[0];
int newPosY = result[1];
for (Player checkee : players) {
if (checkee.snake.getAlive()) {
for (SnakePiece piece : checkee.snake) {
if (!head.equals(piece)) { // make sure it isn't colliding with its own head...
if (newPosX == piece.getPosX() && newPosY == piece.getPosY()) {
// they may collide; take evasive action
if(direction == 2 || direction == 3) {
// move up or down
turn(findFoodDirectionY());
} else {
// move left or right
turn(findFoodDirectionX());
}
return true;
}
}
}
}
}
return false;
}
private int[] expectedPosition(int currentPosX, int currentPosY, int direction) {
int increment = 10;
int newPosX = 0; int newPosY = 0;
switch(direction) {
case 0:
newPosY = currentPosY - increment;
newPosX = currentPosX;
break;
case 1:
newPosY = currentPosY + increment;
newPosX = currentPosX;
break;
case 2:
newPosX = currentPosX + increment;
newPosY = currentPosY;
break;
case 3:
newPosX = currentPosX - increment;
newPosY = currentPosY;
break;
default:
break;
}
int[] result = {newPosX, newPosY};
return result;
}
public boolean wallCollisionDetection() {
int currentDirection = getSnake().getDirection();
int posX = getSnake().getFirst().getPosX();
int posY = getSnake().getFirst().getPosY();
if((posX == 390 && currentDirection == 2) || (posX == 10 && currentDirection == 3)) {
// about to smash into wall
// need to move up or down
getSnake().setDirection(0); // at least try to go up
turn(findFoodDirectionY());
return true;
} else if ((posY == 390 && currentDirection == 1) || (posY == 10 && currentDirection == 0)) {
// need to move left or right
getSnake().setDirection(3);
turn(findFoodDirectionX());
return true;
} else {
return false;
}
}
public void turn(int newDirection) {
// 0 = up; 1 = down; 2 = right; 3 = left
int oldDirection = getSnake().getDirection();
switch(newDirection) {
case 0: // up
if(oldDirection != 0) { // not already up
if(oldDirection == 1) {
// down - need to move on X axis first instead
turn(findFoodDirectionX());
} else {
// left or right
getSnake().setDirection(0);
}
}
break;
case 1: // down
if(oldDirection != 1) { // not already down
if(oldDirection == 0) {
// up - need to move on X axis first instead
turn(findFoodDirectionX());
} else {
// left or right
getSnake().setDirection(1);
}
}
break;
case 2: // right
if(oldDirection != 2) { // not already right
if(oldDirection == 3) {
// left - need to move on Y axis first instead
turn(findFoodDirectionY());
} else {
// up or down
getSnake().setDirection(2);
}
}
break;
case 3: // left
if(oldDirection != 3) { // not already left
if (oldDirection == 2) {
// right - need to move on Y axis first instead
turn(findFoodDirectionY());
} else {
// up or down
getSnake().setDirection(3);
}
}
break;
}
}
public void randomAI() {
timer = new Timeline((new KeyFrame(
Duration.millis(100),
event -> randomAITimerTick())));
timer.setCycleCount(Animation.INDEFINITE);
timer.play();
}
public void randomAITimerTick() {
Random rng = new Random();
snake.setDirection(rng.nextInt(3));
}
public Food getFood() {
return food;
}
public void setFood(Food food) {
this.food = food;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
Java 2D 贪吃蛇游戏项目源码
共42个文件
class:10个
java:10个
xml:7个
0 下载量 63 浏览量
2023-08-29
11:18:32
上传
评论
收藏 568KB RAR 举报
温馨提示
Java 2D 贪吃蛇游戏项目源码 2D贪吃蛇游戏是一个简单的Java应用游戏。整个游戏都是2D版本。它是古老而经典的贪吃蛇游戏的复制品。要制作这个游戏,您必须使用 Eclipse IDE。此外,该游戏为基于 Java 的游戏代码树立了一个很好的示例。
资源推荐
资源详情
资源评论
收起资源包目录
Java 2D 贪吃蛇游戏项目源码.rar (42个子文件)
Snake game
.classpath 289B
resources
icon.hqx 169KB
icon.ico 361KB
icon.icns 124KB
icon.png 17KB
src
DisplayView.fxml 1KB
SnakePiece.java 2KB
Snake.java 3KB
MainController.java 3KB
Food.java 843B
Game.java 4KB
AI.java 5KB
DisplayController.java 4KB
Main.java 3KB
Human.java 1KB
META-INF
MANIFEST.MF 40B
Player.java 417B
.idea
description.html 188B
uiDesigner.xml 9KB
vcs.xml 180B
misc.xml 627B
compiler.xml 711B
modules.xml 250B
gradle.xml 211B
encodings.xml 166B
bin
DisplayView.fxml 1KB
AI.class 5KB
MainController.class 5KB
Human.class 13KB
Food.class 1KB
Game.class 6KB
Player.class 829B
Snake.class 3KB
Main.class 3KB
SnakePiece.class 2KB
DisplayController.class 8KB
META-INF
MANIFEST.MF 40B
.project 388B
Snake.iml 503B
download
Version List.md 309B
Snake.jar 274KB
License.md 11KB
共 42 条
- 1
资源评论
小云同志你好
- 粉丝: 1049
- 资源: 931
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功