I have an image with walls in it for my little maze game but currently, my player can move around freely meaning on top of the walls. If I want to restrict this free movement with walls, do I have to paint multiple rectangles one by one and align them properly so that they match the walls in my image, and if my player is hitting a certain wall, I would have to make a collision detection check? Or would I need to make a Wall class and create wall objects with methods which can check if they are in contact with the player, can restrict the movement? How would I go on about achieving this?
Asked
Active
Viewed 102 times
0
-
1I'd use a model which contains `Shape` instances (rectangles) to represent the walls. So I think that amounts to 'a little of both'. – Andrew Thompson Apr 04 '22 at 18:49
-
1`Shape::intersects` will let your _model_ determine if a move is permitted, and your view can `draw()` them easily; see these [examples](https://stackoverflow.com/search?tab=votes&q=%5bswing%5d%20draw%20shape%20intersects). – trashgod Apr 04 '22 at 18:56
-
1You certainly have to do collision detection. How you implement that, by looking at colors in the image, by having separate objects for parts of the maze, is totally up to you. – David Conrad Apr 04 '22 at 18:58
-
1@DavidConrad *"by looking at colors in the image"* It's also possible to [turn particular colors in an image into a shape](https://stackoverflow.com/a/7059497/418556). It takes some (OK many) CPU operations to do so, but if the time is filled with an 'intro demo' or such, the user will likely not notice it. – Andrew Thompson Apr 04 '22 at 19:12