lundi 3 août 2015

Game with bitmaps randomly appearing from the right

So I am trying to make a Game in which there is a square that goes up and down and then there is red squares that appear from the right hand side and slide left. So far I have only managed to make one red Square appear randomly and slide to the left but I can't seem to make others. What I am trying to do is set something like a timer so that every second or so a new red square will appear randomly. I will post the code that I have so far further down. When I run the game all that happens is a see the square and there is one red square going from right to left. Please help me or suggest how I could fix it to make it work.

The code

public class Game extends Activity implements OnTouchListener {

GameView gameView;
Bitmap square;
Bitmap redSquare;
float x, y;
Random random;
float redSquareY, redSquareX;
long lastSquareTime;
ArrayList<Bitmap> list;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    gameView = new GameView(this);
    gameView.setOnTouchListener(this);
    square = BitmapFactory.decodeResource(getResources(), R.drawable.square);
    x = y = 200;
    list = new ArrayList<Bitmap>();
    startRedSquares();
    setContentView(gameView);
    if(System.nanoTime() - lastSquareTime > 1500000000){ startRedSquares();  
    }
    } 

private void startRedSquares() {
    redSquare = BitmapFactory.decodeResource(getResources(), R.drawable.redsquare);
    list.add(redSquare);
    lastSquareTime = System.nanoTime();
    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;
}
}




Aucun commentaire:

Enregistrer un commentaire