Kontakt

Fakturační údaje

IČ: 87189224, BÚ: 1958653063/0800

Contact info in QR code

Falling Bricks

Informace

Legendární logická hra ve zbrusu novém silně návykovém provedení!
Tento projekt byl vytvořen jako semestrální práce do předmětu Y36ALG (Algoritmizace).
Popis hry na Wikipedii.

Stáhni a hraj!

Dokumentace

Zdrojové kódy (prohlížení)

  1: package fallingbricks;
  2: import javax.swing.JComponent;
  3: import javax.swing.Timer;
  4: import java.awt.event.*;
  5: import java.awt.*;
  6: 
  7: /**
  8:  * Game controller
  9:  *
 10:  * @author Ondrej Macoszek
 11:  */
 12: public class Game extends JComponent implements ActionListener, KeyListener
 13: {
 14:     /**
 15:      * Matrix instance holds last position of about previous fallen bricks
 16:      */
 17:     public static Matrix matrix;
 18: 
 19:     /**
 20:      * Current falling brick instance
 21:      */
 22:     public static Brick fallingBrick;
 23: 
 24:     /**
 25:      * Score counter instance
 26:      */
 27:     public static Score score;
 28: 
 29:     /**
 30:      * Timer for main game loop
 31:      */
 32:     public Timer timer;
 33: 
 34:     /**
 35:      * Interval of rendering entire game (millisecondes)
 36:      */
 37:     public int renderInterval = 10;
 38: 
 39:     /**
 40:      * Interval of falling brick down move (millisecondes)
 41:      */
 42:     public int fallingInterval = 500;
 43: 
 44:     /**
 45:      * Dimension of canvas for fallen bricks
 46:      */
 47:     public Dimension canvasDim;
 48: 
 49:     /**
 50:      * Dimension of smallest part of brick
 51:      */
 52:     public Dimension fieldDim;
 53: 
 54:     /**
 55:      * Indicates game over
 56:      */
 57:     public boolean gameOver = false;
 58: 
 59:     /**
 60:      * Indicates paused game
 61:      */
 62:     public boolean paused = false;
 63: 
 64:     /**
 65:      * Singleton instance
 66:      */
 67:     private static Game instance;
 68: 
 69:     /**
 70:      * Constructor, do nothing
 71:      */
 72:     private Game()
 73:     {
 74:     }
 75: 
 76:     /**
 77:      * Get instance of current game instance, if no available will create
 78:      * @return Game object instance
 79:      */
 80:     public static Game getInstance()
 81:     {
 82:         if(instance == null)
 83:         {
 84:             instance = new Game();
 85:         }
 86:         return instance;
 87:     }
 88: 
 89:     /**
 90:      * Starts the game.
 91:      * Create new matrix, score counter, get new random brick,
 92:      * and initialize timer for rendering the game
 93:      */
 94:     public void start()
 95:     {
 96:         matrix = new Matrix();
 97:         score = new Score();
 98:         getNewBrick();
 99:         gameOver = false;
100:         timer = new Timer(renderInterval, this);
101:         timer.start();
102:         Debug.msg("== GAME STARTED ==");
103:     }
104: 
105:     /**
106:      * Pause the game
107:      */
108:     public void pause()
109:     {
110:         timer.stop();
111:         fallingBrick.falling.stop();
112:         paused = true;
113:         Debug.msg("== GAME PAUSED ==");
114:     }
115: 
116:     /**
117:      * Resume the game
118:      */
119:     public void resume()
120:     {
121:         timer.start();
122:         fallingBrick.falling.start();
123:         paused = false;
124:         Debug.msg("== GAME RESUMED ==");
125:     }
126: 
127:     /**
128:      * Stops the game.
129:      * Turn off timers, and sets gameOver to true.
130:      */
131:     public void stop()
132:     {
133:         timer.stop();
134:         gameOver = true;
135:         fallingBrick.stop();
136:         Debug.msg("== GAME OVER ==");
137:     }
138: 
139:     /**
140:      * Method called by timer. This is the main game loop. It checks
141:      * the game state (game running/over), if falling brick has been
142:      * blocked, handles full rows in matrix, write blocked bricks, 
143:      * retrieve new random bricks and calls the repaint method.
144:      *
145:      * @param evt is not used
146:      */
147:     public void actionPerformed(ActionEvent evt)
148:     {
149:         if (gameOver == false)
150:         {
151:             if (fallingBrick.isBlocked)
152:             {
153:                 Debug.msg("== Brick is blocked ==");
154:                 matrix.writeBrick(fallingBrick);
155:                 matrix.removeFullRows();
156:                 if (matrix.reachTop())
157:                 {
158:                     stop();
159:                 }
160:                 else
161:                 {
162:                     getNewBrick();
163:                 }
164:             }
165:             repaint(1);
166:         } 
167:         else
168:         {
169:             stop();
170:         }
171:     }
172: 
173:     /**
174:      * Paint method. Use graphic enviroment from JComponent to render game
175:      * playground and score information.
176:      */
177:     @Override public void paint(Graphics g)
178:     {
179:         // Set canvas and field dimensions according to widow size
180:         if ( (canvasDim == null) && (fieldDim == null) )
181:         {
182:             setDimensions();
183:         }
184: 
185:         // Fill background of canvas
186:         g.setColor(Color.WHITE);
187:         g.fillRect(0, 0, canvasDim.width, fieldDim.height*matrix.fields.length);
188: 
189:         // Draw separating line between canvas and score
190:         g.setColor(Color.BLACK);
191:         g.drawLine(0, fieldDim.height*matrix.fields.length, canvasDim.width, fieldDim.height*matrix.fields.length);
192: 
193:         // Can't paint falling brick and score if are not initialized
194:         if ( (fallingBrick==null) || (score==null) )
195:         {
196:             return;
197:         }
198: 
199:         // Render matrix of already fallen bricks
200:         matrix.render(g);
201: 
202:         // Render current falling brick
203:         fallingBrick.render(g);
204: 
205:         // Display score informations according to game state
206:         if (gameOver)
207:         {
208:             score.renderGameOver(g);
209:         }
210:         else
211:         {
212:             score.render(g);
213:         }
214:     }
215: 
216:     /**
217:      * Set canvas and field dimensions according to window and matrix size
218:      */
219:     public void setDimensions()
220:     {
221:         canvasDim = this.getSize();
222:         int fieldSize = (int)Math.floor( (double)canvasDim.width  / (double)matrix.dimension.width );
223:         fieldDim = new Dimension(fieldSize,fieldSize);
224:         score.position = new Point(0, fieldDim.height * matrix.fields.length + 1);
225:         score.dimension = new Dimension(canvasDim.width, canvasDim.height - fieldDim.height * matrix.fields.length - 1);
226:     }
227:    
228:     /**
229:      * Reset current falling brick (stop its timer).
230:      * Generate new brick and start its falling timer.
231:      */
232:     public void getNewBrick()
233:     {
234:         if(fallingBrick != null)
235:         {
236:             fallingBrick.stop();
237:         }
238:         fallingBrick = new Brick();
239:         fallingBrick.start();
240:     }
241: 
242: 
243:     /**
244:      * KeyListener implementation of keyPressed method. According to pressed key
245:      * move or rotate falling brick, and also pause or start game.
246:      * @param evt KeyEvent object
247:      */
248:     public void keyPressed(KeyEvent evt)
249:     {
250:         int keyCode = evt.getKeyCode();
251:         switch(keyCode)
252:         {
253:             case KeyEvent.VK_UP:
254:                 fallingBrick.rotate();
255:                 break;
256:             case KeyEvent.VK_DOWN:
257:                 fallingBrick.moveDown();
258:                 break;
259:             case KeyEvent.VK_LEFT:
260:                 fallingBrick.moveLeft();
261:                 break;
262:             case KeyEvent.VK_RIGHT:
263:                 fallingBrick.moveRight();
264:                 break;
265:             case KeyEvent.VK_ENTER:
266:                 if(gameOver==true)
267:                 {
268:                     start();
269:                 }
270:                 break;
271:             case KeyEvent.VK_P:
272:                 if(paused==true)
273:                 {
274:                     resume();
275:                 }
276:                 else
277:                 {
278:                     pause();
279:                 }
280:                 break;
281:         }
282:     }
283: 
284:     /**
285:      * KeyListener implementation of other methods
286:      * these methods are not used
287:      */
288:     public void keyTyped(KeyEvent evt) {}
289:     public void keyReleased(KeyEvent evt) {}
290: 
291: }
292: