It's not uncommon to see new game developers writing clones of classic games. The games are usually simple enough to write in a day or two, and generally don't need to be optimized for modern hardware. This makes them a great starting point for learning the basics of game development. While I have tinkered around with more complex games, I still enjoy making simple games or programs and sharing them on here with anyone who's watching.
I spent much of the day yesterday and a few hours today writing a simple Tetris clone and stomping out any inconspicuous bugs that managed to find a home in my program. While the concept and mechanics of the game are the same, a few details (scoring and levels, for example) aren't really based off of any particular version of the game, so don't expect much consistency with another version.
With my last tutorial, I wrote it in a way that involved writing the code as the different game systems were developed before providing the complete source code and a download link in the last part. While it's a more realistic approach to the development process of a game, it ended up being confusing, messy, and time consuming.
This time around, I'm going to approach things a bit differently. The source code and binary downloads will be available at the end of this post. The source code is well documented and pretty much everything is explained in comments.
Be warned, the source code isn't the cleanest, and there's well over 9,000 different constants declared in it. I've never been great at writing simple games like this, because it's generally not necessary to write high level systems to handle everything.
One last thing, if you find any bugs or need some clarification on something in the code, feel free leave a comment below and I'll respond whenever as soon as I can.
Anyway, without further ado, here are the downloads!
Source Code
could you tell how to make the bricks auto falling
ReplyDeleteHye bro.. can I know how to change the blocks color??
ReplyDeleteThe block properties are defined in TileType.java. The first parameter for each piece is its base color, which the light and dark edges are calculated from.
DeleteBoardPanel.java defines two constants, MIN_COLOR and MAX_COLOR, that each color components should not exceed. The reason why is explained in the source code.
This comment has been removed by the author.
DeleteSorry. But I new with java and do not understand the code very well. Let say that if I want to change the "l" type block from light blue to green what should have been done? Really appreciate it if you could help me.. thanks :)
ReplyDeleteNo problem! The properties for each piece can be found (and edited) in TileType.java.
DeleteThe first parameter for each TileType is a Color. The constructor for Color is; Color(int red, int green, int blue). Each component can be between 0 and 255, though I clamped them to BoardPanel.COLOR_MIN and BoardPanel.COLOR_MAX to ensure the light and dark colors are calculated correctly.
For example, the base color for the "I" type is: Color(BoardPanel.COLOR_MIN, BoardPanel.COLOR_MAX, BoardPanel.COLOR_MAX), which is similar to Cyan.
If we wanted to make the "I" type green, we would change the color parameters to represent green Color(BoardPanel.MIN_COLOR, BoardPanel.MAX_COLOR, BoardPanel.MIN_COLOR).
If it helps, you can imagine MIN_COLOR as representing 0, and MAX_COLOR as representing 255.
Yes!! It work.. thanks man.. really appreciate it.. :)
ReplyDeleteHye! its me again :)
ReplyDeletecan you explain to me isOccupied method. I dont understand what it do.
theres so many question that i want to ask you. is it okay to comment here or just pm you?
thx :)
Welcome back! Whichever method you prefer is fine by me, though I'm more likely to respond quickly here.
DeleteI assume that the confusion is stemming from calculating the array index, rather than what the method actually does. You can find a pretty decent explanation here: http://stackoverflow.com/questions/2151084/map-a-2d-array-onto-a-1d-array-c
Thank you for your reply
ReplyDeleteIll try to understand the it.
Thank again :)
Hey man. i enhance your game . i put a splash screen. picture at side panel and add reset command. :)
ReplyDeletecan you please mail me your enhanced version. nasbils@hotmail.com. working on my final project and it could really help me
Deletehye. how to check for color in each column in one row.
ReplyDeletei do it like this. but its wrong.
if(tiles[y * COL_COUNT + x-1].getBaseColor()=="WHITE")
thx.
TileType.getBaseColor() returns a Color object, so you' need to compare it to another color, rather than a String.
DeleteColor color = ...; //Whatever color you want to compare.
if(tiles[row * COL_COUNT + col].getBaseColor().equals(color))
Also, I changed the board a while back to use a 2D array, which should make it a bit easier to use.
thanks for the reply.
Deleteim trying to add a restriction in the game. id like to make the line to be cleared only if they are the same color. i add a condition in method checkLine like below but theres error. can you show me the right way to do it? Its for education puposes. THX
for(int col = 1; col < COL_COUNT; col++) {
if(!isOccupied(col, line) && tiles[line * COL_COUNT + col].getBaseColor().equals(tiles[line * COL_COUNT + (col-1)].getBaseColor())) {
return false;
}
}
You're not checking the first column in the loop. You could do something like...
Delete-------------------------
TileType tile = getTile(0, line); //Get the first tile on the line.
if(tile == null) {
return false; //First tile is empty
}
for(int i = 1; i < COL_COUNT; i++) {
if(getTile(i, line) != tile) {
return false;
}
}
return true;
-------------------------
By the way, you can compare an enum to another enum, so you don't need to compare the colors together.
Hye. sorry forgot to thank you.
ReplyDeleteim currently not able to do the modification but its okay.
Thanks for your help. Really appreciate it. :)
can i put a logo instead of the color like a tetris with coke fanta and sprite logos
ReplyDeleteas an example.
Yeah, you can. But you'd need to make a few changes to the code to make it work.
DeleteYou're going to need to load and store the different images when you initialize the game (before entering the main loop). You could probably store the images in a Map of some sort.
You'll also need to replace the rendering code to display the image rather than drawing each individual tile, which would be pretty easy to do.
Hello :)
ReplyDeleteI put text field on the sidePanel.java but when i run the program its not shown. How do i put some textfields on it ? Thank you :)
Hello :)
ReplyDeleteYou can share the complete code for me?
I need the code to your project ..
My e-mail address is duylam1610@gmail.com
Thank kiu very very much
Hello,
ReplyDeleteI found the source code and I wanted to play around with it but when I imported it into Eclipse, it said that TileType cannot be resolved. I looked online and I couldn't find anything helpful. Do you have any ideas of what I could do?
very nice blog
ReplyDeleteCongratulations for your Tetris implementation and thank you for sharing it. What is the license of the code? Can it be modified and redistributed for non-comercial uses conveniently mentioning you as the main author? I suggest you to add a text file with the details of the license to all your Github projects; they are really interesting but people using them would probably need to know what they can (or cannot) do with the code.
ReplyDeleteIt worked properly.
ReplyDeleteA very good feeling.
I am new to java programming (all programming) and am trying to familiarize myself with testing. How would you go about unit testing this software, particularly the TileType.java class? I would like to check that the tiles are being generated properly, but have no clue how to write it in a test.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteWhat is the purpose of the Clock Class?
ReplyDeleteI am brand new to java. I was wondering, how do I open the file? Thanks!
ReplyDeleteTetris is really a great game! Played by millions in fact. There a hundreds of variation including Block Puzzle Jewel. Check it out.
ReplyDeleteexcellent tetris a query if you see a figure before 2 fall at the same time as you would or where you could change it
ReplyDeleteBro how to download source code
ReplyDeletebro how can i change blocks to my own circular balls pls help
ReplyDelete