導航:首頁 > 編程語言 > java俄羅斯方塊報告

java俄羅斯方塊報告

發布時間:2023-02-26 11:41:20

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;i++)
for (int j=0; j<columnNum;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<rowNum;i++){
boolean isfull = true;

L1:for(int j=0;j<columnNum;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 <columnNum; 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編寫俄羅斯方塊游戲的源代碼

俄羅斯方塊——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;i++) for (int j=0; j<columnNum;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<rowNum;i++){ boolean isfull = true; L1:for(int j=0;j<columnNum;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 <columnNum; 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); } } 22

③ java做俄羅斯方塊

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;i++)
for (int j=0; j<columnNum;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();
}

④ 用Java設計網路版俄羅斯方塊會遇到哪些難點

(Java ) . 1.1 開發一個俄羅斯方塊游戲。游戲者移動和旋轉窗口內落下的方塊,方塊在一行堆滿後就可以消掉,並得到相應的分數;如果方塊堆積至窗口頂端,即告負。 1.2 在游戲程序中,我們可以將它看成3 個對象,分別是程序窗體主類對象、方塊數據管理對象、控制游戲自動下落的定時器線程對象、三個背景音樂對象。 窗體主類對象: 方塊數據管理對象: 控制游戲自動下落的定時器線程對象: 三個背景音樂對象: 1.3 開發工具:Sun NetBeans IDE 6.1 NetBeans IDE 是一個為軟體開發者提供的自由、開源的集成開發環境。您可以從中獲得您所需要的所有工具,用 Java、C/C++ 甚至是 Ruby 來創建專業的桌面應用程序、企業應用程序、web 和移動應用程序。此 IDE 可以在多種平台上運行,包括 Windows、Linux、Mac OS X 以及 Solaris;它易於安裝且非常方便使用。6.0 發行版包含了重要的增強功能和新特性,包括完全重寫的編輯器基礎結構、對擴展語言的支持、新的生產率特性,以及一個能讓您根據實際需求安裝並配置 IDE 的簡化安裝過程。 . 2.1 游戲數據與界面顯示相分離,用游戲結構數據描述游戲的狀態,玩家操作或游戲自行走一步,程序中都通過修改游戲數據來體現,即每走一步,程序會修改當前的游戲數據,判斷游戲是否結束了,也是通過對游戲數據的分析來作出結論。游戲界面是根據當時游戲數據來繪制的,當數據改變時,要清除原圖形並重繪。總之,游戲的邏輯設計是針對游戲數據,而不是游戲界面。界面只是間接地向玩家顯示結果。因此,在設計函數時,大致分二類:與玩家操作事件有關的數據處理函數,與界面效果有關的圖形繪制函數。游戲運行過程由窗體監聽到的鍵盤事件控制 主要流程圖如下: 製造新的方塊 方 向鍵 的控 制與方法 IsCanChangeTo() Anthem類 游戲背景音樂當游戲開始時啟動 Class RussionGame clearblock() makeblock() moveright() movedown() moveleft() turnleft() turnright() Anthem2 類 按鍵的聲音當觸發方向鍵的方法時響應 formKeyPressed() 當游戲結束後啟動另一首音樂 定義一個線程類,在後台自動地按游戲速度,移動方塊。 CheckAndCutLine() IsOver() Anthem3 類 檢查某一行是否為全填充,如是,消掉並返回1 IsHitBottom() 判斷當前方塊是否已觸底,並處理 TimerRuner 游戲數據管理對象:主要管理著兩方面數據:方塊的坐標數據和游戲空間數據。用成員數組變數描述游戲空間狀態,根據游戲空間狀態判斷游戲是否結束。用它的成員變數保存方塊的形狀數據及坐標數據,定義當方塊走動方塊數據變化的處理方法。此外,還把各種游戲屬性數據作為其成員變數。 控制游戲自動下落的定時器線程對象:是一個線程類派生對象,獨立運行,每隔一段時間控制方塊下落下格。 窗體界面主類對象:負責繪制游戲圖象、包含游戲設置的各種控制項(如:設置速度的文本框、顯示得分的標簽、開始及暫停按鈕),負責游戲的各種屬性數據的讀入及顯示輸出,最重要的是:它還是一個鍵盤事件處理類,監聽玩家的鍵盤操作,處理鍵盤事件,在鍵盤事件處理函數中調用游戲數據管理對象的方法,控制游戲的運行。我們還把游戲數據管理對象、控制游戲自動下落的定時器線程對象作為它的成員變數。 往面板中加入需要的控制項(2 個 Jlable,2 個 JcomboBox,4 個 Jbottun),並布置好它們的位置,並重命名控制項對象變數的名稱,如上圖: 2.3 1. 首先對於方塊的構造分析,可以用一個三維數組來表示,方塊總共有四種基本形,其它形狀可由這四種基本形通過旋轉得到,如下圖: class RussionBlockGame { final int sp_WIDTH = 20; // final int sp_HIGHT = 20; // final int boxtypes[4][4][2]={ {{-1,0},{0,0},{1,0},{2,0}}, {{-1,0},{0,0},{1,0},{1,-1}}, {{-1,0},{0,0},{1,0},{0,-1}}, {{-1,0},{0,0},{1,0},{-1,-1}} };/* */ int box[4][2]; /* */ int cx, cy; /* */ int type; /* ( 0,1,2,3)*/ int block_box[][]=new int[4][2]; /* */ int block_cx, block_cy; /* */ int block_type; /* ( 0,1,2,3)*/ int gamespace[][] = new int[sp_HIGHT][sp_WIDTH] ; void makeblock()// { block_type = (int)(Math.random()*100)%4;//產生一個1-4 的隨機數 for(int i=0; i<4;i++) block_box[i] = types[block_type][i]; block_cx=sp_WIDTH/2; block_cy=0; } HIGHT WIDTH (cx,cy)= (11,4) (WIDTH-1,HIGHT-1) 游戲空間可以看成由sp_WIDTH × sp_HIGHT 個正方形小格子構成,每格子都有一個相對於左上角的坐標。可以用一個sp_WIDTH × sp_HIGHT 的二維數組表示游戲空間。如下: int gamespace[sp_WIDTH][sp_HIGHT]; 某格子對應的數組元素的值為1,表示此格子已被方塊填充,為0 表示未被填充。 在游戲空間中,被方塊填充了的格子為深灰色,未被填充的格子為白色(底色),灰色格子觸及空間頂部時,游戲結束。即gamespace[0](二維數組第一排)中有元素的值為1 時,游戲結束。下面是判斷游戲是否結束的程序: boolean IsGameOver() { boolean flag=false; for(int i=0; i=sp_WIDTH || y>=sp_HIGHT|| (y>0 && gamespace[y][x]==1)) { IsCan = false; break;} } return IsCan; } 4. , 判斷方塊是否已落到底,就是判斷方塊中每個小正方形格的正下方位置是否已被填充或出下界。 如已到底,把方塊所在的位置(格子)填充(對應 gamespace 1),還要查看是否有某行已被全填充,把被全填充的行消掉,並給用戶加分,程序片段如下: boolean IsHitBottom( ) / { boolean flag =false ; int i, x, y; for (i=0; i<4; i++) { x= block_cx + block_box[i][0]; y= block_cy + block_box[i][1] + 1 ; if ( y>=sp_HIGHT|| gamespace[y][x]==1) { flag = true; break; } } if( flag ) { for (i=0; i<4; i++) { x= block_cx + block_box[i][0] ; y= block_cy + block_box[i][1] ; if(y>=0) { gamespace[y][x]=1;} } for (i=0; i<4; i++) { y= block_cy + block_box[i][1] ; while(y>=0 && CheckAndCutLine(y)) m_score += 100; } isplaying = ! IsGameOver(); } return flag; } 開始音樂時已被循環播放 w1.audioClips.loop(); Anthem2 w2 = new Anthem(); 當觸發到按鍵事件時執行formKeyPressed 方法,每當系統取得某一個按鍵的鍵碼時程序都會自動執行w2.audioClips2.play();直到游戲結束。 Anthem2 w3 = new Anthem() ; 當觸發到game.IsOver 方法時證明游戲已結束這時程序會調用 w1.audioClips.stop(); w3.audioClips3.play(); 背景音樂消失,結束音樂開始。 6. 游戲窗體的設計視圖中,選擇「開始游戲」按鈕,再右鍵點擊「開始游戲」按鈕,從菜單中選「事件」->「Action」事件類型->「actionPerformed」介面方法,將轉到源視圖中事件處理代碼處,加入我們的處理代碼,使得游戲開始,如下: private void jButton_startActionPerformed(java.awt.event.ActionEvent evt) { game.isplaying=true; this.requestFocusInWindow(); new TimerRuner(this); } 用同樣的方法編寫「暫停游戲」、「結束游戲」、「退出遊戲」的點擊事件處理代碼,代碼如下: private void jButton_stopActionPerformed(java.awt.event.ActionEvent evt) { game.isplaying=false; } private void jButton_overActionPerformed(java.awt.event.ActionEvent evt) { game.isplaying=false; game.cleargamespace(); } private void jButton_exitActionPerformed(java.awt.event.ActionEvent evt) { game.isplaying=false; System.exit(0); } 2.4 功能: 具有等級功能,不同的等級游戲的難度不一樣; 具有分數功能,消行可以得到分數; 具有聲音,有背景音和執行不同的操作出現不同的聲音; 具有設置功能,可以用來設置初始等級、開始行數、按鍵設置等。 3. /number2 方塊向下 /number4:方塊向下 /number3:方塊向右 PageDown 方塊向右翻傳 End 方塊向左傳 Start 游戲開始 背景音樂的開關 Stop 暫停游戲 over 停止游戲 Speed:調整速度 :選擇方塊的背景色(默認為淺藍) 程序運行結果大部分按照預期設計一樣,但背景色的改變有時響應過慢,而且每次打開音樂後必須重新按下start 鍵,這是該游戲的缺陷之一。鑒於此,已作出部分修正,雖然還沒有達到游戲更加人性化,但基本上能滿足游戲的多方面的需要了。

⑤ java編寫的俄羅斯方塊游戲

以下為一個俄羅斯方塊的源代碼,以---------線分隔一個類。
郁悶太長了,不能全部粘貼上來,要的話在線M我吧。
import javax.microedition.midlet.*;
import javax.microedition.lci.*;
import java.io.IOException;
/**
* <p>Title: 俄羅斯方塊</p>
*
* <p>Description: 俄羅斯方塊游戲</p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: Star Group</p>
*
* @author: Part of this programe comes from a open-source project in the Web(www.hyweb.net).
* Our group makes some remakeble improvement to it.
* @version 1.0
*/
public class RussianGameMIDlet extends MIDlet {
static RussianGameMIDlet instance;
private UIController controller = new UIController(this);
private Splash splash;
private Image imgStart;
Display display = null;
public RussianGameMIDlet() {
instance = this;
display = Display.getDisplay(this);
}

public void startApp() {
try
{
imgStart = Image.createImage("/start.png");
} catch (IOException e) {
}
StringBuffer infoStart = new StringBuffer("俄羅斯方塊");
splash = new Splash(this.controller,infoStart, imgStart);
controller.setSplash(splash);
controller.handleEvent(UIController.EventID.EVENT_START_SPLASH);
}

public void pauseApp() {

}

public void destroyApp(boolean unconditional) {
}

public static void quitApp() {
instance.destroyApp(true);
instance.notifyDestroyed();
instance = null;
}

}
--------------------------------------------------------------------------------------------------------------------------------------------

⑥ 關於游戲俄羅斯方塊(JAVA語言做的)項目答辯時,老師通常會問什麼問題求助高手,並解答,謝謝

我以前同學也是做這個畢業設計的。不過是用C++做的。我記得評委提了幾個問題。
1、俄羅斯方塊已經是很老的游戲了,你做的俄羅斯方塊與我們平時見過的有什麼區別?創新點在哪裡?這個問題是百分之百會問的。
回答:就要看你到底有沒有創新,怎麼去和一般的俄羅斯方塊比較,要好好總結一下。
2、這個畢業設計的工作量、代碼量。
回答:從你開始收集需求分析開始,時間統計,代碼量粗略統計。
3、你覺得這個俄羅斯方塊還有什麼不足,怎麼改進?
回答:比如界面要更加人性化,音效還不夠完美……還要增加多人對戰模式等等
4、你實現項目的主要技術,核心演算法有哪些?
回答:介紹主要技術,演算法參考。
基本上記得的就這些,希望你順利通過。

⑦ java俄羅斯方塊製作方法 全面哦

代碼多了,傳不過去 分開給你傳吧
還是發你郵箱吧
下面是一部分代碼

代碼如下:
package com.tarena.tetris;//包名倒寫
//導包
import java.awt.image.BufferedImage;
/**
* 格子類
*/
public class Cell {
//1定義屬性
private int row;//行
private int col;//列
private BufferedImage image;//圖片
//2構造器
public Cell(int row, int col, BufferedImage image) {
super();
this.row = row;
this.col = col;
this.image = image;
}
//3屬性訪問方法
public int getRow() {
return row;
}
public void setRow(int row) {
this.row = row;
}
public int getCol() {
return col;
}
public void setCol(int col) {
this.col = col;
}
public BufferedImage getImage() {
return image;
}
public void setImage(BufferedImage image) {
this.image = image;
}
//5移動方法
public void drop() {
row++;
}
public void moveLeft() {
col--;
}
public void moveRight() {
col++;
}
//4重寫toString
public String toString() {
return row + "," + col;
}
}

-----------------------------------------------------------------------
package com.tarena.tetris;//包名倒寫
//導包
import java.util.Arrays;
import java.util.Random;
/**
* 四格方塊類:包含七個子類T I J L S Z O
*/
public abstract class Tetromino {
//1創建四個格子
protected Cell[] cells = new Cell[4];
//9
protected State[] states;//旋轉狀態
protected int index = 10000;//旋轉狀態的序號 state[index%state.length]
//8旋轉 內部類
protected class State {
int row0,col0,row1,col1,row2,col2,row3,col3;//轉一圈 8個數
//構造器
public State(int row0, int col0, int row1, int col1, int row2,
int col2, int row3, int col3) {
super();
this.row0 = row0;
this.col0 = col0;
this.row1 = row1;
this.col1 = col1;
this.row2 = row2;
this.col2 = col2;
this.row3 = row3;
this.col3 = col3;
}
}
//10
/** 向右轉 */
public void rotateRight() {
//1取得變換的下個數據狀態state[n]
index++;
State s = states[index%states.length];
//2取得當前軸的row,col
Cell o = cells[0];
int row = o.getRow();
int col = o.getCol();
//3旋轉以後的數據=(row,col) + state[n]
cells[1].setRow(row + s.row1);
cells[1].setCol(col + s.col1);
cells[2].setRow(row + s.row2);
cells[2].setCol(col + s.col2);
cells[3].setRow(row + s.row3);
cells[3].setCol(col + s.col3);
}
//11
/** 向左轉 */
public void rotateLeft() {
//1取得變換的下個數據狀態state[n]
index--;
State s = states[index%states.length];
//2取得當前軸的row,col
Cell o = cells[0];
int row = o.getRow();
int col = o.getCol();
//3旋轉以後的數據=(row,col) + state[n]
cells[1].setRow(row + s.row1);
cells[1].setCol(col + s.col1);
cells[2].setRow(row + s.row2);
cells[2].setCol(col + s.col2);
cells[3].setRow(row + s.row3);
cells[3].setCol(col + s.col3);
}
//2工廠方法,隨機產生四個格子
public static Tetromino randomOne() {
//4隨機產生七種四格方塊
Random random = new Random();
int type = random.nextInt(7);
switch(type) {
case 0 : return new T();
case 1 : return new I();
case 2 : return new J();
case 3 : return new L();
case 4 : return new S();
case 5 : return new Z();
case 6 : return new O();
}
return null;
}
//5移動方法
public void softDrop() {
for(int i=0; i<cells.length; i++) {//迭代每一個方塊
cells[i].drop();//使每一個方塊下落
}
}
public void moveLeft() {
for(int i=0; i<cells.length; i++) {//迭代每一個方塊
cells[i].moveLeft();//使每一個方塊左移
}
}
public void moveRight() {
for(int i=0; i<cells.length; i++) {//迭代每一個方塊
cells[i].moveRight();//使每一個方塊右移
}
}
//6重寫toString
public String toStrig() {
return Arrays.toString(cells);
}
}
//3創建子類T繼承父類Tetromino
class T extends Tetromino {
//7
public T() {
cells[0] = new Cell(0,4,Tetris.T);
cells[1] = new Cell(0,3,Tetris.T);
cells[2] = new Cell(0,5,Tetris.T);
cells[3] = new Cell(1,4,Tetris.T);
//12
states = new State[4];
states[0] = new State(0,0, 0,-1, 0,1, 1,0);
states[1] = new State(0,0, -1,0, 1,0, 0,-1);
states[2] = new State(0,0, 0,1, 0,-1, -1,0);
states[3] = new State(0,0, 1,0, -1,0, 0,1);
}
}
//3創建子類I繼承父類Tetromino
class I extends Tetromino {
//7
public I() {
cells[0] = new Cell(0,4,Tetris.I);
cells[1] = new Cell(0,3,Tetris.I);
cells[2] = new Cell(0,5,Tetris.I);
cells[3] = new Cell(0,6,Tetris.I);
//12
states = new State[] {new State(0,0, 0,1, 0,-1, 0,-2),
new State(0,0, -1,0, 1,0, 2,0)};
}
}
//3創建子類J繼承父類Tetromino
class J extends Tetromino {
//7
public J() {
cells[0] = new Cell(0,4,Tetris.J);
cells[1] = new Cell(0,3,Tetris.J);
cells[2] = new Cell(0,5,Tetris.J);
cells[3] = new Cell(1,5,Tetris.J);
//12
states = new State[]{
new State(0,0, 0,-1, 0,1, 1,1),
new State(0,0, -1,0, 1,0, 1,-1),
new State(0,0, 0,1, 0,-1, -1,-1),
new State(0,0, 1,0, -1,0, -1,1)};
}
}
//3創建子類L繼承父類Tetromino
class L extends Tetromino {
//7
public L() {
cells[0] = new Cell(0,4,Tetris.L);
cells[1] = new Cell(0,3,Tetris.L);
cells[2] = new Cell(0,5,Tetris.L);
cells[3] = new Cell(1,3,Tetris.L);
//12
states = new State[]{
new State(0,0, 0,-1, 0,1, 1,-1),
new State(0,0, -1,0, 1,0, -1,-1),
new State(0,0, 0,1, 0,-1, -1,1),
new State(0,0, 1,0, -1,0, 1,1)};
}
}
//3創建子類S繼承父類Tetromino
class S extends Tetromino {
//7
public S() {
cells[0] = new Cell(0,4,Tetris.S);
cells[1] = new Cell(0,5,Tetris.S);
cells[2] = new Cell(1,3,Tetris.S);
cells[3] = new Cell(1,4,Tetris.S);
//12
states = new State[]{
new State(0,0, 0,1, 1,-1, 1,0),
new State(0,0, -1,0, 1,1, 0,1)};
}
}
//3創建子類Z繼承父類Tetromino
class Z extends Tetromino {
//7
public Z() {
cells[0] = new Cell(1,4,Tetris.Z);
cells[1] = new Cell(0,3,Tetris.Z);
cells[2] = new Cell(0,4,Tetris.Z);
cells[3] = new Cell(1,5,Tetris.Z);
//12
states = new State[]{
new State(0,0, -1,-1, -1,0, 0,1),
new State(0,0, -1,1, 0,1, 1,0)};
}
}
//3創建子類O繼承父類Tetromino
class O extends Tetromino {
//7
public O() {
cells[0] = new Cell(0,4,Tetris.O);
cells[1] = new Cell(0,5,Tetris.O);
cells[2] = new Cell(1,4,Tetris.O);
cells[3] = new Cell(1,5,Tetris.O);
//12
states = new State[]{
new State(0,0, 0,1, 1,0, 1,1),
new State(0,0, 0,1, 1,0, 1,1)};
}
}

⑧ 如何用java編寫出一個俄羅斯方塊小程序

packagecom.test.games;

importjava.awt.Graphics;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.KeyEvent;
importjava.awt.event.KeyListener;

importjavax.swing.JFrame;
importjavax.swing.JMenu;
importjavax.swing.JMenuBar;
importjavax.swing.JMenuItem;
importjavax.swing.JOptionPane;
importjavax.swing.JPanel;
importjavax.swing.Timer;

{
publicTetris(){
Tetrisbloka=newTetrisblok();
addKeyListener(a);
add(a);
}

publicstaticvoidmain(String[]args){
Tetrisframe=newTetris();
JMenuBarmenu=newJMenuBar();
frame.setJMenuBar(menu);
JMenugame=newJMenu("游戲");
JMenuItemnewgame=game.add("新游戲");
JMenuItempause=game.add("暫停");
JMenuItemgoon=game.add("繼續");
JMenuItemexit=game.add("退出");
JMenuhelp=newJMenu("幫助");
JMenuItemabout=help.add("關於");
menu.add(game);
menu.add(help);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(220,275);
frame.setTitle("Tetris內測版");
//frame.setUndecorated(true);
frame.setVisible(true);
frame.setResizable(false);

}
}

//創建一個俄羅斯方塊類
{

//blockType代表方塊類型
//turnState代表方塊狀態
privateintblockType;
privateintscore=0;

privateintturnState;

privateintx;

privateinty;

privateinti=0;

intj=0;
intflag=0;
//定義已經放下的方塊x=0-11,y=0-21;
int[][]map=newint[13][23];

//方塊的形狀第一組代表方塊類型有S、Z、L、J、I、O、T7種第二組代表旋轉幾次第三四組為方塊矩陣
privatefinalintshapes[][][]=newint[][][]{
//i
{{0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0},{0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0},
{0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0},
{0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0}},
//s
{{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},
{1,0,0,0,1,1,0,0,0,1,0,0,0,0,0,0}},
//z
{{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}},
//j
{{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}},
//o
{{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,0}},
//l
{{1,0,0,0,1,0,0,0,1,1,0,0,0,0,0,0},{1,1,1,0,1,0,0,0,0,0,0,0,0,0,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}},
//t
{{0,1,0,0,1,1,1,0,0,0,0,0,0,0,0,0},{0,1,0,0,1,1,0,0,0,1,0,0,0,0,0,0},
{1,1,1,0,0,1,0,0,0,0,0,0,0,0,0,0},
{0,1,0,0,0,1,1,0,0,1,0,0,0,0,0,0}}};

//生成新方塊的方法
publicvoidnewblock(){
blockType=(int)(Math.random()*1000)%7;
turnState=(int)(Math.random()*1000)%4;
x=4;
y=0;
if(gameover(x,y)==1){

newmap();
drawwall();
score=0;
JOptionPane.showMessageDialog(null,"GAMEOVER");
}
}

//畫圍牆
publicvoiddrawwall(){
for(i=0;i<12;i++){
map[i][21]=2;
}
for(j=0;j<22;j++){
map[11][j]=2;
map[0][j]=2;
}
}

//初始化地圖
publicvoidnewmap(){
for(i=0;i<12;i++){
for(j=0;j<22;j++){
map[i][j]=0;
}
}
}

//初始化構造方法
Tetrisblok(){
newblock();
newmap();
drawwall();
Timertimer=newTimer(1000,newTimerListener());
timer.start();
}

//旋轉的方法
publicvoidturn(){
inttempturnState=turnState;
turnState=(turnState+1)%4;
if(blow(x,y,blockType,turnState)==1){
}
if(blow(x,y,blockType,turnState)==0){
turnState=tempturnState;
}
repaint();
}

//左移的方法
publicvoidleft(){
if(blow(x-1,y,blockType,turnState)==1){
x=x-1;
}
;
repaint();
}

//右移的方法
publicvoidright(){
if(blow(x+1,y,blockType,turnState)==1){
x=x+1;
}
;
repaint();
}

//下落的方法
publicvoiddown(){
if(blow(x,y+1,blockType,turnState)==1){
y=y+1;
delline();
}
;
if(blow(x,y+1,blockType,turnState)==0){
add(x,y,blockType,turnState);
newblock();
delline();
}
;
repaint();
}

//是否合法的方法
publicintblow(intx,inty,intblockType,intturnState){
for(inta=0;a<4;a++){
for(intb=0;b<4;b++){
if(((shapes[blockType][turnState][a*4+b]==1)&&(map[x+b+1][y+a]==1))
||((shapes[blockType][turnState][a*4+b]==1)&&(map[x+b+1][y+a]==2))){

return0;
}
}
}
return1;
}

//消行的方法
publicvoiddelline(){
intc=0;
for(intb=0;b<22;b++){
for(inta=0;a<12;a++){
if(map[a][b]==1){

c=c+1;
if(c==10){
score+=10;
for(intd=b;d>0;d--){
for(inte=0;e<11;e++){
map[e][d]=map[e][d-1];

}
}
}
}
}
c=0;
}
}

//判斷你掛的方法
publicintgameover(intx,inty){
if(blow(x,y,blockType,turnState)==0){
return1;
}
return0;
}

//把當前添加map
publicvoidadd(intx,inty,intblockType,intturnState){
intj=0;
for(inta=0;a<4;a++){
for(intb=0;b<4;b++){
if(map[x+b+1][y+a]==0){
map[x+b+1][y+a]=shapes[blockType][turnState][j];
}
;
j++;
}
}
}

//畫方塊的的方法
publicvoidpaintComponent(Graphicsg){
super.paintComponent(g);
//畫當前方塊
for(j=0;j<16;j++){
if(shapes[blockType][turnState][j]==1){
g.fillRect((j%4+x+1)*10,(j/4+y)*10,10,10);
}
}
//畫已經固定的方塊
for(j=0;j<22;j++){
for(i=0;i<12;i++){
if(map[i][j]==1){
g.fillRect(i*10,j*10,10,10);

}
if(map[i][j]==2){
g.drawRect(i*10,j*10,10,10);

}
}
}
g.drawString("score="+score,125,10);
g.drawString("抵制不良游戲,",125,110);
g.drawString("拒絕盜版游戲。",125,170);
// g.drawString("注意自我保護,",125,90);
// g.drawString("謹防受騙上當。",125,110);
// g.drawString("適度游戲益腦,",125,130);
// g.drawString("沉迷游戲傷身。",125,150);
// g.drawString("合理安排時間,",125,170);
// g.drawString("享受健康生活。",125,190);
}

//鍵盤監聽
publicvoidkeyPressed(KeyEvente){
switch(e.getKeyCode()){
caseKeyEvent.VK_DOWN:
down();
break;
caseKeyEvent.VK_UP:
turn();
break;
caseKeyEvent.VK_RIGHT:
right();
break;
caseKeyEvent.VK_LEFT:
left();
break;
}

}

//無用
publicvoidkeyReleased(KeyEvente){
}

//無用
publicvoidkeyTyped(KeyEvente){
}

//定時器監聽
{
publicvoidactionPerformed(ActionEvente){

repaint();
if(blow(x,y+1,blockType,turnState)==1){
y=y+1;
delline();
}
;
if(blow(x,y+1,blockType,turnState)==0){

if(flag==1){
add(x,y,blockType,turnState);
delline();
newblock();
flag=0;
}
flag=1;
}
;
}
}
}

閱讀全文

與java俄羅斯方塊報告相關的資料

熱點內容
安卓全服是什麼意思 瀏覽:145
程序員那麼可愛陸漓和姜逸城吻戲 瀏覽:802
android獲取窗口大小 瀏覽:180
程序員為世界帶來的貢獻 瀏覽:214
程序員招聘自薦信 瀏覽:693
魔獸鍵位設置命令宏 瀏覽:645
程序員沒有目標了 瀏覽:828
搶答器c程序編程 瀏覽:703
什麼app可以自己玩 瀏覽:76
刨客app是什麼 瀏覽:963
cad輸入命令欄不見了 瀏覽:834
做故事集可以用什麼app 瀏覽:692
qq郵箱發送壓縮包 瀏覽:672
程序員桌面機器人 瀏覽:589
xjr快速開發平台源碼 瀏覽:159
java介面runnable 瀏覽:31
python怎麼運行web伺服器 瀏覽:349
notepad編程代碼 瀏覽:740
什麼安卓的毛病最少 瀏覽:611
hp的pjl設備訪問命令 瀏覽:635