comparison game/Engine.java @ 55:974ec36c562e

Updates.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 23 Feb 2011 20:16:26 +0200
parents cc7943cd7f2d
children 8718cc1c6586
comparison
equal deleted inserted replaced
54:cc7943cd7f2d 55:974ec36c562e
61 public static final int boardSize = 9; 61 public static final int boardSize = 9;
62 public static final int boardMiddle = 4; 62 public static final int boardMiddle = 4;
63 Piece[][] board; 63 Piece[][] board;
64 64
65 public boolean flagGameOver; 65 public boolean flagGameOver;
66 public int gameScore;
66 67
67 public Piece currPiece, nextPiece; 68 public Piece currPiece, nextPiece;
68 int currX, currY, currPoint; 69 int currX, currY, currPoint;
69 70
70 int score;
71 71
72 SoundManager soundManager; 72 SoundManager soundManager;
73 Sound sndPlaced; 73 Sound sndPlaced;
74 74
75 public GameBoard(SoundManager smgr) 75 public GameBoard(SoundManager smgr)
84 84
85 nextPiece = new Piece(PieceType.ACTIVE); 85 nextPiece = new Piece(PieceType.ACTIVE);
86 pieceFinishTurn(); 86 pieceFinishTurn();
87 87
88 flagGameOver = false; 88 flagGameOver = false;
89 score = 0; 89 gameScore = 0;
90 90
91 soundManager = smgr; 91 soundManager = smgr;
92 // sndPlaced = soundManager.getSound("sounds/placed.wav"); 92 // sndPlaced = soundManager.getSound("sounds/placed.wav");
93 } 93 }
94 94
203 } 203 }
204 204
205 public void pieceFinishTurn() 205 public void pieceFinishTurn()
206 { 206 {
207 boolean finished = false; 207 boolean finished = false;
208 int connections = 0;
209
208 while (!finished) 210 while (!finished)
209 { 211 {
210 if (currX >= 0 && currX < boardSize && currY >= 0 && currY < boardSize) 212 if (currX >= 0 && currX < boardSize && currY >= 0 && currY < boardSize)
211 { 213 {
214 connections++;
212 finished = pieceCheck(board[currX][currY]); 215 finished = pieceCheck(board[currX][currY]);
213 } 216 }
214 else 217 else
215 { 218 {
216 // Outside of the board, game over 219 // Outside of the board, game over
217 finished = true; 220 finished = true;
218 flagGameOver = true; 221 flagGameOver = true;
219 } 222 }
220 } 223 }
224
225 gameScore += connections * connections;
221 226
222 if (flagGameOver) 227 if (flagGameOver)
223 { 228 {
224 currPiece = null; 229 currPiece = null;
225 System.out.print("GameOver!\n"); 230 System.out.print("GameOver!\n");
260 float gameClock, gameFrames; 265 float gameClock, gameFrames;
261 Thread animThread; 266 Thread animThread;
262 boolean animEnable = false; 267 boolean animEnable = false;
263 268
264 Font fontMain, font1, font2; 269 Font fontMain, font1, font2;
270 FontMetrics metrics1, metrics2;
271
265 GameBoard lauta = null; 272 GameBoard lauta = null;
266 BufferedImage lautaBG = null, lautaBGScaled = null; 273 BufferedImage lautaBG = null, lautaBGScaled = null;
267 Dimension lautaDim; 274 Dimension lautaDim;
268 275
269 static final AudioFormat sfmt = new AudioFormat(22050, 16, 1, true, false); 276 static final AudioFormat sfmt = new AudioFormat(22050, 16, 1, true, false);
287 lautaBG = ImageIO.read(res.getStream()); 294 lautaBG = ImageIO.read(res.getStream());
288 295
289 try { 296 try {
290 res = new ResourceLoader("graphics/font.ttf"); 297 res = new ResourceLoader("graphics/font.ttf");
291 fontMain = Font.createFont(Font.TRUETYPE_FONT, res.getStream()); 298 fontMain = Font.createFont(Font.TRUETYPE_FONT, res.getStream());
299
292 font1 = fontMain.deriveFont(24f); 300 font1 = fontMain.deriveFont(24f);
293 font2 = fontMain.deriveFont(32f); 301 font2 = fontMain.deriveFont(64f);
294 } 302 }
295 catch (FontFormatException e) 303 catch (FontFormatException e)
296 { 304 {
297 System.out.print("Could not initialize fonts.\n"); 305 System.out.print("Could not initialize fonts.\n");
298 } 306 }
299 307
300 // musa = smgr.getSound("sounds/gamemusic.wav"); 308 musa = soundManager.getSound("sounds/gamemusic.wav");
301 } 309 }
302 catch (IOException e) 310 catch (IOException e)
303 { 311 {
304 JOptionPane.showMessageDialog(null, 312 JOptionPane.showMessageDialog(null,
305 e.getMessage(), 313 e.getMessage(),
369 widgets.setScale(dim.width, dim.height); 377 widgets.setScale(dim.width, dim.height);
370 378
371 System.out.print("scale changed\n"); 379 System.out.print("scale changed\n");
372 } 380 }
373 381
382 if (metrics1 == null)
383 metrics1 = g2.getFontMetrics(font1);
384
385 if (metrics2 == null)
386 metrics2 = g2.getFontMetrics(font2);
387
374 // Background image, pieces 388 // Background image, pieces
375 g2.drawImage(lautaBGScaled, 0, 0, null); 389 g2.drawImage(lautaBGScaled, 0, 0, null);
376 lauta.paint(g2, 90, 140, 65); 390 lauta.paint(g2, 90, 140, 65);
377 lauta.nextPiece.paint(g2, 830, 325, 90); 391
392 if (!lauta.flagGameOver)
393 {
394 lauta.nextPiece.paint(g2, 830, 325, 90);
395 }
396 else
397 {
398 String text = "Game Over!";
399 int textWidth = metrics2.stringWidth(text);
400 g2.setFont(font2);
401
402 g2.setPaint(new Color(0.0f, 0.0f, 0.0f, 0.5f));
403 g2.drawString(text, (dim.width - textWidth) / 2 + 5, dim.height / 2 + 5);
404
405 double f = Math.sin(gameClock * 0.1) * 4.0;
406 g2.setPaint(Color.white);
407 g2.drawString(text, (dim.width - textWidth) / 2 + (float) f, dim.height / 2 + (float) f);
408 }
409
378 widgets.paint(g2); 410 widgets.paint(g2);
379 411
380 // Scores, etc 412 // Scores, etc
413 g2.setFont(font2);
414 g2.setPaint(Color.white);
415
416 g2.drawString(""+ String.format("%05d", lauta.gameScore), dim.width - 230, 220);
417
381 g2.setFont(font1); 418 g2.setFont(font1);
382 g2.setPaint(Color.white);
383
384 long currTime = new Date().getTime(); 419 long currTime = new Date().getTime();
385 g2.drawString("fps = "+ ((gameFrames * 1000) / (currTime - startTime)), dim.width - 120, 20); 420 g2.drawString("fps = "+ ((gameFrames * 1000) / (currTime - startTime)), dim.width - 120, 20);
386 } 421 }
387 422
388 public void startThreads() 423 public void startThreads()
458 { 493 {
459 // Progress game animation clock 494 // Progress game animation clock
460 gameClock++; 495 gameClock++;
461 496
462 // Animate components 497 // Animate components
463 lauta.animate(gameClock); 498 if (!lauta.flagGameOver)
464 lauta.nextPiece.animate(gameClock); 499 {
500 lauta.animate(gameClock);
501 lauta.nextPiece.animate(gameClock);
502 }
465 503
466 // Repaint with a frame limiter 504 // Repaint with a frame limiter
467 if (gameClock % 3 == 1) 505 if (gameClock % 3 == 1)
468 { 506 {
469 repaint(); 507 repaint();