Results 1 to 3 of 3
Thread: Array question
- 11-13-2011, 07:07 AM #1
Member
- Join Date
- Nov 2011
- Posts
- 4
- Rep Power
- 0
Array question
Hi
I am to design a hunters game in java which is going to be run from cmd(strictly no GUI) I have created a 12x12 array which is my board, I need to move my character using keys, and make a Hunter(NPC) move towards [0][0] from [12][12]. How do I store each players and hunters coordinates (To check for collisions etc)?
Thanks!Java Code:public class Hunters { String [][] a2; private static int score; private static String player = "P"; private static String move; public static void main(String[] args){ Scanner in = new Scanner(System.in); String [][]a2 = new String [12][12]; String emptyfield = "X"; score = 5; for (int r = 0 ; r < a2.length; r++){ for (int c= 0; c <a2[r].length; c++){ a2 [r][c] = emptyfield; a2[0][0] = player; System.out.print(" "+a2[r][c]); } System.out.println(""); } System.out.println("Input your move"); move = in.nextLine(); if (move.equalsIgnoreCase("w")){ //move up //repaint //check for collision //check for health }else if(move.equalsIgnoreCase("s")){ //move down //repaint //check for collision //check for health }else if(move.equalsIgnoreCase("d")){ //move right //repaint //check for collision //check for health }else if(move.equalsIgnoreCase("a")){ //move left //repaint //check for collision //check for health } } }
- 11-23-2011, 07:43 PM #2
Member
- Join Date
- Feb 2011
- Location
- 'Mhericuh
- Posts
- 13
- Rep Power
- 0
Re: Array question
The easiest way to store coordinates in a 2D system is to use a pair of integers or a Point. You're structure is very awkward though, a couple of notes on that:
Putting everything in the main() method only alright for the smallest of programs. A game should definitely use some other methods, even a small one like this - maybe even some other classes.
Getting input from the command line is not very usual, unless you're making a text-based game. Most games would open a window (JFrame is usually used) and use a Listener (implement KeyListener, MouseListener, etc.) to get input.
For your original question though, some code would look like this:
I left a lot of stuff out that you'll need to add, but that's the main idea. One problem is that if the user types something in that is not "a", "w", "s", or "d" then he doesn't move but the hunter does - you might want to add a final else statement to deal with that, maybe a while loop or something. You can also see that it checks for collisions both after the user moves and after the hunter does, if you want simultaneous moves, you only want to check after the hunter but if you don't, then you will need to wait for a second or so between when the user moves and when the hunter does, the best way is with Thread.sleep().Java Code:public class Hunters { String [][] a2; private static int score; private static String player = "P"; private static String move; private static Point playerLoc; private static Point hunterLoc; public static void main(String[] args) { Scanner in = new Scanner(System.in); String [][]a2 = new String [12][12]; String emptyfield = "X"; score = 5; playerLoc = new Point(6, 6); hunterLoc = new Point(12, 12); for (int r = 0 ; r < a2.length; r++) { for (int c= 0; c <a2[r].length; c++) { a2 [r][c] = emptyfield; a2[0][0] = player; System.out.print(" "+a2[r][c]); } System.out.println(""); } System.out.println("Input your move"); move = in.nextLine(); if (move.equalsIgnoreCase("w")) { //move up playerLoc.y--; //repaint repaint(); //check for collision if (playerLoc.equals(hunterLoc)) { // collision! decrease health, etc? } //check for health } else if(move.equalsIgnoreCase("s")) { //move down playerLoc.y++; //repaint repaint(); //check for collision if (playerLoc.equals(hunterLoc)) { // collision! decrease health, etc? } //check for health } else if(move.equalsIgnoreCase("d")) { //move right playerLoc.x++; //repaint repaint(); //check for collision if (playerLoc.equals(hunterLoc)) { // collision! decrease health, etc? } //check for health } else if(move.equalsIgnoreCase("a")) { //move left playerLoc.x--; //repaint repaint(); //check for collision if (playerLoc.equals(hunterLoc)) { // collision! decrease health, etc? } //check for health } // move hunter hunterLoc.x--; hunterLoc.y--; //check for collision if (playerLoc.equals(hunterLoc)) { // collision! decrease health, etc? } } public static void repaint() { // drawing code here: text based or JFrame/Graphics based? } }
- 11-24-2011, 02:28 PM #3
Member
- Join Date
- Nov 2011
- Posts
- 4
- Rep Power
- 0
Re: Array question
Hi,
Thanks for the reply, I know that command line assignment is a very strange idea, but there's nothing I can do about it.
Thing is, I will need about 40+ hunter to spawn throughout the game and each one should follow a defined random path I just don't know where to start with building those NPCs, my code so far :
Java Code: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{ System.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); } }
Similar Threads
-
Question about a 2d array example
By silverglade in forum New To JavaReplies: 8Last Post: 06-06-2011, 11:56 PM -
Array question..
By gerarda in forum New To JavaReplies: 14Last Post: 03-02-2011, 01:42 AM -
Question about array
By hei1233212000 in forum New To JavaReplies: 2Last Post: 09-18-2010, 03:55 PM -
Array Question
By sc001 in forum New To JavaReplies: 1Last Post: 02-14-2010, 04:57 AM -
Array question
By McChill in forum New To JavaReplies: 5Last Post: 02-20-2009, 02:18 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks