在文本文件中移动播放器 [英] Moving a player in a text file

查看:59
本文介绍了在文本文件中移动播放器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个基本的迷宫游戏,我需要一些帮助来使玩家在我制作的迷宫中移动.在菜单中启动游戏后,我的玩家就会以"*"表示.

我从文本文件加载了迷宫,使用1英寸表示空白,0英寸表示墙壁,2表示播放器,3表示出口.我可以编写什么代码来允许播放器在空白区域"内移动?

这是一个文本迷宫的示例:

Hi, I''m creating a basic maze game and I need a little help in moving my player around the maze I made. My players is represented by a "*" once the game is started in the menu.

I loaded the maze from a text file, using 1''s for a clear space, 0''s for walls, 2 for my player and 3 for the exit. What code could I write to allow the player to move over the "clear spaces"?

Here is an example of a text maze:

020000000000
010111111110
010010000010
010010101010
011111111010
000000000030







import javax.swing.JOptionPane;

public class aMazeGame {

	//Declarations : This is where all the variables are declared 
	//which need to be available to several methods
	
	char anyChar, starter;
	int userInput = 0;
	char mover;
	public static final char[][] array = null;
			
	static int rows; 
	static int cols; // variables to get the numbers of rows and columns from my text file
	
	enum Direction {NORTH, SOUTH, EAST, WEST};
	
	public static void main(String[] args) {
	
		menu();
		
		
	}
	
	public static void menu (){
		int userInput;
		
		
		TextIO.putln("MazeGame");
		TextIO.putln();
		TextIO.putln();
		TextIO.putln("Please select an Option :");
		TextIO.putln("1 - to Play the Game");
		TextIO.putln("2 - for Instructions");
		TextIO.putln("3 - to Exit the Game");  
		
		userInput = TextIO.getlnInt();
		
		switch (userInput){
		case 1 : playGame();
		
		case 2 : instructions();
		
		case 3 : endGame();
		
		default : NotA();
		}
		
		//return userInput;
		
		
	}
	public static void playGame() {
		// make it all work
		beMaze ();
		player();
	
	}
	
	

	public static void instructions() {
		// Display instructions
		char anyChar;
		
		TextIO.putln();
		TextIO.putln("Controls");
		TextIO.putln();
		TextIO.putln("Move your player with these keys :");
		TextIO.putln("W - Up");
		TextIO.putln("S - Down");
		TextIO.putln("D - Right" );
		TextIO.putln("A - Left");
		TextIO.putln("* = Your Player");
		TextIO.putln();
		TextIO.putln("Press any key to continue");
		anyChar = TextIO.getAnyChar();
		
		menu();
		
		
	}

	public static void endGame() {
		
		System.exit(0);
		}
	
	public static void NotA() {
		char anyChar;
		menu();
	}
	
	public static void beMaze(){
		
		TextIO.putln();
		TextIO.putln();
		TextIO.putln();
					
			TextIO.readFile("C:\\Users\\Elliot\\Documents\\New folder\\sample_maze2.txt");
			
			
			int rows = TextIO.getInt(); //variables to get the numbers of rows and 
			int cols = TextIO.getInt(); // columns from my text file
			
						
			int[][] array = new int[25][39];
			// this will create a 2-dimension array with the data
			//from my rows and columns in my maze text file
			
			//this bit dictates the size of the array
			for (int i=0; i < rows; i++) {
				for (int j=0; j < cols; j++) {
					
					//this bit fills the array with the corresponding data
					array[i][j] = TextIO.getChar();
				}
				TextIO.getln();
			}
			TextIO.readStandardInput(); 
			// This tells the prog the file is done and can be closed
			
			
			//this section displays my maze using blocks
			for (int i=0; i < rows; i++) {
				for (int j=0; j < cols; j++) {
					if (array[i][j]=='0') TextIO.putf("O");
					if (array[i][j]=='1') TextIO.putf(" ");
					if (array[i][j]=='2') TextIO.putf("*");
					if (array[i][j]=='3') TextIO.putf("X");
					
				}
					TextIO.putln();	
				}
			
			
			}	
		
	//I want the section below to be the part where the input to move the player happens.
	public static void player() {
		TextIO.putln();
		TextIO.putln();
		TextIO.putln("Where would you like to move?");
		char starter = TextIO.getChar();
		
	
}
}

推荐答案

一些伪代码怎么样?
1.从用户那里获取移动方向.
2.获取当前位置为X(列)和Y(行).
3.获取从播放器移动为DIR的方向.
4.根据方向将新位置确定为newX(col)和newY(row).
5.新位置是否在我们的数组范围内?
否-告诉玩家无法完成的任务,从1开始.
6.确定新位置的当前内容.
7.它是开放空间吗?
是-从当前位置删除播放器,将播放器放置在新位置,然后转到1.
8.是出口吗?
是-告诉玩家他们赢了,进入菜单.
9.必须是一堵墙,告诉玩家他们不能在那儿移动,然后回到1.
抱歉,我没有为ya编写代码,也没有为java编写代码,因此即使它与c ++/c#相似,我也可能无法获得正确的语法.希望它能对您有所帮助.
How about some pseudo code, would that do?
1. Get direction to move from user.
2. Get current location as X (col) and Y (row).
3. Get direction to move from player as DIR.
4. Determine new location based on direction as newX (col) and newY (row).
5. Does the new location fall within our array bounds?
No - Tell player that can''t be done, start back at 1.
6. Determine current content of the new location.
7. Is it open space?
Yes - Remove player from current location, put player in new location, got to 1.
8. Is it the exit?
Yes - Tell the player they won, go to your menu.
9. Must be a wall, tell the player they can''t move there, go back to 1.
Sorry I didn''t code it for ya, I don''t code java, so I probably wouldn''t get the syntax quite correct even though it''s similar to c++/c#. Hope it helps at any rate.


这篇关于在文本文件中移动播放器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