Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

snakeysnake game ( learning jaba by building android games second edition ) In java and in android studio. Add a pause button to the game;

snakeysnake game (learning jaba by building android games second edition)
In java and in android studio. Add a pause button to the game; clicking on it again resumes the game.
package com.gamecodeschool.c17snake;
class SnakeGame extends SurfaceView implements Runnable{
// Objects for the game loop/thread
private Thread mThread = null;
// Control pausing between updates
private long mNextFrameTime;
// Is the game currently playing and or paused?
private volatile boolean mPlaying = false;
private volatile boolean mPaused = true;
// for playing sound effects
private SoundPool mSP;
private int mEat_ID =-1;
private int mCrashID =-1;
// The size in segments of the playable area
private final int NUM_BLOCKS_WIDE =40;
private int mNumBlocksHigh;
// How many points does the player have
private int mScore;
// Objects for drawing
private Canvas mCanvas;
private SurfaceHolder mSurfaceHolder;
private Paint mPaint;
// A snake ssss
private Snake mSnake;
// And an apple
private Apple mApple;
// This is the constructor method that gets called
// from SnakeActivity
public SnakeGame(Context context, Point size){
super(context);
// Work out how many pixels each block is
int blockSize = size.x / NUM_BLOCKS_WIDE;
// How many blocks of the same size will fit into the height
mNumBlocksHigh = size.y / blockSize;
// Initialize the SoundPool
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_MEDIA)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build();
mSP = new SoundPool.Builder()
.setMaxStreams(5)
.setAudioAttributes(audioAttributes)
.build();
} else {
mSP = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
}
try {
AssetManager assetManager = context.getAssets();
AssetFileDescriptor descriptor;
// Prepare the sounds in memory
descriptor = assetManager.openFd("get_apple.ogg");
mEat_ID = mSP.load(descriptor,0);
descriptor = assetManager.openFd("snake_death.ogg");
mCrashID = mSP.load(descriptor,0);
} catch (IOException e){
// Error
}
// Initialize the drawing objects
mSurfaceHolder = getHolder();
mPaint = new Paint();
// Call the constructors of our two game objects
mApple = new Apple(context,
new Point(NUM_BLOCKS_WIDE,
mNumBlocksHigh),
blockSize);
mSnake = new Snake(context,
new Point(NUM_BLOCKS_WIDE,
mNumBlocksHigh),
blockSize);
}
// Called to start a new game
public void newGame(){
// reset the snake
mSnake.reset(NUM_BLOCKS_WIDE, mNumBlocksHigh);
// Get the apple ready for dinner
mApple.spawn();
// Reset the mScore
mScore =0;
// Setup mNextFrameTime so an update can triggered
mNextFrameTime = System.currentTimeMillis();
}
// Handles the game loop
@Override
public void run(){
while (mPlaying){
if(!mPaused){
// Update 10 times a second
if (updateRequired()){
update();
}
}
draw();
}
}
// Check to see if it is time for an update
public boolean updateRequired(){
// Run at 10 frames per second
final long TARGET_FPS =10;
// There are 1000 milliseconds in a second
final long MILLIS_PER_SECOND =1000;
// Are we due to update the frame
if(mNextFrameTime <= System.currentTimeMillis()){
// Tenth of a second has passed
// Setup when the next update will be triggered
mNextFrameTime =System.currentTimeMillis()
+ MILLIS_PER_SECOND / TARGET_FPS;
// Return true so that the update and draw
// methods are executed
return true;
}return false;
}
// Update all the game objects
public void update(){
// Move the snake
mSnake.move();
// Did the head of the snake eat the apple?
if(mSnake.checkDinner(mApple.getLocation())){
// This reminds me of Edge of Tomorrow.
// One day the apple will be ready!
mApple.spawn();
// Add to mScore
mScore = mScore +1;
// Play a sound
mSP.play(mEat_ID,1,1,0,0,1);
}
// Did the snake die?
if (mSnake.detectD

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Essential SQLAlchemy Mapping Python To Databases

Authors: Myers, Jason Myers

2nd Edition

1491916567, 9781491916568

More Books

Students also viewed these Databases questions