import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.imageio.*;
import javax.swing.*;
import javax.swing.Timer;
public class MyGame extends JFrame {
public MyGame(){
GameBody gamebody=new GameBody();
gamebody.setBounds(5,10,500,600); //
gamebody.setOpaque(false);
gamebody.setLayout(null);
addKeyListener(gamebody);
add(gamebody);
int w=Toolkit.getDefaultToolkit().getScreenSize().width;
int h=Toolkit.getDefaultToolkit().getScreenSize().height;
final JButton login=new JButton(new ImageIcon("image/cxks.png"));
login.setContentAreaFilled(false);
login.setMargin(new Insets(0,0,0,0));
login.setBorderPainted(false);
login.setBounds(340,320,120,26);
gamebody.add(login);
login.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) { //登录的按钮
if(e.getSource()==login){
requestFocus(true); //获得焦点,不用失去焦点
gamebody.resetMap(); //重置地图
gamebody.drawWall(); //冲重新绘制边界墙体
gamebody.createshape(); //重新产生新的地图
gamebody.setStart(false); //唤醒定时下落的线程
gamebody.score=0; //将分数置为零
repaint();
}
}
});
final JButton pauselogin=new JButton(new ImageIcon("image/zt.png"));
pauselogin.setContentAreaFilled(false);
pauselogin.setMargin(new Insets(0,0,0,0));
pauselogin.setBorderPainted(false);
pauselogin.setBounds(340,370,120,26);
gamebody.add(pauselogin);
pauselogin.addMouseListener(new MouseListener(){ //暂停的按钮
//鼠标点击事件,可以分别判断不同的事件,做出不同的反应
public void mouseClicked(MouseEvent e){
if(e.getButton()==e.BUTTON1 ){ //单击左键暂停
gamebody.setStart(true); //将自动下落线程关闭
//requestFocus(true); //同时整个JFrame失去焦点,无法操作,但可以点击按钮
}
else if(e.getButton()==e.BUTTON3 ){ //右击暂停,继续游戏
gamebody.setStart(false); //唤醒自动下落线程
requestFocus(true);
}
/* if(e.getClickCount()==2){ //左键双击,也可以继续游戏
gamebody.setStart(false);
requestFocus(true);
}*/
}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
});
setTitle("俄罗斯方块");
setResizable(false);
setFocusable(true);
setBounds((w-500)/2,(h-600)/2,500,600);
setLayout(null);
setVisible(true);
setDefaultCloseOperation(3);
}
public static void main(String[] args) {
new MyGame();
}
//创建需要定义的局部变量和游戏类
class GameBody extends JPanel implements KeyListener{
private int shapeType=-1; //定义方块的类型 定义的为7种
private int shapeState=-1; //定义方块为何种状态,每种都有四种状态
private int nextshapeType=-1; //定义下一块产生的类型
private int nextshapeState=-1; //定义下一块的方块的状态
private final int CELL=25; //定义方格的大小
private int score=0; //定义显示的成绩
private int left; //定义初始图形与两边的墙的距离
private int top; //定义初始图形与上下墙的距离
private int i=0; //表示列
private int j=0; //表示行
public int flag=0;
public volatile boolean start=false; //暂停的判断条件,为轻量锁,保持同步的
Random randomcolor=new Random();
Random random=new Random();
//定义地图的大小,创建二位的数组
int[][] map=new int[13][23];
//初始化地图
public void resetMap(){
for(i=0;i<12;i++){
for(j=0;j<22;j++){ //遍历的范围不能小
map[i][j]=0;
}
}
}
//画围墙的方法
public void drawWall(){
for(j=0;j<22;j++) //0到21行
{
map[0][j]=2;
map[11][j]=2; //第0行和第11行为墙
}
for(i=0;i<12;i++){ //0到11列
map[i][21]=2; //第21行划墙
}
}
//定义随机的图形种类
private final int[][][] shapes=new int[][][]{
// 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, 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 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 } }
};
//产生新图形的方法
public void createshape(){
if(shapeType==-1&&shapeState==-1){
shapeType = random.nextInt(shapes.length);
shapeState = random.nextInt(shapes[0].length);
}else{
shapeType=nextshapeType;
shapeState=nextshapeState;
}
nextshapeType = random.nextInt(shapes.length);
nextshapeState = random.nextInt(shapes[0].length);
//shapeType=(int)(Math.random()*1000)%7;
评论0