Skip to content

Instantly share code, notes, and snippets.

@yosoufe
Last active May 8, 2016 12:46
Show Gist options
  • Save yosoufe/a9f47de67657f17bdf99cfa2e0a37245 to your computer and use it in GitHub Desktop.
Save yosoufe/a9f47de67657f17bdf99cfa2e0a37245 to your computer and use it in GitHub Desktop.
Android App for the course "BEGIN PROGRAMMING". When the ball hits the smiley, it becomes sad smiley and after 3 seconds it becomes again a happy smiley.
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 = -100;
private float mBallY = -100;
//The speed (pixel/second) of the ball in direction X and Y
private float mBallSpeedX = 0;
private float mBallSpeedY = 0;
private Bitmap mPaddle;
private float mPaddleX = 0;
private float mPaddleSpeedX = 0;
private float mMinDistanceBallPaddle;
private Bitmap mSmiley;
private float mMinDistanceBallSmiley;
private float mShownPosX;
private float mShownPosY;
private float mSmileyX;
private float mSmileyY;
private float mSmileyHitTime = 0;
private boolean isSmileySad = false;
private float mSadX = -100;
private float mSadY = -100;
private Bitmap mSad;
//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);
mPaddle = BitmapFactory.decodeResource
(gameView.getContext().getResources(),
R.drawable.yellow_ball);
mSmiley = BitmapFactory.decodeResource
(gameView.getContext().getResources(),
R.drawable.smiley_ball);
mSad = BitmapFactory.decodeResource
(gameView.getContext().getResources(),
R.drawable.sad_ball);
}
//This is run before a new game (also after an old game)
@Override
public void setupBeginning() {
//Initialise speeds
mBallSpeedX = mCanvasWidth/2;
mBallSpeedY = mCanvasHeight/2;
//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;
mPaddleX = mCanvasWidth /2;
mShownPosX = mCanvasWidth/2;
mShownPosY = mSmiley.getWidth()/2;
mSmileyX = mShownPosX;
mSmileyY = mShownPosY;
mMinDistanceBallPaddle = (mPaddle.getWidth() /2 + mBall.getWidth() /2) * (mPaddle.getWidth() /2 + mBall.getWidth() /2);
mMinDistanceBallSmiley = (mSmiley.getWidth() /2 + mBall.getWidth() /2) * (mSmiley.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);
canvas.drawBitmap(mPaddle , mPaddleX - mPaddle.getWidth()/2 , mCanvasHeight - mPaddle.getHeight()/2,null);
canvas.drawBitmap(mSmiley,mSmileyX - mSmiley.getWidth()/2, mSmileyY - mSmiley.getHeight()/2, null);
canvas.drawBitmap(mSad,mSadX - mSad.getWidth()/2, mSadY - mSad.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
//mBallSpeedX = (x - mBallX) * 2;
//mBallSpeedY = (y - mBallY) * 2;
//mBallX=x;
//mBallY=y;
mPaddleX = x;
mPaddleSpeedX = 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.
If the ball moves too fast try and decrease 70f
If the ball moves too slow try and increase 70f
*/
/*
mBallSpeedX = mBallSpeedX + 70f * xDirection;
mBallSpeedY = mBallSpeedY - 70f * yDirection;
*/
mPaddleSpeedX = mPaddleSpeedX + 70f * xDirection;
if (mPaddleX<=0 && mPaddleSpeedX <0) {
mPaddleSpeedX =0;
mPaddleX = 0;
}
if (mPaddleX>=mCanvasWidth && mPaddleSpeedX > 0){
mPaddleSpeedX =0;
mPaddleX = mCanvasWidth;
}
}
//This is run just before the game "scenario" is printed on the screen
@Override
protected void updateGame(float secondsElapsed) {
float distanceBallPaddle;
if(isSmileySad){
mSmileyHitTime = mSmileyHitTime + secondsElapsed;
}
if (mBallSpeedY>0){
distanceBallPaddle = (mPaddleX-mBallX)*(mPaddleX-mBallX) + (mCanvasHeight - mBallY)*(mCanvasHeight - mBallY);
if (mMinDistanceBallPaddle > distanceBallPaddle){
float speedOfBall = (float)Math.sqrt(mBallSpeedY * mBallSpeedY + mBallSpeedX * mBallSpeedX);
mBallSpeedY = mBallY - mCanvasHeight;
mBallSpeedX = mBallX - mPaddleX;
float newSpeedOfBall = (float)Math.sqrt(mBallSpeedY * mBallSpeedY + mBallSpeedX * mBallSpeedX);
mBallSpeedX = mBallSpeedX * speedOfBall / newSpeedOfBall;
mBallSpeedY = mBallSpeedY * speedOfBall / newSpeedOfBall;
}
}
float distanceBallSmiley = (mSmileyX-mBallX)*(mSmileyX-mBallX) + (mSmileyY - mBallY)*(mSmileyY - mBallY);
if(mMinDistanceBallSmiley > distanceBallSmiley){
float speedOfBall = (float)Math.sqrt(mBallSpeedY * mBallSpeedY + mBallSpeedX * mBallSpeedX);
mBallSpeedY = mBallY - mSmiley.getHeight()/2;
mBallSpeedX = mBallX - mCanvasWidth/2;
float newSpeedOfBall = (float)Math.sqrt(mBallSpeedY * mBallSpeedY + mBallSpeedX * mBallSpeedX);
mBallSpeedX = mBallSpeedX * speedOfBall / newSpeedOfBall;
mBallSpeedY = mBallSpeedY * speedOfBall / newSpeedOfBall;
updateScore(1);
mSadX = mShownPosX;
mSadY = mShownPosY;
isSmileySad = true;
} else if(isSmileySad && mSmileyHitTime > 3){
isSmileySad = false;
mSadY = -100;
mSadX = -100;
mSmileyHitTime = 0;
}
//Move the ball's X and Y using the speed (pixel/sec)
mBallX = mBallX + secondsElapsed * mBallSpeedX;
mBallY = mBallY + secondsElapsed * mBallSpeedY;
if ((mBallX <= mBall.getWidth()/2 && mBallSpeedX<0) || (mBallX >= mCanvasWidth - mBall.getWidth()/2 && mBallSpeedX>0)){
mBallSpeedX = -mBallSpeedX;
} else if ((mBallY <= mBall.getWidth()/2 && mBallSpeedY<0) ){//|| (mBallY >= mCanvasHeight - mBall.getWidth()/2 && mBallSpeedY>0)
mBallSpeedY = -mBallSpeedY;
}
mPaddleX = mPaddleX + secondsElapsed * mPaddleSpeedX;
if (mBallY >= mCanvasHeight){
setState(GameThread.STATE_LOSE);
}
}
}
// 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