📄 gamehigh.java
字号:
import java.util.Random;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.GameCanvas;
public class GameHigh extends GameCanvas implements Runnable{
Graphics m_g;
String showinfo="";
GameData m_gamedata;
Random m_rand;
Thread m_thread;
int width=0;
int height=0;
int G_BOXWIDTH=0;
int G_BOXHEIGHT=0;
int G_BOXWIDTH2=0;
int G_BOXHEIGHT2=0;
byte style=0; //当前动态单元类型(典型)
int nextstyle=0; //当前预览单元类型(典型)
boolean GAMESTART=false;
public GameHigh(boolean arg0) {
super(arg0);
System.out.println("开始GameHigh");
m_rand = new Random(10000);
m_gamedata = new GameData();
GAMESTART=false;
width=0;
height=0;
G_BOXWIDTH=0;
G_BOXHEIGHT=0;
style=0; //当前动态单元类型(典型)
nextstyle=0; //当前预览单元类型(典型)
}
public void StartGame()
{
GAMESTART=true;
m_thread = new Thread(this);
System.out.println("开始Thead");
m_thread.start();
}
public void paint(Graphics g)
{
/*
showinfo=new Integer(height).toString();
g.setColor(255,255,255);
g.fillRect(0,0,width,height);
g.setColor(0,200,0);
g.drawString(showinfo,10,30,Graphics.LEFT | Graphics.TOP);
*/
}
public void run()
{
m_gamedata.InitRusHigh();
nextstyle=m_rand.nextInt(); //0~6预览典型方块类型,生成下次的单元类型
if(nextstyle<0)
nextstyle=-nextstyle;
nextstyle%=7;
style=(byte)nextstyle;
nextstyle=m_rand.nextInt(); //0~6预览典型方块类型,生成下次的单元类型
if(nextstyle<0)
nextstyle=-nextstyle;
nextstyle%=7;
m_gamedata.setnowstyle((byte)style);
m_gamedata.SetCenterinfo(m_gamedata.gameboxab[style][0],
m_gamedata.gameboxab[style][1],
m_gamedata.boxindex[style]==2?1:m_gamedata.boxindex[style]);
m_g=this.getGraphics();
width=this.getWidth();
height=this.getHeight();
G_BOXHEIGHT=height/22;
G_BOXWIDTH=G_BOXHEIGHT;
G_BOXWIDTH2=(width-G_BOXWIDTH*12)/5;
G_BOXHEIGHT2=G_BOXWIDTH2;
for(;;)
{
boolean nowtodown=false;
int keystate=this.getKeyStates();
if((keystate & LEFT_PRESSED)!=0)
{
//单元左移
if(m_gamedata.checkleft()==true)
{
m_gamedata.SetToLeft();
}
}
if((keystate & RIGHT_PRESSED)!=0)
{
//单元右移
if(m_gamedata.checkright()==true)
{
m_gamedata.SetToRight();
}
}
if((keystate & DOWN_PRESSED)!=0)
{
//单元下降
if(m_gamedata.checkdown()==true)
{
nowtodown=true;
m_gamedata.SetToDown();
}
}
if((keystate & UP_PRESSED)!=0)
{
//单元变形
if(m_gamedata.checkround()==true)
{
m_gamedata.SetToRound();
}
}
if(m_gamedata.checkover()==true)
{//游戏结束检测
GAMESTART=false;
m_g.setColor(0,0,0);
m_g.fillRect(0,0,width,height);
m_g.setColor(255,255,0);
m_g.drawString("游戏结束",80,100,Graphics.LEFT | Graphics.TOP);
m_g.drawString("按开始键重新新的游戏",30,120,Graphics.LEFT | Graphics.TOP);
this.flushGraphics();
break;
}
else
{//游戏没有结束
if(m_gamedata.checkdown()==true)
{//单元允许下降
m_gamedata.SetToDown();
}
else
{//单元落地
m_gamedata.setbindown(); //落地设置
if(m_gamedata.checkline()==true) //检测收行
{
System.out.println("LINEOK");
}
style=(byte)nextstyle;
nextstyle=m_rand.nextInt(); //0~6预览典型方块类型,生成下次的单元类型
if(nextstyle<0)
nextstyle=-nextstyle;
nextstyle%=7;
m_gamedata.setnowstyle((byte)style);
m_gamedata.SetCenterinfo(m_gamedata.gameboxab[style][0],
m_gamedata.gameboxab[style][1],
m_gamedata.boxindex[style]==2?1:m_gamedata.boxindex[style]);
}
}
//绘制背景
m_g.setColor(255,255,255);
m_g.fillRect(0,0,width,height);
m_g.setColor(0,0,128);
m_g.drawRect(G_BOXWIDTH-1,G_BOXHEIGHT-1,G_BOXWIDTH*10+1,G_BOXHEIGHT*20+1);
m_g.setColor(0,0,128);
m_g.drawRect(G_BOXWIDTH+G_BOXWIDTH*11,G_BOXHEIGHT-1,G_BOXWIDTH2*4+5,G_BOXHEIGHT2*6+1);
//绘制预览
if(nextstyle>=0 && nextstyle<=6)
{
int x0=G_BOXWIDTH*13+13;
int y0=G_BOXHEIGHT*3+10;
int boxstyle=m_gamedata.boxindex[nextstyle];//得到预览的典型方块类型
m_g.setColor(0,0,128);
for(int i=0;i<4;i++) //显示典型方块类型的每个子方块
{
int x1=m_gamedata.m_cell[boxstyle].ab[i][0];
int y1=m_gamedata.m_cell[boxstyle].ab[i][1];
x1=x0+x1*G_BOXWIDTH2; //大多数方框类型的坐标偏移
y1=y0+y1*G_BOXHEIGHT2;
m_g.fillRect(x1,y1,G_BOXWIDTH2,G_BOXHEIGHT2);
}
}
//绘制游戏区
for(int x=1;x<=10;x++)
{
for(int y=1;y<=20;y++)
{
if(m_gamedata.bin1[y][x]>=1 && m_gamedata.bin1[y][x]<=7)
{
int x1=x*G_BOXWIDTH;
int y1=y*G_BOXHEIGHT;
m_g.setColor(128,128,128);
m_g.fillRect(x1,y1,G_BOXWIDTH,G_BOXHEIGHT);
}
if(m_gamedata.bin2[y][x]>=1 && m_gamedata.bin2[y][x]<=7)
{
switch(m_gamedata.bin2[y][x])
{
case 1:m_g.setColor(0,0,155);break;
case 2:m_g.setColor(0,155,155);break;
case 3:m_g.setColor(155,0,155);break;
case 4:m_g.setColor(155,155,0);break;
case 5:m_g.setColor(0,155,0);break;
case 6:m_g.setColor(155,0,0);break;
case 7:m_g.setColor(128,0,128);break;
default:m_g.setColor(128,0,0);break;
}
int x1=x*G_BOXWIDTH;
int y1=y*G_BOXHEIGHT;
m_g.fillRect(x1,y1,G_BOXWIDTH,G_BOXHEIGHT);
}
}
}
this.flushGraphics();
try
{
if(nowtodown==false)
{
Thread.sleep(120);
}
else
{
Thread.sleep(30);
}
}
catch(InterruptedException ie){}
}
}
protected void keyPressed(int keyCode)
{
showinfo=this.getKeyName(keyCode);
if(showinfo.compareTo("SOFT2")==0 && GAMESTART==false)
this.StartGame();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -