?? blockcanvas.java
字號:
?import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import com.nec.media.AudioClip;
import com.nec.media.AudioListener;
import com.nec.media.Media;
/**
* Canvas for block game
*/
public class BlockCanvas extends Canvas implements Runnable, AudioListener {
// State-related
private int state; // State
private final int ACTIVE = 1;
private final int GAME_OVER = 2;
private final int CLEAR = 3;
// Block-related
private final int BLOCK_H = 7; // Number of horizontal blocks
private final int BLOCK_V = 5; // Number of vertical blocks
private final int BLOCK_WIDTH = getWidth() / BLOCK_H;
private final int BLOCK_HEIGHT = BLOCK_WIDTH / 2;
private boolean block[][] = new boolean[BLOCK_H][BLOCK_V];
private int blockCount; // Number of blocks
// Paddle-related (here referred to as a bar)
private final int BAR_HEIGHT = 11;
private final int BAR_WIDTH = 23;
private int barX = 0;
private int barY = getHeight() - BAR_HEIGHT;
private int barMoveX = 0;
// Ball-related
private final int BALL_HEIGHT = 10;
private final int BALL_WIDTH = 10;
private int ballX;
private int ballY;
private int ballMoveX = 5;
private int ballMoveY = 5;
private Thread th;
// Images
private Image barImg = null;
private Image ballImg = null;
private Image blockImg = null;
// Sounds
AudioClip bgm; // Background music
private AudioClip ballSound; // Sound of bouncing ball
private AudioClip blockSound; // Sound of destroying blocks
/********************************************
* Constructor
********************************************/
public BlockCanvas() {
//Loads the audio data
bgm = Media.getAudioClip("/bgm.mid");
ballSound = Media.getAudioClip("/ball.mid");
blockSound = Media.getAudioClip("/block.mid");
bgm.addAudioListener(this);
// Loads the images
try {
barImg = Image.createImage("/bar.png");
ballImg = Image.createImage("/ball.png");
blockImg = Image.createImage("/block.png");
} catch (Exception e) {
e.printStackTrace();
}
start(); // Starts the game
}
/**
* Starts the game
*/
public void start() {
// Initializes the ball
ballX = barX;
ballY = barY - BALL_HEIGHT;
// Initializes the blocks
for (int i = 0; i < BLOCK_H; i++) {
for (int j = 0; j < BLOCK_V; j++) {
block[i][j] = true;
}
}
blockCount = BLOCK_H * BLOCK_V;
// Starts the game
th = new Thread(this);
th.start(); // Starts threads
state = this.ACTIVE; // Sets the state to active
bgm.play(); // Plays the background music
}
/**
* Drawing methods
*/
protected void paint(Graphics g) {
// Clears the screen
g.setColor(255, 255, 255);
g.fillRect(0, 0, getWidth(), getHeight());
if (state == ACTIVE) {
// Block display
g.setColor(0, 0, 255);
for (int i = 0; i < BLOCK_H; i++) {
for (int j = 0; j < BLOCK_V; j++) {
if (block[i][j]) {
g.drawImage(
blockImg,
i * BLOCK_WIDTH,
(j + 1) * BLOCK_HEIGHT,
Graphics.LEFT | Graphics.TOP);
}
}
}
// Displays the ball
g.drawImage(ballImg, ballX, ballY, Graphics.LEFT | Graphics.TOP);
// Displays the paddle
g.drawImage(barImg, barX, barY, Graphics.LEFT | Graphics.TOP);
} else if (state == CLEAR) { // Clears the level
g.setColor(0, 0, 0);
g.drawString(
"GAME CLEAR!!!!",
getWidth() / 2,
getHeight() / 2,
Graphics.HCENTER | Graphics.BASELINE);
} else if (state == GAME_OVER) { // Ends the game
g.setColor(0, 0, 0);
g.drawString(
"GAME OVER....",
getWidth() / 2,
getHeight() / 2,
Graphics.HCENTER | Graphics.BASELINE);
}
}
/**
* Operation executed by threads
*/
public void run() {
while (state == ACTIVE) {
moveBall(); // Moves the ball
moveBar(); // Moves the paddle
repaint(); // Repaints them
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
break;
}
}
}
/**
// Moves the ball
*/
public void moveBall() {
ballX += ballMoveX;
ballY += ballMoveY;
// Bouncing
// Bounces off a wall
if (ballX < 0) {
ballMoveX *= -1;
ballX = 0;
// Plays the sound effect
ballSound.play();
} else if (getWidth() < ballX + BALL_HEIGHT) {
ballX = getWidth() - BALL_HEIGHT;
ballMoveX *= -1;
// Plays the sound effect
ballSound.play();
}
if (ballY < 0) {
ballMoveY *= -1;
ballY = 0;
// Plays the sound effect
ballSound.play();
} else if (ballY > getHeight()) { // If the ball passes through
// The game ends
state = GAME_OVER;
}
// Bounces off the paddle
if (ballY + BALL_HEIGHT > barY
&& ballX + BALL_WIDTH > barX
&& ballX < barX + BAR_WIDTH) {
ballMoveY *= -1;
ballY = barY - BALL_HEIGHT;
if (barMoveX < 0) {
ballMoveX -= 2;
} else if (barMoveX > 0) {
ballMoveX += 2;
}
// Plays the sound effect
ballSound.play();
}
// Hits a block and bounces off
for (int i = 0; i < BLOCK_H; i++) {
for (int j = 0; j < BLOCK_V; j++) {
if (block[i][j]) {
if (ballX + BALL_WIDTH > i * BLOCK_WIDTH
&& ballX < (i + 1) * BLOCK_WIDTH) {
if (ballY + BALL_HEIGHT > (j + 1) * BLOCK_HEIGHT
&& ballY < (j + 2) * BLOCK_HEIGHT) {
// Clears blocks
block[i][j] = false;
blockCount--;
ballMoveY *= -1;
// Plays the sound effect
blockSound.play();
// Checks whether or not the level has been cleared
if (blockCount == 0) {
state = CLEAR;
}
}
}
}
}
}
}
/**
* Moves the paddle
*/
public void moveBar() {
barX += barMoveX;
if (barX < 0) {
barX = 0;
} else if (barX + BAR_WIDTH > getWidth()) {
barX = getWidth() - BAR_WIDTH;
}
}
/*****************************************************
* Handling for audio events
*****************************************************/
public void audioAction(AudioClip sound, int event, int param) {
// Replays the loop
if (sound == bgm) {
if (event == AudioListener.AUDIO_COMPLETE) {
bgm.play();
}
}
}
/*****************************************
* Key event handling
*****************************************/
/**
* When a key is pressed
*/
protected void keyPressed(int key) {
if (state == ACTIVE) {
if (getGameAction(key) == Canvas.RIGHT) {
barMoveX = 6;
} else if (getGameAction(key) == Canvas.LEFT) {
barMoveX = -6;
}
repaint();
} else {
// Restart
this.start();
}
}
/**
* When a key is released
*/
protected void keyReleased(int key) {
barMoveX = 0;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -