當前位置:遊戲中心平台 - 遊戲盒子 - 求壹個JAVA遊戲代碼!!!急!!!

求壹個JAVA遊戲代碼!!!急!!!

俄羅斯方塊——由——java源代碼提供

導入Java . awt . *;

導入Java . awt . event . *;

//俄羅斯方塊類

公共類ERS_Block擴展框架{

公共靜態布爾isPlay = false

public static int level=1,score = 0;

公共靜態TextField scoreField、levelField

公共靜態MyTimer計時器;

GameCanvas gameScr

public static void main(String[]argus){

ERS_Block ers = new ERS_Block("俄羅斯方塊遊戲V1.0作者:Vincent ");

window listener win _ listener = new win listener();

ers . addwindowlistener(win _ listener);

}

//俄羅斯方塊類的構造方法

ERS_Block(字符串標題){

超級(標題);

setSize(600,480);

set layout(new GridLayout(1,2));

game Cr = new game canvas();

games Cr . addkey listener(games Cr);

timer = new my timer(gameScr);

timer . set daemon(true);

timer . start();

timer . suspend();

添加(gameScr);

Panel rights Cr = new Panel();

rights Cr . set layout(new GridLayout(2,1,0,30));

rights Cr . setsize(120500);

add(rights Cr);

//右側信息表單的布局

my panel info SCR = new my panel();

info SCR . set layout(new GridLayout(4,1,0,5));

infoScr.setSize(120,300);

rights Cr . add(infos Cr);

//定義標簽和初始值

Label scorep =新標簽(" score:",Label。左);

標簽級別p =新標簽("系列:",標簽。左);

score field = new TextField(8);

level field = new TextField(8);

score field . set editable(false);

level field . set editable(false);

info SCR . add(scorep);

info SCR . add(score field);

info SCR . add(levelp);

info SCR . add(level field);

scorep.setSize(新維度(20,60));

scoreField.setSize(新維度(20,60));

levelp.setSize(新維度(20,60));

levelField.setSize(新維度(20,60));

score field . settext(" 0 ");

level field . settext(" 1 ");

//右側控件按鈕窗體的布局

my panel control SCR = new my panel();

control SCR . set layout(new GridLayout(5,1,0,5));

rights Cr . add(controls Cr);

//定義按鈕播放

Button play_b = new Button("開始遊戲");

play_b.setSize(新維度(50,200));

play_b.addActionListener(新命令(Command.button_play,gameScr));

//向上定義按鈕級別

Button level_up_b = new Button("完善系列");

level_up_b.setSize(新維度(50,200));

level _ up _ b . addactionlistener(new Command(Command . button _ level up,gameScr));

//向下定義按鈕級別

按鈕level _ down _ b =新建按鈕(“減少系列”);

level_down_b.setSize(新維度(50,200));

level _ down _ b . addactionlistener(new Command(Command . button _ level down,gameScr));

//定義按鈕級暫停

按鈕pause _ b =新建按鈕(“遊戲暫停”);

pause_b.setSize(新維度(50,200));

pause _ b . addactionlistener(new Command(Command . button _ pause,gameScr));

//定義按鈕退出

Button quit_b = new Button("退出遊戲");

quit_b.setSize(新維度(50,200));

quit_b.addActionListener(新命令(Command.button_quit,gameScr));

controls Cr . add(play _ b);

control SCR . add(level _ up _ b);

control SCR . add(level _ down _ b);

control SCR . add(pause _ b);

control SCR . add(quit _ b);

set visible(true);

games Cr . request focus();

}

}

//重寫MyPanel類,在面板周圍留出空間。

MyPanel類擴展面板{

public Insets getInsets(){

返回新的Insets(30,50,30,50);

}

}

//遊戲畫布類

