数组路径中的 NPC
我要创建一个在终端中工作的游戏(无 GUI),我可以使用按键在整个数组中移动我的角色,现在我需要创建将在数组的 12,12 点生成的 NPC(猎人),并使用随机预定义路径移动到 0,0(必须使用 12x12 数组中的所有路径)有人可以对此进行一些说明吗?
package hunters;
import java.io.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;
public class Hunters {
private static int score;
private static String player = "P";
private static String move;
private static String emptyfield = "X";
private static String [][]a2 = new String [12][12];
private static int pr,pc;
private static String hunter = "H";
private static int hr=11,hc=11;
public static void paint_board(){
for (int r = 0 ; r < a2.length; r++){
for (int c= 0; c <a2[r].length; c++){
a2 [r][c] = emptyfield;
a2[pr][pc] = player;
a2[hr][hc]= hunter;
System.out.print(" "+a2[r][c]);
}
System.out.println("");
}
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
score = 0;
paint_board();
do{
ystem.out.println("Input your move");
move = in.nextLine();
if (move.equalsIgnoreCase("w")){
//move up
pr = pr -1;
//repaint
paint_board();
//check for collision
//check for health
}else if(move.equalsIgnoreCase("s")){
//move down
pr = pr +1;
for (int i = 0; i <20; i++) System.out.println();
//repaint
paint_board();
//check for collision
//check for health
}else if(move.equalsIgnoreCase("d")){
//move right
pc = pc +1;
//repaint
paint_board();
//check for collision
//check for health
}else if(move.equalsIgnoreCase("a")){
//move left
pc = pc -1;
for (int i = 0; i < 20; i++) System.out.println("");
//repaint
paint_board();
//check for collision
//check for health
}
}while(score !=5);
}
}
I am to create a game to work in the terminal(no GUI), I am able to move my character throughout the array using keys, now I need to create NPCs(Hunters) that will spawn at 12,12 point of the array, and move to 0,0 with a random pre-defined path ( it is essential that all paths within 12x12 array are used) Could anyone flash some light on this please?
package hunters;
import java.io.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;
public class Hunters {
private static int score;
private static String player = "P";
private static String move;
private static String emptyfield = "X";
private static String [][]a2 = new String [12][12];
private static int pr,pc;
private static String hunter = "H";
private static int hr=11,hc=11;
public static void paint_board(){
for (int r = 0 ; r < a2.length; r++){
for (int c= 0; c <a2[r].length; c++){
a2 [r][c] = emptyfield;
a2[pr][pc] = player;
a2[hr][hc]= hunter;
System.out.print(" "+a2[r][c]);
}
System.out.println("");
}
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
score = 0;
paint_board();
do{
ystem.out.println("Input your move");
move = in.nextLine();
if (move.equalsIgnoreCase("w")){
//move up
pr = pr -1;
//repaint
paint_board();
//check for collision
//check for health
}else if(move.equalsIgnoreCase("s")){
//move down
pr = pr +1;
for (int i = 0; i <20; i++) System.out.println();
//repaint
paint_board();
//check for collision
//check for health
}else if(move.equalsIgnoreCase("d")){
//move right
pc = pc +1;
//repaint
paint_board();
//check for collision
//check for health
}else if(move.equalsIgnoreCase("a")){
//move left
pc = pc -1;
for (int i = 0; i < 20; i++) System.out.println("");
//repaint
paint_board();
//check for collision
//check for health
}
}while(score !=5);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需使用广度优先搜索(BFS)即可。
这将为您提供一张地图,显示从 (12,12) 到达地图上的每个自由字段至少需要多少步。
然后,你可以让你的 NPC 根据随机决策,从 (0,0) 开始,朝着数字最大且小于他们当前所在区域的区域之一的方向向后移动。 。
这样,你就会得到最短的路径之一,然后你只需要让你的 NPC 沿着这条路径行走即可。
然后,如果您想要更多不可预测的行为,您可以添加进一步的随机化,使它们现在绕道而行,即让它们有时根据随机决策选择非最佳的下一个字段。
然后花一些时间在众多游戏开发博客之一上 - 即谷
歌游戏开发广度优先
游戏开发a-star
(a-star是一种更复杂的路径查找算法)
这样你应该能够学习关于这些事情的很多内容 - 如果没有必要的数学背景,维基百科文章有时很难理解,因此我建议寻找游戏开发的特定内容。
Simply use breadth-first search (BFS).
This gives you a map showing how many steps it takes at least to get to every free field on your map from (12,12).
Then you can make your NPCs move backward field by field backward from (0,0) in the direction of one of the fields with the greatest number that is smaller than that of the field they're currently standing on, based on a random desicion.
This way, you will get one of the shortest paths, which you then just have to let your NPCs walk along.
Then you can add further randomization to make them take a detour now and then if you want more unpredictable behavior - i.e. make them sometimes choose a non-optimal next field, based on a random decision.
And then go and spend some time on one of the many game development blogs out there - i.e. google
game development breadth-first
game development a-star
(a-star is a more sophsticated path-finding algorithm)
This way you should be able learn a lot about these things - wikipedia articles are sometimes rather difficult to understand without the necessary mathematical background, hence my suggestion of looking for game development specific content.