Results 1 to 8 of 8
- 11-13-2008, 01:34 PM #1
Member
- Join Date
- Oct 2008
- Location
- Aberystwyth
- Posts
- 55
- Rep Power
- 0
How do i place objects onto a grid?
Hi all again, I've created a 2D grid for my snake game using the following code:
However, I need to create a new object called food and find a way of placing it in random places on the grid once the snake has ate it (Level 1) or when the time runs out(Level 2-5)Java Code:public static class Grid { Grid[][] grid = new Grid[31][31]; // 31x31 grid static void fill(char grid[][], char _){} }
I have also created a snake class that will have to be placed on the grid and eventually moved by user input.
One thing at a time though, is there anyway i can place the food randomly on the grid for the snake to eat?
by the way I'm not using swing or any GUI its just a game in a terminal.
- 11-13-2008, 03:40 PM #2
You should be able to just use the random class to get an x,y position and then say like grid[x][y] = new Food(). You might need to type cast some stuff or change grid to type Object[][] in order to allow different object types into it
- 11-14-2008, 02:33 PM #3
Member
- Join Date
- Oct 2008
- Location
- Aberystwyth
- Posts
- 55
- Rep Power
- 0
So basically i have to create an Object[][] grid instead of grid[][] grid? So then the Object grid can hold other objects such as food and snake etc. I should also use the random method to randomly put the food in a grid position?
Is this right or have i got the wrong idea?
Thanks.
- 11-14-2008, 03:50 PM #4
Do a quick search for array of arrays on this site. I asked a question similar to this and that topic went into a pretty good explanation.
- 11-14-2008, 04:32 PM #5
Member
- Join Date
- Oct 2008
- Location
- Aberystwyth
- Posts
- 55
- Rep Power
- 0
Thanks for the post my friend. Really appreciated.
- 11-14-2008, 05:12 PM #6
No problem. Did you get it working? I only redirected you because my first time attempting this was a week or two ago and I'm not comfortable enough explaining it and didn't want to lead you astray.
- 11-14-2008, 05:56 PM #7
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
To place food randomly, you should search how to generate random numbers (I think that there is a Random class) and make sure you're picking out legal values within your grid dimensions as well as legal spots on your grid.
I honestly don't think should use an array of actual Objects, as classes are pretty heavy duty for what you're trying to do. You can use characters much more easily, or you can also try to expand your Java knowledge database by learning enumerations. If this is a project with a deadline, do it however you want, but I suggest afterwards going back and trying it all three ways (Object[][], char[][], enum[][]). It'll just help you understand better how and when to use enums.
- 11-14-2008, 06:49 PM #8
Member
- Join Date
- Oct 2008
- Location
- Aberystwyth
- Posts
- 55
- Rep Power
- 0
I'll bare that in mind. I'm considering changing it to char[][] as it seems more simple, but we'll see. I'll post back how everything goes in the near future.
Don't worry you didn't. I've found someone on the Sun forums to who is helping me out. He's teaching me the things i need to know step by step working with me. kinda cool. :)
Originally Posted by xcallmejudasx
Thanks all for your help so far, I'm sure I'll be posting back soon. ;)
Similar Threads
-
how to get the grid values
By jazz2k8 in forum Advanced JavaReplies: 2Last Post: 11-06-2008, 02:11 PM -
How can i copy a folder from one place to another..
By rajeshgubba in forum New To JavaReplies: 4Last Post: 06-14-2008, 02:21 AM -
How to place panel into frame
By vivek_9912 in forum AWT / SwingReplies: 2Last Post: 11-19-2007, 11:21 PM -
Help with Grid Layout
By coco in forum AWT / SwingReplies: 1Last Post: 08-06-2007, 08:03 PM -
how to place an image in an applet
By paty in forum Java AppletsReplies: 2Last Post: 08-04-2007, 05:15 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks