Answered step by step
Verified Expert Solution
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.
There is a SnakeGame, SnakeActivity, Snake and Apple class.
The full code is in picture
package com.gamecodeschool.csnake;
class SnakeGame extends SurfaceView implements Runnable
Objects for the game loopthread
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 mEatID ;
private int mCrashID ;
The size in segments of the playable area
private final int NUMBLOCKSWIDE ;
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 SnakeGameContext context, Point size
supercontext;
Work out how many pixels each block is
int blockSize size.x NUMBLOCKSWIDE;
How many blocks of the same size will fit into the height
mNumBlocksHigh size.y blockSize;
Initialize the SoundPool
if BuildVERSION.SDKINT Build.VERSIONCODES.LOLLIPOP
AudioAttributes audioAttributes new AudioAttributes.Builder
setUsageAudioAttributesUSAGEMEDIA
setContentTypeAudioAttributesCONTENTTYPESONIFICATION
build;
mSP new SoundPool.Builder
setMaxStreams
setAudioAttributesaudioAttributes
build;
else
mSP new SoundPool AudioManager.STREAMMUSIC, ;
try
AssetManager assetManager context.getAssets;
AssetFileDescriptor descriptor;
Prepare the sounds in memory
descriptor assetManager.openFdgetapple.ogg";
mEatID mSPloaddescriptor;
descriptor assetManager.openFdsnakedeath.ogg";
mCrashID mSPloaddescriptor;
catch IOException e
Error
Initialize the drawing objects
mSurfaceHolder getHolder;
mPaint new Paint;
Call the constructors of our two game objects
mApple new Applecontext
new PointNUMBLOCKSWIDE,
mNumBlocksHigh
blockSize;
mSnake new Snakecontext
new PointNUMBLOCKSWIDE,
mNumBlocksHigh
blockSize;
Called to start a new game
public void newGame
reset the snake
mSnake.resetNUMBLOCKSWIDE, mNumBlocksHigh;
Get the apple ready for dinner
mApple.spawn;
Reset the mScore
mScore ;
Setup mNextFrameTime so an update can triggered
mNextFrameTime System.currentTimeMillis;
Handles the game loop
@Override
public void run
while mPlaying
ifmPaused
Update times a second
if updateRequired
update;
draw;
Check to see if it is time for an update
public boolean updateRequired
Run at frames per second
final long TARGETFPS ;
There are milliseconds in a second
final long MILLISPERSECOND ;
Are we due to update the frame
ifmNextFrameTime System.currentTimeMillis
Tenth of a second has passed
Setup when the next update will be triggered
mNextFrameTime System.currentTimeMillis
MILLISPERSECOND TARGETFPS;
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?
ifmSnakecheckDinnermApplegetLocation
This reminds me of Edge of Tomorrow.
One day the apple will be ready!
mApple.spawn;
Add to mScore
mScore mScore ;
Play a sound
mSPplaymEatID;
Did the snake die?
if mSnakedetectD
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started