📄 brickctrl.java
字号:
import java.awt.* ;
import java.awt.event.* ;
import javax.swing.* ;
import java.util.ArrayList;
import java.util.Random;00000
import javax.swing.Timer ;
public class BrickCtrl extends JPanel
{
private static final int[][] dy //colomn用4*4坐标表示7种砖块
= {
{ -1 , 0 , 0 , 1 } ,
{ 1 , 1 , 0 , 0 } , //brick 1
{ -1 , 0 , 0 , 1 } ,
{ 1 , 1 , 0 , 0 } ,
{ 0 , 1 , 0 ,-1 } ,
{ 0 , 0 , 1 , 1 } ,
{ 0 , 1 , 0 ,-1 } ,
{ 0 , 0 , 1 , 1 } , //brick 2
{ -1 ,0 ,0 , 0 } ,
{ -1,-1,0, 1 } ,
{ 0 ,0 ,0 ,1} ,
{ 1, 1 ,0 ,-1} , //brick 3
{ 1,0,0,0} ,
{-1,-1,0,1} ,
{0,0,0,-1} ,
{-1,0,1,1} , //brick 4
{0,0,0,-1} ,
{ -1,0 ,0,1} ,
{0,0,0,1} ,
{0,0,-1,1} , //brick 5
{-2,-1,0,1} ,
{0,0,0,0} ,
{-2,-1,0,1} ,
{0,0,0,0} , //brick 6
{0,0,1,1} ,
{0,0,1,1} ,
{0,0,1,1} ,
{0,0,1,1} //brick 7
} ;
private static final int[][] dx //rows
= {
{ 0 , 0 , 1 , 1} ,
{-1 , 0 , 0 , 1} ,
{ 0 , 0 , 1 , 1} ,
{ -1, 0 , 0 , 1} ,//brick1
{0,0,1,1} ,
{-1,0,0,1} ,
{0,0,1,1} ,
{-1,0,0,1} ,//brick2
{-1,-1,0,1},
{0,1,0,0},
{-1,0,1,1} ,
{-1,0,0,0} , //brick3
{-1,-1,0,1} ,
{-1,0,0,0} ,
{-1,0,1,1} ,
{0,0,0,1} , //brick4
{-1,0,1,0 } ,
{0,0,1,0} ,
{-1,0,1,0} ,
{-1,0,0,0} , //brick 5
{0,0,0,0} ,
{-1,0,1,2} ,
{0,0,0,0} ,
{-1,0,1,2} , //brick 6
{0,1,0,1} ,
{0,1,0,1} ,
{0,1,0,1} ,
{0,1,0,1} , //brick 7
} ;
private static final int shapekind[]
= { 0,1,4,5,8,9,10,11,12,13,14,15, 16,17,18,19,20,21,24 } ;
private static final int NEW_Y = 1 ;
private static final int NEW_X = 5 ;
private ArrayList brick ;
private int [][] table ; //表示网格面板是否有方块
private Timer timer ; //定时器
private Random rand ;
private JPanel panel ;
private JPanel panel1;
private JButton button1;//开始按扭
private JButton button2;//暂停按扭
private JLabel label;//显示分数的标签
private int pos_y ; //相对位置Y
private int pos_x ; //相对位置X
private int shape ; //whick brick
private int X;//用来记录分数
private int speed=1;//用来控制速度
// private music mu; //用来播放音乐
// =======================Contrustor=================================
public BrickCtrl()
{
setLayout(new BorderLayout()) ;
setBackground(Color.white);
setPreferredSize(new Dimension(400,400));
brick = new ArrayList(200) ;
panel = new JPanel() ;
panel1=new JPanel();
panel.setLayout(new GridLayout(20,10) ) ;
panel1.setLayout(new GridLayout(3,1) ) ;
button1=new JButton("start!");
button2=new JButton("pause!");
button1.addActionListener(new ButtonListener());
button2.addActionListener(new ButtonListener());
label=new JLabel("score:");
panel1.add(label);
panel1.add(button1);
panel1.add(button2);
for(int i=0;i<20;i++)
for(int j=0;j<10;j++)
{
JButton button = new JButton( ) ;
button.addKeyListener(new KeyProcessor() ) ;
panel.add(button) ;
button.setBackground(Color.white) ;
brick.add(button) ;
pos_x = 5 ;
pos_y = 1 ;
}
add(new JButton(" " ),BorderLayout.NORTH);
add(new JButton(" " ),BorderLayout.WEST);
add(new JButton(" "),BorderLayout.SOUTH);
add(panel,BorderLayout.CENTER);
add(panel1,BorderLayout.EAST);
table = new int[22][12] ;
for(int i=1;i<21;i++)
for(int j=1;j<11;j++) //初始化区域内没有方块
{
table[i][j] = 0 ;
}
for(int i=0;i<22;i++)
{
table[i][0] = 1 ; //保证左边不出边界
table[i][11] = 1 ; //保证右边不出边界
}
for(int i=0;i<12;i++)
{
table[0][i] = 1 ; //保证最上面不出边界
table[21][i] = 1 ; //保证最下面不出边界
}
rand = new Random(1000) ;
ActionListener timelistener = new TimeListener() ;
timer = new Timer(800,timelistener) ;
}
// ====================IsMoveable=================================== 判断是否能够移动
public boolean IsMoveable(int cx ,int cy) //他的每种变形都要进行判断
{
for(int i=0;i<4;i++)
{
int y = cy + dy[shape][i] ; //将相对位置转化为坐标中的实际坐标(x)
int x = cx + dx[shape][i] ; //将相对位置转化为坐标中的实际坐标(Y)
if(table[y][x] == 1)
return false ;
}
return true ;
}
public void NewBrick() //产生新的砖块
{
pos_x = NEW_X ;
pos_y = NEW_Y ;
int dex = rand.nextInt(19);
shape = shapekind[dex] ;
for(int i=0;i<4;i++)
{
int y = pos_y + dy[shape][i] ;
int x = pos_x + dx[shape][i] ;
if( y >= 1 )
{
table[y][x] = 1 ;
}
}
DrawBrick() ;
}
// ==========================DrawBrick=================================
public void DrawBrick() //填充方块
{
for(int i=1;i<=20;i++)
for(int j=1;j<=10;j++)
{
if(table[i][j] == 1)
( (JButton)brick.get( (i-1)*10+j-1)).setBackground(Color.yellow) ;
else
( (JButton)brick.get( (i-1)*10+j-1)).setBackground(Color.white) ;
}
}
public void CleanCurrentBrick() //清楚当前的方块
{
for(int i=0;i<4;i++)
{
int x = pos_x + dx[shape][i] ;
int y = pos_y + dy[shape][i] ;
if( y>=1)
{
table[y][x] = 0 ;
}
}
}
public void MoveToPos(int cx,int cy) //移动到某个位置
{
for(int i=0;i<4;i++)
{
int x = cx + dx[shape][i] ;
int y = cy + dy[shape][i] ;
if( y>=1 )
table[y][x] = 1 ;
}
}
public void MoveRight() //向右移动
{
int y = pos_y ;
int x = pos_x + 1 ;
CleanCurrentBrick() ;
if(! IsMoveable(x,y) )
{
MoveToPos(pos_x,pos_y) ;
return ;
}
pos_x = pos_x + 1 ;
MoveToPos(pos_x,pos_y) ;
DrawBrick() ;
}
public void MoveLeft() //向右移动
{
int y = pos_y ;
int x = pos_x - 1 ;
CleanCurrentBrick() ;
if(! IsMoveable(x,y) ) //判断是否能够移动
{
MoveToPos(pos_x,pos_y) ;
return ;
}
pos_x = pos_x - 1 ;
MoveToPos(pos_x,pos_y) ;
DrawBrick() ;
}
public int addspeed()//加速度
{
if(X==100)
{
speed++;
X=0;
}
return speed;
}
public void BrickSpeed() //方块下落的速度
{
int y = pos_y + speed ;
int x = pos_x ;
CleanCurrentBrick() ;
if(! IsMoveable(x,y) )
{
MoveToPos(pos_x,pos_y) ;
return ;
}
pos_y = pos_y + 1 ;
MoveToPos(pos_x ,pos_y) ;
DrawBrick() ;
}
public void CheckLineUp() //清除满的一行
{
int count ;
for(int i=20;i>=1;i--)
{
count = 0 ;
for(int j=1;j<11;j++)
{
if( table[i][j]== 1 ) //判断是否每行是不是都被块填满
count++ ;
}
if(count == 10) //填满则对起进行如下操作
{
X++; //每消掉一行,对其进行记分
label.setText("score:"+X);
for(int n=i;n>=1;n--)
for(int m=1;m<=10;m++)
table[n][m] = table[n-1][m] ;
for(int k=1;k<=10;k++)
table[1][k] = 0 ;
i++ ; // make current the same as last time
}
}
DrawBrick() ; //对其进行重画
}
public void BrickChange() //方块改变形状
{
int shapeTemp = shape ;
CleanCurrentBrick() ;
shape = shape + 1 ;
if(shape % 4 == 0)
shape = shape - 4 ;
if( ! IsMoveable(pos_x,pos_y) )
shape = shapeTemp ;
MoveToPos(pos_x,pos_y) ;
DrawBrick() ;
}
public boolean IsDownable()
{
CleanCurrentBrick() ;
for(int i=0;i<4;i++) //判断他的每一种变形是不是都能移动
{
int x = pos_x + dx[shape][i] ;
int y = pos_y + dy[shape][i] + 1 ; //下移一步
if(table[y][x] == 1 && y >= 1)
{
MoveToPos(pos_x,pos_y) ;
return false ;
}
}
return true ;
}
public boolean IsGameOver() //判断游戏是否结束,每行都被块填满了,则游戏结束
{
for(int i=1;i<11;i++)
{
if( table[1][i] == 1 )
{
return true ;
}
}
return false ;
}
private class TimeListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
if( ! IsDownable() )
{
CheckLineUp() ;
if( IsGameOver() )
{
timer.stop() ;
JOptionPane.showMessageDialog(
null, "Game Over", "GameOver", JOptionPane.INFORMATION_MESSAGE);
}
else
NewBrick() ;
}
else
{
CleanCurrentBrick() ;
pos_y++ ;
MoveToPos(pos_x,pos_y) ;
DrawBrick() ;
}
}
}
private class KeyProcessor implements KeyListener
{
public void keyPressed(KeyEvent event)
{
int key = event.getKeyCode() ;
if(key == KeyEvent.VK_LEFT)
MoveLeft() ;
else if(key == KeyEvent.VK_RIGHT)
MoveRight() ;
else if(key == KeyEvent.VK_UP)
BrickChange() ;
else if(key == KeyEvent.VK_DOWN)
BrickSpeed() ;
}
public void keyReleased(KeyEvent event) { }
public void keyTyped(KeyEvent event) { }
}
private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
NewBrick();
if(event.getSource()==button1)
timer.start() ;
if(event.getSource()==button2)
timer.stop();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -