Skip to content

Instantly share code, notes, and snippets.

@deepakkj
Last active August 29, 2015 14:08
Show Gist options
  • Save deepakkj/826eaf58426c811ae197 to your computer and use it in GitHub Desktop.
Save deepakkj/826eaf58426c811ae197 to your computer and use it in GitHub Desktop.
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;
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;
//This is run before anything else, so we can prepare things here
public TheGame(GameView gameView) {
//House keeping
super(gameView);
//Prepare the image so we can draw it on the screen (using a canvas)
mBall = BitmapFactory.decodeResource
(gameView.getContext().getResources(),
R.drawable.small_red_ball);
}
//This is run before a new game (also after an old game)
@Override
public void setupBeginning() {
//Initialise speeds
mBallSpeedX = 0;
mBallSpeedY = 0;
//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;
}
@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);
}
//This is run whenever the phone is touched by the user
@Override
protected void actionOnTouch(float x, float y) {
//Increase/decrease the speed of the ball making the ball move towards the touch
//to make the ball stop whenever the screen is touched,hence zero and then make it move in that direction
//mBallSpeedX = x-mBallX;
//mBallSpeedY = y-mBallY;
mBallX = x;
mBallY = y;
mBallSpeedX = 0;
mBallSpeedY = 0;
}
//This is run whenever the phone moves around its axises
@Override
protected void actionWhenPhoneMoved(float xDirection, float yDirection, float zDirection) {
//Increase/decrease the speed of the ball
mBallSpeedX = mBallSpeedX - 1.5f * xDirection;
mBallSpeedY = mBallSpeedY - 1.5f * yDirection;
//mBallSpeedX = 0;
//mBallSpeedY = 0;
}
//This is run just before the game "scenario" is printed on the screen
@Override
protected void updateGame(float secondsElapsed) {
//Move the ball's X and Y using the speed (pixel/sec)
mBallX = mBallX + secondsElapsed * mBallSpeedX;
mBallY = mBallY + secondsElapsed * mBallSpeedY;
}
}
// This file is part of the course "Begin Programming: Build your first mobile game" from futurelearn.com
// Copyright: University of Reading and Karsten Lundqvist
// It is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// It is is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
//
// You should have received a copy of the GNU General Public License
// along with it. If not, see <http://www.gnu.org/licenses/>.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment