We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hey guys so I play around with coding games but I am wondering what the best way to set boundaries for a player, the way I use now is using co-ordinates points when the player reaches them he can't pass but it becomes excruciating for bigger projects any ideas ? (I was looking at the Sprites library and noticed that the tank demo uses colors then overlaid with the map and I presume when the player interacts with those colors it triggers a certain action but still not sure how to use that).
Answers
Context please.
Show some code
2D or 3D Game? Maze? Map?
When you have a data map like a 2d array the border should be part of the map
Yes sorry I meant for 2d something along the lines of pacman for example I made pacman and coding each side of the wall for the pacman not to go through the walls and took 800 lines all together so something along those lines to code / work with. What would be more efficent ?
Separate the code and the data of the level
Eg you decode the labyrinth as a text file that you load
Think of this map as grid
Each cell is a ghost a wall a path or food element
The text file
Or generate randomly which has disadvantages
Copy the text into a grid where you place pacman in using a nested for loop. The grid here is a 2D array (see tutorials!!!) of type class Cell that you have to write.
Cell[][] grid;
Now when pacman or ghost moves basically check the next field in the current direction if it’s a Path or wall etc. and either move there or stop
Note that this is not an easy game to make although it’s old, especially movement of ghosts but there are good examples in the forum
Chrisir ;-)
In the tank demo all the graphics are sprites and all the sprites are the same size (w x h) so the whole game is like a chessboard grid. The tank is prevented from going through boundaries by checking sprite collisions (Sprites supports pixel level collision detection)
The demo also uses a simple image to define the starting positions of the boundaries, targets and sprites but you could use a text file instead.
@quark and @Chrisir so if I made something like lets say a square grid of 1's around and told the character once he meets that kind of number he cannot pass through and different number eat etc. (I saw this used in minecraft where each type of block had it own number assigned to it).
Yes, I think so
If you don’t like W, just use 1
;-)
How do I load the text file into the program/ 2d array ?
use loadStrings and then a for loop over it
Then use another for loop and use
grid[j][i]=loadedText[i].charAt(j);
;-)
so let's say this is your map in the text file (no empty line or
return
allowed at its end! Place the cursor at the end of last data line and pressdelete
often to remove those!)then we could read and display it with this sketch.
please note that the grid is getting its size from the text file.
Also, since nearly all commands have x,y as an order, so has the grid. So i is the row number (Y) but is the 2nd parameter in grid[ j ][ i ], and j is X.
E.g. here:
grid[j][i]=loadedText[i].charAt(j);
Best, Chrisir ;-)
Pointlessly continued here:
https://forum.processing.org/two/discussion/26632/using-the-grid-in-a-pacman-based-game
Closing this