Skip to content

Commit

Permalink
完成Ghost Piece
Browse files Browse the repository at this point in the history
TODO : Make key more sensitive
  • Loading branch information
Eddy0402 committed Sep 6, 2013
1 parent 666dfad commit 2bc4911
Show file tree
Hide file tree
Showing 13 changed files with 239 additions and 54 deletions.
8 changes: 4 additions & 4 deletions edu/ncku/eddy/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ public void keyPressed(KeyEvent e) {
TestOutput.sysout(keycode);

if (keycode == 10) {
if (!Launcher.gameEngine.isGameRunning()) {
Launcher.gameEngine.startGame();
if (!targetEngine.isGameRunning() && ! targetEngine.gameGo && !targetEngine.gameReady) {
targetEngine.startGame();
}
}

if (Launcher.gameEngine.isGameRunning()) {
if (targetEngine.isGameRunning()) {
switch (keycode) {
case 37:
// 左
Expand All @@ -56,7 +56,7 @@ public void keyPressed(KeyEvent e) {
break;
case 40:
// 下(softdrop)
targetEngine.drop();
targetEngine.moveDown();
break;
case 38:
// 上(180度轉)
Expand Down
96 changes: 88 additions & 8 deletions edu/ncku/eddy/Display.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
package edu.ncku.eddy;

import java.awt.AlphaComposite;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.io.File;

import javax.imageio.ImageIO;

import edu.ncku.eddy.game.component.Block;
import edu.ncku.eddy.game.component.Block.BlockType;
import edu.ncku.eddy.game.component.Piece;
import edu.ncku.eddy.game.component.Piece.BlockMovingPosition;
import edu.ncku.eddy.util.Config;

public class Display extends Canvas {

Expand Down Expand Up @@ -46,7 +52,7 @@ public void update() {
@Override
public void paint(Graphics g) {

// initialize
// Background
drawBackground(g.create());

// if playing
Expand All @@ -63,14 +69,29 @@ public void paint(Graphics g) {

if (line < 20) {
if (block.getBlockType() != BlockType.None) {
drawBlock(g.create(), block.getBlockType(), positionX, positionY, true);
drawBlock(g.create(), block.getBlockType(), positionX, positionY, 1);
}
}
col++;
}
line++;
}

// draw ghost
if (Config.enableGhost) {
Piece currentGhostPiece = gameEngine.getCurrentPiece().getGhost();
for (BlockMovingPosition blockpPosition : currentGhostPiece.getBlocks()) {
if (blockpPosition.line < 20) {
int positionX = LEFT + blockpPosition.col * 16;
int positionY = TOP + 304 - blockpPosition.line * 16;

BlockType type = convertToBlockType(currentGhostPiece.getType());

drawBlock(g.create(), type, positionX, positionY, .3f);
}
}
}

// draw piece
Piece currentPiece = gameEngine.getCurrentPiece();
for (BlockMovingPosition blockpPosition : currentPiece.getBlocks()) {
Expand All @@ -80,7 +101,7 @@ public void paint(Graphics g) {

BlockType type = convertToBlockType(currentPiece.getType());

drawBlock(g.create(), type, positionX, positionY, false);
drawBlock(g.create(), type, positionX, positionY, .8f);
}
}

Expand All @@ -93,7 +114,7 @@ public void paint(Graphics g) {

BlockType type = convertToBlockType(holdPiece.getType());

drawBlock(g.create(), type, positionX, positionY, false);
drawBlock(g.create(), type, positionX, positionY, .8f);
}

}
Expand All @@ -117,13 +138,72 @@ public void paint(Graphics g) {
positionY = positionY - 8;
}

drawBlock(g.create(), type, positionX, positionY, false);
drawBlock(g.create(), type, positionX, positionY, .8f);
}
nextIndex++;
}

}

// Hint
if (gameEngine.gameReady || gameEngine.gameGo || gameEngine.gameStop
|| gameEngine.gameOver) {
Graphics2D g2d = (Graphics2D) g.create();

if (gameEngine.gameReady || gameEngine.gameGo) {
int nextIndex = 0;
for (Piece nextPiece : gameEngine.getRandomizer().getNextPieces()) {
for (BlockMovingPosition blockpPosition : nextPiece.getBlocks()) {
int positionX = LEFT + 190 + blockpPosition.col * 16;
int positionY = TOP + 35 - blockpPosition.line * 16
+ nextIndex * 55;

BlockType type = convertToBlockType(nextPiece.getType());

// °¾²¾
if (type != BlockType.O && type != BlockType.I) {
positionX = positionX + 8;
}

if (type == BlockType.I) {
positionY = positionY - 8;
}

drawBlock(g.create(), type, positionX, positionY, .8f);
}
nextIndex++;
}
}

if (gameEngine.gameReady) {
g2d.translate(110, 250);
g2d.setColor(Color.gray);
g2d.setFont(new Font("SansSerif", Font.BOLD, 24));
g2d.drawString("Ready?", 0, 0);
}

if (gameEngine.gameGo) {
g2d.translate(125, 250);
g2d.setColor(Color.gray);
g2d.setFont(new Font("SansSerif", Font.BOLD, 24));
g2d.drawString("Go!", 0, 0);
}

if (gameEngine.gameStop) {
g2d.translate(75, 250);
g2d.setColor(Color.white);
g2d.setFont(new Font("SansSerif", Font.BOLD, 15));
g2d.drawString("Stopped. press Enter to restart.", 0, 0);
}

if (gameEngine.gameOver) {
g2d.translate(75, 250);
g2d.setColor(Color.white);
g2d.setFont(new Font("SansSerif", Font.BOLD, 15));
g2d.drawString("Game Over! press Enter to restart.", 0, 0);
}
}

}

private BlockType convertToBlockType(Piece.Type pieceType) {
Expand All @@ -146,7 +226,7 @@ private BlockType convertToBlockType(Piece.Type pieceType) {
return null;
}

private void drawBlock(Graphics g, BlockType type, int x, int y, boolean isLocked) {
private void drawBlock(Graphics g, BlockType type, int x, int y, float alphaValue) {
Graphics2D g2d = (Graphics2D) g;

g2d.translate(x, y);
Expand All @@ -161,8 +241,8 @@ private void drawBlock(Graphics g, BlockType type, int x, int y, boolean isLocke
ex.printStackTrace();
}

// ³z©ú«×¡A©|¥¼¸Ñ¨Mif
// (isLocked)g2d.setComposite(AlphaComposite.getInstance(0, 0.8f));
Composite alpha = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alphaValue);
g2d.setComposite(alpha);

g2d.drawImage(bgImage, null, null);
}
Expand Down
81 changes: 62 additions & 19 deletions edu/ncku/eddy/GameEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import java.util.Date;
import java.util.Random;

import javax.swing.JOptionPane;

import edu.ncku.eddy.game.component.Block;
import edu.ncku.eddy.game.component.Block.BlockType;
import edu.ncku.eddy.game.component.Field;
Expand All @@ -26,10 +24,15 @@ public class GameEngine {
public Randomizer getRandomizer() {
return randomizer;
}

//遊戲提示
public boolean gameReady;
public boolean gameGo;
public boolean gameStop;
public boolean gameOver;

private boolean gameRunning = false;
private boolean shouldRedraw;

private Thread gameThread;

// drop失敗兩次則lock
Expand All @@ -53,22 +56,25 @@ public GameEngine() {
}

public void startGame() {
gameStop = false;
gameOver = false;

gameField.reset();

// 種子碼
long seed = new Random().nextLong();


//初始化各數據
holdPiece = null;
pieceIndex = 0;
clearedLine = 0;
usedPieceCount = 0;

// 種子碼
long seed = new Random().nextLong();
randomizer = new Randomizer(seed);
getNewPiece();


gameThread = new GameDisplayThread(this);
gameRunning = true;
gameThread.start();

}

private void getNewPiece() {
Expand All @@ -80,23 +86,27 @@ private void getNewPiece() {
}

public void gameOver() {
gameOver = true;
Launcher.gameDisplay.update();
if (gameThread != null && gameThread.isAlive()) {
gameThread.interrupt();
}
gameRunning = false;
JOptionPane.showMessageDialog(null, "Game Over! Press Enter to restart.");

}

public void stopGame() {
gameStop=true;
Launcher.gameDisplay.update();
if (gameThread != null && gameThread.isAlive()) {
gameThread.interrupt();
}
gameRunning = false;
JOptionPane.showMessageDialog(null, "Game Stoped! Press Enter to restart.");

}

public void pause() {
// TODO:暫緩
// TODO:尚未實作
}

public void moveLeft() {
Expand All @@ -110,8 +120,10 @@ public void moveRight() {
}

public void rotatePiece(RotationMethod rotationMethod) {
if (currentPiece.rotatePiece(rotationMethod))
if (currentPiece.rotatePiece(rotationMethod)){
currentPiece.regenerateGhostPiece();
shouldRedraw = true;
}
}

public void hold() {
Expand Down Expand Up @@ -157,6 +169,13 @@ public void drop() {
shouldRedraw = true;
}

public void moveDown() {
if (currentPiece.moveDown()) {
shouldRedraw = true;
}

}

public void lockPiece() {

usedPieceCount++;
Expand Down Expand Up @@ -233,13 +252,35 @@ public GameDisplayThread(GameEngine targetEngine) {
public void run() {

time40L = "";
tickCount = 0;
tickCount = 0;
shouldRedraw = true;

//Ready? Go階段
gameReady=true;
Launcher.gameDisplay.update();
try {
Thread.sleep(800);
} catch (Exception e) {
e.printStackTrace();
}

gameGo=true;
gameReady=false;
Launcher.gameDisplay.update();
try {
Thread.sleep(800);
} catch (Exception e) {
e.printStackTrace();
}
gameGo=false;
Launcher.gameDisplay.update();

getNewPiece();
gameRunning = true;
startTimeMillis = System.currentTimeMillis();

// 遊戲迴圈
do {
tick();
tick();
} while (isGameRunning());

}
Expand All @@ -253,8 +294,8 @@ public void interrupt() {
public void tick() {
tickCount++;

// 每秒下降一次
if (tickCount - lastDrop > 100) {
// 每0.5秒下降一次
if (tickCount - lastDrop > 50) {
targetEngine.drop();
TestOutput.sysout("drop tick");
lastDrop = tickCount;
Expand Down Expand Up @@ -288,4 +329,6 @@ public void tick() {
}
}
}


}

0 comments on commit 2bc4911

Please sign in to comment.