import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.Graphics;
import java.io.*;
import java.util.StringTokenizer;
public class BlockGame{
public BlockGame(){
BlockFrame frame =new BlockFrame();//游戏界面居中显示
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize =frame.getSize();
if(frameSize.height>screenSize.height){
frameSize.height= screenSize.height;
}
if(frameSize.width>screenSize.width){
frameSize.width=screenSize.width;
}
frame.setLocation((screenSize.width-frameSize.width)/2,(screenSize.height-frameSize.height)/2);
frame.setVisible(true);
}
// 主方法,游戏入口
public static void main(String []args){
new BlockGame();
}
}
class BlockFrame extends JFrame implements ActionListener{
JPanel contentPane;
BorderLayout borderLayout1=new BorderLayout(); //定义菜单
JMenuBar jMenuBar1=new JMenuBar();
JMenu jMenu1=new JMenu();
JMenuItem start = new JMenuItem();
JMenuItem pause=new JMenuItem();
JMenuItem end=new JMenuItem();
JMenuItem quit=new JMenuItem();
JMenu jMenu2=new JMenu();
JMenuItem level=new JMenuItem();
JMenuItem score=new JMenuItem();
JMenu jMenu3=new JMenu();
JMenuItem about=new JMenuItem();
JPanel jPanel1=new JPanel();
JLabel jLabel1=new JLabel();
Game game=new Game(this);
public BlockFrame(){
contentPane=(JPanel) this.getContentPane();
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(412,535));
this.setTitle("俄罗斯方快游戏");
jMenu1.setText("游戏");
start.setText("开始游戏");
pause.setText("暂停游戏");
end.setText("结束游戏");
quit.setText("关闭");
jMenu2.setText("控制");
level.setText("设置级别");
score.setText("查看分数");
jMenu3.setText("帮助");
about.setText("关于");
jMenuBar1.add(jMenu1);
jMenuBar1.add(jMenu2);
jMenuBar1.add(jMenu3);
jMenu1.add(start);
jMenu1.add(pause);
jMenu1.add(end);
jMenu1.addSeparator();
jMenu1.add(quit);
jMenu2.add(level);
jMenu2.add(score);
jMenu3.add(about);
contentPane.add(jMenuBar1,BorderLayout.NORTH);
contentPane.setBackground(new Color(80,123,166));
jLabel1.setText("Z 键控制方块旋转");
contentPane.add(jPanel1,BorderLayout.SOUTH);
jPanel1.add(jLabel1,null);
contentPane.add(game,BorderLayout.CENTER);
start.addActionListener(this);
pause.addActionListener(this);
end.addActionListener(this);
quit.addActionListener(this);
score.addActionListener(this);
level.addActionListener(this);
about.addActionListener(this);
}
Thread thread=null;
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand()=="开始游戏"){
if(thread==null){
thread=new Thread(game);
thread.start(); }
else {try{thread.resume();} catch(Exception er){} }
jLabel1.setText("开始游戏");
this.repaint();
}
else if(e.getActionCommand()=="暂停游戏"){
if(thread!=null){
try{thread.suspend();} catch(Exception er){}
jLabel1.setText("暂停游戏");
this.repaint();} else ;
}
else if(e.getActionCommand()=="结束游戏"){
if(thread!=null) { try{thread.stop(); insertScoreReport(game.score); dispose(); new BlockGame(); } catch(Exception er){} thread=null;
jLabel1.setText("结束游戏");
this.repaint();} else ;
}else if(e.getActionCommand()=="关闭"){
dispose();
System.exit(0);
}
else if(e.getActionCommand()=="设置级别"){
LevelDialog ld = new LevelDialog(this);
this.repaint();
}
else if(e.getActionCommand()=="查看分数"){
ReportDialog ad =new ReportDialog(this);
this.repaint();
}
else if(e.getActionCommand()=="关于"){
AboutDialog ad = new AboutDialog (this);
this.repaint();
}
}
public void insertScoreReport(int nTheScore){
Score score=new Score();
System.out.print(nTheScore);
if(score.isScoreTop(nTheScore)){
SaveScoreDialog d = new SaveScoreDialog(this,nTheScore);
}
this.repaint();
}
}
/**********************************************************************/
class Game extends JPanel implements Runnable,KeyListener{
int x1=150;int y1=2;
int x2=25,y2=25;
boolean f=true; int dengj; int sd=600;
int x;int y;boolean ff=true;boolean bbb=true;boolean fff=true; boolean bb=true; int bbbbb=0; int score=0; int nnn=0; int suliang=0; BlockFrame t; BorderLayout borderLayout1 = new BorderLayout();
//定义一个数组容器22*16
final int COLS=16;
final int ROWS=22;
int s[][]=new int[ROWS][COLS];
//装预览方块的数组
int s1[][]=new int[4][4];
int s2[][]=new int[4][4];
final int s3[][]=new int[4][4];
//7种方块
int ss1[] []={{0,0,0,0},{0,0,1,0},{0,0,1,0},{0,0,1,1}}; //L
int ss2[] []={{0,0,0,0},{0,0,2,0},{0,2,2,2},{0,0,0,0}}; //山
int ss3[] []={{0,0,0,0},{0,0,3,0},{0,0,3,0},{0,3,3,0}}; //反L
int ss4[] []={{0,0,0,0},{4,4,0,0},{0,4,4,0},{0,0,0,0}}; //Z
int ss5[] []={{0,0,0,0},{0,5,5,0},{5,5,0,0},{0,0,0,0}}; //反Z
int ss6[] []={{0,0,0,0},{0,6,6,0},{0,6,6,0},{0,0,0,0}}; //田
int ss7[] []={{0,0,0,0},{0,0,0,0},{8,8,8,8},{0,0,0,0}}; //一
public Game(BlockFrame t){
super();
dengj=1;
this.setLayout (null);
this.t=t; this.setLayout(borderLayout1);
t.addKeyListener(this);
}
private void csfk(){
bbbbb=(int)(Math.random()*7);
switch(bbbbb){
case 0:s1=ss1;break;case 1:s1=ss2;break;case 2:s1=ss3;break;case 3:s1=ss4;break;case 4:s1=ss5;break;case 5:s1=ss6;break;case 6:s1=ss7;break;
}}
public void update(Graphics g){paint(g);f=false;
}
public synchronized void paint(Graphics g){
int k=0;
if(f==true){
for(int r=4;r<ROWS;r+=1){
for(int t=3;t<COLS-3;t+=1){
Color rrr=new Color(40,40,40);
g.setColor(rrr);
g.fill3DRect(x1,y1,x2,y2,fff);
x1+=x2;
} x1=150;y1+=y2;
}x1=150; y1=2;}
for(int r=4;r<ROWS;r+=1){
for(int t=3;t<COLS-3;t+=1){
if(s[r][t]==0){Color rrr=new Color(70,70,70);
g.setColor(rrr); g.fill3DRect(x1,y1,x2,y2,fff);
x1+=x2;}
else{
switch(s[r][t]){
case 1: {g.setColor(Color.blue);break;}
case 2: {g.setColor(Color.orange);break;}
case 3: {g.setColor(Color.green);break;}
case 4: {g.setColor(Color.yellow);break;}
case 5: {g.setColor(Color.magenta);break;}
case 6: {g.setColor(Color.red);break;}
case 7: {g.setColor(Color.pink);break;}
case 8: {g.setColor(Color.cyan);break;}
}
g.fill3DRect(x1,y1,x2,y2,ff); x1+=x2;}
} x1=150;y1+=y2;
}x1=150; y1=2;
Color rrr=new Color(50,50,50);
g.setColor(rrr);
int xx=x2; int yy=220;
for(int i=0;i<4;i++){
for(int r=0;r<4;r++){
if(s1[i][r]==0){g.setColor(rrr); g.fill3DRect(xx,yy,x2,y2,fff);
xx+=x2;
}
else{
switch(s1[i][r]){
case 1: {g.setColor(Color.blue);break;}
case 2: {g.setColor(Color.orange);break;}
case 3: {g.setColor(Color.green);break;}
case 4: {g.setColor(Color.yellow);break;}
case 5: {g.setColor(Color.magenta);break;}
case 6: {g.setColor(Color.red);break;}
case 7: {g.setColor(Color.pink);break;}
case 8: {g.setColor(Color.cyan);break;}
}
g.fill3DRect(xx,yy,x2,y2,fff); xx+=x2;}
} xx=x2;;yy+=y2;
}
g.setColor(Color.blue); g.fill
评论0