这是关于俄罗斯方块的游戏程序 运行时出现问题 但不知道是什么问题 求高手解答
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Game extends Frame implements ActionListener{
private Button left=new Button("left");
private Button right=new Button("right");
private Button turn=new Button("turn");
CreateBlack s=new CreateBlack();
Game(){
super("俄罗斯方块第一版 ");
                    setSize(600,600);
                     Panel buttons=new Panel();
                     setLayout(new FlowLayout());
                     buttons.add(right);buttons.add(left);
                     buttons.add(turn);
                     setLayout(new BorderLayout());
                     add("North",buttons);
                     add("Center",s);
                     left.addActionListener(this);                
                     right.addActionListener(this);
                     turn.addActionListener(this);
                     setVisible(true);
}
        public void actionPerformed(ActionEvent e){
                     if(e.getSource()==right){
                          s.left();
                          }
                     if(e.getSource()==left){
                           s.right();
                          }
                      if(e.getSource()==turn){
                       s.turn();
                      }
                       }         public static void main(String args[]){
             Game a=new Game();
            }
 }
import java.awt.*;
import java.awt.event.*;
import javax.swing.Timer;
class CreateBlack extends Canvas{
 int x,y,i,j,flag;
 int map[][]=new int[13][23];
 int sharp,state;
 private final int sharps[][][]=new int[][][]{
                  {{0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0},//长棍
                       {0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0},
                       {0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0},
                       {0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0}},
                    {{0,0,0,0,0,1,1,0,0,1,1,0,0,0,0,0},//正方形
                       {0,0,0,0,0,1,1,0,0,1,1,0,0,0,0,0},
                       {0,0,0,0,0,1,1,0,0,1,1,0,0,0,0,0},
                       {0,0,0,0,0,1,1,0,0,1,1,0,0,0,0,0}},
                    {{0,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0},//左z
                       {0,0,1,0,0,0,1,0,0,1,0,0,0,1,0,0},
                       {0,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0},
                       {0,0,1,0,0,0,1,0,0,1,0,0,0,1,0,0}},
                    {{0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0},// 右z
                       {0,1,0,0,0,1,0,0,0,0,1,0,0,0,0,0},
                       {0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0},
                       {0,1,0,0,0,1,0,0,0,0,1,0,0,0,0,0}},
                    {{0,1,1,0,0,1,0,0,0,1,0,0,0,0,0,0},//右f
                       {0,1,0,0,0,1,1,1,0,0,0,0,0,0,0,0},
                       {0,0,1,0,0,0,1,0,0,1,1,0,0,0,0,0},
                       {0,0,0,0,1,1,1,0,0,0,1,0,0,0,0,0}},
                    {{0,1,1,0,0,0,1,0,0,0,1,0,0,0,1,0},//左f
                       {0,1,1,0,0,0,1,0,0,0,1,0,0,0,0,0},
                       {0,1,0,0,0,1,0,0,0,1,1,0,0,0,0,0},
                       {0,1,0,0,0,1,0,0,0,1,1,0,0,0,0,0},
                       {0,0,1,0,1,1,1,0,0,0,0,0,0,0,0,0}},
                    {{0,0,1,0,0,1,1,1,0,0,0,0,0,0,0,0},//t
                       {0,0,1,0,0,1,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,1,0,0,1,0,0,0,0,0}}};
          CreateBlack(){
               newBlack();
               drawWall();
               newMap();
               Timer time=new Timer(1000,new TimeListener());
               time.start();
         }
 public void newBlack(){//产生新图形
      sharp=((int)(Math.random()*1000))%7;
      state=((int)(Math.random()*1000))%4;
      x=4;
      y=0;
      if(gameOver(x,y)){
       drawWall();
       newMap();
      }
 }
 public void drawWall(){//画边界
  for(i=0;i<13;i++)
     map[i][0]=2;
  for(j=0;j<21;j++){
  map[0][j]=2;
  map[12][j]=2;
  }
 }
 public void newMap(){//初始化地图
  for(i=0;i<13;i++)
    for(j=0;j<23;j++)
     map[i][j]=0;
 }
 public boolean check(int x,int y,int sharp,int state){//判断是否合法
  int a,b;
  for(a=0;a<4;a++)
    {for(b=0;b<4;b++)
       if(((sharps[sharp][state][a*4+b]==1)&&(map[x+b+1][y+a]==1))||((sharps[sharp][state][a*4+b]==1)&&(map[x+b+1][y+a]==2)))
            return false;
    }
    return true;
 }
 public boolean gameOver(int x,int y){//判断挂的方法
    if(check(x,y,sharp,state))
       return true;
    return false;
 }
 public void deline(){//消行
  int a,b,c=0;
  for(a=0;a<22;a++){
  for(b=0;b<12;b++)
    if(map[a][b]==1)
       c++;
     if(c==10){
      for(int d=a;d<0;d--){
       for(int e=0;e<11;e++)
       map[d][e]=map[d][e-1];
        }
     }    
  }
 }
   public void addBlack(int x,int y,int sharp,int state){//添加
        int j=0;
        for(int a=0;a<4;a++){
         for(int b=0;b<4;b++){
         if(map[x+b+1][y+a]==0)
            map[x+b+1][y+a]=sharps[sharp][state][j];
          } 
          j++; 
        }
        
   }
   public void paint(Graphics g){
        super.paint(g);
        for(i=0;i<16;i++){
         if(sharps[sharp][state][j]==1){
         g.fillRect((j%4+1+x)*10,(j/4+y)*10,10,10);
         }
        }
        for(int a=0;a<12;a++){
         for(int b=0;b<22;b++)
              if(map[a][b]==1){
                g.setColor(Color.black);
               g.fillRect(a*10,b*10,10,10);
              }
        }
   }   public void turn(){//变形
    int turnState;
    turnState=state;
    state=(turnState%4+1);
    if(check(x,y,sharp,state)){
    }
    if(check(x,y,sharp,state)){
    state=turnState;
    }
    repaint();
   }
   public void left(){左移
    if(check(x-1,y,sharp,state)){
    x=x-1;
    repaint();
    }
   }
   public void right(){//右移
    if(check(x+1,y,sharp,state)){
    x=x=1;
    repaint();
    }
       
   }
   
