实施课程

时间:2012-10-29 21:31:08

标签: java class

这些天我正在努力学习java。现在我刚学会了如何使用多个类,我正在构建游戏“破砖机”,但我不明白为什么这不起作用。

 public void run() {
    BricksandBox world = new BricksandBox();
    world.createWorld();
}

以下是'BricksandBox'代码的一部分:

public void createWorld(){
        buildCanvas();
        int i = 0;
        while(i < NBRICK_ROWS){
        i++;
        createARow();
        setCollumNumber();

        }
    }
    /*builds the gamecenter */
    private void buildCanvas(){
        GRect can = new GRect(APPLICATION_WIDTH, APPLICATION_HEIGHT);
        add(can);

    }

    private void createARow(){


        for (int i = 1; i <= NBRICKS_PER_ROW; i++){
            //int y = the brickYoffset + BrickHeight+BrickSep this moves
            // the brick one down. 
            int y = BRICK_Y_OFFSET+((BRICK_SEP+BRICK_HEIGHT)*collumNumber);
            /* 
             * i-1 because it can't be set to zero. else there will be 11 bricks.
             * so you do i-1.
             */
            int x = BRICK_SEP/2+(BRICK_SEP*(i-1))+(BRICK_WIDTH*(i-1));
            GRect brick = new GRect(x,y,BRICK_WIDTH,BRICK_HEIGHT);
            brick.setFilled(true);

            //setting the color
            if(collumNumber <= 1){
                brick.setColor(Color.RED);
            }
            else if(collumNumber <= 3){
                brick.setColor(Color.ORANGE);
            }
            else if(collumNumber <= 5){
                brick.setColor(Color.YELLOW);
            }
            else if(collumNumber <= 7){
                brick.setColor(Color.GREEN);
            }
            else if(collumNumber <= 9){
                brick.setColor(Color.CYAN);
            }
            add(brick);
            }

    }   

    private void setCollumNumber(){
        //MeeBezig. create a value that counts up from 1 to 10.
        //1 is the first row number. 2 is the second row number etc...
        int x = BRICK_SEP/2+(BRICK_SEP*collumNumber)+(BRICK_WIDTH*collumNumber/2);
        int y = BRICK_Y_OFFSET+((BRICK_HEIGHT)*collumNumber);


        if(contains(x,y)){

            collumNumber++;

            }

    }


    /** 
     * a number that counts from one to ten
     */
    private int collumNumber;'

首先,这部分只是在主文件中。但是对于分解,我试图把它放在一个不同的类中。但是现在当我运行程序时,它只显示一个空白画布。

提前致谢!

0 个答案:

没有答案