I am working out a concept for a Tetris style game with a twist. I have worked through a few tutorials for the basic logic but with the way we are wanting to do this, I am not sure if the logic is even possible in our time frame.
Basically, we are using custom images for the Tetris pieces. We have three different colors for each and a different image for each possible rotation. On top of that, our game will be trying to pair letters together so we have images with each possible rotation with each possible letter placement. For example, for the "T" shape, we have right now 92 images of each possible rotation for just ONE of the three color's we want to use with the four letters we are using. After thinking about it for the past few days, there is going to be hundreds (maybe even over a thousand) images.
However, that's not the issue. The only way I can think of checking is using a TON (like well over a thousand) IF statements. For example, if piece 1 which is a "T" shape with an "A" on the right side, we need to check if "B" is next to any of the other possible positions the piece can ever be in. If a piece with an "A" on it is next to (either above, below, or on either side) a piece with a "B" on it, a good match with be registered. If at any point two letters that are the same land next to each other, the whole row of pieces that the are a bad match will disappear causing any pieces above it to fall down. Once these pieces land again, every single piece needs to be checked again to see if its new position is a part of any possible letter pair. The checking will always need to be happening for every single piece that is spawned onto the game board so the more pieces there are, the more checking that needs to always be happening.
So, is this something that will simply take way to long (we have just over a month) or is there something else that can be done besides a TON of IF statements? Is something like this even possible? After all we are talking most likely 1000 images and every single image needs to always be getting checked if it:
Has hit another image and needs to stop
Check if where the player placed it is next to another letter and if it is a good
letter match or not (this is the big issue)
Once a whole piece disappears, the pieces need to shift down again and get all the checking needs to happen once again.
The plan is to do this in java since its the only language I have a good understanding of but I am open to any and all suggestions. If its a lost cause, please let me know. Would like to come up with another concept before I invest a lot of time into this if it is a lost cause.
EDIT:
Here is a very quickly tossed together image that gives you an idea of what it looks like:
http://imageshack.us/a/img560/1814/dg80.png
Looking at this image, you see 4 pieces already in place at the bottom of the board and the player is guiding the straight piece down. The player wants to match the "A" at the bottom of the line with the "A" that is in the right "square" of the "T" shape. Once the piece has landed next to the "T", the code needs to say "Oh hey, another "A" just landed next to me so I am a good match and we both need to disappear". Once that has happened, the other pieces will shift down and the checking needs to happen again. Since there are SO many combinations of pieces and with players having free reign to put pieces in crazy spots, the checking needs to be insane. Every single piece needs to check with all the other pieces to ensure a valid combo is never missed.
Here is an example of what a piece looks like:
http://imageshack.us/a/img23/6632/1g3q.png
What you will probably want to do is think of your Gamefield as a 2 dimensional array with a set of cells. Each row is N cells wide and there are M rows. Now whenever a piece gets dropped anywhere you register the letters in the piece in the gamefield array.
Lets assume your pieces look like this:
public class GamePience {
public Character letter;
public Color color;
}
This gives you
GamePience[][] gamefield = new GamePience [M][N]; // null is empty, otherwise gilled
When a piece is dropped you copy it's letters to the array (for simplicity I'm assuming pieces are also 2 dimensional arrays because that works well)
gamefield[0][2] = piece[0][0];
Now after a letter is placed and copied to the field you can simply check the over the field
boolean destroyedLine = false;
do {
for (int x = 0; x < gamefield.length; x++) {
for (int y = 0; y < gamefield[x].length; y++) {
GamePiece cur = gamefield[x][y];
if (cur != null && x < MAX_FIELD_WIDTH && gamefield[x+1][y] != null) {
// there are gamepieces in both!
GamePiece neighbour = gamefield[x+1][y];
if (cur.letter == neighbour.letter) {
destroyLine(x); // this method should null out the line and shift everything down, you'll probably want to loop trough the whole line first and check if there are pieces in every cell
destroyedLine = true;
break;
}
}
}
}
} while(destroyedLine);
You can also easily check against the gamefield while dropping down pieces to check if the cells are already filled.
Now, all you need to do is fill out that logic and write some code to draw the gamefield and you are set ;) (you might want to think about using an LinkedList for the gamefield because that makes deleting rows trivial)
Ok so I am creating a ball tilting game, where you navigate a ball around a maze. The ball works, and is all good in the area however I am having some problems in the maze.
The maze is programmatically created through a .txt file that contains 1's and 0's (1's being walls, and 0's being floor.) I have a for loop that goes through the whole .txt file and places a white(wall) or black(floor) square at the proper coordinates on a canvas. This is similar to the apps-for-android Amazed game if you have seen it link. However, instead of placing wall-type detection on his tiles, he just resets the game and deducts a life point if the user rolls onto a 'void' tile.
However I am looking to have 'walls' and not 'voids'. Where if the ball touches the wall, it doesn't go beyond that point, but can still move freely on the other axis. Unless it hits a corner, of course.
The problem I am having is actually creating this detection. I have tried a number of ways but nothing so far has been successful or useful.
I have tried:
- Have an ArrayList of all of the coordinates of the wall tiles, and every game tick, it checks to see if the ball is touching one. However this just slowed the game down to a crawl and was just extremely terrible.
- Check to see what tile the ball is on, and if it's on a wall tile, to stop it on an axis but let it move freely on the other, however this still didn't work as there were some issues when the ball was to the right of the wall tile, but also under it, it couldnt move up, as the code detected it being under the ball.
And a few other ways but I really cannot remember! It's been a bit hectic.
So, I am asking, if anyone has any experience in this area could they please give me a push in the right direction? Im hoping to have the maze walls sorted by Tuesday however it's looking grim!
If you need any more information, please ask.
Thank you in advance for any and all replies.
Instead of having an ArrayList that you have to iterate over, why not store the maze the same way you do the text file? Use a 2D boolean array filled with true for wall and false for floor, or vice versa.
This simplifies finding a wall considerably. All you have to do is figure out where in your grid the ball is. Then you can just check the cells immediately surrounding it. Even if you include diagonals(which probably isn't necessary for an all-90-degree maze), this equates to checking 8 booleans from an array, basically instant.
As for the axis issue, just stop movement in that direction. No need to freeze a whole axis. For instance, if the wall is right of you, just don't allow x to go higher. No reason to not let it lower unless there's a wall to the left.
This is just a general idea, though. Since it's homework, no code for you ;)
Edit:
The 2D array is just that, a boolean[][] which holds true in each of the spots where you want a wall to be. When you read in your text file, just assign it straight away.
Assuming each line in your text corresponds to an y row, and each 0/1 is the x for that column, when you read a 1, assign map[x][y] = true.
I'm honestly not sure what else you need elaboration on. It's a common format to do simple tile-based maps, though. If you google "2d array tile map", you'll find several resources to help with it.
This is probably a stupid question but I can not seem to either remember how to stop the program from doing this or I never learned it.
I'm creating a blackjack game which draws random cards with the paintComponent. When the hit button is clicked, I want it to draw the next card and generate the new total. However, whenever I click the hit button, it draws the new card perfect, but it then creates all new cards on top of my already drawn cards.
How do I stop the repaint method from picking new random cards and keep them the same throughout the program? I should mention, all the cards are stored in an array and are called to with a random number generator. Also, this happens when I resize the java window.
If asked, I will post the code but I feel like this has a simple solution.
You will need to move your 'select random card' logic outside of the paint method.
It might be a better idea to calculate all the cards in your array when you first build it, rather than selecting randomly at paint time. In other words, when you create the 'deck' array, build the entire deck so that every card knows what they are, before the user even begins to be dealt any cards from the deck. This way, there is no risk of them changing during gameplay. If you're only talking about 52 cards, or a small multiple of 52 cards, then the array is still pretty small and it'll be quick to randomise the entire array.
Alternatively, you could put a check on your "select random card" method that says something like this...
int cardValue = -1;
paintComponent(){
if (cardValue == -1){
cardValue = drawRandomCard();
}
// now paint it.
}
ie - restrict the drawRandomCard() to only run if the card doesn't already have a value.
Ultimately though, the best solution would be to completely separate the painting code from the logic - its bad coding practice for GUI activities like painting to be mixed in with programming logic.
How do I go about creating a hexagon-shaped graph / chart, viewable on the display window, similar to the one in the middle of this screen-shot?
Clicking on one of the buttons on the outer edge of the chart would increase the corresponding stat, but the stat opposite it would decrease(For example, if someone clicked the 'str' stat until it was at the highest it could be, then the 'int' stat would be at its lowest).
Is there a way to do this in Java using LWJGL?
The graph could be constructed out of solid-colored triangles. The buttons would move the outer points of the triangles in or out.
(If what you want help with is detecting a mouse press on a button or drawing a triangle, you should probably read the LWGL tutorials)
So I'm building the pacman game in Java to teach myself game programming.
I have the basic game window with the pacman sprite and the ghost sprites drawn, the pacman moves with the arrow keys, doesn't move beyond the walls of the window, etc. Now I'm trying to build the maze, as in this picture:
Without giving me the direct/complete solution to this, can someone guide me as to how this can be built? I'm talking only about the boundaries and the pipes('T' marks) here which you can't go through and you have to go around. Not the dots which the pacman eats yet.
Here are my questions:
1) What's the most efficient algorithm/method for creating this maze? Will it have to be drawn each time the paint() method is called or is there a way to draw it only at the start of the game and never again?
2) How will this actually be drawn to the screen? I assume the fillRect() will be used?
3) Any hints on collision detection (so the pacman/ghosts can't go through the walls) would be helpful.
4) Any hints on how the vacant space between the pipes will be calculated so the dots can be filled between them will also be very helpful.
Thanks
I wouldn't do it that way.
I'd draw the graphical map and then create a 2D data array which represents the map. The data map would be responsible for determining collisions, eating dots, where candy is and where the ghosts are. Once all the logic for everything is handled just use the 2D array to display everything in their proper pixel coordinates over the graphical map.
For example the user is pressing the left key. First you determine that pacman is at element 3, 3. Element 3, 2 contains information denoting a wall so you can implement the code to make him ignore the command.
EDIT:
Each element would represent about where a dot could be. For example:
No, looking at the board I would say the array would look something like this.
d,d,d,d,d,d,d,d,d,d,d,d,w,w,d,d,d,d,d,d,d,d,d,d,d,d
d,w,w,w,w,d,w,w,w,w,w,d,w,w,d,w,w,w,w,w,d,w,w,w,w,d
p,w,w,w,w,d,w,w,w,w,w,d,w,w,d,w,w,w,w,w,d,w,w,w,w,p
d,w,w,w,w,d,w,w,w,w,w,d,w,w,d,w,w,w,w,w,d,w,w,w,w,d
d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d
And so on. You might want to pick a more flexible data structure than just characters however since some areas need to contain a bunch of different information. IE even though the ghost spawning area is blank, pacman isn't allowed in there. The movement of the ghosts and pacman is different for the side escapes, the candy spawn point is a blank spot but if you want to remain flexible you'll want to denote where this is on a per map basis.
Another thing you'll want to remember is that pacman and the ghosts are often inbetween points so containing information that represents a percentage of a space they're taking up between 1,2 and 1,3 is important for collision detection as well as determining when you want to remove dots, powerups and candy from the board.
You can paint the map into a BufferedImage and just drawImage that on every paint(). You'll get quite reasonable performance this way.
If you are happy with the walls being solid, you can draw each square wall block with fillRect. If you wish to get the same look as in the picture, you need to figure how to draw the lines in the right way and use arcs for corners.
The Pacman game map is made of squares and Pacman and the ghosts always move from one square to the neighbouring square in an animated step (i.e. you press right, the pacman moves one square to the right). That means that collision detection is easy: simply don't allow moves to squares that are not empty.
I do not understand what you are trying to ask here.
1) Just to give my advice on redrawing. Something that you can do if you find redrawing the entire image is slow, is determine only the elements that have changed on the screen and redraw those. An approach for this would be the following: Determine the sprites that have moved. Determine (approximate) a rectangle around those sprites. Redraw those rectangles only. This way you are only refreshing parts of the screen and not the whole screen. This should result in an increase in performance over redrawing the entire screen.
The other answers have been reasonable for the other questions you have asked.