 class  TimeListener implements ActionListener{
public void actionPerformed(ActionEvent e){
repaint();
if(check(x,y+1,sharp,state)){
y=y+1;
deline();
}
if(!check(x,y+1,sharp,state)){
if(flag==1){
addBlack(x,y,sharp,state);
deline();
newBlack();
flag=0;
}
flag=1;
}
   
}
  }
}

解决方案 »

  1.   

    是数组越界问题,我现在只是帮你把越界的东西改对了,不过程序实现还是有点问题,你自己继续研究吧import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;public class Game extends Frame implements ActionListener
    {
    private Button left=new Button("left");
    private Button right=new Button("right");
    private Button turn=new Button("turn");
    CreateBlack s=new CreateBlack();

    Game()
    {
    super("俄罗斯方块第一版 ");
    setSize(600,600);
    Panel buttons=new Panel();
    setLayout(new FlowLayout());
    buttons.add(right);buttons.add(left);
    buttons.add(turn);
    setLayout(new BorderLayout());
    add("North",buttons);
    add("Center",s);
    left.addActionListener(this);
    right.addActionListener(this);
    turn.addActionListener(this);
    setVisible(true);
    }

    public void actionPerformed(ActionEvent e)
    {
    if(e.getSource()==right)
    {
    s.left();
    }
    if(e.getSource()==left)
    {
    s.right();
    }
    if(e.getSource()==turn)
    {
    s.turn();
    }
    } public static void main(String args[])
    {
    Game a=new Game();
    }
    }class CreateBlack extends Canvas
    {
    int x,y,i,j,flag;
    int map[][]=new int[13][23];
    int sharp,state;
    private final int sharps[][][]=new int[][][] {
    {{0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0},//长棍
    {0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0},
    {0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0},
    {0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0}},
    {{0,0,0,0,0,1,1,0,0,1,1,0,0,0,0,0},//正方形
    {0,0,0,0,0,1,1,0,0,1,1,0,0,0,0,0},
    {0,0,0,0,0,1,1,0,0,1,1,0,0,0,0,0},
    {0,0,0,0,0,1,1,0,0,1,1,0,0,0,0,0}},
    {{0,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0},//左z
    {0,0,1,0,0,0,1,0,0,1,0,0,0,1,0,0},
    {0,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0},
    {0,0,1,0,0,0,1,0,0,1,0,0,0,1,0,0}},
    {{0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0},// 右z
    {0,1,0,0,0,1,0,0,0,0,1,0,0,0,0,0},
    {0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0},
    {0,1,0,0,0,1,0,0,0,0,1,0,0,0,0,0}},
    {{0,1,1,0,0,1,0,0,0,1,0,0,0,0,0,0},//右f
    {0,1,0,0,0,1,1,1,0,0,0,0,0,0,0,0},
    {0,0,1,0,0,0,1,0,0,1,1,0,0,0,0,0},
    {0,0,0,0,1,1,1,0,0,0,1,0,0,0,0,0}},
    {{0,1,1,0,0,0,1,0,0,0,1,0,0,0,1,0},//左f
    {0,1,1,0,0,0,1,0,0,0,1,0,0,0,0,0},
    {0,1,0,0,0,1,0,0,0,1,1,0,0,0,0,0},
    {0,1,0,0,0,1,0,0,0,1,1,0,0,0,0,0},
    {0,0,1,0,1,1,1,0,0,0,0,0,0,0,0,0}},
    {{0,0,1,0,0,1,1,1,0,0,0,0,0,0,0,0},//t
    {0,0,1,0,0,1,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,1,0,0,1,0,0,0,0,0}}};

    CreateBlack()
    {
    newBlack();
    drawWall();
    newMap();
    Timer time=new Timer(1000,new TimeListener());
    time.start();
    }

    public void newBlack()
    {//产生新图形
    sharp=((int)(Math.random()*1000))%7;
    state=((int)(Math.random()*1000))%4;
    x=4;
    y=0;
    if(gameOver(x,y))
    {
    drawWall();
    newMap();
    }
    }

    public void drawWall()
    {//画边界
    for(i=0;i<13;i++)
    map[i][0]=2;
    for(j=0;j<21;j++)
    {
    map[0][j]=2;
    map[12][j]=2;
    }
    }

    public void newMap()
    {//初始化地图
    for(i=0;i<13;i++)
    for(j=0;j<23;j++)
    map[i][j]=0;
    }

    public boolean check(int x,int y,int sharp,int state)
    {//判断是否合法
    int a,b;
    for(a=0;a<4;a++)
    {
    for(b=0;b<4;b++)
    if(((sharps[sharp][state][a*4+b]==1)&&(map[x+b+1][y+a]==1))||((sharps[sharp][state][a*4+b]==1)&&(map[x+b+1][y+a]==2)))
    return false;
    }
    return true;
    }

    public boolean gameOver(int x,int y)
    {//判断挂的方法
    if(check(x,y,sharp,state))
    return true;
    return false;
    }

    public void deline()
    {//消行
    int a,b,c=0;
    for(a=0;a<22;a++)
    {
    for(b=0;b<12;b++)
    if(map[b][a]==1) //a和b反了
    c++;
    if(c==10)
    {
    for(int d=a;d<0;d--)
    {
    for(int e=0;e<11;e++)
    map[d][e]=map[d][e-1];
    }
    }
    }
    }

    public void addBlack(int x,int y,int sharp,int state)
    {//添加
    int j=0;
    for(int a=0;a<4;a++)
    {
    for(int b=0;b<4;b++)
    {
    if(map[x+b+1][y+a]==0)
    map[x+b+1][y+a]=sharps[sharp][state][j];
    }
    j++;
    }
    }

    public void paint(Graphics g)
    {
    super.paint(g);
    for(j=0;j<16;j++) //这里原来是i
    {
    if(sharps[sharp][state][j]==1)
    {
    g.fillRect((j%4+1+x)*10,(j/4+y)*10,10,10);
    }
    }
    for(int a=0;a<12;a++)
    {
    for(int b=0;b<22;b++)
    if(map[a][b]==1)
    {
    g.setColor(Color.black);
    g.fillRect(a*10,b*10,10,10);
    }
    }
    }

    public void turn()
    {//变形
    int turnState;
    turnState=state;
    state=(turnState%4+1);
    if(check(x,y,sharp,state))
    {

    }
    if(check(x,y,sharp,state))
    {
    state=turnState;
    }
    repaint();
    }

    public void left()
    {//左移
    if(check(x-1,y,sharp,state))
    {
    x=x-1;
    repaint();
    }
    }

    public void right()
    {//右移
    if(check(x+1,y,sharp,state))
    {
    x=x=1;
    repaint();
    }
    }

    class TimeListener implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
    repaint();
    if(check(x,y+1,sharp,state))
    {
    y=y+1;
    deline();
    }
    if(!check(x,y+1,sharp,state))
    {
    if(flag==1)
    {
    addBlack(x,y,sharp,state);
    deline();
    newBlack();
    flag=0;
    }
    flag=1;
    }
    }

    }