Results 1 to 12 of 12
  1. #1
    Nakira is offline Member
    Join Date
    Oct 2008
    Location
    Aberystwyth
    Posts
    55
    Rep Power
    0

    Default [SOLVED] How to create a Grid?

    Hi all again. Not long been given some work to do by the University but I'm having trouble setting up my grid for the game to play on. I have to create a terminal based snake game with a grid sized 31x31. However, i haven't a clue how to create a grid.
    Any help here would be grateful.
    Thanks in advance.
    Nak.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    i haven't a clue how to create a grid.
    and I'm afraid to say that neither do I. You may wish to provide more information, a lot more.

  3. #3
    Nakira is offline Member
    Join Date
    Oct 2008
    Location
    Aberystwyth
    Posts
    55
    Rep Power
    0

    Default

    theres not really much more i could say. Its a Snake game similar to the snake games on the phones. :)
    I need to create a simple grid for the game to basically be set on. The grid will have to be 31x31.
    The snake will move one space at a time i think and will eventually eat some food which is then randomly placed on the grid somewhere else.
    Thats the basis of level 1 out of 5. But at the moment all i need to do is create a 2D grid for it all to go onto. I think i would have to use arrays. But im not sure how to create a grid using them.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    Are you talking about a GUI thing or an internal container in your program?

    In the program you could use a 2 dim array:
    Type[][] twoDim = new Type[31][31]; // 31x31 grid

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Either way, they'd be best off creating an internal "data" grid first and then could use this in a GUI or (my guess is more likely given her first post where she mentions "terminal") in console output.

  6. #6
    Nakira is offline Member
    Join Date
    Oct 2008
    Location
    Aberystwyth
    Posts
    55
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Either way, they'd be best off creating an internal "data" grid first and then could use this in a GUI or (my guess is more likely given her first post where she mentions "terminal") in console output.
    Just to let you know this "her" is a he... lol.

    but yeah I'll need to use a 2d array i think. it's just a game my lecturer wants me to make for my project. Thanks for your help I'll post back if i get stuck again. :)

  7. #7
    Nakira is offline Member
    Join Date
    Oct 2008
    Location
    Aberystwyth
    Posts
    55
    Rep Power
    0

    Default

    Is there anyway to get the grid to display? i would also like your opinion on my basic code so far. I'm not entirely sure if I'm going about this the right way. If you see something that would be best done differently then feel free to comment.
    Java Code:
    public class SnakeGame
    {
    	public static void main(String[] args)
    	{
    		
    	}
    
    	public class Grid
    	{
    		Grid[][] twoDimGrid = new Grid[31][31]; // 31x31 grid
    	}
    
    	public class Snake
    	{
    		//Creating the different parts of the snake(Head and main body)
    		char SnakeHead = '*';
    		char Snake = '~';
    	}
    }

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    Is there anyway to get the grid to display?
    Lots of ways. What are your requirements? Do you have a design?
    Where/how do you want to display? Using println() or in a GUI?

  9. #9
    Nakira is offline Member
    Join Date
    Oct 2008
    Location
    Aberystwyth
    Posts
    55
    Rep Power
    0

    Default

    Just using print() would be good enough. I'm basically creating a terminal game of Snake. However, each time the snake moves the grid wont auto update. Is there someway of refreshing it after every key press?

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    creating a terminal game
    By terminal I thought you meant that the output was via println() to a console screen.
    someway of refreshing it after every key press
    No, you would have to print the whole grid again.

    You'll want to look into using a GUI. One way would be to use a JPanel, override its paintComponent() method and draw the grid,etc using the appropriate Graphics draw... method.
    There are several examples here on how to do that. Use search for paintComponent. Copy a simple one to your PC and play with it to see how it works.

  11. #11
    Nakira is offline Member
    Join Date
    Oct 2008
    Location
    Aberystwyth
    Posts
    55
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    By terminal I thought you meant that the output was via println() to a console screen.

    No, you would have to print the whole grid again.

    You'll want to look into using a GUI. One way would be to use a JPanel, override its paintComponent() method and draw the grid,etc using the appropriate Graphics draw... method.
    There are several examples here on how to do that. Use search for paintComponent. Copy a simple one to your PC and play with it to see how it works.
    Thanks for your help. its greatly appreciated.

  12. #12
    Nakira is offline Member
    Join Date
    Oct 2008
    Location
    Aberystwyth
    Posts
    55
    Rep Power
    0

    Default

    Argh. Call me stupid if you like, but i cannot get the grid to print to the terminal. my grid is still empty but it should still work just not display anything.
    Is it possible to have more help on printing this grid please?

    Here is what i've got so far:
    Java Code:
    public class SnakeGame
    {
    	//create main method(All Java prgrams need one to run ;))
    	public static void main(String[] args)
    	{
    		
    	}
    	//Create a 2D grid for the game to be played on
    	public class Grid
    	{
    		Grid[][] twoDimGrid = new Grid[31][31]; // 31x31 grid
    	}
    	void print()
    	{
    		print(Grid.twoDimGrid(this));
    	}
    }

Similar Threads

  1. how to get the grid values
    By jazz2k8 in forum Advanced Java
    Replies: 2
    Last Post: 11-06-2008, 03:11 PM
  2. Grid layout frames GUI
    By fritz1474 in forum AWT / Swing
    Replies: 1
    Last Post: 10-15-2008, 03:04 AM
  3. J2ME Canvas grid
    By bartosz666 in forum CLDC and MIDP
    Replies: 0
    Last Post: 07-12-2008, 04:13 AM
  4. Help creating a changing grid
    By adlb1300 in forum New To Java
    Replies: 11
    Last Post: 10-24-2007, 02:41 PM
  5. Help with Grid Layout
    By coco in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 09:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •