Guest User

Untitled

a guest
Apr 26th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.40 KB | None | 0 0
  1. /** I apologise for this messy code! It's because I'm a complete beginner, but I'd really appreciate any advice in improving it/cleaning it up!
  2. The majority has been gathered from various internet search results, and so I'm making this available for any other learners wanting
  3. to play around with the code/app
  4. - Remember to replace occurrances of "ball" with the name of your video file
  5. - The background video wouldn't run for me on the emulator, so try running directly to your android device
  6. - Place your video in a "raw" folder that sits within the "res" folder.
  7. */
  8.  
  9. package com.example.android.tabletennisscore;
  10.  
  11. import android.media.MediaPlayer;
  12. import android.net.Uri;
  13. import android.os.Bundle;
  14. import android.support.v7.app.AppCompatActivity;
  15. import android.view.View;
  16. import android.widget.TextView;
  17. import android.widget.VideoView;
  18.  
  19. public class MainActivity extends AppCompatActivity {
  20.  
  21.  
  22.  
  23. int scorePlayerA = 0;
  24. int scorePlayerB = 0;
  25. int setsPlayerA = 0;
  26. int setsPlayerB = 0;
  27. int gamesPlayerA = 0;
  28. String winner = "";
  29.  
  30. @Override
  31. protected void onCreate(Bundle savedInstanceState) {
  32. super.onCreate(savedInstanceState);
  33. setContentView(R.layout.activity_main);
  34.  
  35. final VideoView videoview = findViewById(R.id.videoView);
  36. Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.ball);
  37. videoview.setVideoURI(uri);
  38. videoview.start();
  39. videoview.setOnCompletionListener ( new MediaPlayer.OnCompletionListener() {
  40.  
  41. @Override
  42. public void onCompletion(MediaPlayer mediaPlayer) {
  43. videoview.start();
  44. }
  45. });
  46.  
  47.  
  48. displayForPlayerA(0);
  49. displayForPlayerB(0);
  50.  
  51. }
  52.  
  53.  
  54.  
  55.  
  56. /** ~~~~ to add: Once game total reaches 5 compare this to other player and if it's more than 2 points above oponent
  57. * then delcare the winner. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  58. * ~~~~~~Once game reaches end then reset everything automatically
  59. *
  60. * declares loser also if player is less than oponent?:::::::::::::::::::::::::::
  61. * if (setsPlayerA < setsPlayerB) {
  62. winner = "Pants";
  63. displayForPlayerAResult(winner);
  64. }~~~~~~~~~~~~~~~~~~~~~~~~
  65. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  66. */
  67.  
  68.  
  69. /**
  70. * Increase the Points score for Player A by 1 point.
  71. */
  72. public void addPointForPlayerA(View v) {
  73. scorePlayerA = scorePlayerA + 1;
  74. displayForPlayerA(scorePlayerA);
  75.  
  76. if (scorePlayerA < 11) {
  77. displayForPlayersResults("");
  78. displayForPlayerASets(setsPlayerA);
  79. }
  80.  
  81. if (scorePlayerA == 11) {
  82. winner = "Player A wins Set!";
  83. setsPlayerA = setsPlayerA + 1;
  84. displayForPlayersResults(winner);
  85. displayForPlayerASets(setsPlayerA);
  86. scorePlayerA = 0;
  87. }
  88.  
  89.  
  90. if (setsPlayerA == 5) {
  91. /**gamesPlayerA = gamesPlayerA + 1;
  92. displayForPlayerAGames(gamesPlayerA);*/
  93. winner = "Player A wins match!";
  94. displayForPlayersResults(winner);
  95. /**displayForPlayerA(0);*/
  96. setsPlayerA = 0;
  97.  
  98. }
  99. }
  100.  
  101.  
  102. /**
  103. * Displays the current points for Player A.
  104. */
  105. public void displayForPlayerA(int score) {
  106. TextView scoreView = (TextView) findViewById(R.id.player_a_score);
  107. scoreView.setText(String.valueOf(score));
  108. }
  109.  
  110.  
  111. public void displayForPlayersResults(String i) {
  112. TextView r = (TextView) findViewById(R.id.playersResults);
  113. r.setText("" + i);
  114. }
  115.  
  116. /**
  117. * Displays the sets for Player A.
  118. */
  119. public void displayForPlayerASets(int i) {
  120. TextView r = (TextView) findViewById(R.id.player_a_sets);
  121. r.setText("" + i + " of 5");
  122. }
  123.  
  124. /**
  125. * Displays the games for Player A.
  126.  
  127. public void displayForPlayerAGames(int i) {
  128. TextView r = (TextView) findViewById(R.id.player_a_games);
  129. r.setText("" + i);
  130. } */
  131.  
  132. /** ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  133. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  134. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  135. */
  136.  
  137. /**
  138. * Increase the score for Player B by 1 point.
  139. */
  140. public void addPointForPlayerB(View v) {
  141. scorePlayerB = scorePlayerB + 1;
  142. displayForPlayerB(scorePlayerB);
  143.  
  144. if (scorePlayerB < 11) {
  145. displayForPlayersResults("");
  146.  
  147. displayForPlayerBSets(setsPlayerB);
  148. }
  149.  
  150. if (scorePlayerB == 11) {
  151. winner = "Player B Wins Set!";
  152. setsPlayerB = setsPlayerB + 1;
  153.  
  154. displayForPlayersResults(winner);
  155. displayForPlayerBSets(setsPlayerB);
  156. scorePlayerB = 0;
  157. }
  158.  
  159.  
  160. if (setsPlayerB == 5) {
  161. /**gamesPlayerB = gamesPlayerB + 1;
  162. displayForPlayerBGames(gamesPlayerB);*/
  163. winner = "Player B wins match!";
  164. displayForPlayersResults(winner);
  165.  
  166. /**displayForPlayerB(0);*/
  167. setsPlayerB = 0;
  168.  
  169. }
  170. }
  171.  
  172. /**
  173. * Displays the current points for Player A.
  174. */
  175. public void displayForPlayerB(int score) {
  176. TextView scoreView = (TextView) findViewById(R.id.player_b_score);
  177. scoreView.setText(String.valueOf(score));
  178. }
  179.  
  180. /**
  181. * Displays the result for Player A. //wins
  182. */
  183. /**public void displayForPlayerBResult(String i) {
  184. TextView r = (TextView) findViewById(R.id.player_b_result);
  185. r.setText("" + i);
  186. }
  187.  
  188.  
  189. * Displays the sets for Player A.
  190. */
  191. public void displayForPlayerBSets(int i) {
  192. TextView r = (TextView) findViewById(R.id.player_b_sets);
  193. r.setText("" + i + " of 5");
  194. }
  195.  
  196. /**
  197. * Displays the games for Player A.
  198.  
  199. public void displayForPlayerBGames(int i) {
  200. TextView r = (TextView) findViewById(R.id.player_b_games);
  201. r.setText("" + i);
  202. }*/
  203.  
  204. /**
  205. * Reset the score for Players B and A
  206. */
  207. public void Reset(View v) {
  208. scorePlayerB = 0;
  209. scorePlayerA = 0;
  210. winner = "";
  211. setsPlayerA = 0;
  212. setsPlayerB = 0;
  213. displayForPlayerB(scorePlayerB);
  214. displayForPlayerA(scorePlayerA);
  215. displayForPlayersResults(winner);
  216. displayForPlayerASets(setsPlayerA);
  217.  
  218. displayForPlayerBSets(setsPlayerB);
  219.  
  220. }
  221. }
Add Comment
Please, Sign In to add comment