java用二维数组编写地图_二维数组和从地图上掉下来的检测(JAVA)

这篇博客介绍了如何使用Java的二维数组来创建一个简单的地图,并实现玩家在地图上的移动。当玩家尝试离开地图边界时,程序会进行检测并给出相应提示。同时,博客还涉及到了怪物遭遇、钥匙和宝箱收集等游戏元素。
摘要由CSDN通过智能技术生成

我正在打印数字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 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.");

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值