We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello.
I am making a Tetris game in processing and I was wondering how I would go about making a collision system for my game.
I figured that the collision system would also be tied to the display system of the game, considering I have a vague idea of how it would work.
I'm assuming an easy way to do this would be to use the pixel array as a way of checking whether blocks were next to each other or at the bottom. Keep in mind that the screen resolution is going to be with 35 as the size of the block and a height of 550.
I'm also guessing that having a block class that generates each shape when it is passed into will also help.
Is what I'm saying correct? If so, how would I code this? If not, what is a better solution?
Also, I'm planning of having a special gamemode for my tetris game where after a certain amount of time after a new block is generated (probably an arbitrary number, like 5 seconds) another block comes down which you have no control over until the block you control stops moving.
How would I go about doing this as well?
(a reply soon would be greatly appreciated as the project is due at the end of the working week - other aspects of the project are finished or are towards completion!)
Thanks.
Answers
I have seen different people go about making logic like this before.
In general the idea is to keep a boolean array (or a double boolean array if you prefer to think that way) which represents whether or not a block is present in a cell. Lets say that false means that no block is occupying a cell and true means that a block is occupying a cell.
As a block falls, you check if any of the cells directly below it are true / occupied, if it is then you know that the block must stop there. Say you have one of those 2x2 blocks falling, you would be checking the two cells directly below it as it falls to see if they are true / occupied.
The reason to do it this way is to avoid testing collisions with numbers (more efficient). As for display, the boolean array gives enough information to represent the blocks visually but now the logic for display is not tied into the logic for collisions.
If this all makes sense, then perhaps an integer array may be useful instead which works very similarly to the boolean array. 0 could represent no block occupying a cell and any other number could represent that a block is occupying a cell but the advantage is that you could associate the numbers with whatever colors you will need for display.
Personally I don't think of this as object oriented, just tests on a boolean / integer array with the one block falling marked as the "active" one. Seeing as how when a line is filled in Tetris and sections of blocks are removed but not necessarily the entire block, this would make object oriented conditions unnecessarily difficult but not so for a boolean / integer array.
As said, movements in a Tetris game are highly constrained (no partial lateral block shift), so you don't need to do a pixel-level collision check (unlike a platform game, for example), but a simple check in a block-level grid is enough.
In other terms, have data representing the entities (block & area) to do the collision and alignment logic, and manage the display from it.
In short, the area is the matrix:
final boolean[][] area = new boolean[ROWS][COLS];
.Each element represents either the presence or absence of a fixed-size tile square.
hello,
just a few remarks. There are also L and T shaped pieces. It is not easy to know which blocks in the falling piece are "down". Because other blocks within the piece are on the lower site, each time when you rotate.
To avoid the hassle to determine which blocks within the current piece ("L") are on the lower site, you could (and that's the idea here) go upwards from the piles and check for a collision within the grid.
Grids
So you got 2 grids: The entire game grid A and one falling piece with a grid B:
x
x
x
x x x
copy B into A temporarily, check for collision (is there a stone of the piece directly above one pile?) if so, copy B into A permanently, otherwise delete B from A, let it fall by one line and start again.
I made a piece editor once, to create new pieces like (+ or . . or . : )
the blocks within one piece don't have to be connected.
Best, Chrisir ;-)
Thanks for the help.
I've managed to cobble something that starts to resemble a collision system together here, but I'm getting 'Expecting EOF, got whatever' issues.
I'm fixing some of these by putting them into methods, but this won't work for the movement system or the collision system as they actually have to return stuff.
What can I do to fix this?
Also, how do I do the generation of new blocks?
Just doing a quick look, I've spotted a bug at line #61:
You're assigning the value
true
to variable collide rather than just checking for that! [..]tetris was a topic here some weeks ago
this is what I made back then (since you asked how to start a new block)
it has no collision
most of it is by other guys here in the forum
line 61: what gotoloop is saying:
to check for equalness use == so it's
if (collide == true) {
or just
if (collide) {
The sign = is merely for assinging a value, not for comparison
Best, Chrisir ;-)
Thanks.
What do you think is causing the 'Expecting EOF, found 'thing'' error?
Many times a missing semicolon or closing curly brace can cause that.
I advise you to hit CTRL+T inside Processing's IDE. It aligns and indents the code.
So it's easier to spot if there's anything amiss somewhere!
Alright, I'm getting closer now.
I guess I need at this point to figure out downwards collisions/preventing an out-of-bounds error, but also how to transfer the 'nocollide' to things outside the method, as well as figuring out how to only prevent movement rather than stopping the block. Does anyone have any suggestions for this?
Alright, I've gotten some real life help to create what is pretty much an almost-completed Game & Collision system.
It's coded in Java, so it wasn't much work to convert it to processing.
However, If I wanted to put this into the game itself, what would I do? As in, how would I fit this into the void setup/game as well as displaying the 2-d array? Each block is supposed to be 35 pixels wide.
I also plan on using this interface for the game, so here is that. Ignore terrible colours.
If you either store 0 or 1 into your 2D matrix array, declaring it as
boolean
instead would make it easier! ;)P.S.:
public static void main(argc, argv[]) {
seems a mix of C & Java code! :PIn your processing code it says in the 1st line collision of Blicks
Do you mean bricks?
Also it says SDD major works
What does that mean? Please
I'd rather store it as an Integer.
Blicks is the name of the game.
SDD is Software Design Development, a school subject. This is a project for this. Yes, the help I have been getting from here is allowed since I haven't been able to ask anyone else for help in real life until now.
So...I'm pretty sure I could do a For loop in which I find the numbers of the array and set various fills and rect()s, while I also set the background to the above image and having the score displayed as text. Is this the right method?
At least lemme demonstrate why
boolean[][]
would be easier: O:-)if (board[i][j] == 1)
would be justif (board[i][j])
.if (board[i][j] == 0)
would be justif (!board[i][j])
.And invert the states would be
board[i][j] ^= true;
.Compare that to this 1 you'd have to use:
board[i][j] = board[i][j] == 0? 1 : 0;
.Now you can make a conscious choice between
boolean[][]
&int[][]
! (*)Oh right. I understand now. Unfortunately, I just realised that the boolean thing won't work because initially the sides and the bottom are set to different numbers (2 and 3 for the sides, 4 for the bottom, 5 for the settled blocks) so that it can prevent out-of-bounds errors (It can't accidentally delete the sides or bottom or top.)
Regardless, do you think there is any way of doing collision with this system?
In a Tetris type game, collision is based on whether a tile is on or off! It's just 2 states!
Outta-bounds is about any coordinates < 0 or >= length. No need for other states!
Before each bottom tile of the piece descends 1 line, we gotta check whether the position right below it is turned off.
Otherwise we gotta ourselves a collision. And the piece is finished and stays in place!