Tutorial [Java]: Tetris Game

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

Posted in , , , , , , . Bookmark the permalink. RSS feed for this post.

29 Responses to Tutorial [Java]: Tetris Game

  1. could you tell how to make the bricks auto falling

    ReplyDelete
  2. Hye bro.. can I know how to change the blocks color??

    ReplyDelete
    Replies
    1. The 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.

      BoardPanel.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.

      Delete
    2. This comment has been removed by the author.

      Delete
  3. Sorry. 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 :)

    ReplyDelete
    Replies
    1. No problem! The properties for each piece can be found (and edited) in TileType.java.

      The 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.

      Delete
  4. Yes!! It work.. thanks man.. really appreciate it.. :)

    ReplyDelete
  5. Hye! its me again :)

    can 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 :)

    ReplyDelete
    Replies
    1. Welcome back! Whichever method you prefer is fine by me, though I'm more likely to respond quickly here.

      I 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

      Delete
  6. Thank you for your reply
    Ill try to understand the it.
    Thank again :)

    ReplyDelete
  7. Hey man. i enhance your game . i put a splash screen. picture at side panel and add reset command. :)

    ReplyDelete
    Replies
    1. can you please mail me your enhanced version. nasbils@hotmail.com. working on my final project and it could really help me

      Delete
  8. hye. how to check for color in each column in one row.
    i do it like this. but its wrong.

    if(tiles[y * COL_COUNT + x-1].getBaseColor()=="WHITE")

    thx.

    ReplyDelete
    Replies
    1. TileType.getBaseColor() returns a Color object, so you' need to compare it to another color, rather than a String.

      Color 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.

      Delete
    2. thanks for the reply.
      im 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;
      }
      }

      Delete
    3. You're not checking the first column in the loop. You could do something like...

      -------------------------
      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.

      Delete
  9. Hye. sorry forgot to thank you.
    im currently not able to do the modification but its okay.
    Thanks for your help. Really appreciate it. :)

    ReplyDelete
  10. can i put a logo instead of the color like a tetris with coke fanta and sprite logos
    as an example.

    ReplyDelete
    Replies
    1. Yeah, you can. But you'd need to make a few changes to the code to make it work.

      You'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.

      Delete
  11. Hello :)
    I 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 :)

    ReplyDelete
  12. Hello :)
    You 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

    ReplyDelete
  13. Hello,
    I 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?

    ReplyDelete
  14. Congratulations 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.

    ReplyDelete
  15. It worked properly.
    A very good feeling.

    ReplyDelete
  16. 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.

    ReplyDelete
  17. This comment has been removed by the author.

    ReplyDelete
  18. What is the purpose of the Clock Class?

    ReplyDelete
  19. I am brand new to java. I was wondering, how do I open the file? Thanks!

    ReplyDelete

Search

Swedish Greys - a WordPress theme from Nordic Themepark. Converted by LiteThemes.com.