如何在数组中移动块

时间:2013-04-24 15:06:31

标签: java android arrays paint

嘿你好我试图让我的块根据它在数组中的位置移动所以如果它在数组[0] [0]并且我在屏幕上绘制它我希望它在我放一个新块时移动在[0] [1]并删除[0] [0]中的内容但我遇到麻烦可以帮忙吗?

package com.example.coloroblind;

import java.util.ArrayList;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.Log;
import android.view.View;

public class GameView extends View
{

    block array[][];
    int bottom, top;
    private float rectY = 10;
    private float rectX = 10;
    boolean start = false;
    boolean move = false;
    int w;// The width
    int h;// The hieght
    int x, y;
    // the x and y location of the block& index of the array

    // Needed for the block color
    private Paint paint;

    // Constructor
    public GameView(Context context)
    {
        super(context);
        paint = new Paint();
    }

    // Called back to draw the view. Also called by invalidate().
    @Override
    protected void onDraw(Canvas canvas)
    {
        // starts and initializes the array with the one block before going into the main program
        // sets the hight and width the size of the screen
        if (start == false) {
            w = this.getWidth() - 55;
            h = this.getHeight() - 55;
            bottom = h;
            top = 0;
            firstCreateBlockArray();// creates the first block
            start = true;
        }

        paint.setColor(Color.BLUE);

        for (int x = 0; x < w; x++) {
            for (int y = 0; y < h; y++) {
                if (array[x][y] != null)
                    canvas.drawRoundRect(array[x][y], 6, 6, paint);
                update(x, y);// this is suppose to move the array block
            }
        }

        // Delay
        try {
            Thread.sleep(300);
        } catch (InterruptedException e) {
        }

        invalidate(); // Force a re-draw
    }

    private void firstCreateBlockArray()
    {
        // Creates the array2d of blocks and sets to null
        array = new block[w][h];
        for (int x = 0; x < w; x++) {
            for (int y = 0; y < h; y++) {
                array[x][y] = null;
            }
        }
        int hori = (int) (Math.random() * w) + 1;
        array[hori][0] = new block(hori, 0);// sets the horizonatall of the block randomly

    }

    // suppose ot change position of the block in the array
    private void update(int x, int y)
    {
        // Detect collision and react

        if (y < bottom - 1) {
            array[x][y + 1] = array[x][y];
            // array[x][y]=null;
        }
    }
}

0 个答案:

没有答案