Advertisement
Guest User

For Frosty

a guest
Mar 6th, 2015
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. public class TheGame extends GameThread{
  2.  
  3. //Will store the image of a ball
  4. private Bitmap mBall;
  5.  
  6. //The X and Y position of the ball on the screen (middle of ball)
  7. private float mBallX = 0;
  8. private float mBallY = 0;
  9.  
  10. //The speed (pixel/second) of the ball in direction X and Y
  11. private float mBallSpeedX = 0;
  12. private float mBallSpeedY = 0;
  13.  
  14. //This is run before anything else, so we can prepare things here
  15. public TheGame(GameView gameView) {
  16. //House keeping
  17. super(gameView);
  18.  
  19. //Prepare the image so we can draw it on the screen (using a canvas)
  20. mBall = BitmapFactory.decodeResource
  21. (gameView.getContext().getResources(),
  22. R.drawable.football2);
  23. }
  24.  
  25. //This is run before a new game (also after an old game)
  26. @Override
  27. public void setupBeginning() {
  28. //Initialise speeds
  29. mBallSpeedX = 0;
  30. mBallSpeedY = 0;
  31.  
  32. //Place the ball in the middle of the screen.
  33. //mBall.Width() and mBall.getHeigh() gives us the height and width of the image of the ball
  34. mBallX = mCanvasWidth / 10;
  35. mBallY = mCanvasHeight / 5;
  36. }
  37.  
  38. @Override
  39. protected void doDraw(Canvas canvas) {
  40. //If there isn't a canvas to draw on do nothing
  41. //It is ok not understanding what is happening here
  42. if(canvas == null) return;
  43.  
  44. super.doDraw(canvas);
  45.  
  46. //draw the image of the ball using the X and Y of the ball
  47. //drawBitmap uses top left corner as reference, we use middle of picture
  48. //null means that we will use the image without any extra features (called Paint)
  49. canvas.drawBitmap(mBall, mBallX - mBall.getWidth() / 2, mBallY - mBall.getHeight() / 2, null);
  50. }
  51.  
  52. //This is run whenever the phone is touched by the user
  53.  
  54. @Override
  55. protected void actionOnTouch(float x, float y) {
  56. //Increase/decrease the speed of the ball making the ball move towards the touch
  57. mBallSpeedX = (x - mBallX)-4;
  58. mBallSpeedY = (y - mBallY)-4;
  59. }
  60.  
  61.  
  62.  
  63. //This is run whenever the phone moves around its axises
  64.  
  65. @Override
  66.  
  67. protected void actionWhenPhoneMoved(float xDirection, float yDirection, float zDirection) {
  68. /*
  69. Increase/decrease the speed of the ball.
  70. If the ball moves too fast try and decrease 70f
  71. If the ball moves too slow try and increase 70f
  72. */
  73.  
  74.  
  75. mBallSpeedX = mBallSpeedX + 70f * xDirection;
  76. mBallSpeedY = mBallSpeedY - 70f * yDirection;
  77. }
  78.  
  79.  
  80. //This is run just before the game "scenario" is printed on the screen
  81. @Override
  82. protected void updateGame(float secondsElapsed) {
  83. //Move the ball's X and Y using the speed (pixel/sec)
  84. mBallX = mBallX + secondsElapsed * mBallSpeedX;
  85. mBallY = mBallY + secondsElapsed * mBallSpeedY;
  86. }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement