0

我是 android 应用程序的新手,我刚开始写一个简单的游戏。我使用一个类扩展视图并使用一些线程来制作一个简单的游戏我想问我是否走错了方向?我应该使用其他方式吗?我认为我在这里使用线程是错误的,对吗?我只是用

Game gameview;
gameview= new Game(this);
setContentView(gameview);

在扩展活动的类中调用它

public class Game extends View {
Bitmap background;
Bitmap nomouthbear;
Bitmap nomouthbearget;
Bitmap apple;
float bearX;
float bearY;
List<Apple> appleList;
int appleCount = 0;
float appleX;
float appleY;
float applescore;
float applespeed;
int totalscore = 0;
float canvasheight;
float canvaswidth;
float randomNumber;// random number for apple appear in X
float randomNumber2;// random number for score and speed
float randomNumber3;
int applecombo = 0; // count for how many apple that the bear eat in combo
boolean checkbeareat; // for check if bear eat any of apple or not.
boolean bearEating = false;
Thread thread2;
boolean ineatingthread = false;

int time = 20; // game time =60sec

public Game(Context context) {
    super(context);

    // setup image source
    nomouthbear = BitmapFactory.decodeResource(getResources(),
            R.drawable.nomouthbear_net);
    apple = BitmapFactory.decodeResource(getResources(), R.drawable.apple);
    nomouthbearget = BitmapFactory.decodeResource(getResources(),
            R.drawable.nomouthbearget);
    background = BitmapFactory.decodeResource(getResources(),
            R.drawable.background);
    // init bear x,y
    bearX=0;
    bearY =0;
    // setup background
    if (background != null) {
        setBackgroundDrawable(new BitmapDrawable(background));
    }
    // open a list which hold all apple
    appleList = new ArrayList<Apple>();

    // make a thread to creat apple every 2 sec
    Thread thread = new Thread() {
        @Override
        public void run() {
            try {
                while (true) {
                    sleep(2000);
                    randomNumber = (float) Math.random();
                    randomNumber2 = (float) Math.random();

                    // avoid start at edge point

                    appleX = canvaswidth * randomNumber;
                    if (appleX >= canvaswidth - apple.getWidth() / 2)
                        appleX = canvaswidth - apple.getWidth() / 2;
                    if (appleX <= nomouthbear.getWidth() / 2)
                        appleX = nomouthbear.getWidth();
                    applescore = 1000 * randomNumber2;
                    // check if speed too low
                    applespeed = 10 * randomNumber2;
                    if (applespeed <= 3) {
                        applespeed = 3;
                    }
                    // add new apple in the apple list
                    appleList.add(new Apple(appleX, 65, applescore,
                            applespeed));
                    appleCount++;

                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    };
    // start the thread
    thread.start();
    // thread for timer

    Thread thread1 = new Thread() {
        @Override
        public void run() {
            try {
                while (true)

                {

                        sleep(1000);
                        if(time>=0)
                    time--;

                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    };
    thread1.start();

//thread for controlling the bear eat pic appear time       
    Thread thread2 = new Thread() {
        @Override
        public void run() {
            try {
                while (true)

                {

                    if (bearEating == true) {

                            sleep(200);
                            bearEating = false;

                    }


                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    };
    thread2.start();




}

@Override
protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub
    super.onDraw(canvas);



    if (time <= 0) {
        Paint textPaint = new Paint();
        textPaint.setColor(Color.BLACK);
        textPaint.setTextAlign(Align.LEFT);
        textPaint.setTextSize(65);
        canvas.drawText(
                "Game Over!",
                canvas.getWidth() / 4, canvas.getHeight() / 2, textPaint);
        canvas.drawText(
                "Your Score is ",
                canvas.getWidth() / 4, canvas.getHeight() / 2+65, textPaint);
        canvas.drawText(
                Integer.toString(totalscore),
                canvas.getWidth() / 4, canvas.getHeight() / 2+130, textPaint);






    } else {
        checkbeareat = false;
        canvasheight = canvas.getHeight();
        canvaswidth = canvas.getWidth();
        // draw score text
        Paint textPaint = new Paint();
        textPaint.setColor(Color.BLACK);
        textPaint.setTextAlign(Align.LEFT);
        textPaint.setTextSize(65);
        canvas.drawText("Score: " + Integer.toString(totalscore), 50, 50,
                textPaint);
        canvas.drawText("Time left: " + Integer.toString(time), 50, 120,
                textPaint);
        // if have apple
        if (appleCount != 0) {
            for (int i = 0; i < appleCount; i++) {
                if (appleList.get(i).checkstate()) {

                    // make every apple fall down

                    if ((appleList.get(i).getAppleY() + apple.getHeight() / 2) <= canvas
                            .getHeight()) {

                        appleList.get(i).setAppleY(
                                appleList.get(i).getAppleY()
                                        + appleList.get(i).getspeed());
                    } else {
                        // appleList.get(i).setAppleY(appleList.get(i).getAppleY());
                        applecombo = 0;
                        appleList.get(i).destoryApple();
                    }

                    // check if bear eat the apple

                    if (bearX + nomouthbear.getWidth() / 2 >= appleList
                            .get(i).getAppleX()
                            && bearX <= appleList.get(i).getAppleX()
                            && bearY>= appleList
                                    .get(i).getAppleY()
                            && bearY-nomouthbear.getHeight()/2 <= appleList.get(i).getAppleY()) {
                        // add score
                        totalscore += appleList.get(i).getAppleScore();
                        // change the state of apple to false so wont draw
                        // it
                        // again
                        appleList.get(i).destoryApple();
                        // draw bear ate
                        canvas.drawBitmap(nomouthbearget, bearX
                                - nomouthbear.getWidth() / 2, bearY
                                - nomouthbear.getHeight(), null);
                        checkbeareat = true;
                        bearEating = true;

                        applecombo++;

                    }

                    // draw apple
                    if (appleList.get(i).checkstate()) {
                        canvas.drawBitmap(
                                apple,
                                appleList.get(i).getAppleX()
                                        - (apple.getWidth() / 2),
                                appleList.get(i).getAppleY()
                                        - (apple.getHeight() / 2), null);
                    }

                }
            }
        }

        // draw bear
        // canvas.drawBitmap(nomouthbear, 0, 0, null);
        if (bearEating == false) {

                if (bearX == 0 && bearY == 0){

                    canvas.drawBitmap(nomouthbear, 0, canvas.getHeight()- nomouthbear.getHeight()*2, null);}

                else{
                    canvas.drawBitmap(nomouthbear,
                            bearX - nomouthbear.getWidth() / 2, bearY
                                    - nomouthbear.getHeight(), null);}


        } else {
            canvas.drawBitmap(nomouthbearget,
                    bearX - nomouthbear.getWidth() / 2,
                    bearY - nomouthbear.getHeight(), null);
        }
        invalidate();
    }
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    // TODO Auto-generated method stub

    if (event.getX() >= nomouthbear.getWidth() / 2
            && event.getX() <= canvaswidth - nomouthbear.getWidth() / 2) {
        bearX = event.getX();
    } else if (event.getX() >= canvaswidth - nomouthbear.getWidth() / 2)
        bearX = canvaswidth - nomouthbear.getWidth() / 2;
    else
        bearX = nomouthbear.getWidth() / 2;

    bearY = canvasheight - nomouthbear.getHeight();

    return true;
}

}
4

3 回答 3

0

你不应该invalidateonDraw-调用invalidate是被调用的原因onDraw。如果您希望视图定期更新自身,那么您应该invalidate通过向视图发布一个可运行的对象来每隔一段时间从您的线程(例如 Thread1)调用。

在多线程方面,您的代码是完全不安全的(即它是不可预测的)——没有同步,甚至没有使用 volatile。

于 2013-06-26T13:57:52.347 回答
0

您所做的仅适用于最简单的游戏,因为它在 UI 线程中处理渲染和计算,当事情变得更复杂时,它会变得滞后。

您应该使用GLSurfaceView,查看其文档并阅读Android 中的 OpenGL

如果您真的不想使用 OpenGL,请考虑使用常规SurfaceView,但请注意,设置起来有点麻烦,并且无法顺利执行。

于 2013-06-26T13:58:10.427 回答
0

您可以使用 View 和 onDraw 创建简单的游戏,但它非常缓慢且非常不专业。你不能用它来创造好的游戏。要创建游戏,您应该使用 openGL (GLSurfaceView)。我推荐你的书:JF DiMarzio Practical Android 4 Games Development这是一本学习在 Android 上使用 OpenGL 的好书。

于 2013-06-26T14:00:32.870 回答