java俄罗斯⽅块代码_俄罗斯⽅块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);
简单的java游戏代码
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(newCommand(Command.button_levelup,gameScr)); //定义按钮Level Down
Button level_down_b =new Button('降低级数');
level_down_b.setSize(new Dimension(50,200));
level_down_b.addActionListener(newCommand(Command.button_leveldown,gameScr)); //定义按钮Level Pause
Button pause_b =new Button('游戏暂停');
pause_b.setSize(new Dimension(50,200));
pause_b.addActionListener(newCommand(Command.button_pause,gameScr));
//定义按钮Quit
Button quit_b = new Button('退出游戏');
quit_b.setSize(new Dimension(50,200));
quit_b.addActionListener(newCommand(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);
}
}
//重写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;
repaint();
}
//重新刷新画布⽅法
public void paint(Graphics g){
for(int i = 0; i
for(int j = 0; 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 = rowNum || 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++;

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。