java - 位图从右侧随机出现的游戏

标签 java android eclipse random bitmap

所以我正在尝试制作一个游戏,其中有一个上下移动的方 block ,然后有从右侧出现并向左滑动的红色方 block 。到目前为止,我只设法使一个红色方 block 随机出现并向左滑动,但我似乎无法制作其他方 block 。我想做的是设置类似计时器的东西,以便每隔一秒左右就会随机出现一个新的红色方 block 。当我运行游戏时,所发生的只是看到一个正方形,并且有一个红色正方形从右到左。请告诉我可以添加什么以使更多的红色方 block 出现。请帮忙。

代码:

public class Game extends Activity implements OnTouchListener {

GameView gameView;
Bitmap square;
Bitmap redSquare;
float x, y;
Random random;
float redSquareY, redSquareX;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    gameView = new GameView(this);
    gameView.setOnTouchListener(this);
    square = BitmapFactory.decodeResource(getResources(), R.drawable.square);
    redSquare = BitmapFactory.decodeResource(getResources(), R.drawable.redsquare);
    x = y = 200;
    setContentView(gameView);
    redSquare = BitmapFactory.decodeResource(getResources(), R.drawable.redsquare);
    random = new Random();
    redSquareX = 500;
    redSquareY = random.nextInt(400);
}

@Override
protected void onPause() {
    super.onPause();
    gameView.pause();
}

@Override
protected void onResume() {
    super.onResume();
    gameView.resume();
}

public class GameView extends SurfaceView implements Runnable{

    Thread thread = null;
    SurfaceHolder holder;
    boolean running = false;
    Canvas canvas;

    public GameView(Context context) {
        super(context);
    holder = getHolder();

    }

    @Override
    public void run() {

        while(running == true){ 
            if(!holder.getSurface().isValid()){
                continue;
            } 

            canvas = holder.lockCanvas();
            canvas.drawRGB(100, 100, 100);
            canvas.drawBitmap(square, x - (square.getWidth()/2), y - (square.getHeight()/2), null);
            canvas.drawBitmap(redSquare, redSquareX, redSquareY, null);
            holder.unlockCanvasAndPost(canvas);
            y = y + 4;
            redSquareX = redSquareX - 1;

        }

        }

    public void pause(){
        running = false;
        while(true){
            try{
                thread.join();
            } catch (InterruptedException e){
                e.printStackTrace();
            } break;
        } thread = null;
    }

    public void resume(){
        running = true;
        thread = new Thread(this);
        thread.start();
    }

 }

@Override
public boolean onTouch(View view, MotionEvent event) {

    try {
        Thread.sleep(50);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    switch(event.getAction()){
    case MotionEvent.ACTION_DOWN:
        y = y - 100;
        break;

    }

    return true;
}
}

最佳答案

我对你的设置有点困惑,但我会尝试一下。

if(System.nanoTime() - lastSquareTime > 1500000000) { 
    startRedSquares();  
}

这段代码不应该出现在onCreate中。 onCreate 只被调用一次,所以它永远不会用这个 if 语句开始创建更多的红色方 block 。您应该在 run 方法(即游戏循环)中检查该计时器。所以,更像这样:

while(running == true){ 
            if(!holder.getSurface().isValid()){
                continue;
            } 

            canvas = holder.lockCanvas();
            canvas.drawRGB(100, 100, 100);
            canvas.drawBitmap(square, x - (square.getWidth()/2), y - (square.getHeight()/2), null);
            canvas.drawBitmap(redSquare, redSquareX, redSquareY, null);
            holder.unlockCanvasAndPost(canvas);
            y = y + 4;

            if(SOME TIMER HAS EXPIRED) {
                //add another red square to the list
                //reset timer 
            }
            //DECREMENT THE TIMER 
            //CHECK IF ANY SQUARE IS OFF THE SCREEN AND REMOVE IT FROM LIST
            //LOOP THROUGH LIST AND MOVE ALL REDSQUARES

            redSquareX = redSquareX - 1;

}

或者,我还建议为您的 redsquares 创建一个类,然后创建 RedSquare 对象的 ArrayList。然后,当您循环访问每个 RedSquare 对象时,可以调用它们自己的方法(updateMovement、checkIfOffScreen、drawRedSquare)或任何您想要调用的方法。

关于java - 位图从右侧随机出现的游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31798320/

相关文章:

使用 Eclipse 进行 Android 开发在更新之前要备份哪些文件夹

java - 比 jdb 更好的 Java 远程调试器?

java - Encoding.UTF32.GetString 相当于Java

c# - 资源不包含 Id 的定义 - c# Xamarin Visual Studio

android - SherlockFragmentActivity setSupportProgressBarIndeterminateVisibility 不起作用

java - 如何在 web.xml 中添加类?

java - java 在某一行停止读取文本文件

java - Cannot Cast com.google.gson.JsonArray to com.google.gson.JsonObject 的解决方案是什么

java - 我手动强制退出应用程序并重新启动后,异步任务不会调用 OnPostExecute 或 onProgressUpdate

android - 我们如何等待 HttpURLConnection 完成