Maze Game Rectangle Collision With Character - java

so I am creating a maze game and I have the image/sprite that is able to move. I also have Draw.FillRects's as the board of the maze. I was just wondering how I am able to set boundaries or even store all these rectangles as an array so I can further use helper.intersect or possibly a loop to see if it intersected with any of the rectangles.
Thanks

Related

How to implment walls in Maze game in Java GUI

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?

Recommendation on JavaFX grid

I am currently developing a game on Java and I am required to have GUI elements for it (using JavaFX). The game will be similar to Snakes And Ladders. There must a zig zag board.During the game play, after a dice roll, a player will have to move. I am not sure what would be the best solution when it comes to having a grid (board) on the game. What I need is to have separate indexes on every squares, so I could tell game that this is just a regular square, or the square that you stepped on has a specific function to do (for example go back few steps). Would it be the best solution to implement this game using GridPane to draw the board? Thank you.
Well, the best thing for you would be to go with GridPane of a size for example 16x16 with setting your own ColumnConstraints and RowConstraints for it which will resemble a board on which snake can move.
As for game logic, you can implement that each index of a board( GridPane ) will have a child of a special object( call it Field ) which will hold Rectangle, shape with width and height of that board index, and some specific logic regarding feature for that field.
Bare in mind that gridpane and buttons resembling logic of a game, draw dice and similar, should be children of AnchorPane.
Link:
ColumnConstraints, RowConstraints

Is there a way to track a rectangle on a JPanel (Like 3rd person)?

I've made a few 2D games using JPanels and the Graphics draw methods. For the game I'm currently working on, I'd like to be able track the player as they walk, from an aerial view (sort of like the earlier Pokemon games, where as you move the camera tracks you), but without having to hard code it (make it that when I walk, the x and y values of every other feature including the background moves the opposite direction). Is there another way to do this or will I have to hard code it?
Use Graphics.translate(x,y) where x and y positions are the positions of your camera or player. This will translate the origin of the graphics context to the point x, y.

Randomly generate entity on 2D board avoiding collisions

I am attempting to make a snake clone, and I wanted the snake to be 5 or so pixels wide, and moves in 1 pixel increments. This means the snake can be closer than one snake width to itself. This complicates things when randomly generating food, which will be a 5x5 pixel square. I need / want an algorithm that reduces the random generation to 5x5 squares that contain no pieces of the snake.
Part of the problem is that, since the food is 5x5, the collision detection has to be checked for each pixel within the food to see if the snake is partially within the food. This leaves me with two problem
Problem
How do I reduce the search space when randomly generating the food?
I've thought about doing a quad tree, subdividing the space until the divisions are smaller than the food I want to place. I've also thought about using awt's Rectangle, and figuring out if the food's generated position is contained within a list of the rectangles that makes up the player.
The best solution I can think of is to generate a 2D array, containing all possible rectangles, with their upper left corner as the index values. Then generate another 2D array that maps each pixel to each rectangle that contains it, or even just starting points, the whole rectangle isn't needed (25 for each). Then after each move, update the corner maps of the head and tail, marking each of the mapped rectangles that it is occupied. Then do a simpler random search using the mapped corner array, (this reduces it down to a 1x1 pixel search space).
The main problem I am trying to avoid is that when the snake fills 90% of the playable space, any basic search-fail-repeat system will take a noticeable amount of time to complete, and worst case, never generate a food square.
After 7 months I have a solution. I have not tested this yet, because I still have to implement it.
The way I'm handling the snake body is by a list of rectangles defined by the top left X,Y coordinate, width, and height.
What I can do is create a tree of degree 8 starting with the whole board, and then place each snake part into the tree, with the top left corner offset by the size of the apple. When a part is placed, I can then split the board into 9 sections based on the 4 corners, 4 edges, and center of the snake. Ignoring board parts containing the snake, and board parts with dimensions <= 0, I can place these into the tree.
Since the snake parts were already adjusted for the apple size, any point within a non-occupied leaf node can be used to place an apple

Java 2D- Actual Player Collision Detection | Ignore Transparency

So I'm creating a 2D game platform that works with Tiles. Currently it won't let the player go through the any tiles and works fine. Though rather then stopping the player at a solid tile. I would like to stop the player at an actual object. Pretend the triangle is in a tile.
Whats Happening:
What I want:
I would like the player to be able to walk through the tile until their is no more transparents. Basically walk on the triangle.
Player Class http://pastebin.com/SJrzSvVV
Tile Class http://pastebin.com/V3nqxh61]
TileMap Class http://pastebin.com/fuj8dR5K
You should check out the intersects() method provided by the Java2D API.
You can define the two sprites as shapes and adjust the coordinates when the two shapes intersect. I'm guessing you are splitting up a BufferedImage to create the animation frames.
You can draw a Rectangle or Ellipse frame around your sprites and check those for collisions. The frames do not have to be visible but making them visible helps when debugging.
Figured it out, essentially what I did was I was creating this game using tiles. I created something to scan a tile and find out where it's transparent, and allow players to go through it or not depending on whether the transparency is already inside something in the tile or not.

Categories