GameCanvas類擴展Canvas實現KeyListener{

final int unitSize = 30//小正方形的邊長

int rowNum//正方形中的行數

int columnNum//規則網格中的列數

int maxAllowRowNum//允許不剪多少行?

int blockInitRow//新塊起始行的坐標。

int blockInitCol//新塊的起始列坐標。

int[][]scrar;//屏幕陣列

b座;//對方快速報價

//畫布類的構造方法

GameCanvas(){

rowNum = 15;

column num = 10;

maxAllowRowNum = rowNum-2;

b =新塊(this);

block initrow = rowNum-1;

blockInitCol = column num/2-2;

scrArr = new int[32][32];

}

//初始化屏幕並清除屏幕數組。

void initScr(){

for(int I = 0;我& ltrowNumi++)

for(int j = 0;j & ltcolumnNumj++)

scrArr[j]= 0;

b . reset();

repaint();

}

//刷新畫布方法

公共空白油漆(圖形g){

for(int I = 0;我& ltrowNumi++)

for(int j = 0;j & ltcolumnNumj++)

drawUnit(i,j,scrArr[j]);

}

//畫正方形的方法

public void drawUnit(int row,int col,int type){

scrArr[row][col]= type;

graphics g = get graphics();

Tch(type){ //表示快速繪制的方法。

案例0:g . set color(color . black);打破;//以背景為顏色進行繪制

案例1:g . set color(color . blue);打破;//畫壹個下落的正方形。

案例二:g . set color(color . magenta);打破;//畫壹個已經落後的方法。

}

g.fill3DRect(col*unitSize,getSize()。height-(row+1)*unitSize,unitSize,unitSize,true);

g . dispose();

}

公共塊getBlock(){

返回b;//返回對塊實例的引用。

}

//返回屏幕數組中(row,col)位置的屬性值。

public int getScrArrXY(int row,int col){

if(row & lt;0 | | row & gt= rowNum | | col & lt0 | | col & gt= columnNum)

return(-1);

其他

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;我& ltrowNumi++){

boolean isfull = true

l 1:for(int j = 0;j & ltcolumnNumj++)

if(scrArr[j] == 0){

k++;

isfull = false

破l 1;

}

if(is full)full _ line _ num++;

如果(k!= 0 & amp& ampk-1!= i & amp& amp!isfull)

for(int j = 0;j & ltcolumnNumj++){

if (scrArr[j] == 0)

drawUnit(k-1,j,0);

其他

drawUnit(k-1,j,2);

scrArr[k-1][j]= scrArr[j];

}

}

for(int I = k-1;我& ltrowNumi++){

for(int j = 0;j & ltcolumnNumj++){

drawUnit(i,j,0);

scrArr[j]= 0;

}

}

ERS _ block . score+= full _ line _ num;

ERS _ block . score field . settext("+ERS _ block . score);

}

//判斷遊戲是否結束的方法

布爾is gamend(){

for(int col = 0;col & ltcolumnNumcol ++){

if(scrArr[maxAllowRowNum][col]!=0)

返回true

}

返回false

}

公共void鍵入的(KeyEvent e){

}

public void key released(key event e){

}

//處理鍵盤輸入的方法

公共void按鍵(按鍵事件e){

如果(!ERS_Block.isPlay)

返回;

tch(e.getKeyCode()){

案例關鍵事件。VK _ DOWN:b . falldown();打破;

案例關鍵事件。VK _左:b . LEFT move();打破;

案例關鍵事件。VK _右:b . RIGHT move();打破;

案例關鍵事件。VK _空間:b . left turn();打破;

}

}

}

//處理控制類

