Java版俄罗斯方块

 

转载于:http://www.iteye.com/topic/595321

程序经过稍微的修改如下:

import java.awt.*;   
import java.awt.event.*;   
import javax.swing.*;   
public class Terris extends JFrame implements Runnable, KeyListener {
    static final String MY_PATH="D:/workspace/Russian/src/img/";
    private short isPlaying=0,xOffSet = 2, yOffSet = 0, blockType = (short) Math.round(Math.random() * 6), blockRotation = 0, blockColor = (short) Math.round(Math.random() * 5);   
    private short matrix[][] = new short[21][10];//   
    private short block[][][][] = {{{ { 0, 1, 0, 0 }, { 0, 1, 0, 0 }, { 0, 1, 0, 0 },{ 0, 1, 0, 0 } },/* l */
                                    { { 0, 0, 0, 0 }, { 1, 1, 1, 1 }, { 0, 0, 0, 0 },{ 0, 0, 0, 0 } } },/*-*/
                                    
                                    {{ { 0, 0, 0, 0 }, { 1, 1, 0, 0 }, { 0, 1, 1, 0 },{ 0, 0, 0, 0 } }, /* z */
                                    { { 0, 0, 0, 0 }, { 0, 0, 1, 0 }, { 0, 1, 1, 0 },{ 0, 1, 0, 0 } } },/* z| */
                                    
                                    {{ { 0, 0, 0, 0 }, { 0, 1, 1, 0 }, { 1, 1, 0, 0 },{ 0, 0, 0, 0 } }, /* xz */
                                    { { 0, 1, 0, 0 }, { 0, 1, 1, 0 }, { 0, 0, 1, 0 },{ 0, 0, 0, 0 } } },/* xz| */
                                    
                                    { { { 0, 0, 0, 0 }, { 0, 1, 1, 0 }, { 0, 1, 1, 0 }, { 0, 0, 0, 0 } } },/** []*/
                                    
                                    {{ { 0, 1, 1, 0 }, { 0, 1, 0, 0 }, { 0, 1, 0, 0 },{ 0, 0, 0, 0 } },
                             { { 0, 0, 0, 0 }, { 1, 1, 1, 0 }, { 0, 0, 1, 0 },{ 0, 0, 0, 0 } },
                             { { 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 } } },/* f */
                             
                             {{ { 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 } },
                             { { 0, 1, 0, 0 }, { 0, 1, 0, 0 }, { 0, 1, 1, 0 },{ 0, 0, 0, 0 } },
                             { { 0, 0, 0, 0 }, { 1, 1, 1, 0 }, { 1, 0, 0, 0 },{ 0, 0, 0, 0 } } },/* xf */    
                             
                             {{ { 0, 1, 0, 0 }, { 1, 1, 1, 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 } },
                             { { 0, 0, 0, 0 }, { 1, 1, 1, 0 }, { 0, 1, 0, 0 },{ 0, 0, 0, 0 } },
                             { { 0, 1, 0, 0 }, { 1, 1, 0, 0 }, { 0, 1, 0, 0 },{ 0, 0, 0, 0 } } } };/* t */  
    private Image[] images = {new ImageIcon(MY_PATH+"Red.gif").getImage(),new ImageIcon(MY_PATH+"Blue.gif").getImage(),new ImageIcon((MY_PATH+"Pink.gif")).getImage(),new ImageIcon((MY_PATH+"BBlue.gif")).getImage(),new ImageIcon((MY_PATH+"Orange.gif")).getImage(),new ImageIcon((MY_PATH+"Green.gif")).getImage(),new ImageIcon(MY_PATH+"Red.gif").getImage()};   
    public Terris() {   
        setSize(160, 335);   
        setVisible(true);   
        createBufferStrategy(2);   
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
        addKeyListener(this);   
    }   
    public void paint(Graphics g) {   
        Graphics tg = this.getBufferStrategy().getDrawGraphics();   
        tg.fillRect(5, 30, 150, 340);   
        for (int i = 0; i < 21; i++)   
            for (int j = 0; j < 10; j++) {   
                if (matrix[i][j] != 0)   
                    tg.drawImage(images[matrix[i][j]], j * 15 + 5, i * 15 + 15,null);   
                if (i < 4 && j < 4&& block[blockType][blockRotation][i][j] != 0)   
                    tg.drawImage(images[blockColor + 1],((j + xOffSet) * 15) + 5,((i + yOffSet) * 15) + 15, null);   
            }   
        this.getBufferStrategy().show();   
    }   
    public static void main(String[] args) {   
        new Thread(new Terris()).start();   
    }   
    public void run() {   
        while (isPlaying==0)   
            try {   
                if (check(0, 0, 0, 1))   
                    yOffSet += 1;   
                else {   
                    if (yOffSet == 0) {   
                        isPlaying = 1;   
                        continue;   
                    }   
                    freezeAndNew();   
                }   
                repaint();   
                Thread.sleep(600);   
            } catch (InterruptedException e) {   
            }   
    }   
    private boolean check(int left, int right, int up, int down) {   
        for (int i = 0; i < 4; i++)   
            for (int j = 0; j < 4; j++)   
                if (((xOffSet + j - left + right < 0 || xOffSet + j - left+ right >= 10) && block[blockType][((blockRotation + up) >= block[blockType].length ? 0: (blockRotation + up))][i][j] != 0)|| ((yOffSet + i + down >= 21) && block[blockType][((blockRotation + up) >= block[blockType].length ? 0: (blockRotation + up))][i][j] != 0)|| (block[blockType][((blockRotation + up) >= block[blockType].length ? 0: (blockRotation + up))][i][j] != 0 && matrix[yOffSet+ i + down][xOffSet + j - left + right] != 0))   
                    return false;   
        return true;   
    }   
    private void freezeAndNew() {   
        boolean[] clear = new boolean[4];   
        for (int i = 0; i < 4; i++){   
            for (int j = 0; j < 4; j++)   
                if (block[blockType][blockRotation][i][j] != 0)   
                    matrix[i + yOffSet][j + xOffSet] = (short) (blockColor + 1);   
            clear[i]=i + yOffSet>=matrix.length?false:(matrix[i + yOffSet][0]!=0&&matrix[i + yOffSet][1]!=0&&matrix[i + yOffSet][2]!=0&&matrix[i + yOffSet][3]!=0&&matrix[i + yOffSet][4]!=0&&matrix[i + yOffSet][5]!=0&&matrix[i + yOffSet][6]!=0&&matrix[i + yOffSet][7]!=0&&matrix[i + yOffSet][8]!=0&&matrix[i + yOffSet][9]!=0);   
        }   
        for(int i=0;i<clear.length;i++)   
            if(clear[i])   
                for(int j=yOffSet+i;j>0;j--)   
                    matrix[j]=matrix[j-1];   
        yOffSet = blockRotation = 0;   
        xOffSet = 2;   
        blockType = (short) Math.round(Math.random() * 6);   
        blockRotation = (short) Math.round((Math.random() * (block[blockType].length - 1)));   
        blockColor = (short) Math.round(Math.random() * 5);   
    }   
    public void keyPressed(KeyEvent e) {// 38-上 40-下 37-左 39-右   
        if ((e.getKeyCode() == 65 || e.getKeyCode() == 37) && check(1, 0, 0, 0)&&isPlaying==0) {// left   
            xOffSet--;   
        } else if ((e.getKeyCode() == 68 || e.getKeyCode() == 39)&& check(0, 1, 0, 0)&&isPlaying==0) {// right   
            xOffSet++;   
        } else if ((e.getKeyCode() == 87 || e.getKeyCode() == 38)&& check(0, 0, 1, 0)&&isPlaying==0) {// up   
            blockRotation = (short) ((blockRotation + 1) >= block[blockType].length ? 0: (blockRotation + 1));   
        } else if ((e.getKeyCode() == 83 || e.getKeyCode() == 40)&& check(0, 0, 0, 1)&&isPlaying==0) {// down   
                yOffSet += 1;   
        } else if((e.getKeyCode() == 83 || e.getKeyCode() == 40)&& isPlaying==0)   
                freezeAndNew();   
        repaint();   
    }   
    public void keyReleased(KeyEvent arg0) {   
    }   
    public void keyTyped(KeyEvent arg0) {   
    }   
}   

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值