/*
* 创建日期 2005-6-30
*
* TODO 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
package game;
import java.util.Random;
import java.util.Timer;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.midlet.MIDletStateChangeException;
/**
* @author Administrator
*
* TODO 要更改此生成的类型注释的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
public class GameScreen extends Canvas implements CommandListener {
private final RussiaBoxMIDlet midlet;
private final Command exitCommand;
private final Command newGameCommand;
public Graphics graph;
private Random rand = new Random();
public Timer timer = new Timer();
public Square sq = new Square();
public boolean isGameOver = true;
public int screenWidth, screenHeight;
public boolean isNextOne = true;
public int SQUARE_AMOUNT = 8;
public int UNIT_SIZE = 15;
public int xFirstUnitPos = 4;
private int i = 0;
private int j = 0;
public Image fangkuai;
public int Score = 0;
public boolean IsFilled[][] = new boolean[SQUARE_AMOUNT][SQUARE_AMOUNT];
private static final int WHITE = 0x00FFFFFF;
/**
*
*/
public GameScreen(RussiaBoxMIDlet midlet) {
this.midlet = midlet;
// 获取屏幕大小
screenWidth = getWidth();
screenHeight = getHeight();
// 添加屏幕命令
exitCommand = new Command("退出", Command.EXIT, 1);
newGameCommand = new Command("开始", Command.SCREEN, 2);
addCommand(exitCommand);
addCommand(newGameCommand);
// 侦听命令按键
setCommandListener(this);
// 装载位图片段
try
{
fangkuai = Image.createImage("/fangkuai.png");
}
catch (Exception e) {}
}
/* (非 Javadoc)
* @see javax.microedition.lcdui.CommandListener#commandAction(javax.microedition.lcdui.Command, javax.microedition.lcdui.Displayable)
*/
public void commandAction(Command arg0, Displayable arg1) {
if (arg0 == exitCommand) {
timer.cancel();
isGameOver = true;
try {
// 退出
midlet.quit();
} catch (MIDletStateChangeException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
else if (arg0 == newGameCommand) {
// 开始游戏
initialize();
}
}
private void initialize() {
// 初始化数据
for (i = 0; i < SQUARE_AMOUNT; i++)
{
for (j = 0; j < SQUARE_AMOUNT; j++)
IsFilled[j][i] = false;
}
for (i = 0; i < 4; i++)
{
sq.ps_x[i] = sq.ps_y[i]= 0;
sq.type = 0;
}
Score = 0;
// 白色清空画布
graph.setColor(WHITE);
graph.fillRect(0, 0, screenWidth, screenHeight);
// 重绘屏幕
isGameOver = false;
isNextOne = true;
repaint();
}
public void paint(Graphics g) {
graph = g;
if (isGameOver == true)
return;
// 重绘
if (isNextOne == true)
OnNewSquare();
}
public void erasebackground(){
// 白色清空画布
graph.setColor(WHITE);
// 擦除上次停留位置
for (i = 0; i < 4; i++)
{
if (sq.ps_x[i] >= 0 && sq.ps_y[i] >= 0)
graph.fillRect(sq.ps_x[i] * UNIT_SIZE, sq.ps_y[i] * UNIT_SIZE, UNIT_SIZE, UNIT_SIZE);
}
}
// 生成方块
public void OnNewSquare(){
int id = (int)(rand.nextLong());
id = (int)(id % 6);
switch (id){
/* @@@@ */
case 0:
sq.type = 0;
for (i = 0; i < 4; i++)
{
sq.ps_x[i] = xFirstUnitPos + i;
sq.ps_y[i] = 0;
graph.drawImage(fangkuai, sq.ps_x[i] * UNIT_SIZE, sq.ps_y[i] * UNIT_SIZE, 0);
}
break;
/* @
@@@ */
case 1:
sq.type = 1;
for (i = 0; i < 4; i++)
{
if (i == 0)
{
// @
sq.ps_x[i] = xFirstUnitPos;
sq.ps_y[i] = 0;
graph.drawImage(fangkuai, sq.ps_x[i] * UNIT_SIZE, sq.ps_y[i] * UNIT_SIZE, 0);
}
else
{
// @@@
sq.ps_x[i] = xFirstUnitPos + i - 1;
sq.ps_y[i] = 1;
graph.drawImage(fangkuai, sq.ps_x[i] * UNIT_SIZE, sq.ps_y[i] * UNIT_SIZE, 0);
}
}
break;
/* @@
@@ */
case 2:
sq.type = 2;
for (i = 0; i < 4; i++)
{
if (i == 0 || i == 1)
{
// @@
sq.ps_x[i] = xFirstUnitPos + i;
sq.ps_y[i] = 0;
graph.drawImage(fangkuai,sq.ps_x[i] * UNIT_SIZE, sq.ps_y[i] * UNIT_SIZE, 0);
}
else
{
// @@
sq.ps_x[i] = xFirstUnitPos + i - 2;
sq.ps_y[i] = 1;
graph.drawImage(fangkuai,sq.ps_x[i] * UNIT_SIZE, sq.ps_y[i] * UNIT_SIZE, 0);
}
}
break;
/* @
@@@*/
case 3:
sq.type = 3;
for (i = 0; i < 4; i++)
{
if (i == 0)
{
// @
sq.ps_x[i] = xFirstUnitPos + 1;
sq.ps_y[i] = 0;
graph.drawImage(fangkuai, sq.ps_x[i] * UNIT_SIZE, sq.ps_y[i] * UNIT_SIZE, 0);
}
else
{
// @@@
sq.ps_x[i] = xFirstUnitPos + i - 1;
sq.ps_y[i] = 1;
graph.drawImage(fangkuai, sq.ps_x[i] * UNIT_SIZE, sq.ps_y[i] * UNIT_SIZE, 0);
}
}
break;
/* @@
@@ */
case 4:
sq.type = 4;
for (i = 0; i < 4; i++)
{
if (i == 0 || i == 1)
{
// @@
sq.ps_x[i] = xFirstUnitPos + i;
sq.ps_y[i] = 0;
graph.drawImage(fangkuai, sq.ps_x[i] * UNIT_SIZE, sq.ps_y[i] * UNIT_SIZE, 0);
}
else
{
// @@
sq.ps_x[i] = xFirstUnitPos + i - 1;
sq.ps_y[i] = 1;
graph.drawImage(fangkuai, sq.ps_x[i] * UNIT_SIZE, sq.ps_y[i] * UNIT_SIZE, 0);
}
}
break;
/* @@
@@ */
default:
sq.type = 5;
for (i = 0;i < 4; i++)
{
if (i == 0 || i == 1)
{
// @@
sq.ps_x[i] = xFirstUnitPos + i + 1;
sq.ps_y[i] = 0;
graph.drawImage(fangkuai, sq.ps_x[i] * UNIT_SIZE, sq.ps_y[i] * UNIT_SIZE, 0);
}
else
{
// @@
sq.ps_x[i] = xFirstUnitPos + i - 2;
sq.ps_y[i] = 1;
graph.drawImage(fangkuai, sq.ps_x[i] * UNIT_SIZE, sq.ps_y[i] * UNIT_SIZE, 0);
}
}
break;
}
// 开启定时器
Thinking thinking = new Thinking(this);
timer.schedule(thinking, 500, 500);
}
protected void keyPressed(int keyCode) {
if (isGameOver == true)
return;
// 得到按键动作
int gameAction = getGameAction(keyCode);
switch (gameAction) {
// 右移
case RIGHT:
// 进行右移可移性检测
for (i = 0; i < 4; i++)
{
if (sq.ps_x[i] + 1 > SQUARE_AMOUNT - 1 || IsFilled[sq.ps_x[i] + 1][sq.ps_y[i]] == true)
return;
}
// 擦除背景
erasebackground();
// 在新位置进行绘制
for (i = 0; i < 4; i++)
{
sq.ps_x[i] = sq.ps_x[i] + 1;
graph.drawImage(fangkuai, sq.ps_x[i] * UNIT_SIZE, sq.ps_y[i] * UNIT_SIZE, 0);
}
break;
// 左移
case LEFT:
// 进行左移可移性检测
for (i = 0; i < 4; i++)
{
if (sq.ps_x[i] - 1 < 0 || IsFilled[sq.ps_x[i] - 1][sq.ps_y[i]] == true)
return;
}
// 擦除背景
erasebackground();
// 在新位置进行绘制
for (i = 0;i < 4; i++)
{
sq.ps_x[i] = sq.ps_x[i] - 1;
graph.drawImage(fangkuai, sq.ps_x[i] * UNIT_SIZE, sq.ps_y[i] * UNIT_SIZE, 0);
}
break;
// 方块变形
case UP:
for (i = 0; i < 4; i++)
{
if (sq.ps_y[i] < 2)
return;
}
// 擦除背景
erasebackground();
// 确定每个方块的中轴方向
int shaft = 0;
switch(sq.type)
{
/* @#@@ */
case 0:
shaft = 1;
break;
/* @
#@@ */
case 1:
shaft = 1;
break;
/* @@
@@ */
case 2:
return;
/* @
@#@*/
case 3:
shaft = 2;
break;
/* @@
#@ */
case 4:
shaft = 2;
评论1