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

您没有登录

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

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





发贴: 13
积分: 0
于 2004-03-19 16:34 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
//接上面

class Workspace {
  private static final int workspaceWidth = 10;
  private static final int workspaceHeight = 30;
  
  public int getWorkspaceWidth(){ return workspaceWidth;}
  public int getWorkspaceHeight(){ return workspaceHeight;}
  
  private int[][] workspace = new int[workspaceHeight][workspaceWidth];
  
  public void setWorkspaceValue(int x,int y){
    workspace[y][x] = 1;
  }
  public int getWorkspaceValue(int x,int y){
    return workspace[y][x];
  }
  /**
   *扫描workspace 是否有满的行
   *返回一个数组,数组的值为满的行对应的行数
   * @return
   */
  public int[] scanWorkspace(){
    int w;
    int[] fullLines = new int[workspaceHeight];
    for(int h = 0; h < workspaceHeight; h++){
      for(w = 0; w < workspaceWidth; w++){
        if(workspace[h][w] == 0) break;
      }
      if(w == workspaceWidth) fullLines[h] = h;
      else fullLines[h] = 0;
    }
    int[] lines = {0, 0, 0, 0};
    int i = 0;
    for(int h = 0; h < workspaceHeight; h++){
      if(fullLines[h] != 0){
        lines[i] = fullLines[h];
        i++;
      }
    }
    return lines;
  }
  public void deleteLines(int[] lines){
    for(int i = 0; i < lines.length; i++){
      if(lines[i] != 0){
        for(int y = lines[i]; y > 0; y--)
          for(int x = 0; x < workspaceWidth; x++)
            workspace[y][x] = workspace[y-1][x];
      }
    }
  }
  
}

class Game extends Thread {
  
  private int score;
  private int level;
  private int speed;
  
  protected int getScore(){ return score; }
  protected int getLevel(){ return level; }
  protected int getSpeed(){ return speed; }
  
  private int[][] levelSpeed = {{0, 2000, 4000, 6000, 8000,10000,13000,16000,20000,0},
     {0, 500, 400, 300, 200, 100, 80, 65, 50, 0}};
  
  private int[] linesScore = {0, 100, 300, 500, 1000 };
  
  private Workspace ws = new Workspace();
  private Block block = new Block();
  
  private int[][] newshape = new int[4][4];//用于对方块的打操作
  private int shapePosX ;
  private int shapePosY ;
  
  protected synchronized void generateNewShape(){// 产生新的方块
    newshape = block.generateShape();
   shapePosX = (int)ws.getWorkspaceWidth()/2;
    shapePosY = ws.getWorkspaceHeight();
  }
  public int getWorkspaceValue(int x,int y){
    return ws.getWorkspaceValue(x,y);
  }
  /**
   * 将产生的方块映射到workspace里
   * @param x
   * @param y
   * @param shape
   */
  protected void blockToWorkspace(int x,int y,int[][] shape){
    for(int yy = 0; yy < shape.length; yy++){
      for(int xx = 0; xx < shape[yy].length; xx++){
        //使方块不会超出workspace的范围
        if(shape[yy][xx] == 1 && y+yy < ws.getWorkspaceHeight()
         && x+xx < ws.getWorkspaceWidth()) {
          ws.setWorkspaceValue(x+xx,y+yy);
        }
      }
    }
  }
  
  protected boolean canPlace(int x,int y,int[][] shape){
    if((x >= 0)&&(x < ws.getWorkspaceWidth())//未越界
     &&(y >= 0)&&(y < ws.getWorkspaceHeight())){
      for(int yy = 0; yy < block.getBlockCorner(); y++){
        for(int xx = 0; xx < block.getBlockCorner(); xx++){
          //workspace是否不为空
          if((shape[yy][xx] == 1)&&(ws.getWorkspaceValue(xx+x,yy+y) == 1))
            return false;
        }
      }
      return true;
    }
    else { return false; }//越界
  }
  
  protected synchronized void moveLeft(){
    if(canPlace(shapePosX-1,shapePosY,newshape)){
      shapePosX = shapePosX - 1;
      blockToWorkspace(shapePosX,shapePosY,newshape);
    }
  }
  
  protected synchronized void moveRight(){
    if(canPlace(shapePosX+1,shapePosY,newshape)){
      shapePosX = shapePosX + 1;
      blockToWorkspace(shapePosX,shapePosY,newshape);
    }
  }
  
  protected synchronized void moveDown(){
    if(canPlace(shapePosX,shapePosY+1,newshape)){
      shapePosY = shapePosY + 1;
      blockToWorkspace(shapePosX,shapePosY,newshape);
    }
  }
  
  protected synchronized void turn(){
    int[][] turnedShape = block.turn(newshape);
    if(shapePosX+1 >= ws.getWorkspaceWidth())//shape挨着右壁
      return ;
    if(canPlace(shapePosX,shapePosY,turnedShape)){
     for(int y = 0; y < block.getBlockCorner(); y++)
       for(int x = 0; x < block.getBlockCorner(); x++){
         //shape 紧挨的右壁是否为空的
         if(newshape[y][x]==1 && newshape[y][x+1]!=1 &&
         ws.getWorkspaceValue(shapePosX+x+1,shapePosY+y)==1)
           return ;
      }
     newshape = turnedShape;
     blockToWorkspace(shapePosX,shapePosY,newshape);
     return ;
    }
    else return ;
  }
  
  protected boolean gameOver(){
    for(int x = 0; x < ws.getWorkspaceWidth(); x++){
      if(ws.getWorkspaceValue(x,0)==1)
        return true;  
    }
    return false;
  }
  private synchronized void gameHalt(int sp){
    try{
      Thread.sleep(sp);
    }catch(InterruptedException e){}
  }
  /**
   * 俄罗斯方块的主要运行函数
   */
  public void run(){
    score = 0;
    level = 1;
    speed = 500;
  
    generateNewShape();//产生新的方块
    blockToWorkspace(shapePosX,shapePosY,newshape);
    while(true){
      System.out.println("game start!!");
      gameHalt(speed);//方块停顿
      moveDown();
      if(!canPlace(shapePosX,shapePosY,newshape)){
        int[] lines = ws.scanWorkspace();
       ws.deleteLines(lines);
       System.out.println("game start!!");
       int colines = 0;//一次共删除的行数
       for(int i = 0; i < lines.length; i++){
         if(lines[i] != 0) colines++;
       }
       score += linesScore[colines];//加分
       if(score >= levelSpeed[0][level]){//够分时过关
         level++;
         speed = levelSpeed[1][level];
       }
       if(gameOver()) return;
        generateNewShape();//产生新的方块
      }
    }
  }
  
}




话题树型展开
人气 标题 作者 字数 发贴时间
3559 俄罗斯方块的源码~~~过来看一下 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