Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.b1sha_000.hw1;
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.view.SurfaceHolder;
- import android.view.SurfaceView;
- import android.view.MotionEvent;
- import java.util.Random;
- import java.util.ArrayList;
- // This is the ‘‘ game engine ’ ’.
- public class BubbleShooterView extends SurfaceView implements SurfaceHolder . Callback {
- private Bubble bubbleBall;
- private BubbleShooterThread bst;
- private ArrayList<Bubble> bubbleArray;
- private ArrayList<Integer> colorArray;
- public BubbleShooterView ( Context context ) {
- super ( context ) ;
- // Notify the SurfaceHolder that you ’d like to receive
- // SurfaceHolder callbacks .
- getHolder () . addCallback ( this ) ;
- setFocusable ( true ) ;
- // Initialize game state variables . DON ’T RENDER THE GAME YET .
- // . . .
- colorArray = new ArrayList<Integer>();
- colorArray.add(Color.RED);
- colorArray.add(Color.GRAY);
- colorArray.add(Color.BLUE);
- colorArray.add(Color.GREEN);
- colorArray.add(Color.CYAN);
- colorArray.add(Color.MAGENTA);
- bubbleArray = new ArrayList<Bubble>();
- }
- @Override
- public void surfaceCreated ( SurfaceHolder holder ) {
- // Construct game initial state ( bubbles , etc .)
- int maxX = this.getWidth();
- int maxY = this.getHeight();
- int bubbleX = maxX/10;
- int i,j;
- int mR = maxX/20;
- int bubbleY = maxY/(2*mR);
- Random rng = new Random();
- for(i=0; i<(bubbleY/2); i++){
- for(j=0; j<(maxX/bubbleX)-1; j++){
- int mColor = rng.nextInt(6);
- if(i%2==0) {
- Bubble B = new Bubble(this,mR,colorArray.get(mColor),(i*2*mR)+mR,(j*2*mR)+mR,255,0,0);
- bubbleArray.add(B);
- }
- if(i%2==1){
- Bubble B = new Bubble(this,mR,colorArray.get(mColor), (i*2*mR)+mR, (j*2*mR)+2*mR,255,0,0);
- bubbleArray.add(B);
- }
- }
- }
- int mColor = rng.nextInt(6);
- Bubble ShooterBall = new Bubble(this,mR,colorArray.get(mColor),(maxY-mR),(maxX/2),255,0,0);
- bubbleArray.add(ShooterBall);
- //BubbleShooterThread bst;
- // Launch animator thread .
- bst = new BubbleShooterThread ( this ) ;
- bst . start () ;
- }
- @Override
- public void surfaceChanged ( SurfaceHolder holder ,
- int format , int width , int height ) {
- // Respond to surface changes , e . g . , aspect ratio changes .
- }
- @Override
- public void surfaceDestroyed ( SurfaceHolder holder ) {
- // The cleanest way to stop a thread is by interrupting it .
- // BubbleShooterThread regularly checks its interrupt flag .
- bst . interrupt () ;
- }
- @Override
- public void onDraw ( Canvas c ) {
- super . onDraw ( c ) ;
- renderGame ( c ) ;
- }
- @Override
- public boolean onTouchEvent ( MotionEvent e ) {
- // Update game state in response to events :
- // touch - down , touch - up , and touch - move .
- // Current finger position .
- bubbleArray.get(ShooterBall);
- int curX = Math.round(e.getX ()) ;
- int curY = Math.round(e.getY ()) ;
- switch ( e . getAction () ) {
- case MotionEvent . ACTION_DOWN :
- // Update Game State .
- break ;
- case MotionEvent . ACTION_MOVE :
- // Update Game State .
- break ;
- case MotionEvent . ACTION_UP :
- int newX = Math.round(e.getX());
- int newY = Math.round(e.getY());
- // Update Game State .
- ShooterBall.setVelX(Math.abs(curX-newX));
- ShooterBall.getVelY(Math.abs(curY-newY));
- break ;
- }
- return true ;
- }
- public void advanceFrame ( Canvas c ) {
- // Update game state to animate moving or exploding bubbles
- // ( e . g . , advance location of moving bubble ) .
- // ...
- renderGame ( c ) ;
- }
- private void renderGame ( Canvas c ) {
- // Render the game elements : bubbles ( fixed , moving , exploding )
- // and aiming arrow .
- Paint paint = new Paint () ;
- paint . setStyle ( Paint . Style . FILL ) ;
- paint . setColor ( Color . WHITE ) ;
- paint . setAntiAlias ( true ) ;
- c . drawPaint ( paint ) ;
- for (Bubble b: bubbleArray){
- int color = b.getColor();
- int centerX = b.getX();
- int centerY = b.getY();
- int radius = b.getR();
- int alpha = b.getAlpha();
- paint . setColor ( color ) ;
- paint . setAlpha ( alpha ) ; // Needed for animating disappearance .
- c . drawCircle ( centerX , centerY , radius , paint ) ;
- }
- // Let each object render itself to the canvas .
- // This way , we can add more object types later without
- // having to modify this loop .
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement