java游戲俄羅斯方塊-九游会j9娱乐平台
ⅰ java的俄羅斯方塊代碼
俄羅斯方塊——java源代碼提供
import java.awt.*;
import java.awt.event.*;
//俄羅斯方塊類
public class ers_block extends frame{
public static boolean isplay=false;
public static int level=1,score=0;
public static textfield scorefield,levelfield;
public static mytimer timer;
gamecanvas gamescr;
public static void main(string[] argus){
ers_block ers = new ers_block("俄羅斯方塊游戲 v1.0 author:vincent");
windowlistener win_listener = new winlistener();
ers.addwindowlistener(win_listener);
}
//俄羅斯方塊類的構造方法
ers_block(string title){
super(title);
setsize(600,480);
setlayout(new gridlayout(1,2));
gamescr = new gamecanvas();
gamescr.addkeylistener(gamescr);
timer = new mytimer(gamescr);
timer.setdaemon(true);
timer.start();
timer.suspend();
add(gamescr);
panel rightscr = new panel();
rightscr.setlayout(new gridlayout(2,1,0,30));
rightscr.setsize(120,500);
add(rightscr);
//右邊信息窗體的布局
mypanel infoscr = new mypanel();
infoscr.setlayout(new gridlayout(4,1,0,5));
infoscr.setsize(120,300);
rightscr.add(infoscr);
//定義標簽和初始值
label scorep = new label("分數:",label.left);
label levelp = new label("級數:",label.left);
scorefield = new textfield(8);
levelfield = new textfield(8);
scorefield.seteditable(false);
levelfield.seteditable(false);
infoscr.add(scorep);
infoscr.add(scorefield);
infoscr.add(levelp);
infoscr.add(levelfield);
scorep.setsize(new dimension(20,60));
scorefield.setsize(new dimension(20,60));
levelp.setsize(new dimension(20,60));
levelfield.setsize(new dimension(20,60));
scorefield.settext("0");
levelfield.settext("1");
//右邊控制按鈕窗體的布局
mypanel controlscr = new mypanel();
controlscr.setlayout(new gridlayout(5,1,0,5));
rightscr.add(controlscr);
//定義按鈕play
button play_b = new button("開始游戲");
play_b.setsize(new dimension(50,200));
play_b.addactionlistener(new command(command.button_play,gamescr));
//定義按鈕level up
button level_up_b = new button("提高級數");
level_up_b.setsize(new dimension(50,200));
level_up_b.addactionlistener(new command(command.button_levelup,gamescr));
//定義按鈕level down
button level_down_b =new button("降低級數");
level_down_b.setsize(new dimension(50,200));
level_down_b.addactionlistener(new command(command.button_leveldown,gamescr));
//定義按鈕level pause
button pause_b =new button("游戲暫停");
pause_b.setsize(new dimension(50,200));
pause_b.addactionlistener(new command(command.button_pause,gamescr));
//定義按鈕quit
button quit_b = new button("退出遊戲");
quit_b.setsize(new dimension(50,200));
quit_b.addactionlistener(new command(command.button_quit,gamescr));
controlscr.add(play_b);
controlscr.add(level_up_b);
controlscr.add(level_down_b);
controlscr.add(pause_b);
controlscr.add(quit_b);
setvisible(true);
gamescr.requestfocus();
}
}
//重寫mypanel類,使panel的四周留空間
class mypanel extends panel{
public insets getinsets(){
return new insets(30,50,30,50);
}
}
//游戲畫布類
class gamecanvas extends canvas implements keylistener{
final int unitsize = 30; //小方塊邊長
int rownum; //正方格的行數
int columnnum; //正方格的列數
int maxallowrownum; //允許有多少行未削
int blockinitrow; //新出現塊的起始行坐標
int blockinitcol; //新出現塊的起始列坐標
int [][] scrarr; //屏幕數組
block b; //對方快的引用
//畫布類的構造方法
gamecanvas(){
rownum = 15;
columnnum = 10;
maxallowrownum = rownum - 2;
b = new block(this);
blockinitrow = rownum - 1;
blockinitcol = columnnum/2 - 2;
scrarr = new int [32][32];
}
//初始化屏幕,並將屏幕數組清零的方法
void initscr(){
for(int i=0;i
b.reset();
repaint();
}
//重新刷新畫布方法
public void paint(graphics g){
for(int i = 0; i < rownum; i )
for(int j = 0; j < columnnum; j )
drawunit(i,j,scrarr[j]);
}
//畫方塊的方法
public void drawunit(int row,int col,int type){
scrarr[row][col] = type;
graphics g = getgraphics();
tch(type){ //表示畫方快的方法
case 0: g.setcolor(color.black);break; //以背景為顏色畫
case 1: g.setcolor(color.blue);break; //畫正在下落的方塊
case 2: g.setcolor(color.magenta);break; //畫已經落下的方法
}
g.fill3drect(col*unitsize,getsize().height-(row 1)*unitsize,unitsize,unitsize,true);
g.dispose();
}
public block getblock(){
return b; //返回block實例的引用
}
//返回屏幕數組中(row,col)位置的屬性值
public int getscrarrxy(int row,int col){
if (row < 0 || row >= rownum || col < 0 || col >= columnnum)
return(-1);
else
return(scrarr[row][col]);
}
//返回新塊的初始行坐標方法
public int getinitrow(){
return(blockinitrow); //返回新塊的初始行坐標
}
//返回新塊的初始列坐標方法
public int getinitcol(){
return(blockinitcol); //返回新塊的初始列坐標
}
//滿行刪除方法
void deletefullline(){
int full_line_num = 0;
int k = 0;
for (int i=0;i
l1:for(int j=0;j
k ;
isfull = false;
break l1;
}
if(isfull) full_line_num ;
if(k!=0 && k-1!=i && !isfull)
for(int j = 0; j < columnnum; j ){
if (scrarr[j] == 0)
drawunit(k-1,j,0);
else
drawunit(k-1,j,2);
scrarr[k-1][j] = scrarr[j];
}
}
for(int i = k-1 ;i < rownum; i ){
for(int j = 0; j < columnnum; j ){
drawunit(i,j,0);
scrarr[j]=0;
}
}
ers_block.score = full_line_num;
ers_block.scorefield.settext("" ers_block.score);
}
//判斷游戲是否結束方法
boolean isgameend(){
for (int col = 0 ; col
return true;
}
return false;
}
public void keytyped(keyevent e){
}
public void keyreleased(keyevent e){
}
//處理鍵盤輸入的方法
public void keypressed(keyevent e){
if(!ers_block.isplay)
return;
tch(e.getkeycode()){
case keyevent.vk_down:b.falldown();break;
case keyevent.vk_left:b.leftmove();break;
case keyevent.vk_right:b.rightmove();break;
case keyevent.vk_space:b.leftturn();break;
}
}
}
//處理控制類
class command implements actionlistener{
static final int button_play = 1; //給按鈕分配編號
static final int button_levelup = 2;
static final int button_leveldown = 3;
static final int button_quit = 4;
static final int button_pause = 5;
static boolean pause_resume = true;
int curbutton; //當前按鈕
gamecanvas scr;
//控制按鈕類的構造方法
command(int button,gamecanvas scr){
curbutton = button;
this.scr=scr;
}
//按鈕執行方法
public void actionperformed (actionevent e){
tch(curbutton){
case button_play:if(!ers_block.isplay){
scr.initscr();
ers_block.isplay = true;
ers_block.score = 0;
ers_block.scorefield.settext("0");
ers_block.timer.resume();
}
scr.requestfocus();
break;
case button_levelup:if(ers_block.level < 10){
ers_block.level ;
ers_block.levelfield.settext("" ers_block.level);
ers_block.score = 0;
ers_block.scorefield.settext("" ers_block.score);
}
scr.requestfocus();
break;
case button_leveldown:if(ers_block.level > 1){
ers_block.level--;
ers_block.levelfield.settext("" ers_block.level);
ers_block.score = 0;
ers_block.scorefield.settext("" ers_block.score);
}
scr.requestfocus();
break;
case button_pause:if(pause_resume){
ers_block.timer.suspend();
pause_resume = false;
}else{
ers_block.timer.resume();
pause_resume = true;
}
scr.requestfocus();
break;
case button_quit:system.exit(0);
}
}
}
//方塊類
class block {
static int[][] pattern = {
{0x0f00,0x4444,0x0f00,0x4444},//用十六進至表示,本行表示長條四種狀態
{0x04e0,0x0464,0x00e4,0x04c4},
{0x4620,0x6c00,0x4620,0x6c00},
{0x2640,0xc600,0x2640,0xc600},
{0x6220,0x1700,0x2230,0x0740},
{0x6440,0x0e20,0x44c0,0x8e00},
{0x0660,0x0660,0x0660,0x0660}
};
int blocktype; //塊的模式號(0-6)
int turnstate; //塊的翻轉狀態(0-3)
int blockstate; //快的下落狀態
int row,col; //塊在畫布上的坐標
gamecanvas scr;
//塊類的構造方法
block(gamecanvas scr){
this.scr = scr;
blocktype = (int)(math.random() * 1000)%7;
turnstate = (int)(math.random() * 1000)%4;
blockstate = 1;
row = scr.getinitrow();
col = scr.getinitcol();
}
//重新初始化塊,並顯示新塊
public void reset(){
blocktype = (int)(math.random() * 1000)%7;
turnstate = (int)(math.random() * 1000)%4;
blockstate = 1;
row = scr.getinitrow();
col = scr.getinitcol();
dispblock(1);
}
//實現「塊」翻轉的方法
public void leftturn(){
if(assertvalid(blocktype,(turnstate 1)%4,row,col)){
dispblock(0);
turnstate = (turnstate 1)%4;
dispblock(1);
}
}
//實現「塊」的左移的方法
public void leftmove(){
if(assertvalid(blocktype,turnstate,row,col-1)){
dispblock(0);
col--;
dispblock(1);
}
}
//實現塊的右移
public void rightmove(){
if(assertvalid(blocktype,turnstate,row,col 1)){
dispblock(0);
col ;
dispblock(1);
}
}
//實現塊落下的操作的方法
public boolean falldown(){
if(blockstate == 2)
return(false);
if(assertvalid(blocktype,turnstate,row-1,col)){
dispblock(0);
row--;
dispblock(1);
return(true);
}else{
blockstate = 2;
dispblock(2);
return(false);
}
}
//判斷是否正確的方法
boolean assertvalid(int t,int s,int row,int col){
int k = 0x8000;
for(int i = 0; i < 4; i ){
for(int j = 0; j < 4; j ){
if((int)(pattern[t][s]&k) != 0){
int temp = scr.getscrarrxy(row-i,col j);
if (temp<0||temp==2)
return false;
}
k = k >> 1;
}
}
return true;
}
//同步顯示的方法
public synchronized void dispblock(int s){
int k = 0x8000;
for (int i = 0; i < 4; i ){
for(int j = 0; j < 4; j ){
if(((int)pattern[blocktype][turnstate]&k) != 0){
scr.drawunit(row-i,col j,s);
}
k=k>>1;
}
}
}
}
//定時線程
class mytimer extends thread{
gamecanvas scr;
public mytimer(gamecanvas scr){
this.scr = scr;
}
public void run(){
while(true){
try{
sleep((10-ers_block.level 1)*100);
}
catch(interruptedexception e){}
if(!scr.getblock().falldown()){
scr.deletefullline();
if(scr.isgameend()){
ers_block.isplay = false;
suspend();
}else
scr.getblock().reset();
}
}
}
}
class winlistener extends windowadapter{
public void windowclosing (windowevent l){
system.exit(0);
}
}
ⅱ java 淇勭綏鏂鏂瑰潡
import java.awt.*;
import java.awt.event.*;
//淇勭綏鏂鏂瑰潡綾
public class mytest extends frame{
public static boolean isplay=false;
public static int level=1,score=0;
public static textfield scorefield,levelfield;
public static mytimer timer;
gamecanvas gamescr;
public static void main(string[] argus){
mytest ers = new mytest("淇勭綏鏂鏂瑰潡娓告垙");
windowlistener win_listener = new winlistener();
ers.addwindowlistener(win_listener);
}
// 淇勭綏鏂鏂瑰潡綾葷殑鏋勯犳柟娉
mytest(string title){
super(title);
setsize(600,480);
setlayout(new gridlayout(1,2));
gamescr = new gamecanvas();
gamescr.addkeylistener(gamescr);
timer = new mytimer(gamescr);
timer.setdaemon(true);
timer.start();
timer.suspend();
add(gamescr);
panel rightscr = new panel();
rightscr.setlayout(new gridlayout(2,1,0,30));
rightscr.setsize(120,500);
add(rightscr);
// 鍙寵竟淇℃伅紿椾綋鐨勫竷灞
mypanel infoscr = new mypanel();
infoscr.setlayout(new gridlayout(4,1,0,5));
infoscr.setsize(120,300);
rightscr.add(infoscr);
// 瀹氫箟鏍囩懼拰鍒濆嬪
label scorep = new label("鍒嗘暟:",label.left);
label levelp = new label("綰ф暟:",label.left);
scorefield = new textfield(8);
levelfield = new textfield(8);
scorefield.seteditable(false);
levelfield.seteditable(false);
infoscr.add(scorep);
infoscr.add(scorefield);
infoscr.add(levelp);
infoscr.add(levelfield);
scorep.setsize(new dimension(20,60));
scorefield.setsize(new dimension(20,60));
levelp.setsize(new dimension(20,60));
levelfield.setsize(new dimension(20,60));
scorefield.settext("0");
levelfield.settext("1");
// 鍙寵竟鎺у埗鎸夐挳紿椾綋鐨勫竷灞
mypanel controlscr = new mypanel();
controlscr.setlayout(new gridlayout(5,1,0,5));
rightscr.add(controlscr);
// 瀹氫箟鎸夐挳play
button play_b = new button("寮濮嬫父鎴");
play_b.setsize(new dimension(50,200));
play_b.addactionlistener(new command(command.button_play,gamescr));
// 瀹氫箟鎸夐挳level up
button level_up_b = new button("鎻愰珮綰ф暟");
level_up_b.setsize(new dimension(50,200));
level_up_b.addactionlistener(new command(command.button_levelup,gamescr));
// 瀹氫箟鎸夐挳level down
button level_down_b =new button("闄嶄綆綰ф暟");
level_down_b.setsize(new dimension(50,200));
level_down_b.addactionlistener(new command(command.button_leveldown,gamescr));
// 瀹氫箟鎸夐挳level pause
button pause_b =new button("娓告垙鏆傚仠");
pause_b.setsize(new dimension(50,200));
pause_b.addactionlistener(new command(command.button_pause,gamescr));
// 瀹氫箟鎸夐挳quit
button quit_b = new button("閫鍑烘父鎴");
quit_b.setsize(new dimension(50,200));
quit_b.addactionlistener(new command(command.button_quit,gamescr));
controlscr.add(play_b);
controlscr.add(level_up_b);
controlscr.add(level_down_b);
controlscr.add(pause_b);
controlscr.add(quit_b);
setvisible(true);
gamescr.requestfocus();
}
}
//閲嶅啓mypanel綾伙紝浣縋anel鐨勫洓鍛ㄧ暀絀洪棿
class mypanel extends panel{
public insets getinsets(){
return new insets(30,50,30,50);
}
}
//娓告垙鐢誨竷綾
class gamecanvas extends canvas implements keylistener{
final int unitsize = 30; //灝忔柟鍧楄竟闀
int rownum; //姝f柟鏍肩殑琛屾暟
int columnnum; //姝f柟鏍肩殑鍒楁暟
int maxallowrownum; //鍏佽告湁澶氬皯琛屾湭鍓
int blockinitrow; //鏂板嚭鐜板潡鐨勮搗濮嬭屽潗鏍
int blockinitcol; //鏂板嚭鐜板潡鐨勮搗濮嬪垪鍧愭爣
int [][] scrarr; //灞忓箷鏁扮粍
block b; //瀵規柟蹇鐨勫紩鐢
// 鐢誨竷綾葷殑鏋勯犳柟娉
gamecanvas(){
rownum = 15;
columnnum = 10;
maxallowrownum = rownum - 2;
b = new block(this);
blockinitrow = rownum - 1;
blockinitcol = columnnum/2 - 2;
scrarr = new int [32][32];
}
// 鍒濆嬪寲灞忓箷錛屽苟灝嗗睆騫曟暟緇勬竻闆剁殑鏂規硶
void initscr(){
for(int i=0;i
b.reset();
repaint();
}
// 閲嶆柊鍒鋒柊鐢誨竷鏂規硶
public void paint(graphics g){
for(int i = 0; i < rownum; i )
for(int j = 0; j < columnnum; j )
drawunit(i,j,scrarr[i][j]);
}
// 鐢繪柟鍧楃殑鏂規硶
public void drawunit(int row,int col,int type){
scrarr[row][col] = type;
graphics g = getgraphics();
switch(type){ //琛ㄧず鐢繪柟蹇鐨勬柟娉
case 0: g.setcolor(color.black);break; //浠ヨ儗鏅涓洪滆壊鐢
case 1: g.setcolor(color.blue);break; //鐢繪e湪涓嬭惤鐨勬柟鍧
case 2: g.setcolor(color.magenta);break; //鐢誨凡緇忚惤涓嬬殑鏂規硶
}
g.fill3drect(col*unitsize,getsize().height-(row 1)*unitsize,unitsize,unitsize,true);
g.dispose();
}
public block getblock(){
return b; //榪斿洖block瀹炰緥鐨勫紩鐢
}
// 榪斿洖灞忓箷鏁扮粍涓(row,col)浣嶇疆鐨勫睘鎬у
public int getscrarrxy(int row,int col){
if (row < 0 || row >= rownum || col < 0 || col >= columnnum)
return(-1);
else
return(scrarr[row][col]);
}
// 榪斿洖鏂板潡鐨勫垵濮嬭屽潗鏍囨柟娉
public int getinitrow(){
return(blockinitrow); //榪斿洖鏂板潡鐨勫垵濮嬭屽潗鏍
}
// 榪斿洖鏂板潡鐨勫垵濮嬪垪鍧愭爣鏂規硶
public int getinitcol(){
return(blockinitcol); //榪斿洖鏂板潡鐨勫垵濮嬪垪鍧愭爣
}
// 婊¤屽垹闄ゆ柟娉
void deletefullline(){
int full_line_num = 0;
int k = 0;
for (int i=0;i
l1:for(int j=0;j
k ;
isfull = false;
break l1;
}
if(isfull) full_line_num ;
if(k!=0 && k-1!=i && !isfull)
for(int j = 0; j < columnnum; j ){
if (scrarr[i][j] == 0)
drawunit(k-1,j,0);
else
drawunit(k-1,j,2);
scrarr[k-1][j] = scrarr[i][j];
}
}
for(int i = k-1 ;i < rownum; i ){
for(int j = 0; j < columnnum; j ){
drawunit(i,j,0);
scrarr[i][j]=0;
}
}
mytest.score = full_line_num;
mytest.scorefield.settext("" mytest.score);
}
// 鍒ゆ柇娓告垙鏄鍚︾粨鏉熸柟娉
boolean isgameend(){
for (int col = 0 ; col
return true;
}
return false;
}
public void keytyped(keyevent e){
}
public void keyreleased(keyevent e){
}
// 澶勭悊閿鐩樿緭鍏ョ殑鏂規硶
public void keypressed(keyevent e){
if(!mytest.isplay)
return;
switch(e.getkeycode()){
case keyevent.vk_down:b.falldown();break;
case keyevent.vk_left:b.leftmove();break;
case keyevent.vk_right:b.rightmove();break;
case keyevent.vk_space:b.leftturn();break;
}
}
}
//澶勭悊鎺у埗綾
class command implements actionlistener{
static final int button_play = 1; //緇欐寜閽鍒嗛厤緙栧彿
static final int button_levelup = 2;
static final int button_leveldown = 3;
static final int button_quit = 4;
static final int button_pause = 5;
static boolean pause_resume = true;
int curbutton; //褰撳墠鎸夐挳
gamecanvas scr;
// 鎺у埗鎸夐挳綾葷殑鏋勯犳柟娉
command(int button,gamecanvas scr){
curbutton = button;
this.scr=scr;
}
// 鎸夐挳鎵ц屾柟娉
public void actionperformed (actionevent e){
switch(curbutton){
case button_play:if(!mytest.isplay){
scr.initscr();
mytest.isplay = true;
mytest.score = 0;
mytest.scorefield.settext("0");
mytest.timer.resume();
}
scr.requestfocus();
break;
case button_levelup:if(mytest.level < 10){
mytest.level ;
mytest.levelfield.settext("" mytest.level);
mytest.score = 0;
mytest.scorefield.settext("" mytest.score);
}
scr.requestfocus();
break;
case button_leveldown:if(mytest.level > 1){
mytest.level--;
mytest.levelfield.settext("" mytest.level);
mytest.score = 0;
mytest.scorefield.settext("" mytest.score);
}
scr.requestfocus();
break;
case button_pause:if(pause_resume){
mytest.timer.suspend();
pause_resume = false;
}else{
mytest.timer.resume();
pause_resume = true;
}
scr.requestfocus();
break;
case button_quit:system.exit(0);
}
}
}
//鏂瑰潡綾
class block {
static int[][] pattern = {
{0x0f00,0x4444,0x0f00,0x4444},//鐢ㄥ嶮鍏榪涜嚦琛ㄧず錛屾湰琛岃〃紺洪暱鏉″洓縐嶇姸鎬
{0x04e0,0x0464,0x00e4,0x04c4},
{0x4620,0x6c00,0x4620,0x6c00},
{0x2640,0xc600,0x2640,0xc600},
{0x6220,0x1700,0x2230,0x0740},
{0x6440,0x0e20,0x44c0,0x8e00},
{0x0660,0x0660,0x0660,0x0660}
};
int blocktype; //鍧楃殑妯″紡鍙鳳紙0-6錛
int turnstate; //鍧楃殑緲昏漿鐘舵侊紙0-3錛
int blockstate; //蹇鐨勪笅钀界姸鎬
int row,col; //鍧楀湪鐢誨竷涓婄殑鍧愭爣
gamecanvas scr;
// 鍧楃被鐨勬瀯閫犳柟娉
block(gamecanvas scr){
this.scr = scr;
blocktype = (int)(math.random() * 1000)%7;
turnstate = (int)(math.random() * 1000)%4;
blockstate = 1;
row = scr.getinitrow();
col = scr.getinitcol();
}
// 閲嶆柊鍒濆嬪寲鍧楋紝騫舵樉紺烘柊鍧
public void reset(){
blocktype = (int)(math.random() * 1000)%7;
turnstate = (int)(math.random() * 1000)%4;
blockstate = 1;
row = scr.getinitrow();
col = scr.getinitcol();
dispblock(1);
}
// 瀹炵幇鈥滃潡鈥濈炕杞鐨勬柟娉
public void leftturn(){
if(assertvalid(blocktype,(turnstate 1)%4,row,col)){
dispblock(0);
turnstate = (turnstate 1)%4;
dispblock(1);
}
}
// 瀹炵幇鈥滃潡鈥濈殑宸︾щ鐨勬柟娉
public void leftmove(){
if(assertvalid(blocktype,turnstate,row,col-1)){
dispblock(0);
col--;
dispblock(1);
}
}
// 瀹炵幇鍧楃殑鍙崇щ
public void rightmove(){
if(assertvalid(blocktype,turnstate,row,col 1)){
dispblock(0);
col ;
dispblock(1);
}
}
// 瀹炵幇鍧楄惤涓嬬殑鎿嶄綔鐨勬柟娉
public boolean falldown(){
if(blockstate == 2)
return(false);
if(assertvalid(blocktype,turnstate,row-1,col)){
dispblock(0);
row--;
dispblock(1);
return(true);
}else{
blockstate = 2;
dispblock(2);
return(false);
}
}
// 鍒ゆ柇鏄鍚︽g『鐨勬柟娉
boolean assertvalid(int t,int s,int row,int col){
int k = 0x8000;
for(int i = 0; i < 4; i ){
for(int j = 0; j < 4; j ){
if((int)(pattern[t][s]&k) != 0){
int temp = scr.getscrarrxy(row-i,col j);
if (temp<0||temp==2)
return false;
}
k = k >> 1;
}
}
return true;
}
// 鍚屾ユ樉紺虹殑鏂規硶
public synchronized void dispblock(int s){
int k = 0x8000;
for (int i = 0; i < 4; i ){
for(int j = 0; j < 4; j ){
if(((int)pattern[blocktype][turnstate]&k) != 0){
scr.drawunit(row-i,col j,s);
}
k=k>>1;
}
}
}
}
//瀹氭椂綰跨▼
class mytimer extends thread{
gamecanvas scr;
public mytimer(gamecanvas scr){
this.scr = scr;
}
public void run(){
while(true){
try{
sleep((10-mytest.level 1)*100);
}
catch(interruptedexception e){}
if(!scr.getblock().falldown()){
scr.deletefullline();
if(scr.isgameend()){
mytest.isplay = false;
suspend();
}else
scr.getblock().reset();
}
}
}
}
class winlistener extends windowadapter{
public void windowclosing (windowevent l){
system.exit(0);
}
}
ⅲ 急需一份俄羅斯方塊源代碼,java寫的
使用java實現小游戲:俄羅斯方塊
使用一個二維數組保存游戲的地圖:
//游戲地圖格子,每個格子保存一個方塊,數組紀錄方塊的狀態
privatestatemap[][]=newstate[rows][columns];123
游戲前先將所有地圖中的格子初始化為空:
/*初始化所有的方塊為空*/
for(inti=0;ifor(intj=0;j
ⅳ 求用java編寫俄羅斯方塊游戲的源代碼
俄羅斯方塊——java源代碼提供 import java.awt.*; import java.awt.event.*; //俄羅斯方塊類 public class ers_block extends frame{ public static boolean isplay=false; public static int level=1,score=0; public static textfield scorefield,levelfield; public static mytimer timer; gamecanvas gamescr; public static void main(string[] argus){ ers_block ers = new ers_block("俄羅斯方塊游戲 v1.0 author:vincent"); windowlistener win_listener = new winlistener(); ers.addwindowlistener(win_listener); } //俄羅斯方塊類的構造方法 ers_block(string title){ super(title); setsize(600,480); setlayout(new gridlayout(1,2)); gamescr = new gamecanvas(); gamescr.addkeylistener(gamescr); timer = new mytimer(gamescr); timer.setdaemon(true); timer.start(); timer.suspend(); add(gamescr); panel rightscr = new panel(); rightscr.setlayout(new gridlayout(2,1,0,30)); rightscr.setsize(120,500); add(rightscr); //右邊信息窗體的布局 mypanel infoscr = new mypanel(); infoscr.setlayout(new gridlayout(4,1,0,5)); infoscr.setsize(120,300); rightscr.add(infoscr); //定義標簽和初始值 label scorep = new label("分數:",label.left); label levelp = new label("級數:",label.left); scorefield = new textfield(8); levelfield = new textfield(8); scorefield.seteditable(false); levelfield.seteditable(false); infoscr.add(scorep); infoscr.add(scorefield); infoscr.add(levelp); infoscr.add(levelfield); scorep.setsize(new dimension(20,60)); scorefield.setsize(new dimension(20,60)); levelp.setsize(new dimension(20,60)); levelfield.setsize(new dimension(20,60)); scorefield.settext("0"); levelfield.settext("1"); //右邊控制按鈕窗體的布局 mypanel controlscr = new mypanel(); controlscr.setlayout(new gridlayout(5,1,0,5)); rightscr.add(controlscr); //定義按鈕play button play_b = new button("開始游戲"); play_b.setsize(new dimension(50,200)); play_b.addactionlistener(new command(command.button_play,gamescr)); //定義按鈕level up button level_up_b = new button("提高級數"); level_up_b.setsize(new dimension(50,200)); level_up_b.addactionlistener(new command(command.button_levelup,gamescr)); //定義按鈕level down button level_down_b =new button("降低級數"); level_down_b.setsize(new dimension(50,200)); level_down_b.addactionlistener(new command(command.button_leveldown,gamescr)); //定義按鈕level pause button pause_b =new button("游戲暫停"); pause_b.setsize(new dimension(50,200)); pause_b.addactionlistener(new command(command.button_pause,gamescr)); //定義按鈕quit button quit_b = new button("退出遊戲"); quit_b.setsize(new dimension(50,200)); quit_b.addactionlistener(new command(command.button_quit,gamescr)); controlscr.add(play_b); controlscr.add(level_up_b); controlscr.add(level_down_b); controlscr.add(pause_b); controlscr.add(quit_b); setvisible(true); gamescr.requestfocus(); } } //重寫mypanel類,使panel的四周留空間 class mypanel extends panel{ public insets getinsets(){ return new insets(30,50,30,50); } } //游戲畫布類 class gamecanvas extends canvas implements keylistener{ final int unitsize = 30; //小方塊邊長 int rownum; //正方格的行數 int columnnum; //正方格的列數 int maxallowrownum; //允許有多少行未削 int blockinitrow; //新出現塊的起始行坐標 int blockinitcol; //新出現塊的起始列坐標 int [][] scrarr; //屏幕數組 block b; //對方快的引用 //畫布類的構造方法 gamecanvas(){ rownum = 15; columnnum = 10; maxallowrownum = rownum - 2; b = new block(this); blockinitrow = rownum - 1; blockinitcol = columnnum/2 - 2; scrarr = new int [32][32]; } //初始化屏幕,並將屏幕數組清零的方法 void initscr(){ for(int i=0;i