`
dengminhui
  • 浏览: 167287 次
  • 来自: ...
社区版块
存档分类
最新评论

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) {   
    }   
}   

 

分享到:
评论

相关推荐

    java版俄罗斯方块

    java版俄罗斯方块java版俄罗斯方块java版俄罗斯方块java版俄罗斯方块java版俄罗斯方块java版俄罗斯方块java版俄罗斯方块java版俄罗斯方块java版俄罗斯方块java版俄罗斯方块java版俄罗斯方块java版俄罗斯方块java版...

    JAVA版俄罗斯方块

    【JAVA版俄罗斯方块】是一款基于JAVA编程语言实现的经典游戏,是初学者向高级阶段过渡的理想学习项目。这个项目不仅涵盖了基本的JAVA编程概念,还涉及到图形用户界面(GUI)设计、事件处理、多线程以及算法应用等多...

    Java版俄罗斯方块小游戏源码

    【Java版俄罗斯方块】 增加保存配置信息到文件的功能,声音设置、显示设置、关卡选择等配置信息在修改后将会保存在jar包同级目录下(以jar相同的文件名+.cfg后缀保存) 2010-10-05 【Java版俄罗斯方块】 这个程序...

    Java版俄罗斯方块游戏源程序

    Java版的俄罗斯方块游戏源程序是一套基于Java编程语言实现的经典休闲游戏。这个程序不仅提供了可执行的游戏体验,还为学习者提供了深入理解Java编程和游戏开发的宝贵资源。让我们一起探讨其中涉及的关键知识点。 ...

    达内Java版俄罗斯方块

    《达内Java版俄罗斯方块》是一款基于Java编程语言实现的经典游戏,旨在帮助学习者检验和复习Java SE(Java Standard Edition)的基础知识和编程技能。通过分析和理解这个游戏的源代码,我们可以深入学习Java的核心...

Global site tag (gtag.js) - Google Analytics