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

編寫俄羅斯方塊java

發布時間:2023-03-24 05:57:10

Ⅰ 求一個java俄羅斯方塊的設計思路 不要代碼 只要思路 本人是初學者

1:首先自己定義一個類,比如MyLabel,繼承jlabel,設置大小比如
(40,40)設置成方塊,這就是游戲裡面最小的單位,下落的圖形,就是四個這個樣的單位組合到一起,位置不同。
2:定義自己的圖形(就是游戲中下落的部分)比如MyPic
,這個類是控制MyLabel的,一般是4個MyLabel組合成一個MyPic,
3:定義自己的面板,大小是
(n*40,m*40
)就是上面最小單位的整數行和列,然後建一個二位數組
int
[n][m],數組裡面默認為0,標示這個位置沒有MyLabel
,是空的,如果位置有
MyLabel,設置成1
,這個是數組是,圖形下落的時候判斷是否繼續下落還是要停下了,下面有了就停,否則繼續下落,下落停止後,根據數組,看某一行是否全部為1
是的話,把這行清空
圖形的下落要用定時器或者自己寫線程實現,然後就是判斷下面是否有東西,是下落,還是停止,
圖形的旋轉的話,自己研究吧,位置的變換,也不好做

Ⅱ 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俄羅斯方塊製作方法 全面哦

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

代碼如下:
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 SWING

將游戲區劃成小格
設定一個移動像素,比如5
設定好幾個方塊的類,比如--,⊥等等。都由小格組成
隨機NEW那一個類。
判斷方塊最下面的的小格是否已到「底」,底:下面是小格是否已被添滿。
判斷一行從左到右是否沒有空格,否則刪除這行,再調用向下方法。

Ⅳ 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編寫出一個俄羅斯方塊小程序

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編寫俄羅斯方塊游戲的源代碼

俄羅斯方塊——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相關的資料

熱點內容
歐拉app怎麼下訂單 瀏覽:422
肉文有聲小說 瀏覽:955
求個看片的網址 瀏覽:547
pythonsin函數圖像 瀏覽:329
身體不好當程序員 瀏覽:274
mount命令作用 瀏覽:220
畫江湖之不良人黑白無常雙修刪減 瀏覽:754
朵唯手機如何加密 瀏覽:504
安卓雙清指的什麼 瀏覽:177
phpredis所有keys 瀏覽:988
朋友賣房要解壓嗎 瀏覽:108
sar命令安裝 瀏覽:169
安卓怎麼看我自己去過哪裡 瀏覽:283
演算法分析里log沒有底數嗎 瀏覽:222
伺服器卡頓怎麼連接 瀏覽:957
手機拍照文件夾自動生成 瀏覽:788
瀏覽器如何運行在伺服器端 瀏覽:790
collinux 瀏覽:449
日本歐美韓國推理片電影大分享 瀏覽:615
怎麼下載香港app游戲 瀏覽:217