Destroy image object present in grid when an object enters the grid - java

I am learning game programming. I am trying to make a game similar to Pacman. I have made a grid with x[] and y[] (no using tiled). when sprite is in a specific grid I want to destroy its food item which is an image within the grid and add 1 to score. I tried to use image.destroy(); method but it didn't worked.
I tried, instead of destroying the image to change its location.
image.drawImage(image,x+25,y+25);
it only created a new object for as long as I was inside the specific grid.
The Game loop would not let me destroy the image or move it outside the screen.
I also tried
if(sprite is in grid){score=score+1;}
So once the sprite is in grid the score keeps on incrementing. I just want increment of "1" once my sprite is in specific grid.
Any help would be highly appreciated. Thanks.

Normally your grid[y][x] would contain values indicating what's there? This can be an enum with values EMPTY, WALL or FRUIT.
So when the player enters a cell with grid[player.getY()][player.getX()] == FRUIT, clear the cell to EMPTY, add 1 to the score, and invalidate that rectangle of the grid on screen.
The render() method will then check grid[], find it is empty.. and not draw any fruit.
You should have a single fruit image or ImageBuffer -- this doesn't have any specific position, but acts as an off-screen source for the render() function to draw it whereever it is needed. This doesn't represent any single fruit, so it doesn't get destroyed -- nor does it have a position.
Unlike sprites (for monsters/ player etc), fruit are not expected to be individually animated/ positioned around the screen.

Related

Swing paint program - How to handle selection?

I'm trying to make a paint program using swing. I have several shapes that the user can draw, including ellipses, lines, and rectangles. The shapes can drawn (using graphics2d) with various strokes (line thicknesses) and can be filled or unfilled.
I'm trying to implement a feature such that the user can click a shape with the right mouse button and drag it to move it.
My current strategy is to poll the array of shapes is reverse order (meaning in case of multiple shapes the most recently added one is selected).
Each shape implementation has a method called isSelected(int x, int y). I need to figure out how to determine whether a point falls on the shape. One challenge is that an unfilled shape such as an oval should not be selected when clicked inside, but should be selected if it is filled. Also, selection should respond to the thickness of a stroke. That is, a line should be selectable by clicking out the outer area if it uses a thicker stroke, not just the exact center of the line.
How on earth can I go about implementing this?
I am storing an array of a custom type which implements a 2d shape from this API
Then you could use:
Shape#contains(double, double)
Shape#contains(double, double, double, double)
Shape#contains(Point2D)
Shape#contains(Rectangle2D)
Depending on the information you have and what you want to check

JavaFX Side-Scrolling game: How to generate interactive objects in "extended" scene

So I am trying to create a 2D side-scrolling javafx game.
So far I used AnimationTimer to control movement of my character. But now I am kind of stuck trying to make the stage move.
I can move non-interactive elements using AnimationTimer again. But I am lacking an idea for how should I generate interactive elements in game.
For example, lets say player walks a lot of steps and reaches to take a pickup. Now how do I put this pickup in stage so it is somewhere later in game. To try explain my problem better, consider this pesky image I drew in paint:
Initially, only the screen between green bounds is visible to player. The player must walk forward (and hence the screen must walk forward too) and should find that pickup between two walls. How do I place pickup outside scene's visible view so it comes into view only when player reaches it?
The easy way: You add everything to the scene and give it absolute coordinates. You move the player by changing its coordinates in the scene. Depending on the position of the player you start scrolling. While you scroll the background you also move all other objects about the same x and y coordinates. The visible view has a given width and height. Depending on the player's position, view width/height and object range the objects become visible during scrolling.

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.

is it possible to apply car detection on IMAGE using haar cascade method in OpenCV?

If it is possible than it will detect car once or blinking multiple time on single car
i have done car detection on live feed but it is detecting car and just continue blinking so i need to use it on image. I have already Haar .xml file.
Thanx in advance
Just compare the center of the detection rectangle from the previous frame to that in the current frame. If the center has not changed too much, it is likely the same object. In that case, don't count the "new" rectangle in the current frame.

How to change the color of a large quantity of Rectangles?

So I have a large grid of 289 rectangles (17x17) and I need some way to change each one's color when they are clicked without having to make 289 different event methods. The retangles are colored based on a pattern:
if(y%2==0){
if(x%2==0)
g2.setColor(Color.WHITE)
else
g2.setColor(Color.BLUE)
}else{
if(x%2!=0)
g2.setColor(Color.WHITE)
else
g2.setColor(Color.RED)
}
I don't have any idea where I should start other than creating an big ArrayList of positions for each rectangle and their colors (such as {{0,0,Color.WHITE},{x,0,Color.BLUE}...etc};). I would use a for loop to create each of the rectangles using their parameters, but I don't know how I would create the ArrayList and Event method to detect which, if any, rectangle was clicked. How do I go about this?
EDIT:
I'm saying, how would I know which rectangle was clicked so I can change it's color? If it makes it easier, it's for a game where there are two players, red and blue. The board is made using the script above. When a player of a certain color clicks a white space the board changes that rectangles color to the players color, and that's where I have the trouble. I never have anyway to know when a player clicked one of the rectangles. How would I know when a player clicked a rectangle and how would I change it's color when clicked?
Add an instance of the same MouseListener to each component as it is constructed. There's an example here that changes a circle's color when the mouse is pressed.
Addendum: Based on your revised question, GridButtonPanel shows how a component may know its own coordinates, as well as how to reference a component based on its grid coordinates.

Categories