當前位置:ag真人国际官网-ag旗舰厅官方网站 » 編程語言 » java游戲俄羅斯方塊

java游戲俄羅斯方塊-ag真人国际官网

發布時間: 2024-06-26 09:28:43

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 for (int j=0; j scrarr[j]=0;
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 boolean isfull = true;

l1:for(int j=0;j if(scrarr[j] == 0){
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 if(scrarr[maxallowrownum][col] !=0)
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 for (int j=0; j scrarr[i][j]=0;
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 boolean isfull = true;

l1:for(int j=0;j if(scrarr[i][j] == 0){
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 if(scrarr[maxallowrownum][col] !=0)
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;jmap[i][j]=state.empty;
}
}1234567
玩游戲過程中,我們能夠看到界面上的方塊,那麼就得將地圖中所有的方塊繪制出來,當然,除了需要繪制方塊外,游戲積分和游戲結束的字元串在必要的時候也需要繪制:
/**
*繪制窗體內容,包括游戲方塊,游戲積分或結束字元串
*/
@override
publicvoidpaint(graphicsg){
super.paint(g);
for(inti=0;ifor(intj=0;jif(map[i][j]==state.active){//繪制活動塊
g.setcolor(activecolor);
g.fillroundrect(j*block_size,i*block_size 25,
block_size-1,block_size-1,block_size/5,
block_size/5);
}elseif(map[i][j]==state.stoped){//繪制靜止塊
g.setcolor(stopedcolor);
g.fillroundrect(j*block_size,i*block_size 25,
block_size-1,block_size-1,block_size/5,
block_size/5);
}
}
}

/*列印得分*/
g.setcolor(scorecolor);
g.setfont(newfont("timesnewroman",font.bold,30));
g.drawstring("score:" totalscore,5,70);

//游戲結束,列印結束字元串
if(!isgoingon){
g.setcolor(color.red);
g.setfont(newfont("timesnewroman",font.bold,40));
g.drawstring("gameover!",this.getwidth()/2-140,
this.getheight()/2);
}
}
通過隨機數的方式產生方塊所組成的幾種圖形,一般七種圖形:條形、田形、正7形、反7形、t形、z形和反z形,如生成條形:
map[0][randpos]=map[0][randpos-1]=map[0][randpos 1]
=map[0][randpos 2]=state.active;123
生成圖形後,實現下落的操作。如果遇到阻礙,則不能再繼續下落:
isfall=true;//是否能夠下落
//從當前行檢查,如果遇到阻礙,則停止下落
for(inti=0;ifor(intj=0;j//遍歷到行中塊為活動塊,而下一行塊為靜止塊,則遇到阻礙
if(map[rowindex-i][j]==state.active
&&map[rowindex-i 1][j]==state.stoped){
isfall=false;//停止下落
break;
}
}
if(!isfall)
break;
}123456789101112131415
如果未遇到阻礙,則下落的時候,方塊圖形整體向下移動一行:
//圖形下落一行
for(inti=0;ifor(intj=0;jif(map[rowindex-i][j]==state.active){//活動塊向下移動一行
map[rowindex-i][j]=state.empty;//原活動塊變成空塊
map[rowindex-i 1][j]=state.active;//下一行塊變成活動塊
}
}
}12345678910
向左、向右方向移動時是類似的操作:
/**
*向左走
*/
privatevoidleft(){
//標記左邊是否有阻礙
booleanhasblock=false;

/*判斷是否左邊有阻礙*/
for(inti=0;iif(map[rowindex-i][0]==state.active){//判斷左邊是否為牆
hasblock=true;
break;//有阻礙,不用再循環判斷行
}else{
for(intj=1;jif(map[rowindex-i][j]==state.active
&&map[rowindex-i][j-1]==state.stoped){
hasblock=true;
break;//有阻礙,不用再循環判斷列
}
}
if(hasblock)
break;//有阻礙,不用再循環判斷行
}
}

/*左邊沒有阻礙,則將圖形向左移動一個塊的距離*/
if(!hasblock){
for(inti=0;ifor(intj=1;jif(map[rowindex-i][j]==state.active){
map[rowindex-i][j]=state.empty;
map[rowindex-i][j-1]=state.active;
}
}
}

//重繪
repaint();
}
}3738394041
向下加速移動時,就是減小每次正常狀態下落的時間間隔:
/**
*向下直走
*/
privatevoiddown(){
//標記可以加速下落
immediate=true;
}12345678
如何變換圖形方向,這里僅使用了非常簡單的方法來實現方向變換,當然可以有更優的演算法實現方向變換操作,大家可以自己研究:
/**
*旋轉方塊圖形
*/
privatevoidrotate(){
try{
if(shape==4){//方形,旋轉前後是同一個形狀
return;
}elseif(shape==0){//條狀
//臨時數組,放置旋轉後圖形
state[][]tmp=newstate[4][4];
intstartcolumn=0;
//找到圖形開始的第一個方塊位置
for(inti=0;iif(map[rowindex][i]==state.active){
startcolumn=i;
break;
}
}
//查找旋轉之後是否有阻礙,如果有阻礙,則不旋轉
for(inti=0;i<4;i ){
for(intj=0;j<4;j ){
if(map[rowindex-3 i][j startcolumn]==state.stoped){
return;
}
}
}

if(map[rowindex][startcolumn 1]==state.active){//橫向條形,變換為豎立條形
for(inti=0;i<4;i ){
tmp[i][0]=state.active;
for(intj=1;j<4;j ){
tmp[i][j]=state.empty;
}
}
blockrows=4;
}else{//豎立條形,變換為橫向條形
for(intj=0;j<4;j ){
tmp[3][j]=state.active;
for(inti=0;i<3;i ){
tmp[i][j]=state.empty;
}
}
blockrows=1;
}
//將原地圖中圖形修改為變換後圖形
for(inti=0;i<4;i ){
for(intj=0;j<4;j ){
map[rowindex-3 i][startcolumn j]=tmp[i][j];
}
}
}else{
//臨時數組,放置旋轉後圖形
state[][]tmp=newstate[3][3];
intstartcolumn=columns;
//找到圖形開始的第一個方塊位置
for(intj=0;j<3;j ){
for(inti=0;iif(map[rowindex-j][i]==state.active){
startcolumn=i}
}
}
//判斷變換後是否會遇到阻礙
for(inti=0;i<3;i ){
for(intj=0;j<3;j ){
if(map[rowindex-2 j][startcolumn 2-i]==state.stoped)
return;
}
}
//變換
for(inti=0;i<3;i ){
for(intj=0;j<3;j ){
tmp[2-j][i]=map[rowindex-2 i][startcolumn j];
}
}
//將原地圖中圖形修改為變換後圖形
for(inti=0;i<3;i ){
for(intj=0;j<3;j ){
map[rowindex-2 i][startcolumn j]=tmp[i][j];
}
}

//重繪
repaint();
//重新修改行指針
for(inti=0;i<3;i ){
for(intj=0;j<3;j ){
if(map[rowindex-i][startcolumn j]!=null
||map[rowindex-i][startcolumn j]!=state.empty){
rowindex=rowindex-i;
blockrows=3;
return;
}
}
}
}
}catch(exceptione){
//遇到數組下標越界,說明不能變換圖形形狀,不作任何處理
}
}3738394
當圖形下落遇到阻礙時停止,我們就需要判斷這時是否有某一行或幾行可以消除掉,這時可以先獲取每行中方塊的個數,然後再進行判斷:
int[]blockscount=newint[rows];//記錄每行有方塊的列數
inteliminaterows=0;//消除的行數
/*計算每行方塊數量*/
for(inti=0;iblockscount[i]=0;
for(intj=0;jif(map[i][j]==state.stoped)
blockscount[i] ;
}
}1234567891011
如果有滿行的方塊,則消除掉該行方塊:
/*實現有滿行的方塊消除操作*/
for(inti=0;iif(blockscount[i]==columns){
//清除一行
for(intm=i;m>=0;m--){
for(intn=0;nmap[m][n]=(m==0)?state.empty:map[m-1][n];
}
}
eliminaterows ;//記錄消除行數
}
}12345678910111213
最後我們再重繪顯示積分就可以了。
重復以上的生成圖形、圖形下落、左右下移動、判斷消除行的操作,一個簡單的俄羅斯方塊就完成了。

ⅳ 求用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= 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 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); } } 22

熱點內容
phpjava交互 發布:2024-07-17 16:58:57 瀏覽:356
resin下jsp不能正常編譯 發布:2024-07-17 16:34:44 瀏覽:229
sqlserver如何切換主備伺服器 發布:2024-07-17 16:23:02 瀏覽:299
mc18伺服器ip 發布:2024-07-17 16:23:02 瀏覽:379
仙境傳說手游腳本 發布:2024-07-17 16:09:24 瀏覽:691
matlab命令窗口和新建腳本 發布:2024-07-17 15:51:26 瀏覽:375
建ftp文件夾 發布:2024-07-17 15:51:26 瀏覽:955
魔獸撿物腳本 發布:2024-07-17 15:27:56 瀏覽:130
開發ip伺服器 發布:2024-07-17 15:24:42 瀏覽:388
安卓系統視頻製作哪個好用 發布:2024-07-17 15:10:47 瀏覽:210
网站地图