Java/Processing:基于*图节点的游戏
我正在尝试建立一个小的“模拟”游戏。 这个游戏没有真正的目的,我只是做一些小项目,同时我尝试学习一些初学者编程的输入和输出 这是我的目标:Java/Processing:基于*图节点的游戏,java,arraylist,processing,a-star,Java,Arraylist,Processing,A Star,我正在尝试建立一个小的“模拟”游戏。 这个游戏没有真正的目的,我只是做一些小项目,同时我尝试学习一些初学者编程的输入和输出 这是我的目标: 在处理画布上,有多个“节点”,表示玩家可以移动到的位置 用户将输入玩家所在的位置以及他们想要移动到的位置。(参考节点) 程序将使用A*算法确定最有效的路线 一旦确定了路线,玩家(用圆圈()表示)将以直线从一个节点移动到另一个节点 一旦播放器停止,程序将知道播放器当前所在的节点,并等待进一步的指示 我设法拼凑了我的前三个进球,但下半场给我带来了很大的困惑和头痛
import pathfinder.*;
import java.lang.Math;
// PathFinding_01
Graph graph;
// These next 2 are only needed to display
// the nodes and edges.
GraphEdge[] edges;
GraphNode[] nodes;
GraphNode[] route;
// Pathfinder algorithm
IGraphSearch pathFinder;
// Used to indicate the start and end nodes as selected by the user.
GraphNode startNode, endNode;
PImage bg;
Player midBlue;
Player midRed;
int lastHittingBlue = 99;
int lastHittingRed = 99;
int blueSide = 0;
int redSide = 1;
boolean nodeCount = true;
boolean firstRun = true; //Allows data to be collected on the first node.
boolean movement;
int count;
int x;
int y;
float start;
float runtime;
int test = 1;
// create ArrayList for route nodes
ArrayList<Float> xPos;
ArrayList<Float> yPos;
void setup() {
size(1200,1000); //Set size of window to match size of the background image.
bg = loadImage("background.png");
bg.resize(1200,1000);
start = millis();
textSize(20);
// Create graph
createGraph();
// Get nodes and edges
nodes = graph.getNodeArray();
edges = graph.getAllEdgeArray();
// Now get a path finder object
pathFinder = new GraphSearch_Astar(graph);
// Now get a route between 2 nodes
// You can change the parameter values but they must be valid IDs
pathFinder.search(0,8);
route = pathFinder.getRoute();
//Initialise the X/Y position arraylist.
xPos = new ArrayList<Float>();
yPos = new ArrayList<Float>();
drawGraph();
drawPath();
midBlue = new Player(lastHittingBlue, blueSide);
midRed = new Player(lastHittingRed, redSide);
}
void draw() {
background(0);
text((float)millis()/1000, 10,height/6);
text(start/1000, 10,height/3);
runtime = millis() - start;
text(runtime/1000, 10,height/2);
if (runtime >= 5000.0) {
start = millis();
float printX = midBlue.getXPos();
float printY = midBlue.getYPos();
int pX = round(printX);
int pY = round(printY);
print(pX, " ", pY, "\n");
test += 1;
}
drawGraph();
drawPath();
movement = midBlue.movementCheck();
midBlue.setTargetPosition(xPos.get(test), yPos.get(test));
midBlue.drawPlayer();
text( "x: " + mouseX + " y: " + mouseY, mouseX + 2, mouseY );
//noLoop();
}
void drawGraph() {
// Edges first
strokeWeight(2);
stroke(180, 180, 200);
for (int i = 0; i < edges.length; i++) {
GraphNode from = edges[i].from();
GraphNode to = edges[i].to();
line(from.xf(), from.yf(), to.xf(), to.yf());
}
// Nodes next
noStroke();
fill(255, 180, 180);
for (int i = 0; i < nodes.length; i++) {
GraphNode node = nodes[i];
ellipse(node.xf(), node.yf(), 20, 20);
text(node.id(), node.xf() - 24, node.yf() - 10);
}
}
void drawPath() {
strokeWeight(10);
stroke(200, 255, 200, 160);
for (int i = 1; i < route.length; i++) {
GraphNode from = route[i-1];
GraphNode to = route[i];
while (firstRun) {
xPos.add(from.xf());
yPos.add(from.yf());
firstRun = false;
}
xPos.add(to.xf());
yPos.add(to.yf());
line(from.xf(), from.yf(), to.xf(), to.yf());
if (nodeCount == true) {
count = route.length;
nodeCount = false;
}
}
}
public void createGraph() {
graph = new Graph();
// Create and add node
GraphNode node;
// ID X Y
node = new GraphNode(0, 175, 900);
graph.addNode(node);
node = new GraphNode(1, 190, 830);
graph.addNode(node);
node = new GraphNode(2, 240, 890);
graph.addNode(node);
node = new GraphNode(3, 253, 825);
graph.addNode(node);
node = new GraphNode(4, 204, 750);
graph.addNode(node);
node = new GraphNode(5, 315, 770);
graph.addNode(node);
node = new GraphNode(6, 325, 880);
graph.addNode(node);
node = new GraphNode(7, 440, 880);
graph.addNode(node);
node = new GraphNode(8, 442, 770);
graph.addNode(node);
node = new GraphNode(9, 400, 690);
graph.addNode(node);
node = new GraphNode(10, 308, 656);
graph.addNode(node);
node = new GraphNode(11, 210, 636);
graph.addNode(node);
// Edges for node 0
graph.addEdge(0, 1, 0, 0);
graph.addEdge(0, 2, 0, 0);
graph.addEdge(0, 3, 0, 0);
// Edges for node 1
graph.addEdge(1, 4, 0, 0);
graph.addEdge(1, 5, 0, 0);
graph.addEdge(1, 10, 0, 0);
// Edges for node 2
graph.addEdge(2, 5, 0, 0);
graph.addEdge(2, 6, 0, 0);
graph.addEdge(2, 8, 0, 0);
// Edges for node 3
graph.addEdge(3, 5, 0, 0);
graph.addEdge(3, 8, 0, 0);
graph.addEdge(3, 10, 0, 0);
// Edges for node 4
graph.addEdge(4, 10, 0, 0);
graph.addEdge(4, 11, 0, 0);
// Edges for node 5
graph.addEdge(5, 8, 0, 0);
graph.addEdge(5, 9, 0, 0);
graph.addEdge(5, 10, 0, 0);
// Edges for node 6
graph.addEdge(6, 7, 0, 0);
graph.addEdge(6, 8, 0, 0);
// Edges for node 7
graph.addEdge(7, 0, 0, 0);
// Edges for node 7
graph.addEdge(9, 0, 0, 0);
// Edges for node 7
//graph.addEdge(10, 0, 0, 0);
// Edges for node 7
graph.addEdge(11, 0, 0, 0);
}
class Player {
int lastHitting;
int side; //0 = Blue, 1 = Red.
float xPos;
float yPos;
float xTar;
float yTar;
color circleColour = color(255,0,0);
boolean isPlayerStopped;
int xDir;
int yDir;
Player(int lastHitting, int side) {
this.lastHitting = lastHitting;
this.side = side;
/* Set the Colour of the circle depending on their side selection */
if (this.side == 0) {
circleColour = color(0,0,255);
xPos = 180;
yPos = 900;
} else if (this.side == 1) {
circleColour = color(255,0,0);
xPos = 990;
yPos = 125;
}
}
void drawPlayer() {
fill(circleColour);
circle(xPos,yPos,35);
float speed = 100.0;
PVector dir = new PVector(xTar - xPos, yTar - yPos);
while (dir.mag() > 1.0) {
dir.normalize();
dir.mult(min(speed, dir.mag()));
xPos += dir.x;
yPos += dir.y;
isPlayerStopped = false;
}
if (dir.mag() < 1.0) {
isPlayerStopped = true;
}
}
void setTargetPosition(float targetX, float targetY) {
xTar = targetX;
yTar = targetY;
}
boolean movementCheck() {
return isPlayerStopped;
}
float getXPos() {
return xPos;
}
float getYPos() {
return yPos;
}
}
导入探路者。*;
导入java.lang.Math;
//寻路(01)
图形;
//接下来的2项仅用于显示
//节点和边。
图形边缘[]边;
GraphNode[]节点;
GraphNode[]路线;
//寻路算法
搜索探路者;
//用于指示用户选择的开始和结束节点。
图形节点开始节点,结束节点;
皮马杰bg;
球员米蓝;
中场球员;
int lastHittingBlue=99;
int lastHittingRed=99;
int-blueSide=0;
int redSide=1;
布尔值nodeCount=true;
布尔值firstRun=true//允许在第一个节点上收集数据。
布尔运动;
整数计数;
int x;
int-y;
浮动启动;
浮动运行时;
int检验=1;
//为路由节点创建ArrayList
ArrayList XPO;
ArrayList yPos;
无效设置(){
大小(12001000);//设置窗口大小以匹配背景图像的大小。
bg=loadImage(“background.png”);
bg.调整大小(12001000);
开始=毫秒();
文本大小(20);
//创建图形
createGraph();
//获取节点和边
nodes=graph.getNodeArray();
edges=graph.getAllEdgeArray();
//现在获取一个路径查找器对象
探路者=新图形搜索(图形);
//现在获取两个节点之间的路由
//您可以更改参数值,但它们必须是有效的ID
探路者搜索(0,8);
route=pathFinder.getRoute();
//初始化X/Y位置阵列列表。
xPos=newarraylist();
yPos=新的ArrayList();
绘图();
绘图路径();
midBlue=新玩家(最新HittingBlue,blueSide);
midRed=新玩家(最后一击红色,红方);
}
作废提款(){
背景(0);
文本((浮点)毫秒()/1000,10,高度/6);
文字(起点/1000、10、高度/3);
运行时=毫秒()-开始;
文本(运行时/1000、10、高度/2);
如果(运行时>=5000.0){
开始=毫秒();
float printX=midBlue.getXPos();
float printY=midBlue.getYPos();
int pX=圆形(printX);
int pY=圆形(printY);
打印(pX,“,pY,“\n”);
测试+=1;
}
绘图();
绘图路径();
movement=midBlue.movementCheck();
setTargetPosition(xPos.get(test)、yPos.get(test));
midBlue.drawPlayer();
文本(“x:+mouseX+”y:+mouseY,mouseX+2,mouseY);
//noLoop();
}
void drawGraph(){
//边缘优先
冲程重量(2);
冲程(180、180、200);
对于(int i=0;imovement = midBlue.movementCheck();
midBlue.setTargetPosition(xPosArray.get(test), yPosArray.get(test));
if (dir.mag() > 1.0) {
dir.normalize();
dir.mult(min(speed, dir.mag()));
xPos += dir.x;
yPos += dir.y;
} else {
// We switch target only once, and only when the target has been reached
setTargetPosition(xPosArray.get(test), yPosArray.get(test));
}