class命令實現ActionListener{

靜態最終int button _ play = 1;//給按鈕分配壹個數字

static final int button _ level up = 2;

static final int button _ level down = 3;

靜態final int button _ quit = 4;

靜態final int button _ pause = 5;

靜態布爾pause _ resume = true

int curButton//當前按鈕

GameCanvas scr

//控制按鈕類的構造方法

命令(int按鈕,GameCanvas scr){

curButton =按鈕;

this.scr = scr

}

//按鈕執行方法

public void action performed(action event e){

tch(控制按鈕){

案例按鈕_播放:如果(!ERS_Block.isPlay){

SCR . init SCR();

ERS _ block . is play = true;

ERS _ block . score = 0;

ERS _ block . score field . settext(" 0 ");

ERS _ block . timer . resume();

}

SCR . request focus();

打破;

case button _ level up:if(ERS _ block . level & lt;10){

ERS _ block . level++;

ERS _ block . level field . settext("+ERS _ block . level);

ERS _ block . score = 0;

ERS _ block . score field . settext("+ERS _ block . score);

}

SCR . request focus();

打破;

case button _ level down:if(ERS _ block . level & gt;1){

ERS _ block . level-;

ERS _ block . level field . settext("+ERS _ block . level);

ERS _ block . score = 0;

ERS _ block . score field . settext("+ERS _ block . score);

}

SCR . request focus();

打破;

case button _ pause:if(pause _ resume){

ERS _ block . timer . suspend();

pause _ resume = false

}否則{

ERS _ block . timer . resume();

pause _ resume = true

}

SCR . request focus();

打破;

case button _ quit:system . exit(0);

}

}

}

//塊類

類塊{

靜態int[][]模式= {

{0x0f00,0x4444,0x0f00,0x4444},//用十六進制的to來表示,這壹行表示條形的四種狀態。

{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類的構造方法

Block(GameCanvas scr){

this.scr = scr

block type =(int)(math . random()* 1000)% 7;

turnState =(int)(math . random()* 1000)% 4;

block state = 1;

row = SCR . geti nitrow();

col = SCR . getinitcol();

}

//重新初始化塊並顯示新塊。

公共void重置(){

block type =(int)(math . random()* 1000)% 7;

turnState =(int)(math . random()* 1000)% 4;

block state = 1;

row = SCR . geti nitrow();

col = SCR . getinitcol();

disp block(1);

}

//實現“塊”翻轉的方法

公共void左轉(){

if(assertValid(blockType,(turnState + 1)%4,row,col)){

disp block(0);

turn state =(turn state+1)% 4;

disp block(1);

}

}

//實現“塊”左移的方法

public void leftMove(){

if(assertValid(blockType,turnState,row,col-1)){

disp block(0);

col-;

disp block(1);

}

}

//將塊向右移動。

public void rightMove(){

if(assertValid(blockType,turnState,row,col+1)){

disp block(0);

col++;

disp block(1);

}

}

//實現塊丟棄操作的方法

public boolean fallDown(){

if(blockState == 2)

返回(假);

if(assertValid(blockType,turnState,row-1,col)){

disp block(0);

行-;

disp block(1);

返回(真);

}否則{

block state = 2;

disp block(2);

返回(假);

}

}

//判斷正確與否的方法

boolean assertValid(int t,int s,int row,int col){

int k = 0x8000

for(int I = 0;我& lt4;i++){

for(int j = 0;j & lt4;j++){

if((int)(pattern[t][s]& amp;k)!= 0){

int temp = scr.getScrArrXY(row-i,col+j);

if(temp & lt;0 | |溫度==2)

返回false

}

k = k & gt& gt1;

}

}

返回true

}

//同步顯示的方法

公共同步void dispBlock(int s){

int k = 0x8000

for(int I = 0;我& lt4;i++){

for(int j = 0;j & lt4;j++){

if(((int)pattern[block type][turnState]& amp;k)!= 0){

scr.drawUnit(row-i,col+j,s);

}

k = k & gt& gt1;

}

}

}

}

//定時線程

MyTimer類擴展線程{

GameCanvas scr

公共MyTimer(GameCanvas scr){

this.scr = scr

}

公共無效運行(){

while(true){

嘗試{

sleep((10-ERS _ block . level+1)* 100);

}

catch(InterruptedException e){}

如果(!scr.getBlock()。fallDown()){

SCR . delete full line();

if(SCR . isgamend()){

ERS _ block . is play = false;

掛起();

}否則

scr.getBlock()。reset();

}

}

}

類WinListener擴展WindowAdapter{

public void window closing(window event l){

system . exit(0);

}

}

  • 上一篇:綠娃傳奇的遊戲流程介紹
  • 下一篇:順義哪些地方的情侶環境好?
  • copyright 2024遊戲中心平台