Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.21 KB | None | 0 0
  1. public void finish(){
  2.  
  3. Intent returnIntent = new Intent();
  4. returnIntent.putExtra("GAME_SCORE", gameView.getHitCount());
  5. setResult(RESULT_OK, returnIntent);
  6. super.finish();
  7.  
  8. }
  9.  
  10. public class GameView extends SurfaceView implements SurfaceHolder.Callback {
  11.  
  12. /* Member (state) fields */
  13. private GameLoopThread gameLoopThread;
  14. private Paint paint; //Reference a paint object
  15. /** The drawable to use as the background of the animation canvas */
  16. private Bitmap mBackgroundImage;
  17. // For creating the game Sprite
  18. private Sprite sprite;
  19. // For recording the number of hits
  20. private int hitCount;
  21. // To track if a game is over
  22. private boolean gameOver;
  23. // To play sound
  24. private SoundPlayer sound;
  25.  
  26. //int backButtonCount = 0;
  27.  
  28. public GameView(Context context) {
  29. super(context);
  30. // Focus must be on GameView so that events can be handled.
  31. this.setFocusable(true);
  32. // For intercepting events on the surface.
  33. this.getHolder().addCallback(this);
  34. // Background image added
  35. mBackgroundImage = BitmapFactory.decodeResource(this.getResources(), R.drawable.half_moon);
  36.  
  37. //sound = new SoundPlayer(this);
  38.  
  39. }
  40. /* Called immediately after the surface created */
  41. public void surfaceCreated(SurfaceHolder holder) {
  42. // We can now safely setup the game start the game loop.
  43. ResetGame();//Set up a new game up - could be called by a 'play again option'
  44. mBackgroundImage = Bitmap.createScaledBitmap(mBackgroundImage, getWidth(), getHeight(), true);
  45. gameLoopThread = new GameLoopThread(this.getHolder(), this);
  46. gameLoopThread.running = true;
  47. gameLoopThread.start();
  48. }
  49.  
  50.  
  51.  
  52. // For the countdown timer
  53. private long startTime; // Timer to count down from
  54. private final long interval = 1 * 1000; // 1 sec interval
  55. private CountDownTimer countDownTimer; // Reference to the class
  56. private boolean timerRunning = false;
  57. private String displayTime; // To display the time on the screen
  58.  
  59.  
  60. //To initialise/reset game
  61. private void ResetGame(){
  62. /* Set paint details */
  63. paint = new Paint();
  64. paint.setColor(Color.WHITE);
  65. paint.setTextSize(20);
  66. sprite = new Sprite(this);
  67. hitCount = 0;
  68. // Set timer
  69. startTime = 10; // Start at 10s to count down
  70. // Create new object - convert startTime to milliseconds
  71. countDownTimer = new MyCountDownTimer(startTime*1000, interval);
  72. countDownTimer.start(); // Start the time running
  73. timerRunning = true;
  74. gameOver = false;
  75.  
  76.  
  77. }
  78.  
  79. // Countdown Timer - private class
  80. private class MyCountDownTimer extends CountDownTimer {
  81.  
  82. public MyCountDownTimer (long startTime, long interval) {
  83. super(startTime, interval);
  84. }
  85.  
  86. public void onFinish() {
  87. //displayTime = "Time is up!";
  88. timerRunning = false;
  89. countDownTimer.cancel();
  90. gameOver = true;
  91. }
  92. public void onTick (long millisUntilFinished) {
  93. displayTime = " " + millisUntilFinished / 1000;
  94. }
  95. }
  96.  
  97.  
  98. //This class updates and manages the assets prior to drawing - called from the Thread
  99. public void update(){
  100.  
  101. sprite.update();
  102.  
  103. }
  104. /**
  105. * To draw the game to the screen
  106. * This is called from Thread, so synchronisation can be done
  107. */
  108. public void doDraw(Canvas canvas) {
  109. //Draw all the objects on the canvas
  110. canvas.drawBitmap(mBackgroundImage, 0, 0, null);
  111.  
  112. if (!gameOver) {
  113. sprite.draw(canvas);
  114. canvas.drawText("Time Remaining: " + displayTime, 35, 50, paint);
  115. canvas.drawText("Number of hits: " + hitCount, 250, 50, paint);
  116. } else {
  117. canvas.drawText("Game Over!", 185, 100, paint);
  118. canvas.drawText("To go back to the main menu, press the 'back' key", 15, 150, paint);
  119. }
  120. }
  121.  
  122. //To be used if we need to find where screen was touched
  123. public boolean onTouchEvent(MotionEvent event) {
  124. if (sprite.wasItTouched(event.getX(), event.getY())) {
  125. // This just renews the sprite for now
  126. sprite = new Sprite(this);
  127. //sound.playZapSound();
  128. hitCount++;
  129.  
  130. }
  131. return true;
  132. }
  133.  
  134. public void surfaceDestroyed(SurfaceHolder holder) {
  135. gameLoopThread.running = false;
  136.  
  137. // Shut down the game loop thread cleanly.
  138. boolean retry = true;
  139. while(retry) {
  140. try {
  141. gameLoopThread.join();
  142. retry = false;
  143. } catch (InterruptedException e) {}
  144. }
  145. }
  146.  
  147. public void getHitCount() {
  148.  
  149.  
  150.  
  151. }
  152.  
  153. public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
  154.  
  155. }
  156.  
  157.  
  158. }
  159.  
  160. public class MainMenu extends Activity {
  161.  
  162. private static final int SCORE_REQUEST_CODE = 1;// The request code for the intent
  163.  
  164. TextView tvScore;
  165. String score;
  166. Intent gameIntent;
  167.  
  168. protected void onCreate(Bundle savedInstanceState) {
  169. super.onCreate(savedInstanceState);
  170. setContentView(R.layout.game_start);
  171. }
  172.  
  173. public void startGame(View v){
  174. gameIntent = new Intent(this,GameActivity.class);
  175. startActivityForResult(gameIntent, SCORE_REQUEST_CODE );
  176. }
  177. /* Create Options Menu */
  178. public boolean onCreateOptionsMenu(Menu menu) {
  179. // Inflate the menu; this adds items to the action bar if it is present.
  180. getMenuInflater().inflate(R.menu.main_menu, menu);
  181. return true;
  182. }
  183.  
  184. // Respond to item selected on OPTIONS MENU
  185. public boolean onOptionsItemSelected(MenuItem item) {
  186. // Handle item selection
  187. switch (item.getItemId()) {
  188. //put data in Intent
  189. case R.id.easy:
  190. Toast.makeText(this, "Easy chosen", Toast.LENGTH_SHORT).show();
  191. return true;
  192. case R.id.medium:
  193. Toast.makeText(this, "Medium chosen", Toast.LENGTH_SHORT).show();
  194. return true;
  195. case R.id.hard:
  196. Toast.makeText(this, "Hard chosen", Toast.LENGTH_SHORT).show();
  197. return true;
  198. case R.id.other:
  199. Toast.makeText(this, "Other chosen", Toast.LENGTH_SHORT).show();
  200. return true;
  201. default:
  202. return super.onOptionsItemSelected(item);
  203. }
  204. }
  205.  
  206. protected void onActivityResult(int requestCode, int resultCode, Intent retIntent) {
  207. // Check which request we're responding to
  208. if (requestCode == SCORE_REQUEST_CODE) {
  209. // Make sure the request was successful
  210. if (resultCode == RESULT_OK) {
  211. if (retIntent.hasExtra("GAME_SCORE")) {
  212. int scoreFromGame = retIntent.getExtras().getInt("GAME_SCORE");
  213. tvScore.setText(Integer.toString(scoreFromGame));
  214. }
  215. }
  216. }
  217.  
  218.  
  219.  
  220. }
  221.  
  222.  
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement