Tetris Game /** * @(#)Main.java * * This work is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License, or (at your option) any later version. Java Tetris game chapter shows how to create a Tetris game clone in Java.
I'm trying to make the game tetris in Java as a fun side project.
My game board is a grid of tiles:
grid = new Tile[height][width];
Game Hp Java
And within the grid, I create a new Tile object: activetile = new Tile(this,0, 0); //add new tile to 'this' board
Currently:
I'm able to control a single tile- move it down, left and right
- As you can see from the keyPressed method,
checkBottomFull()
will clear bottom row if full,collisionCheck()
will generate a new piece if block hits floor or another piece below, andcheckEndGame()
will end the game if block is stuck at the top.
- As you can see from the keyPressed method,
I'm struggling with the following:
To create an actual tetris piece, I was thinking I should just generate 3 other instances of Tile, and based on what piece it is (L, O, Bar, Z, etc), set their locations at the appropriate places according to activetile (the single tile I have control over), like so:
The problem with this is, my collision detection for activetile
won't allow it to move appropriately because it will run into its other blocks. I tried to fix that in keyPressed()
by setting the location of block2, block3, block4
AFTER the activetile's new location has been set, like so: (so once activetile
moves down, all the others are allowed to move down so they don't overlap)
This may work for going down, but it won't work for moving left or right because the tiles will overlap.
So, am I correctly creating a new instane of a Bar
piece by generating new blocks like that? Is my thinking correct?
executable
link to source code zip
Tetris Arcade Game
Thanks!
1 Answer
I would take a look at the Polygon class: http://docs.oracle.com/javase/6/docs/api/java/awt/Polygon.html
There are methods provided that can test for collision (insideness) with points on another object. You can also use translate(deltaX, deltaY)
to greatly simplify the 'motion' of your objects.