Java蛇游戏问题

时间:2013-05-29 23:24:52

标签: java

所以我一直在尝试使用Java重新创建游戏Snake,但我遇到了一个我真的不明白的问题。

该程序的基础是有一个无限循环,用于检查玩家在一秒间隔内选择的方向。玩家可以随时按下改变“int direction”值的按钮,但是只有在循环上的定时器启动后蛇才会朝那个方向移动。

我遇到的问题是,在程序执行过程中,单击按钮更改方向无效。方向的改变只会在程序结束后生效,玩家将无法指导蛇的动作。

有没有办法可以解决这个问题,最好不必重新设计整个程序? 。 。

定义变量:网格的空白部分被赋值为0,而蛇所占据的值被赋值为1

public static int x = 5;
public static int y = 5;

public static int grid[][] = new int[11][11];

public static int snakeLength = 1;

public static boolean alive = true;

public static int direction = 1;



public void paint(Graphics g)
{
            for(int r = 0; r<11; r++)
            {
                    for(int e = 0; e<11; e++)
                    {
                            grid[r][e] = 0;
                    }
            }



        grid[5][5] = 1;

我用来理论上改变蛇的方向的按钮

                Rectangle up = new Rectangle(250,550,50,50);
                Rectangle down = new Rectangle(250,650,50,50);
                Rectangle left = new Rectangle(200,600,50,50);
                Rectangle right = new Rectangle(300,600,50,50);

        g.setColor(Color.black);
        g.fillRect(250,550,50,50);
        g.fillRect(250,650,50,50);
        g.fillRect(200,600,50,50);
        g.fillRect(300,600,50,50);     

}

无限循环和一系列分支循环决定下一个动作

向上:方向= 1

down:direction = 2

左:方向= 3

右:方向= 4

  while(alive == true)
  {
                   try 
                   {
                   Thread.sleep(1000);
                   } 
                   catch(InterruptedException ex) 
                   {
                   Thread.currentThread().interrupt();
                   }


        if(direction == 1)
        {
            if(grid[x][y - 1] == 0)
            {
                moveUp(g);

            }

            if(y  == -1)
            {
                fail(g);

            }

            if(grid[x][y - 1] == 1)
            {
                fail(g);

            }

            if(grid[x][y - 1] == 2)
            {
                y = y-1;
                food(g);

            }
        }
        if(direction == 2)
        {
            if(grid[x][y + 1] == 0)
            {

                moveDown(g);
            }

            if(y  == 11)
            {
                fail(g);
            }

            if(grid[x][y + 1] == 1)
            {
                fail(g);
            }

            if(grid[x][y + 1] == 2)
            {
                y = y+1;
                food(g);
            }
        }

        if(direction == 3)
        {
            if(grid[x - 1][y] == 0)
            {
                moveLeft(g);
            }

            if(x  == -1)
            {
                fail(g);
            }

            if(grid[x - 1][y] == 1)
            {
                fail(g);
            }

            if(grid[x - 1][y] == 2)
            {
                x = x-1;
                food(g);
            }
        }

        if(direction == 4)
        {
            if(grid[x + 1][y] == 0)
            {
                moveRight(g);
            }

            if(x == 11)
            {
                fail(g);
            }

            if(grid[x + 1][y] == 1)
            {
                fail(g);
            }

            if(grid[x + 1][y] == 2)
            {
                x = x+1;
                food(g);
            }
        }


    }

我遇到麻烦的方法。在程序完成之前,它似乎没有改变方向的值

public boolean mouseDown(Event e, int x, int y)
{
   if(up.inside(x,y))
   {
    direction = 1;
    System.out.println(direction);

   }
   if(down.inside(x,y))
   {
    direction = 2;
    System.out.println(direction);

   }
   if(left.inside(x,y))
   {
    direction = 3;
    System.out.println(direction);

   }
   if(right.inside(x,y))
   {
    direction = 4;
    System.out.println(direction);

   }

   return true;

}

请注意,为了简洁起见,我省略了其余的代码,但如果它实际上是相关的,我可以提供它。

提前致谢。

0 个答案:

没有答案
相关问题