Advertisement
ydornberg

BubbleShooterView

Apr 19th, 2015
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.08 KB | None | 0 0
  1. package com.example.b1sha_000.hw1;
  2.  
  3. import android.content.Context;
  4. import android.graphics.Canvas;
  5. import android.graphics.Color;
  6. import android.graphics.Paint;
  7. import android.view.SurfaceHolder;
  8. import android.view.SurfaceView;
  9. import android.view.MotionEvent;
  10. import java.util.Random;
  11. import java.util.ArrayList;
  12.  
  13.  
  14. // This is the ‘‘ game engine ’ ’.
  15. public class BubbleShooterView extends SurfaceView implements SurfaceHolder . Callback {
  16.  
  17. private Bubble bubbleBall;
  18. private BubbleShooterThread bst;
  19. private ArrayList<Bubble> bubbleArray;
  20. private ArrayList<Integer> colorArray;
  21.  
  22. public BubbleShooterView ( Context context ) {
  23. super ( context ) ;
  24. // Notify the SurfaceHolder that you ’d like to receive
  25. // SurfaceHolder callbacks .
  26. getHolder () . addCallback ( this ) ;
  27. setFocusable ( true ) ;
  28. // Initialize game state variables . DON ’T RENDER THE GAME YET .
  29. // . . .
  30.  
  31. colorArray = new ArrayList<Integer>();
  32. colorArray.add(Color.RED);
  33. colorArray.add(Color.GRAY);
  34. colorArray.add(Color.BLUE);
  35. colorArray.add(Color.GREEN);
  36. colorArray.add(Color.CYAN);
  37. colorArray.add(Color.MAGENTA);
  38. bubbleArray = new ArrayList<Bubble>();
  39. }
  40. @Override
  41. public void surfaceCreated ( SurfaceHolder holder ) {
  42. // Construct game initial state ( bubbles , etc .)
  43. int maxX = this.getWidth();
  44. int maxY = this.getHeight();
  45. int bubbleX = maxX/10;
  46. int i,j;
  47. int mR = maxX/20;
  48. int bubbleY = maxY/(2*mR);
  49. Random rng = new Random();
  50.  
  51. for(i=0; i<(bubbleY/2); i++){
  52. for(j=0; j<(maxX/bubbleX)-1; j++){
  53. int mColor = rng.nextInt(6);
  54. if(i%2==0) {
  55. Bubble B = new Bubble(this,mR,colorArray.get(mColor),(i*2*mR)+mR,(j*2*mR)+mR,255,0,0);
  56. bubbleArray.add(B);
  57. }
  58. if(i%2==1){
  59. Bubble B = new Bubble(this,mR,colorArray.get(mColor), (i*2*mR)+mR, (j*2*mR)+2*mR,255,0,0);
  60. bubbleArray.add(B);
  61. }
  62. }
  63.  
  64. }
  65. int mColor = rng.nextInt(6);
  66. Bubble ShooterBall = new Bubble(this,mR,colorArray.get(mColor),(maxY-mR),(maxX/2),255,0,0);
  67. bubbleArray.add(ShooterBall);
  68. //BubbleShooterThread bst;
  69. // Launch animator thread .
  70. bst = new BubbleShooterThread ( this ) ;
  71. bst . start () ;
  72. }
  73. @Override
  74. public void surfaceChanged ( SurfaceHolder holder ,
  75. int format , int width , int height ) {
  76. // Respond to surface changes , e . g . , aspect ratio changes .
  77.  
  78. }
  79. @Override
  80. public void surfaceDestroyed ( SurfaceHolder holder ) {
  81. // The cleanest way to stop a thread is by interrupting it .
  82. // BubbleShooterThread regularly checks its interrupt flag .
  83.  
  84. bst . interrupt () ;
  85. }
  86. @Override
  87. public void onDraw ( Canvas c ) {
  88. super . onDraw ( c ) ;
  89. renderGame ( c ) ;
  90. }
  91. @Override
  92. public boolean onTouchEvent ( MotionEvent e ) {
  93. // Update game state in response to events :
  94. // touch - down , touch - up , and touch - move .
  95. // Current finger position .
  96. bubbleArray.get(ShooterBall);
  97. int curX = Math.round(e.getX ()) ;
  98. int curY = Math.round(e.getY ()) ;
  99. switch ( e . getAction () ) {
  100. case MotionEvent . ACTION_DOWN :
  101. // Update Game State .
  102. break ;
  103. case MotionEvent . ACTION_MOVE :
  104. // Update Game State .
  105. break ;
  106. case MotionEvent . ACTION_UP :
  107. int newX = Math.round(e.getX());
  108. int newY = Math.round(e.getY());
  109. // Update Game State .
  110.  
  111. ShooterBall.setVelX(Math.abs(curX-newX));
  112. ShooterBall.getVelY(Math.abs(curY-newY));
  113. break ;
  114. }
  115. return true ;
  116. }
  117. public void advanceFrame ( Canvas c ) {
  118. // Update game state to animate moving or exploding bubbles
  119. // ( e . g . , advance location of moving bubble ) .
  120. // ...
  121. renderGame ( c ) ;
  122. }
  123. private void renderGame ( Canvas c ) {
  124. // Render the game elements : bubbles ( fixed , moving , exploding )
  125. // and aiming arrow .
  126.  
  127. Paint paint = new Paint () ;
  128. paint . setStyle ( Paint . Style . FILL ) ;
  129. paint . setColor ( Color . WHITE ) ;
  130. paint . setAntiAlias ( true ) ;
  131. c . drawPaint ( paint ) ;
  132. for (Bubble b: bubbleArray){
  133.  
  134.  
  135. int color = b.getColor();
  136. int centerX = b.getX();
  137. int centerY = b.getY();
  138. int radius = b.getR();
  139. int alpha = b.getAlpha();
  140.  
  141. paint . setColor ( color ) ;
  142. paint . setAlpha ( alpha ) ; // Needed for animating disappearance .
  143. c . drawCircle ( centerX , centerY , radius , paint ) ;
  144. }
  145. // Let each object render itself to the canvas .
  146. // This way , we can add more object types later without
  147. // having to modify this loop .
  148. }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement