package hujkay.game;
import hujkay.menu.menuTask;
import hujkay.* ;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import java.io.*;
import java.lang.* ;
import java.util.Timer;
public class GameCanvas extends Canvas implements CommandListener{
//背景图片的位置
private int backgroundx , backgroundy ;
// 16 ---- 96
// 17 ---- 157
// 16----96 80/10=8
///16---158 (142)/18=8
//双缓冲
private Image offscreen ;
private int currentblockx, currentblocky ;
//获取屏幕大小
private int wide , height ;
//当前的状态
private int currentstate = GAME_START;
private static int GAME_START = 0 ;
private static int GAME_PAUSE = 1 ;
private static int GAME_HIGHLIGHT = 2 ;
private static int GAME_EXIT = 3 ;
private static int GAME_ERR = 4 ;
private static int GAME_OVER = 5 ;
private Timer timer ;
private GameTask gametask ;
private Block block ;
//棋盘
Image imageground ;
//当前的棋子存放数组//6 * 64
private static int blocklibary[][] = {
{1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0, 0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0, 1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0, 0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,},//横竖
{1,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0, 0,0,1,0,1,1,1,0,0,0,0,0,0,0,0,0, 1,0,0,0,1,0,0,0,1,1,0,0,0,0,0,0, 0,1,1,1,0,1,0,0,0,0,0,0,0,0,0,0},
{0,1,0,0,0,1,0,0,1,1,0,0,0,0,0,0, 1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0, 1,1,0,0,1,0,0,0,1,0,0,0,0,0,0,0, 1,1,1,0,0,0,1,0,0,0,0,0,0,0,0,0},
{1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0, 0,1,0,0,1,1,0,0,1,0,0,0,0,0,0,0, 1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0, 0,1,0,0,1,1,0,0,1,0,0,0,0,0,0,0},//22
{1,0,0,0,1,1,0,0,0,1,0,0,0,0,0,0, 0,1,1,0,1,1,0,0,0,0,0,0,0,0,0,0, 1,0,0,0,1,1,0,0,0,1,0,0,0,0,0,0, 0,1,1,0,1,1,0,0,0,0,0,0,0,0,0,0},//22
{0,1,1,0,0,1,1,0,0,0,0,0,0,0,0,0, 0,1,1,0,0,1,1,0,0,0,0,0,0,0,0,0, 0,1,1,0,0,1,1,0,0,0,0,0,0,0,0,0, 0,1,1,0,0,1,1,0,0,0,0,0,0,0,0,0},//田
};
private int currentblocklibx= 0,currentblockliby= 0 ;
private int nextblocklibx = 0 , nextblockliby = 0;
private int currentblock[][] = new int[4][4] ;
private int nextblock[][] = new int[4][4] ;
// 用于存放棋盘10 * 18
private int chess[][] = new int[10][18];
private int chesscolor[][] = new int[10][18];
private int currentcolor = 0 ;
private int nextcolor = 0;
private boolean istimer = false ;
//分数
private int score = 0;
private int scoreup[] = {1,3,6,12,20} ;
private int level = 3 ;
private int Timercount = 0 ;
private int MAXCOLORCOUNT = 4 ;
private int colorlib[][]={{207,51,5},
{0,0,255},
{21,96,46},
{118,58,10},
};
private Command exitcommand = new Command("Exit",Command.EXIT,3) ;
private Command pausecommand = new Command("Pause",Command.STOP,1);
private Command startcommand = new Command("Start",Command.OK,1);
public void updatecurrentblock()
{
int i , j ;
for( i = 0 ; i < 4; i ++ )
for( j = 0 ; j < 4 ; j ++)
{
if(this.blocklibary[currentblocklibx][ this.currentblockliby * 16 + j + i * 4] == 1)
currentblock[i][j] = 1 ;
else
currentblock[i][j] = 0;
}
}
public int getscore()
{
return this.score ;
}
public boolean canturn()
{
int i , j , next;
next = ( currentblockliby + 1) % 4 ;
//判断是否出界
//左右下
if( currentblockx < 0)//左检测
{
for( i = 0; i < -this.currentblockx; i ++)
for( j = 0 ; j < 4 ; j ++)
{
if( blocklibary[currentblocklibx][next * 16 +j + 4 * i] == 1)
return false ;
}
}
if( currentblockx + 3 > 9)//右检测
{
for( i =10 - currentblockx ;i < 4; i ++ )
for( j = 0 ; j < 4 ; j ++ )
{
if(blocklibary[currentblocklibx][next*16 + i * 4 + j] == 1)
return false ;
}
}
if( currentblocky + 3 > 17)//下检测
{
for( i = 0 ; i < 4 ; i ++ )
for( j = 18 - currentblocky ; j < 4; j ++)
{
if( blocklibary[currentblocklibx][next * 16 + + 4 * i + j] == 1)
return false ;
}
}
//判断是否重合
for( j = 0; j < 4 ; j ++ ) //竖
for( i = 0 ; i < 4 ; i ++ )//横
{
if( this.currentblock[i][j] == 0)
continue ;
if( i + this.currentblockx < 0 || i + this.currentblockx > 9)
continue ;
if( j + next< 0 || j + next > 17)
continue ;
if(this.blocklibary[this.currentblocklibx][next * 16 + 4 * i + j] == 1 && chess[this.currentblockx + i][this.currentblocky + j] == 1)
return false ;
}
return true ;
}
public void turn()
{
currentblockliby = ( currentblockliby + 1 ) % 4 ;
if( canturn())
{
updatecurrentblock();
}
repaint();
}
public int convertx( int x)
{
return x*8 + 16 + backgroundx ;
}
public int converty( int y)
{
return y * 8 + backgroundy + 15;
}
public void clearCanvas(Graphics g )
{
g.setColor(0,240,240);
g.fillRect(0,0,wide,height);
}
public boolean readpictures()
{
try
{
imageground = Image.createImage("/block/game/background.png");
}
catch(Exception e)
{
e.printStackTrace();
return false ;
}
return true ;
}
public void startgame()
{
currentstate = this.GAME_START ;
repaint();
}
public void pausegame()
{
currentstate = this.GAME_PAUSE ;
repaint() ;
}
public void createnextblock()
{
long e = System.currentTimeMillis() ;
this.nextblocklibx = ( int )( e % 6) ;
e = System.currentTimeMillis() ;
this.nextblockliby = ( int ) (e % 4) ;
this.nextcolor = (int)(System.currentTimeMillis() % MAXCOLORCOUNT );
}
public void restorepre()
{
int i , j ;
for( j = 0 ;j < 4; j ++ )
for( i = 0; i < 4 ;i ++)
if( this.currentblock[j][i] == 1 )
{
this.chess[ i + this.currentblockx][ j + this.currentblocky] = 1 ;
this.chesscolor[i + this.currentblockx][j + this.currentblocky] = this.currentcolor ;
}
}
public void computescore()
{
int i , j , count = 0 , ii , jj;
for( j = 0; j < 18 ;j ++ )
{
for( i = 0; i < 10 ;i ++ )
if( chess[i][j] == 0 )
break ;
if( i == 10 )
{
count ++ ;
//上面的数据往下移动
for( ii = 0 ;ii < 10 ;ii ++ )
chess[ii][0] = 0 ;
for( jj = j ; jj > 0; jj -- )
for( ii = 0 ; ii < 10 ;ii ++)
{
chess[ii][jj] = chess[ii ][jj - 1] ;
this.chesscolor[ii][jj] = this.chesscolor[ii][jj - 1] ;
}
}
}
this.addscore(this.scoreup[count]);
}
public void drawlevel( Graphics g)
{
char Buffer[] = new char[10];
if( level == 4)
{
Buffer[0] = 'E' ;Buffer[1] = 'A';Buffer[2]='S';Buffer[3]='Y';Buffer[4]='\0';
}
else if( level == 2)
{
Buffer[0]='N';Buffer[1]='O';Buffer[2]='R';Buffer[3]='M';Buffer[4]='A';Buffer[5]='L';Buffer[6]='\0';
}
else
{
Buffer[0]='H' ;Buffer[1]='A';Buffer[2]='R';Buffer[3]='D';Buffer[4]='\0';
}
g.drawChars(Buffer,0,9,this.backgroundx + 125 , this.backgroundy + 55 , 0);
}
public void drawscore(Graphics g)
{
char Buffer[] = new char[7];
if( score < 0 ) score = 0 ;
if(score == 0)
{
Buffer[0] = '0' ; Buffer[1] = '\0' ;
}
else
{
int i = 0 , tmp = score , c = 0;
while ( tmp!=0 )
{
tmp = tmp / 10 ;
c ++ ;
}
tmp = score ; i = c ; Buffer[i] = '\0' ;
while( tmp != 0)
{
Buffer[--i] = ( char ) (tmp % 10 + '0' ) ;
tmp = tmp / 10 ;
}
}
g.drawChars(Buffer,0,7,124+this.backgroundx,33+this.backgroundy,0);
adjustlevel();
drawlevel( g) ;
}
public void adjustlevel()
{
if( score > 200)
level = 1 ;
else if( score > 100)
level = 2 ;
else level = 4 ;
}
public void drawnextblock( Graphics g)
{
int i , j ;
for( i = 0 ; i < 4; i ++ )
for( j = 0 ; j < 4 ; j ++)
{
if(this.blocklibary[nextblocklibx][ this.nextblockliby * 16 + j + i * 4] == 1)
nextblock[i][j] = 1 ;
else
nextblock[i][j] = 0;
}
g.setColor(this.colorlib[this.nextco
els.rar_JAVA俄罗斯方块
版权申诉
113 浏览量
2022-09-24
16:10:47
上传
评论
收藏 127KB RAR 举报
御道御小黑
- 粉丝: 61
- 资源: 1万+
最新资源
- 基于QT+C++的智能云监护仪项目,能够实时显示使用者心电、血氧、血压波形及其它各种参数+源码(毕业设计&课程设计&项目开发)
- 基于java开发的app接收硬件端传输的心音信号,具有显示心音波形,发出心音的功能+源码(毕业设计&课程设计&项目开发)
- Python 程序语言设计模式思路-行为型模式:职责链模式:将请求从一个处理者传递到下一个处理者
- 9241703124789646.16健身系统2.apk
- postgresql-16.3-1-windows-x64.exe
- Python 程序语言设计模式思路-结构型模式:装饰器讲解及利用Python装饰器模式实现高效日志记录和性能测试
- 基于YOLOv5和DeepSORT的多目标跟踪仿真与记录
- Python 程序语言设计模式思路-创建型模式:原型模式:通过复制现有对象来创建新对象,面向对象编程
- 卸载软件geek卸载软件geek
- Python 程序语言设计模式思路-创建型模式:单例模式,确保一个类的唯一实例(装饰器)面向对象编程、继承
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