Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement