俄羅斯方塊——java源代碼提供
推薦 + 挑錯(cuò) + 收藏(0) + 用戶(hù)評(píng)論(0)
俄羅斯方塊——java源代碼提供
?
import java.awt.*;
import java.awt.event.*;
//俄羅斯方塊類(lèi)
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);
}
//俄羅斯方塊類(lèi)的構(gòu)造方法
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);
//定義標(biāo)簽和初始值
Label scorep = new Label("分?jǐn)?shù):",Label.LEFT);
Label levelp = new Label("級(jí)數(shù):",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("開(kāi)始游戲");
play_b.setSize(new Dimension(50,200));
play_b.addActionListener(new Command(Command.button_play,gameScr));
//定義按鈕Level UP
Button level_up_b = new Button("提高級(jí)數(shù)");
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("降低級(jí)數(shù)");
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();
}
}
//重寫(xiě)MyPanel類(lèi),使Panel的四周留空間
class MyPanel extends Panel{
public Insets getInsets(){
return new Insets(30,50,30,50);
}
}
//游戲畫(huà)布類(lèi)
class GameCanvas extends Canvas implements KeyListener{
final int unitSize = 30; //小方塊邊長(zhǎng)
int rowNum; //正方格的行數(shù)
int columnNum; //正方格的列數(shù)
int maxAllowRowNum; //允許有多少行未削
int blockInitRow; //新出現(xiàn)塊的起始行坐標(biāo)
int blockInitCol; //新出現(xiàn)塊的起始列坐標(biāo)
int [][] scrArr; //屏幕數(shù)組
Block b; //對(duì)方快的引用
//畫(huà)布類(lèi)的構(gòu)造方法
GameCanvas(){
rowNum = 15;
columnNum = 10;
maxAllowRowNum = rowNum - 2;
b = new Block(this);
blockInitRow = rowNum - 1;
blockInitCol = columnNum/2 - 2;
scrArr = new int [32][32];
}
//初始化屏幕,并將屏幕數(shù)組清零的方法
void initScr(){
for(int i=0;i
b.reset();
repaint();
}
//重新刷新畫(huà)布方法
public void paint(Graphics g){
for(int i = 0; i < rowNum; i++)
for(int j = 0; j < columnNum; j++)
drawUnit(i,j,scrArr[j]);
非常好我支持^.^
(0) 0%
不好我反對(duì)
(3) 100%
下載地址
俄羅斯方塊——java源代碼提供下載
相關(guān)電子資料下載
- JavaScript壓縮、混淆和加密技術(shù)原理解析 207
- DDC和PLC的區(qū)別 159
- Prometheus實(shí)戰(zhàn)篇:Exporter知識(shí)概述 75
- Mysql索引是什么東西?索引有哪些特性?索引是如何工作的? 281
- 一文了解Java 21的新功能 81
- Android編譯優(yōu)化之混淆配置 129
- 叮,你有一份來(lái)自 2023 開(kāi)放原子開(kāi)發(fā)者大會(huì)的邀請(qǐng)函,請(qǐng)查收! 83
- 十大排序算法總結(jié) 451
- 一種優(yōu)雅解決MySQL驅(qū)動(dòng)中虛引用導(dǎo)致GC耗時(shí)較長(zhǎng)問(wèn)題的方法 196
- 什么是跨站腳本攻擊?一篇帶你了解什么叫做XSS 141