package tetris;
import java.awt.Font;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JPanel;
import javax.swing.Timer;
/**
* 功能:游戏面板<br>
*/
public class GamePanel extends JPanel implements KeyListener,ActionListener
{
private static final long serialVersionUID = 1L;
/** 游戏逻辑 */
private GameLogic gameLogic;
/** 游戏地图(共21行12列,该地图包含墙和固定死的方块信息) */
private byte[][] map;
/** 是否接收键盘信息(true-接收,false-不接收) */
private boolean isAcceptKey = true;
/** 定时器 */
private Timer timer;
/**
* 功能:构造函数<br>
*/
public GamePanel()
{
//与主窗口大小保持一致(去掉菜单高度)
this.setSize(400,440);
//获得焦点(没焦点就不能截获键盘监听)
this.setFocusable(true);
//键盘监听
this.addKeyListener(this);
//游戏逻辑
this.gameLogic = new GameLogic();
//初始化游戏
this.initGame();
//定时器
timer = new Timer(500,this);
timer.start();
}
/**
* 功能:初始化游戏<br>
*/
private void initGame()
{
//初始化游戏逻辑
this.gameLogic.init();
//得到地图
this.map = this.gameLogic.getMap();
//重画地图
this.repaint();
}
/**
* 功能:绘图<br>
* 备注:给我地图剩下的你就不用管了。<br>
*/
@Override
public void paint(Graphics g)
{
int blockSize = 20; //默认方块大小
int row = 0;
int column = 0;
//调用父类,让其做一些事前的工作,如刷新屏幕等
super.paint(g);
//画大地图中已经固定的方块和围墙
//==================地图==================
//{-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1},
//{-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1},
//{-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1},
//{-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1},
//{-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1},
//{-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1},
//{-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1},
//{-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1},
//{-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1},
//{-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1},
//{-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1},
//{-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1},
//{-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1},
//{-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1},
//{-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1},
//{-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1},
//{-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1},
//{-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1},
//{-1, 0, 0, 7, 7, 0, 0, 0, 0, 0, 0, -1},
//{-1, 0, 0, 7, 7, 0, 0, 1, 1, 1, 1, -1},
//{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1}
//==================结束==================
row = this.map.length;
column = this.map[0].length;
for(int i=0;i<row;i++)
{
for(int j=0;j<column;j++)
{
if(this.map[i][j] == -1) //墙
{
g.setColor(Color.GRAY);
g.fillRect(j * blockSize,i * blockSize,blockSize,blockSize);
g.setColor(Color.WHITE);
g.drawRect(j * blockSize,i * blockSize,blockSize,blockSize);
}
else if(this.map[i][j] > 0) //方块
{
switch(this.map[i][j])
{
case 1:
g.setColor(Color.RED);
break;
case 2:
g.setColor(Color.ORANGE);
break;
case 3:
g.setColor(Color.YELLOW);
break;
case 4:
g.setColor(Color.GREEN);
break;
case 5:
g.setColor(Color.BLUE);
break;
case 6:
g.setColor(Color.PINK);
break;
case 7:
g.setColor(Color.MAGENTA);
break;
}
g.fillRect(j * blockSize,i * blockSize,blockSize,blockSize);
g.setColor(Color.WHITE);
g.drawRect(j * blockSize,i * blockSize,blockSize,blockSize);
}
}
}
//画当前方块
byte[][] curShape = this.gameLogic.getShape();
row = curShape.length;
column = curShape[0].length;
for(int i=0;i<row;i++)
{
for(int j=0;j<column;j++)
{
if(curShape[i][j] > 0) //方块
{
switch(curShape[i][j])
{
case 1:
g.setColor(Color.RED);
break;
case 2:
g.setColor(Color.ORANGE);
break;
case 3:
g.setColor(Color.YELLOW);
break;
case 4:
g.setColor(Color.GREEN);
break;
case 5:
g.setColor(Color.BLUE);
break;
case 6:
g.setColor(Color.PINK);
break;
case 7:
g.setColor(Color.MAGENTA);
break;
}
g.fillRect((j + this.gameLogic.getShapeColumn()) * blockSize,(i + this.gameLogic.getShapeRow()) * blockSize,blockSize,blockSize);
g.setColor(Color.WHITE);
g.drawRect((j + this.gameLogic.getShapeColumn()) * blockSize,(i + this.gameLogic.getShapeRow()) * blockSize,blockSize,blockSize);
}
}
}
//画记分牌
g.setColor(Color.BLACK);
g.setFont(new Font("宋体",Font.PLAIN,12));
g.drawString("当前分数: " + this.gameLogic.getScore(),this.map[0].length * blockSize + 20,30);
//画下一个方块
g.drawString("下一个方块是:",this.map.length * blockSize + 20,60);
byte[][] nextShape = this.gameLogic.getNextShape();
row = nextShape.length;
column = nextShape[0].length;
for(int i=0;i<row;i++)
{
for(int j=0;j<column;j++)
{
if(nextShape[i][j] > 0)
{
switch(nextShape[i][j])
{
case 1:
g.setColor(Color.RED);
break;
case 2:
g.setColor(Color.ORANGE);
break;
case 3:
g.setColor(Color.YELLOW);
break;
case 4:
g.setCo