Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package uk.ac.reading.sis05kol.mooc;
- //Other parts of the android libraries that we use
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Canvas;
- import android.view.MotionEvent;
- public class TheGame extends GameThread{
- //Will store the image of a ball
- private Bitmap mBall;
- //The X and Y position of the ball on the screen (middle of ball)
- private float mBallX = 0;
- private float mBallY = 0;
- //The speed (pixel/second) of the ball in direction X and Y
- private float mBallSpeedX = 0;
- private float mBallSpeedY = 0;
- //Will store the image of a paddle
- private Bitmap mPaddle;
- //The x position of the paddle on the screen (middle of paddle)
- private float mPaddleX = 0;
- //Y-AKSELIA EI OLE MÄÄRITELTY!
- //Will store the image of the big ball
- private Bitmap mBigBall;
- //The X and Y position of the bigball on the screen (middle of ball) before game starts
- private float mBigBallX = 0;
- private float mBigBallY = 0;
- //Will store minimum distance betw. the ball centers and paddle+ball center
- private float mMinDistanceBetweenRedBallAndPaddle = 0; //tämä itse lisätty...
- private float mMinDistanceBetweenRedBallAndBigBall = 0; //tämä itse lisätty...
- //This is run before anything else, so we can prepare things here
- public TheGame(GameView gameView) {
- //House keeping
- super(gameView);
- //Prepare the image of the ball so we can draw it on the screen (using a canvas)
- mBall = BitmapFactory.decodeResource
- (gameView.getContext().getResources(),
- R.drawable.small_red_ball);
- //Prepare the image of the paddle so we can draw it on the screen (using a canvas)
- mPaddle = BitmapFactory.decodeResource
- (gameView.getContext().getResources(),
- R.drawable.yellow_ball);
- //Prepare the image of the big ball so we can draw it on the screen (using a canvas)
- mBigBall = BitmapFactory.decodeResource
- (gameView.getContext().getResources(),
- R.drawable.smiley_ball);
- }
- //This is run before a new game (also after an old game)
- @Override
- public void setupBeginning() {
- //Initialise speeds
- mBallSpeedX = -100;
- mBallSpeedY = -100;
- //Place the ball in the middle of the screen.
- //mBall.Width() and mBall.getHeigh() gives us the height and width of the image of the ball
- mBallX = mCanvasWidth / 2;
- mBallY = mCanvasHeight / 2;
- //Place the paddle in the middle of below canvas edge
- mPaddleX = mCanvasWidth / 2; //Y-aks määritykset ei vaikuta mihinkään suuntaan.
- //Place the big ball in the middle of upper edge.
- //mBall.Width() and mBall.getHeigh() gives us the height and width of the image of the ball
- mBigBallX = mCanvasWidth / 2;
- mBigBallY = mCanvasHeight = 1; //tarttetaanko tätä? tämä vei smileyn yläosaan. mutta paddle samoin siirtyi.
- //collision detection with red and big ball
- mMinDistanceBetweenRedBallAndBigBall = (mBigBall.getWidth() / 2 + mBall.getWidth() / 2) * (mBigBall.getWidth() / 2 + mBall.getWidth() / 2);
- //ITSE LISÄTTy - PUUTTUNEE koodia TÖRMÄYSLASKUISTA SMILEYn OSALTA
- mMinDistanceBetweenRedBallAndPaddle = (mPaddle.getWidth() / 2 + mBall.getWidth() / 2) * (mPaddle.getWidth() / 2 + mBall.getWidth() / 2);
- }
- @Override
- protected void doDraw(Canvas canvas) {
- //If there isn't a canvas to draw on do nothing
- //It is ok not understanding what is happening here
- if(canvas == null) return;
- super.doDraw(canvas);
- //draw the image of the ball using the X and Y of the ball
- //drawBitmap uses top left corner as reference, we use middle of picture
- //null means that we will use the image without any extra features (called Paint)
- canvas.drawBitmap(mBall, mBallX - mBall.getWidth() / 2, mBallY - mBall.getHeight() / 2, null);
- //draw the image of the paddle using the X of the paddle
- canvas.drawBitmap(mPaddle, mPaddleX - mPaddle.getWidth() / 2, mCanvasHeight - mPaddle.getHeight() / 2, null);
- //null means that we will use the image without any extra features (called Paint)
- //VAIHDETTU YHDENMUKAISEKSI - JÄLKILAUSEESEEN EKAKSI MUUTTUJAKSI KORKEUS LEVEYDEN SIJAAN
- canvas.drawBitmap(mBigBall, mBigBallX - mBigBall.getWidth() / 2, mCanvasHeight - mBigBall.getHeight() / 2, null);
- }
- //This is run whenever the phone is touched by the user
- @Override
- protected void actionOnTouch(float x, float y) {
- mPaddleX = x - mPaddle.getWidth() / 2;
- //Increase/decrease the speed of the ball making the ball move towards the touch
- mBallSpeedX = x - mBallX;
- mBallSpeedY = y - mBallY;
- }
- //This is run whenever the phone moves around its axises
- @Override
- protected void actionWhenPhoneMoved(float xDirection, float yDirection, float zDirection) {
- if(mPaddleX >= 0 && mPaddleX <= mCanvasWidth){
- mPaddleX = mPaddleX - xDirection;
- if(mPaddleX < 0) mPaddleX = 0;
- if(mPaddleX > mCanvasWidth) mPaddleX = mCanvasWidth;
- } //TÄSTÄ POISTETTU PÄTKÄ NOPEUKSISTA
- }
- //This is run just before the game "scenario" is printed on the screen
- @Override
- protected void updateGame(float secondsElapsed)
- //make sure the ball does not go outside the canvas & including its size
- {
- if((mBallX <= mBall.getWidth()/2 && mBallSpeedX < 0) || (mBallX >= mCanvasWidth - mBall.getWidth()/2 && mBallSpeedX > 0)) {
- mBallSpeedX = -mBallSpeedX;
- }
- {
- if((mBallY <= mBall.getHeight()/2 && mBallSpeedY < 0) || (mBallY >= mCanvasHeight - mBall.getHeight()/2 && mBallSpeedY > 0)) {
- mBallSpeedY = -mBallSpeedY;
- }
- float distanceBetweenBallAndPaddle = 0;
- //TÄMÄ LISÄTTY kpl 3.6 - ei toimi vielä....
- if(mBallSpeedY > 0) {
- distanceBetweenBallAndPaddle =
- (mPaddleX - mBallX) * (mPaddleX - mBallX) +
- (mCanvasHeight - mBallY) * (mCanvasHeight - mBallY);
- if(mMinDistanceBetweenRedBallAndBigBall >= distanceBetweenBallAndPaddle) {
- float velocityOfBall = (float) Math.sqrt(mBallSpeedX*mBallSpeedX + mBallSpeedY*mBallSpeedY);
- mBallSpeedX = mBallX - mPaddleX;
- mBallSpeedY = mBallY - mCanvasHeight;
- float newVelocity = (float) Math.sqrt(mBallSpeedX*mBallSpeedX + mBallSpeedY*mBallSpeedY);
- mBallSpeedX = mBallSpeedX * velocityOfBall / newVelocity;
- mBallSpeedY = mBallSpeedY * velocityOfBall / newVelocity;
- } //TÄMÄ LISÄTTY kpl 3.6 - ei toimi vielä....
- }
- float distanceBetweenBallAndBall = 0;
- if(mMinDistanceBetweenRedBallAndBigBall >= distanceBetweenBallAndBall) {
- //TODO change mBallSpeedX and mBallSpeedY
- }
- //Move the ball's X and Y using the speed (pixel/sec)
- mBallX = mBallX + secondsElapsed * mBallSpeedX;
- mBallY = mBallY + secondsElapsed * mBallSpeedY;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement