Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 俄罗斯方块的源码~~~过来看一下
jiang_sl





发贴: 13
积分: 0
于 2004-03-19 16:32 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
/**
*俄罗斯方块的源码
* 我测试了很久都通不过,
*程序是没有语法错误的
*大家帮我一把吧,
*把程序修改通过,基本要求是尽可能保持我的设计思路,代码修改量最小
*修改完成后,请把完整的代码贴出来,
*谢谢~~
**/

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Tetris extends JPanel {
  Game game = new Game();
  Workspace ws = new Workspace();
  public Tetris() { game.start(); }
  public void paint(Graphics g){
    g.setColor(Color.BLUE);
    for(int h = 0; h < ws.getWorkspaceHeight(); h++)
      for(int w = 0; w < ws.getWorkspaceWidth(); w++)
        if(game.getWorkspaceValue(w,h)==1){
          g.fill3DRect(w*12,h*12,11,11,true);
        }
    
  }
  public void addKeyListener(KeyEvent e){
    if(e.getKeyCode() == KeyEvent.VK_LEFT)
      game.moveLeft();
    if(e.getKeyCode() == KeyEvent.VK_DOWN)
      game.moveDown();
    if(e.getKeyCode() == KeyEvent.VK_RIGHT)
      game.moveRight();
    if(e.getKeyCode() == KeyEvent.VK_UP)
      game.turn();
  }
  /**
  public void run(){
    
    while(true){
      repaint();
    }
  }
  **/
  //public void repaint(){}
  public static void main(String[] args) {
    JFrame jf = new JFrame();
    Tetris te = new Tetris();
  //  Thread thread = new Thread(te);
  //  thread.start();
    Container cp = jf.getContentPane();
    cp.add(te);
    jf.setSize(350,500);
    jf.setVisible(true);
  }
}

/**
* 方块类定义了方块的基本图形和操作
*/
class Block {
  private static final int BLOCK_WIDTH = 12; // 方块的像素大小
  private static final int BLOCK_HEIGHT = 12;
  
  private static final int BLOCK_TYPE = 7; //方块的基本种类
  private static final int BLOCK_CORNER = 4;//方块的四个转角
  
  public int getBlockWidth() { return BLOCK_WIDTH; }
  public int getBlockHeight() { return BLOCK_HEIGHT; }
  
  public int getBlockType() { return BLOCK_TYPE; }
  public int getBlockCorner() { return BLOCK_CORNER; }
  
  private int[][] emptyShape = {{0, 0, 0, 0},
       {0, 0, 0, 0},
                 {0, 0, 0, 0},
                 {0, 0, 0, 0}};
  
  private int[][] tShape = {{0, 1, 0, 0},
       {1, 1, 1, 0},
               {0, 0, 0, 0},
               {0, 0, 0, 0}};
  
  private int[][] leftsShape = {{1, 1, 0, 0},
   {0, 1, 1, 0},
   {0, 0, 0, 0},
   {0, 0, 0, 0}};
  
  private int[][] rightsShape= {{0, 1, 1, 0},
       {1, 1, 0, 0},
                 {0, 0, 0, 0},
                 {0, 0, 0, 0}};
      
  private int[][] leftlShape = {{1, 1, 1, 0},
       {0, 0, 1, 0},
                 {0, 0, 0, 0},
                 {0, 0, 0, 0}};
  
  private int[][] rightlShape = {{1, 1, 1, 0},
{1, 0, 0, 0},
       {0, 0, 0, 0},
       {0, 0, 0, 0}};
  
  private int[][] boxShape = {{1, 1, 0, 0},
{1, 1, 0, 0},
       {0, 0, 0, 0},
       {0, 0, 0, 0}};
  
  private int[][] stickShape = {{1, 1, 1, 1},
{0, 0, 0, 0},
       {0, 0, 0, 0},
       {0, 0, 0, 0}};
  
  private int[][][] shapeContainer = {tShape,leftsShape,rightsShape,
       leftlShape,rightlShape,boxShape,stickShape};
  /**
   * 产生一个方块
   * @return
   */
  public int[][] generateShape() {
    java.util.Random random = new java.util.Random();
    int ran = random.nextInt(BLOCK_TYPE);
    return shapeContainer[ran];
  }
  
  public int[][] turn(int[][] shape) {
    int[][] tempShape = emptyShape;
    int[][] finalShape = emptyShape;
    int yy = 0;
    //向左旋转 shape
    for(int y = 0; y < BLOCK_CORNER; y++)
      for(int x = 0; x < BLOCK_CORNER; x++)
        tempShape[BLOCK_CORNER - 1 -x][y] = shape[y][x];
    
    /*旋转90度后最上一行会变为空, 0 1 0 0 0 0 0 0
     * 所以要上移 1 1 1 0 ----------> 0 1 0 0
     * 0 0 0 0 ----------> 1 1 0 0
     * 0 0 0 0 0 1 0 0
     */
    for(int y = 0; y < BLOCK_CORNER; y++){
      for(int x = 0; x < BLOCK_CORNER; x++){
        if(tempShape[y][x] == 1){
          yy = y;
          break;
        }
      }
      break;
    }
    for(int y = yy; y < BLOCK_CORNER; y++)
      for(int x = 0; x < BLOCK_CORNER; x++)
        finalShape[y-yy][x] = tempShape[y][x];
    
    return finalShape;
  }  
}




话题树型展开
人气 标题 作者 字数 发贴时间
3560 俄罗斯方块的源码~~~过来看一下 jiang_sl 4212 2004-03-19 16:32
2899 Re:俄罗斯方块的源码~~~过来看一下 jiang_sl 5406 2004-03-19 16:34
2496 Re:俄罗斯方块的源码~~~过来看一下 prettyding 3 2004-03-26 14:34
3155 Re:俄罗斯方块的源码~~~过来看一下 sungo 127 2004-03-26 16:18

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923