#一共创建两个文件
#第一个gameMain是总文件
package ELS; //别忘了创建包 和引入模块
import javax.swing.*;
//总的模块 实现其他的模块和方法
public class gameMain extends JFrame{
gameMain(){
super("俄罗斯方块");
gameWin g = new gameWin();//添加gameWin
add(g);//意思是把gameWin的东西加入面板
setSize(380,420);
setLocation(200,200);
setVisible(true);
}
public static void main(String[] args) {
new gameMain();
}
}
#第二个gameWin是画面板的文件
package ELS;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;//意思生成随机是变量
import javax.swing.Timer;//时间 每多少毫秒 将通知其监听器的 Timer
//用来绘制面板
public class gameWin extends JPanel implements ActionListener,KeyListener{
//ActionListener 意思是增加监听 KeyListener 意思是键盘监听 JPanel 意思是继承面向对象
JButton newGame = new JButton("开始");//按钮
JButton endGame = new JButton("退出");
int fenShu = 0,speed = 0,tFenShu = 0;//分数和速度
gameAct[] act = new gameAct[4]; //意思是方块 是4*4 的大小 并且每个方块都是 4个小方块 绘制而成
gameAct[] actTemp = new gameAct[4]; //下一个方块 所用的数组
Random r = new Random();//生成随机变量
boolean start = false;
int[][] map = new int[10][18];//是用来存放块 用的一个数组
Timer t;
int temp;
JDialog dialot = new JDialog();
JLabel label = new JLabel();
JButton button = new JButton("Ok");
public gameWin() {
for(int i = 0;i<act.length;i++) {
act[i] = new gameAct();//所有的都实例化出来
}
for(int i = 0;i<actTemp.length;i++) {
//下一个方块 实例化出来
actTemp[i] = new gameAct();//所有的都实例化出来
}
for(int i = 0;i<10;i++) {
//用数组来存放位置
for(int j = 0;j<18;j++) {
map[i][j] = 0;// 意思是方块 的坐标等于0 的时候不会绘制 等于1的时候才开始绘制 意思是开始刚落下
}
}
setLayout(new FlowLayout(FlowLayout.RIGHT));//意思是向右
add(newGame);
add(endGame);
//增加监听事件
newGame.addActionListener(this);
endGame.addActionListener(this);
addKeyListener(this);//意思是给它本身也增加一个监听事件
dialot.setLayout(new GridLayout(2,1));//.setLayout意思是窗口布局样式,new GridLayout(2,1)意思是 网络布局样式 表示为 1行,两列
dialot.add(label);
dialot.add(button);
button.addActionListener(this);
dialot.setSize(200, 100);
dialot.setLocation(200,200);
dialot.setVisible(false);
}
//绘制矩形框
public void paintComponent(Graphics g) {
//面板
super.paintComponent(g);
g.drawRect(9, 10, 200, 360);//.darwRect() 意思是绘制空的矩形框
g.drawString("分数"+fenShu, 220, 60);//。drawString 意思是绘制文字
g.drawLine(220,65,360,65);//.drawLine 意思是绘制线
g.drawString("速度"+speed, 220, 90);
g.drawLine(220,95,360,95);
g.drawString("下一个方块", 250, 120);
g.drawString("俄罗斯方块", 220, 340);
g.drawString("lihao",220,360);
///
if(start) {
//画放块
g.setColor(new Color(r.nextInt(250),r.nextInt(250),r.nextInt(250)));//.setColor(new Color(255,0,0) 意思是绘制颜色
for(int i =0;i<4;i