2D数组和从地图上掉下来的检测(JAVA)
我正在打印数字1、2、3、4、5、6、7、8(10/10)的简单地图,每次我左打或右打,都不会告诉我我是否从地图上掉下来,但他问我下一个YX是大于9(从底部和右侧碰撞)还是小于0(从顶部和右侧碰撞),而是问我下一个单元格中是什么(是1,2,3,4,5,6,7,8) 我该怎么检查他是否从地图上掉下来? 请帮忙2D数组和从地图上掉下来的检测(JAVA),java,arrays,collision-detection,Java,Arrays,Collision Detection,我正在打印数字1、2、3、4、5、6、7、8(10/10)的简单地图,每次我左打或右打,都不会告诉我我是否从地图上掉下来,但他问我下一个YX是大于9(从底部和右侧碰撞)还是小于0(从顶部和右侧碰撞),而是问我下一个单元格中是什么(是1,2,3,4,5,6,7,8) 我该怎么检查他是否从地图上掉下来? 请帮忙 import java.util.Scanner; public class Game { static String dir = ""; static int pla
import java.util.Scanner;
public class Game {
static String dir = "";
static int player;
static int chest;
static int monster;
static int key;
static int tile;
static int wall;
static int keysFound = 0;
static int chestsOpened = 0;
static int chestsLeft = 3;
static int sword = 0;
static int angel;
static int pickaxe = 0;
static Scanner scan = new Scanner(System.in);
static int[][] map = new int[10][10];
static int x = map[0][0];
static int y = map[0][0];
static int playerPos = map[x][y];
static int life = 3;
static boolean isInGame;
static String NoTileFound = "trying to knock this wall is pointless try some other move";
static String FreeTile = "you move to a free tile";
public static void main(String[] args) {
MapCreator mapcreate = new MapCreator();
map = mapcreate.getMapcreated();
int isPlaying;
System.out.println(" Welcome to the game, kill monsters and open three chests to win");
System.out.println(" Gather a key to open the chest, collect 3 of theese to win! that's it!");
System.out.println(" Press 1 to play..anything else to exit!");
isPlaying = scan.nextInt();
if (isPlaying == 1) {
isInGame = true;
enterGame();
} else {
System.out.println("You missed out on a good game...");
System.exit(1);
}
}
public static void enterGame() {
System.out.println(
"Type, up, down, left, or right, to move. equipment to see what you have and exit to exit game");
while (isInGame) {
move();
}
}
public static void move() {
dir = scan.nextLine();
switch (dir) {
case "up":
entertileup();
break;
case "down":
entertiledown();
break;
case "left":
entertileright();
break;
case "right":
entertiledown();
break;
case "equipement":
System.out.println("you have " + life + " lifes, " + key + " keys and " + chestsOpened
+ " chests opened, keep searching solider!");
move();
break;
case "exit":
System.out.println("You missed out on a good game...");
System.exit(1);
}
ShowMap mapprint = new ShowMap();
mapprint.print(map);
}
public static void map() {
wall = 1;
tile = 2;
monster = 3;
chest = 4;
key = 5;
sword = 6;
player = 0;
angel = 7;
pickaxe = 8;
if (playerPos == tile) {
System.out.println(FreeTile);
}
if (playerPos == monster) {
monsterEncounter();
}
if (playerPos == chest) {
openChest();
}
if (playerPos == key) {
findKey();
}
if (playerPos == sword) {
findsword();
if (playerPos == angel) {
findangel();
}
if (playerPos == pickaxe) {
findpickaxe();
}
}
}
public static void entertileup() {
if (x + 1 > 9) {
System.out.println(NoTileFound);
move();
} else if (map[x + 1][y] == 1) {
if (pickaxe == 0) {
System.out.println(NoTileFound);
move();
}
else if (pickaxe == 1) {
map[x][y] = 2;
x = x + 1;
playerPos = map[x][y];
map();
}
} else if (map[x + 1][y] == 2)
x = x + 1;
playerPos = map[x][y];
map();
}
public static void entertiledown() {
if (x - 1 < 0) {
System.out.println(NoTileFound);
move();
} else if (map[x - +1][y] == 1) {
if (pickaxe == 0) {
System.out.println(NoTileFound);
move();
}
else if (pickaxe == 1) {
map[x][y] = 2;
x = x - 1;
playerPos = map[x][y];
map();
}
} else if (map[x - 1][y] == 2)
x = x - 1;
playerPos = map[x][y];
map();
}
public static void entertileright() {
if (y - 1 < 0) {
System.out.println(NoTileFound);
move();
} else if (map[x][y - 1] == 1) {
if (pickaxe == 0) {
System.out.println(NoTileFound);
move();
}
else if (pickaxe == 1) {
map[x][y] = 2;
y = y - 1;
playerPos = map[x][y];
map();
}
} else if (map[x][y - 1] == 2)
y = y - 1;
playerPos = map[x][y];
map();
}
public static void entertileleft() {
if (y + 1 > 9) {
System.out.println(NoTileFound);
move();
} else if (map[x][y + 1] == 1) {
if (pickaxe == 0) {
System.out.println(NoTileFound);
move();
}
else if (pickaxe == 1) {
map[x][y] = 2;
y = y + 1;
playerPos = map[x][y];
map();
}
} else if (map[x][y + 1] == 2)
y = y + 1;
playerPos = map[x][y];
map();
}
public static void findsword() {
System.out.println("You found a sword, monsters can't kill you! ");
sword = sword + 1;
map[x][y] = 0;
}
public static void findangel() {
System.out.println("You have found angel! what a mighty creature! He gives you a life");
life = life + 1;
map[x][y] = 0;
}
public static void findpickaxe() {
System.out.println("You have found a pickaxe Niceeee! you can destroy walls now! just simply walk over it");
pickaxe = pickaxe + 1;
map[x][y] = 0;
}
public static void monsterEncounter() {
if (sword == 0) {
if (life > 0) {
System.out.println("WOAH! A MONSTER! You kill him but you lose a life!");
life = life - 1;
System.out.println("You have: " + life + " life(s) left! Don't die out there!");
map[x][y] = 0;
} else {
System.out.println("You have: " + life + " , You encountered too many monsters. GAME OVER.");
System.exit(1);
}
} else {
System.out.println(" You killed a monster with mighty sword, no life loss, nice! ");
map[x][y] = 0;
}
}
public static void findKey() {
System.out.println("You found a key, nice!");
keysFound += 1;
map[x][y] = 2;
}
public static void openChest() {
System.out.println("Chest, nice!!");
if (keysFound >= 1) {
chestsOpened += 1;
int totalChestsFound = chestsLeft - chestsOpened;
chestsLeft -= 1;
System.out.println("You have opened a chest! You have " + totalChestsFound + " chests to go!");
map[x][y] = 2;
keysFound -= 1;
}
if (chestsLeft == 0) {
System.out.println("YOU WON! GOOD GAME SIR, GOOD GAME.");
System.exit(1);
} else {
System.out.println("No keys, no chests. Keep searching soldier.");
}
}
import java.util.Scanner;
公开课游戏{
静态字符串dir=“”;
静态整数播放器;
静态胸腔;
静态智力怪物;
静态int键;
静态int-tile;
静态内墙;
静态int-keysFound=0;
静态int-chestsOpened=0;
静态int-chestsLeft=3;
静态整数=0;
静态内角;
静态整型镐=0;
静态扫描仪扫描=新扫描仪(System.in);
静态int[]map=newint[10][10];
静态int x=map[0][0];
静态int y=map[0][0];
静态int playerPos=map[x][y];
静态寿命=3;
静态布尔符号;
静态字符串NoTileFound=“尝试敲打这堵墙是没有意义的,尝试其他动作”;
静态字符串FreeTile=“您移动到一个自由平铺”;
公共静态void main(字符串[]args){
MapCreator mapcreate=新建MapCreator();
map=mapcreate.getMapcreated();
int显示;
System.out.println(“欢迎来到游戏,杀死怪物并打开三个箱子赢得胜利”);
System.out.println(“收集一把钥匙打开箱子,收集3把钥匙获胜!就这样!”);
System.out.println(“按1播放……还有什么要退出的吗!”);
isplay=scan.nextInt();
如果(显示==1){
isInGame=true;
enterGame();
}否则{
System.out.println(“你错过了一场精彩的比赛…”);
系统出口(1);
}
}
公共静态游戏(){
System.out.println(
“键入,向上、向下、向左或向右移动。设备可查看您拥有的设备,退出可退出游戏”);
while(isInGame){
move();
}
}
公共静态无效移动(){
dir=scan.nextLine();
交换机(dir){
案例“up”:
entertileup();
打破
案例“向下”:
entertiledown();
打破
案例“左”:
entertileright();
打破
案例“权利”:
entertiledown();
打破
案例“设备”:
System.out.println(“您有“+life+”lifes、“+key+”键和“+chestsOpened
+“打开箱子,继续搜索士兵!”);
move();
打破
案例“退出”:
System.out.println(“你错过了一场精彩的比赛…”);
系统出口(1);
}
ShowMap mapprint=新建ShowMap();
地图打印。打印(地图);
}
公共静态void映射(){
墙=1;
瓷砖=2;
怪物=3;
胸围=4;
键=5;
剑=6;
玩家=0;
天使=7;
鹤嘴锄=8;
如果(playerPos==平铺){
System.out.println(FreeTile);
}
if(playerPos==怪物){
怪物遭遇();
}
如果(playerPos==胸部){
开胸();
}
if(playerPos==键){
findKey();
}
if(playerPos==剑){
findsword();
如果(playerPos==天使){
芬丹格尔();
}
if(playerPos==鹤嘴锄){
findpickaxe();
}
}
}
公共静态无效entertileup(){
如果(x+1>9){
System.out.println(NoTileFound);
move();
}else if(map[x+1][y]==1){
如果(鹤嘴锄==0){
System.out.println(NoTileFound);
move();
}
否则如果(鹤嘴锄==1){
map[x][y]=2;
x=x+1;
playerPos=map[x][y];
map();
}
}else if(map[x+1][y]==2)
x=x+1;
playerPos=map[x][y];
map();
}
公共静态无效entertiledown(){
if(x-1<0){
System.out.println(NoTileFound);
move();
}else if(map[x-+1][y]==1){
如果(鹤嘴锄==0){
System.out.println(NoTileFound);
move();
}
否则如果(鹤嘴锄==1){
map[x][y]=2;
x=x-1;
playerPos=map[x][y];
map();
}
}else if(map[x-1][y]==2)
x=x-1;
playerPos=map[x][y];
map();
}
公共静态无效entertileright(){
if(y-1<0){
System.out.println(NoTileFound);
move();
}else if(map[x][y-1]==1){
如果(鹤嘴锄==0){
System.out.println(NoTileFound);
move();
}
否则如果(鹤嘴锄==1){
map[x][y]=2;
y=y-1;
playerPos=map[x][y];
map();
}
}else if(map[x][y-1]==2)
y=y-1;
playerPos=map[x][y];
map();
}
公共静态无效entertileft(){
如果(y+1>9){
System.out.println(NoTileFound);
move();
}else if(map[x][y+1]==1){
如果(鹤嘴锄==0){
System.out.println(NoTileFound);
move();
}
否则如果(鹤嘴锄==1){
map[x][y]=2;
y=y+1;
playerPos=map[x][y];
map();
}
}else if(map[x][y+1]==2)
y=y+1;
playerPos=map[x][y];
map();
}
公共静态void findsword(){
你找到了一把剑,怪物杀不了你;
剑=剑+1;
map[x][y]=0;
}
公共静态void findangel(){
System.out.println(“你找到了天使!多么强大的生物!他给了你生命”);
寿命=寿命+1;
map[x][y]=0;
}
公共静态void findpickaxe(){
println(“你发现了一把鹤嘴锄!你现在可以摧毁墙壁了!只要走过它就行了”);
鹤嘴锄=鹤嘴锄+1;
map[x][y]=0;
}
公共静态void(){
如果(剑==0){
如果(寿命>0){
System.out.println(“WOA