Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.curling.mk;
- import android.app.Activity;
- import android.content.Context;
- import android.content.Intent;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Canvas;
- import android.os.Bundle;
- import android.view.MotionEvent;
- import android.view.View;
- public class CurlingActivity extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- View curlingView = (View) new CurlingView(this);
- curlingView.setBackgroundResource(R.drawable.curlingbckg);
- setContentView(curlingView);
- }
- private class CurlingView extends View {
- private int xMin = 0;
- private int xMax;
- private int yMin = 0;
- private int yMax;
- private float ballRadius;
- private float ballX; // Ball's center (x,y)
- private float ballY;
- private float ballSpeedX = 3; // Ball's speed (x,y)
- private float ballSpeedY = 3;
- private float previousX;
- private float previousY;
- private Bitmap stone;
- public CurlingView(Context context) {
- super(context);
- stone = BitmapFactory.decodeResource(getResources(),R.drawable.stonel);
- ballRadius=(float)stone.getHeight()/2;
- ballX=(float)xMax/2;
- ballY=yMax-ballRadius-1;
- previousX=ballX;
- previousY=ballY;
- //this.setFocusableInTouchMode(true);
- }
- @Override
- public boolean onTouchEvent(MotionEvent event) {
- float currentX = event.getX();
- float currentY = event.getY();
- float deltaX, deltaY;
- float scalingFactor = 10.0f / ((xMax > yMax) ? yMax : xMax);
- switch (event.getAction()) {
- case MotionEvent.ACTION_MOVE:
- // Modify rotational angles according to movement
- deltaX = currentX - previousX;
- deltaY = currentY - previousY;
- ballSpeedX += deltaX * scalingFactor;
- ballSpeedY += deltaY * scalingFactor;
- }
- // Save current x, y
- previousX = currentX;
- previousY = currentY;
- return true; // Event handled
- }
- @Override
- protected void onDraw(Canvas canvas) {
- // Draw the ball
- canvas.drawBitmap(stone,((float)xMax/2)-ballRadius,(float)yMax-(2*ballRadius)-2,null);
- // Update the position of the ball, including collision detection and reaction.
- update(canvas);
- // Delay
- try {
- Thread.sleep(30);
- } catch (InterruptedException e) { }
- invalidate(); // Force a re-draw
- }
- // Detect collision and update the position of the ball.
- private void update(Canvas canvas) {
- // Get new (x,y) position
- if(ballSpeedX<0)
- ballSpeedX+=0.1;
- else
- ballSpeedX-=0.1;
- if(ballSpeedY<0)
- ballSpeedY+=0.1;
- else
- ballSpeedY-=0.1;
- ballX += ballSpeedX;
- ballY += ballSpeedY;
- // Detect collision and react
- if (ballX + ballRadius > xMax) {
- ballSpeedX = -ballSpeedX;
- ballX = xMax-ballRadius;
- } else if (ballX - ballRadius < xMin) {
- ballSpeedX = -ballSpeedX;
- ballX = xMin+ballRadius;
- }
- if (ballY + ballRadius > yMax) {
- ballSpeedY = -ballSpeedY;
- ballY = yMax - ballRadius;
- } else if (ballY - ballRadius < yMin) {
- ballSpeedY = -ballSpeedY;
- ballY = yMin + ballRadius;
- }
- }
- @Override
- public void onSizeChanged(int w, int h, int oldW, int oldH) {
- // Set the movement bounds for the ball
- xMax = w-1;
- yMax = h-1;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement