package demo1;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
//编写俄罗斯方块主类
public class Tetris extends JPanel {
//声明正在下落的方块
private Tetromino currentOne = Tetromino.randomOne();
//声明将要下落的方块
private Tetromino nextOne = Tetromino.randomOne();
//声明游戏主区域
private Cell[][] wall = new Cell[18][9];
//声明单元格的值为48像素
private static final int CELL_SIZE = 48;
//声明游戏分数池
int[] scores_pool = {0,1,2,5,10};
//声明当前获得游戏的分数
private int totalScore = 0;
//当前已消除的行数
private int totalLine = 0;
//声明游戏的三种状态,分别是:游戏中、暂停、游戏结束
public static final int PLAYING = 0;
public static final int PAUSE = 1;
public static final int GAMEOVER = 2;
//声明变量存放当前游戏状态的值
private int game_state;
//声明一个数组,用来显示游戏状态
String[] show_state = {"P[pause]","C[continue]","S[replay]"};
//载入方块图片
public static BufferedImage I;
public static BufferedImage J;
public static BufferedImage L;
public static BufferedImage O;
public static BufferedImage S;
public static BufferedImage T;
public static BufferedImage Z;
public static BufferedImage backImage;
static {
try {
I = ImageIO.read(new File("images/I.png"));
J = ImageIO.read(new File("images/J.png"));
L = ImageIO.read(new File("images/L.png"));
O = ImageIO.read(new File("images/O.png"));
S = ImageIO.read(new File("images/S.png"));
T = ImageIO.read(new File("images/T.png"));
Z = ImageIO.read(new File("images/Z.png"));
backImage = ImageIO.read(new File("images/background.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void paint(Graphics g) {
g.drawImage(backImage, 0, 0, null);
//平移坐标轴
g.translate(22, 15);
//绘制游戏主区域
paintWall(g);
//绘制正在下落的四方格
paintCurrentOne(g);
//绘制下一个将要下落的四方格
paintNextOne(g);
//绘制游戏得分
paintScore(g);
//绘制游戏当前状态
paintState(g);
}
public void start(){
game_state = PLAYING;
KeyListener l = new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
switch (code){
case KeyEvent.VK_DOWN:
sortDropAction(); //下落一格
break;
case KeyEvent.VK_LEFT:
moveLeftAction(); //左移
break;
case KeyEvent.VK_RIGHT:
moveRightAction(); //右移
break;
case KeyEvent.VK_UP:
rotateRightAction(); //顺时针旋转
break;
case KeyEvent.VK_SPACE:
handDropAction(); //瞬间下落
break;
case KeyEvent.VK_P:
//判断当前游戏的状态
if(game_state == PLAYING){
game_state = PAUSE;
}
break;
case KeyEvent.VK_C:
//判断游戏状态
if(game_state == PAUSE){
game_state = PLAYING;
}
break;
case KeyEvent.VK_S:
//表示游戏重新开始
game_state = PLAYING;
wall = new Cell[18][9];
currentOne = Tetromino.randomOne();
nextOne = Tetromino.randomOne();
totalScore = 0;
totalLine = 0;
break;
}
}
};
//将俄罗斯方块窗口设置为焦点
this.addKeyListener(l);
this.requestFocus();
while(true){
//判断,当前游戏状态在游戏中时,每隔0.5秒下落
if(game_state == PLAYING){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
//判断能否下落
if(canDrop()){
currentOne.softDrop();
}else{
//嵌入到墙中
landToWall();
//判断能否消行
destroyLine();
//判断游戏是否结束
if(isGameOver()){
game_state = GAMEOVER;
}else{
currentOne = nextOne;
nextOne = Tetromino.randomOne();
}
}
}
repaint();
}
}
//创建顺时针旋转
public void rotateRightAction(){
currentOne.rotateRight();
//判断是否越界或者是否重合
if(outOfBounds() || coincide()){
currentOne.rotateLeft();
}
}
//瞬间下落
public void handDropAction(){
while(true){
//判断四方格能否下落
if(canDrop()){
currentOne.softDrop();
}else{
break;
}
}
//嵌入到墙中
landToWall();
//判断能否消行
destroyLine();
//判断游戏是否结束
if(isGameOver()){
game_state = GAMEOVER;
}else{
//游戏没有结束,继续生成新的四方格
currentOne = nextOne;
nextOne = Tetromino.randomOne();
}
}
//按键一次四方格下落一格
public void sortDropAction(){
//判断能否下落
if(canDrop()){
//当前四方格下落一格
currentOne.softDrop();
}else{
//将四方格嵌入到墙中
landToWall();
//判断能否消行
destroyLine();
//判断游戏是否结束
if(isGameOver()){
game_state = GAMEOVER;
}else{
//当游戏没有结束时,则继续生成新的四方格
currentOne = nextOne;
nextOne = Tetromino.randomOne();
}
}
}
//四方格嵌入到墙中
private void landToWall() {
Cell[] cells = currentOne.cells;
for (Cell cell : cells) {
int row = cell.getRow();
int col = cell.getCol();
wall[row][col] = cell;
}
}
//判断四方格能否下落
public boolean canDrop(){
Cell[] cells = currentOne.cells;
for (Cell cell : cells) {
int row = cell.getRow();
int col = cell.getCol();
//判断是否到达底部
if(row == wall.length - 1){
return false;
}else if(wall[row + 1][col] != null){ //判断是否有方块
return false;
}
评论0