package game;

import util.Config;
import util.MyFrame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.LinkedList;

public class Tetrisblok {
    //方块的类型
     int blockType;
    //方块的状态
     int turnState;
     int x;
     int y;
     int i=0;
     int j=0;
     Block block;
    //定义已经放下的方块
    static LinkedList<Tetrisblok> list=new LinkedList<>();

  public static  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, 0, 0, 0, 0, 0 },
                    { 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 } },
            // z
            {       { 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                    { 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 },
                    { 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                    { 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 } },
            // j
            {       { 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0 },
                    { 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                    { 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 },
                    { 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
            // o
            {       { 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                    { 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                    { 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                    { 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
            // l
            {
                    { 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0 },
                    { 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                    { 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 },
                    { 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
            // t
            {       { 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                    { 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 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 Tetrisblok(int blockType,int turnState){
        this.blockType=blockType;
        this.turnState=turnState;
    }

    public Tetrisblok(Block block) {
        this.block=block;
        list.add(new Tetrisblok((int)(Math.random()*1000)%7,(int)(Math.random()*1000)%4));
        initblock();
        block.drawwall();
        Timer timer = new Timer((int)(1000/block.speed), new TimerListener());
        timer.start();
    }




    //画下一个方块
    public void drawNextBlock(Graphics g){
        for (j = 0; j < 16; j++) {
            if (shapes[list.get(1).blockType][1][j] == 1) {
                g.fillRect(Config.BlockSize*(Config.COL) + (j % 4 + 1)
                        * Config.BlockSize, (Config.ROW-10)*Config.BlockSize+(j / 4) * Config.BlockSize, Config.BlockSize, Config.BlockSize);
            }
        }
    }
    //初始化方块位置
    public void initblock(){

        x= Config.COL/2;
        y=1;
        if (gameover(x, y)) {
            block.newmap();
            block.score = 0;
            JOptionPane.showMessageDialog(null, "GAME OVER");
        }

        list.add(new Tetrisblok((int)(Math.random()*1000)%7,(int)(Math.random()*1000)%4));
    }

    //旋转的方法
    public void turn(){
        int tempturnState=turnState;
        //赋值一次改变一次形状
        turnState =(turnState+1)%4;
        if(!block.blow(x,y ,blockType ,turnState )){
        }
        if(block.blow(x, y,blockType ,turnState )){
            turnState=tempturnState;
        }
    }

    //左移的方法
    public void left(){
        if(!block.blow(x-1, y, blockType,turnState )){
            x=x-1;
        }
    }

    // 右移的方法
    public void right(){
        if(!block.blow(x+1, y, blockType,turnState )){
            x=x+1;
        }
    }
    //下降的方法
    public void down(){
        if(!block.blow(x, y + 1, blockType, turnState)){
            y=y+1;
           //block.add(x, y, blockType, turnState);
           block.delline();
        }
        if(!block.blow(x, y + 1, blockType, turnState)){

        }
    }
    // 判断你挂的方法
    public  boolean gameover(int x, int y) {
        if (block.blow(x, y, blockType, turnState)) {
            return true;
        }
        return false;
    }
    // 画方块的的方法
    public void draw(Graphics g) {

        // 画当前方块
        for (j = 0; j < 16; j++) {
            if (shapes[blockType][turnState][j] == 1) {
                //一个j%4另一个j/4是为了使x,y坐标不会形成对应关系
                g.fillOval((j % 4 + x + 1) * Config.BlockSize, (j/4 + y) * Config.BlockSize, Config.BlockSize, Config.BlockSize);
            }
        }
        // 画已经固定的方块
        for (j = 0; j < Config.ROW-1; j++) {
            for (i = 0; i < Config.COL-1; i++) {

                if (block.map[i][j] == 1) {

                    g.fillOval(i * Config.BlockSize, j * Config.BlockSize, Config.BlockSize, Config.BlockSize);

                }
                if (block.map[i][j] == 2) {
                    Color c = g.getColor();
                    g.setColor(Color.gray);
                    g.drawRect(i * Config.BlockSize, j * Config.BlockSize, Config.BlockSize, Config.BlockSize);
                    g.setColor(c);
                }
            }
        }

    }

    // 定时器监听
    boolean flag;
    class TimerListener implements ActionListener {
        public void  actionPerformed(ActionEvent e) {

            synchronized (this) {


                if (!block.blow(x, y + 1, blockType, turnState)) {
                    y = y + 1;
                    block.delline();

                }

                if (block.blow(x, y + 1, blockType, turnState)) {

                    if (flag == true) {
                        block.add(x, y, blockType, turnState);
                        block.delline();
                        initblock();
                        list.remove(0);
                        blockType=list.get(0).blockType;
                        turnState=list.get(0).turnState;
                        flag = false;
                    }
                    flag = true;
                }
            }

        }
    }

    // 键盘监听
    public void keyPressed(KeyEvent e) {
        switch (e.getKeyCode()) {
            case KeyEvent.VK_DOWN:
                down();
                break;
            case KeyEvent.VK_UP:
                turn();
                break;
            case KeyEvent.VK_RIGHT:
                right();
                break;
            case KeyEvent.VK_LEFT:
                left();
                break;
        }

    }


}
最近下载更多
ClydeSon  LV5 2023年12月27日
1474371745  LV1 2023年12月23日
微信网友_6699076084797440  LV7 2023年10月30日
lsglsg9  LV22 2022年9月27日
uuuuuu1  LV1 2022年6月3日
姜广坤  LV14 2022年5月21日
二十  LV1 2021年12月8日
15661649928  LV1 2020年12月12日
jrh947869206  LV4 2020年12月7日
Zcjiayouya  LV1 2020年9月6日
最近浏览更多
ClydeSon  LV5 2023年12月27日
1474371745  LV1 2023年12月23日
iceworld 2023年11月30日
暂无贡献等级
1112WHQ  LV7 2023年11月3日
微信网友_6699076084797440  LV7 2023年10月30日
大白8加3  LV1 2023年8月18日
W1234561 2023年7月18日
暂无贡献等级
3210619837 2023年6月23日
暂无贡献等级
小安同学  LV7 2023年5月11日
wind12 2023年3月26日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友