| 网站首页 | 业界新闻 | 群组 | 交易 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
共有 320 人关注过本帖
标题:俄罗斯方块
只看楼主 加入收藏
林月儿
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:湖南
等 级:版主
威 望:111
帖 子:1788
专家分:7795
注 册:2015-3-19
结帖率:100%
  已结贴   问题点数:20  回复次数:3   
俄罗斯方块

程序代码:
package com.huawei.test;

import java.awt.*;
import java.awt.event.*;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.*;

public class Test0127 extends JPanel {
    /**
     *
     
*/
    private static final long serialVersionUID = 1L;
    @Override
    public void paint(Graphics g) {
        g.setColor(Color.white);
        g.fillRect(0, 0, 400, 520);
        for(int i = 0; i < area.length; i++) {
            for(int j = 0; j < area[i].length; j++) {
                if(area[i][j] == 0) {
                    g.setColor(Color.lightGray);
                    for(int ii = 0; ii < moveObj.length; ii++) {
                        if(moveObj[ii]/20==i && moveObj[ii]%20==j) {
                            g.setColor(Color.pink);
                            break;
                        }
                    }
                } else {
                    g.setColor(Color.pink);
                }
                g.fill3DRect(i*size, (area[i].length-1-j)*size, size, size, true);
                g.setColor(Color.pink);
                g.setFont(new Font("gbk", Font.BOLD|Font.ITALIC, 24));
                g.drawString("得分:"+score, 300, 40);
            }
        }
    }
    long moveIndex = 0;
    int size = 24;
    int[][] area = new int[12][20];
    int[] moveObj = new int[4];
    int score;
    public Test0127() {
        JFrame jframe = new JFrame("Test");
        jframe.add(this);
        jframe.setSize(400, 520);
        jframe.setLocation(200, 40);
        jframe.setVisible(true);
        jframe.setResizable(false);
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        moveObj = new int[] {20-2, 20-1, 20*2-2, 20*2-1};
        jframe.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if(e.getKeyCode() == KeyEvent.VK_LEFT) {
                    if(moveObj[0] > 20) {
                        for(int i = 0; i < moveObj.length; i++) {
                            moveObj[i] = moveObj[i] - 20;
                        }
                    }
                }
                if(e.getKeyCode() == KeyEvent.VK_RIGHT) {
                    if(moveObj[3] < (12-1)*20) {
                        for(int i = 0; i < moveObj.length; i++) {
                            moveObj[i] = moveObj[i] + 20;
                        }
                    }
                }
            }
        });
        new Timer().schedule(new TimerTask() {
            
            @Override
            public void run() {
                if(moveIndex % 4 == 0) {
                    int[] tmpMoveObj = new int[4];
                    if(moveObj[0] % 20 > 0) {
                        for(int i = 0; i < moveObj.length; i++) {
                            tmpMoveObj[i] = moveObj[i] - 1;
                            if(area[tmpMoveObj[i]/20][tmpMoveObj[i]%20] == 1) {
                                tmpMoveObj = null;
                                break;
                            }
                        }
                        moveObj = tmpMoveObj == null ? moveObj : tmpMoveObj;
                    } else {
                        tmpMoveObj = null;
                    }
                    if(tmpMoveObj == null) {
                        for(int i = 0; i < moveObj.length; i++) {
                            area[moveObj[i]/20][moveObj[i]%20] = 1;
                        }
                        for(int j = 20 - 1; j >= 0; j--) {
                            int k = 0;
                            for(int i = 0; i < area.length; i++)k+=area[i][j];
                            if(k == area.length) {
                                for(int jj = j; jj < 20-1; jj++) {
                                    for(int i = 0; i < area.length; i++)area[i][jj]=area[i][jj+1];
                                }
                                score++;
                            }
                        }
                        moveObj = new int[] {20-2, 20-1, 20*2-2, 20*2-1};               
                    }
                }
                repaint();
                moveIndex++ ;
            }
        }, 10, 100);
    }
    public static void main(String[] args) {
        new Test0127();
    }
}

附件: 您没有浏览附件的权限,请 登录注册
2019-01-27 15:15
疯狂的小a
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:29
帖 子:404
专家分:1787
注 册:2018-2-6
  得分:14 
怎麽衹有正方形呢,不好玩

假如人生没有梦想,和咸鱼有什么区别!
2019-01-27 15:32
林月儿
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:湖南
等 级:版主
威 望:111
帖 子:1788
专家分:7795
注 册:2015-3-19
  得分:0 
关键它不要钱

知命以下无敌
2019-01-27 15:41
林月儿
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:湖南
等 级:版主
威 望:111
帖 子:1788
专家分:7795
注 册:2015-3-19
  得分:0 
看到c板块有人发,就随手写了百余行的初步实现,当然了这种游戏这么点代码肯定是不可能写完的

下一贴就不写游戏了,写个之前有人提到的数据库方面的吧。

知命以下无敌
2019-01-27 16:00







关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.065937 second(s), 8 queries.
Copyright©2004-2019, BCCN.NET, All Rights Reserved