Advertisement
Guest User

Untitled

a guest
Sep 14th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.05 KB | None | 0 0
  1. package com.curling.mk;
  2.  
  3.  
  4. import android.app.Activity;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.graphics.Bitmap;
  8. import android.graphics.BitmapFactory;
  9. import android.graphics.Canvas;
  10. import android.os.Bundle;
  11. import android.view.MotionEvent;
  12. import android.view.View;
  13.  
  14. public class CurlingActivity extends Activity {
  15.  
  16. /** Called when the activity is first created. */
  17. @Override
  18. public void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. View curlingView = (View) new CurlingView(this);
  21. curlingView.setBackgroundResource(R.drawable.curlingbckg);
  22. setContentView(curlingView);
  23. }
  24.  
  25. private class CurlingView extends View {
  26.  
  27. private int xMin = 0;
  28. private int xMax;
  29. private int yMin = 0;
  30. private int yMax;
  31. private float ballRadius;
  32. private float ballX; // Ball's center (x,y)
  33. private float ballY;
  34. private float ballSpeedX = 3; // Ball's speed (x,y)
  35. private float ballSpeedY = 3;
  36. private float previousX;
  37. private float previousY;
  38. private Bitmap stone;
  39.  
  40. public CurlingView(Context context) {
  41. super(context);
  42. stone = BitmapFactory.decodeResource(getResources(),R.drawable.stonel);
  43.  
  44. ballRadius=(float)stone.getHeight()/2;
  45. ballX=(float)xMax/2;
  46. ballY=yMax-ballRadius-1;
  47. previousX=ballX;
  48. previousY=ballY;
  49. //this.setFocusableInTouchMode(true);
  50. }
  51.  
  52. @Override
  53. public boolean onTouchEvent(MotionEvent event) {
  54. float currentX = event.getX();
  55. float currentY = event.getY();
  56. float deltaX, deltaY;
  57. float scalingFactor = 10.0f / ((xMax > yMax) ? yMax : xMax);
  58. switch (event.getAction()) {
  59. case MotionEvent.ACTION_MOVE:
  60. // Modify rotational angles according to movement
  61. deltaX = currentX - previousX;
  62. deltaY = currentY - previousY;
  63. ballSpeedX += deltaX * scalingFactor;
  64. ballSpeedY += deltaY * scalingFactor;
  65. }
  66. // Save current x, y
  67. previousX = currentX;
  68. previousY = currentY;
  69. return true; // Event handled
  70. }
  71.  
  72. @Override
  73. protected void onDraw(Canvas canvas) {
  74. // Draw the ball
  75. canvas.drawBitmap(stone,((float)xMax/2)-ballRadius,(float)yMax-(2*ballRadius)-2,null);
  76. // Update the position of the ball, including collision detection and reaction.
  77. update(canvas);
  78.  
  79. // Delay
  80. try {
  81. Thread.sleep(30);
  82. } catch (InterruptedException e) { }
  83.  
  84. invalidate(); // Force a re-draw
  85. }
  86.  
  87. // Detect collision and update the position of the ball.
  88. private void update(Canvas canvas) {
  89. // Get new (x,y) position
  90. if(ballSpeedX<0)
  91. ballSpeedX+=0.1;
  92. else
  93. ballSpeedX-=0.1;
  94. if(ballSpeedY<0)
  95. ballSpeedY+=0.1;
  96. else
  97. ballSpeedY-=0.1;
  98. ballX += ballSpeedX;
  99. ballY += ballSpeedY;
  100. // Detect collision and react
  101. if (ballX + ballRadius > xMax) {
  102. ballSpeedX = -ballSpeedX;
  103. ballX = xMax-ballRadius;
  104. } else if (ballX - ballRadius < xMin) {
  105. ballSpeedX = -ballSpeedX;
  106. ballX = xMin+ballRadius;
  107. }
  108. if (ballY + ballRadius > yMax) {
  109. ballSpeedY = -ballSpeedY;
  110. ballY = yMax - ballRadius;
  111. } else if (ballY - ballRadius < yMin) {
  112. ballSpeedY = -ballSpeedY;
  113. ballY = yMin + ballRadius;
  114. }
  115. }
  116.  
  117. @Override
  118. public void onSizeChanged(int w, int h, int oldW, int oldH) {
  119. // Set the movement bounds for the ball
  120. xMax = w-1;
  121. yMax = h-1;
  122. }
  123. }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement