diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..78d51c4 --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +misc.xml \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..63c7c6e --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..69bb0e4 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/CSC-133-Final-Project.iml b/CSC-133-Final-Project.iml new file mode 100644 index 0000000..7ffec7e --- /dev/null +++ b/CSC-133-Final-Project.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/Chapter01/AndroidManifest.xml b/Chapter01/AndroidManifest.xml deleted file mode 100644 index c7bba2a..0000000 --- a/Chapter01/AndroidManifest.xml +++ /dev/null @@ -1,24 +0,0 @@ - - - - - - - - - - - - - - \ No newline at end of file diff --git a/Chapter01/java/SubHunter.java b/Chapter01/java/SubHunter.java deleted file mode 100644 index ce0585d..0000000 --- a/Chapter01/java/SubHunter.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.gamecodeschool.subhunter; - -import android.app.Activity; -import android.os.Bundle; - -public class SubHunter extends Activity { - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - } -} - - - - diff --git a/Chapter02/AndroidManifest.xml b/Chapter02/AndroidManifest.xml deleted file mode 100644 index dbf104e..0000000 --- a/Chapter02/AndroidManifest.xml +++ /dev/null @@ -1,23 +0,0 @@ - - - - - - - - - - - - - - \ No newline at end of file diff --git a/Chapter02/java/SubHunter.java b/Chapter02/java/SubHunter.java deleted file mode 100644 index dee3101..0000000 --- a/Chapter02/java/SubHunter.java +++ /dev/null @@ -1,95 +0,0 @@ -// This is our package -// If you are copy & pasting the code then this line will probably be different to yours -// If so, keep YOUR line- not this one -package com.gamecodeschool.c2subhunter; - -// These are all the classes of other people's -// (Android) code that we use in Sub Hunt -import android.app.Activity; -import android.os.Bundle; -import android.view.MotionEvent; -import android.util.Log; -import android.graphics.Bitmap; -import android.graphics.Canvas; -import android.graphics.Color; -import android.graphics.Paint; -import android.graphics.Point; -import android.view.Display; -import android.widget.ImageView; -import java.util.Random; - - -public class SubHunter extends Activity { - - /* - Android runs this code just before - the app is seen by the player. - This makes it a good place to add - the code that is needed for - the one-time setup. - */ - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - Log.d("Debugging", "In onCreate"); - newGame(); - draw(); - } - - /* - This code will execute when a new - game needs to be started. It will - happen when the app is first started - and after the player wins a game. - */ - void newGame(){ - Log.d("Debugging", "In newGame"); - - } - - /* - Here we will do all the drawing. - The grid lines, the HUD and - the touch indicator - */ - void draw() { - Log.d("Debugging", "In draw"); - } - - /* - This part of the code will - handle detecting that the player - has tapped the screen - */ - @Override - public boolean onTouchEvent(MotionEvent motionEvent) { - Log.d("Debugging", "In onTouchEvent"); - takeShot(); - - return true; - } - - - /* - The code here will execute when - the player taps the screen It will - calculate distance from the sub' - and determine a hit or miss - */ - void takeShot(){ - Log.d("Debugging", "In takeShot"); - draw(); - } - - // This code says "BOOM!" - void boom(){ - - } - - // This code prints the debugging text - void printDebuggingText(){ - - } -} diff --git a/Chapter03/AndroidManifest.xml b/Chapter03/AndroidManifest.xml deleted file mode 100644 index 7093dd3..0000000 --- a/Chapter03/AndroidManifest.xml +++ /dev/null @@ -1,23 +0,0 @@ - - - - - - - - - - - - - - \ No newline at end of file diff --git a/Chapter03/java/SubHunter.java b/Chapter03/java/SubHunter.java deleted file mode 100644 index 92a242f..0000000 --- a/Chapter03/java/SubHunter.java +++ /dev/null @@ -1,146 +0,0 @@ -// This is our package -// If you are copy & pasting the code then this line will probably be different to yours -// If so, keep YOUR line- not this one -package com.gamecodeschool.c3subhunter; - -// These are all the classes of other people's -// (Android) code that we use in Sub Hunt -import android.app.Activity; -import android.os.Bundle; -import android.view.MotionEvent; -import android.util.Log; -import android.graphics.Bitmap; -import android.graphics.Canvas; -import android.graphics.Color; -import android.graphics.Paint; -import android.graphics.Point; -import android.view.Display; -import android.widget.ImageView; -import java.util.Random; - - -public class SubHunter extends Activity { - - // These variables can be "seen" - // throughout the SubHunter class - int numberHorizontalPixels; - int numberVerticalPixels; - int blockSize; - int gridWidth = 40; - int gridHeight; - float horizontalTouched = -100; - float verticalTouched = -100; - int subHorizontalPosition; - int subVerticalPosition; - boolean hit = false; - int shotsTaken; - int distanceFromSub; - boolean debugging = true; - - /* - Android runs this code just before - the app is seen by the player. - This makes it a good place to add - the code that is needed for - the one-time setup. - */ - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - // Get the current device's screen resolution - Display display = getWindowManager().getDefaultDisplay(); - Point size = new Point(); - display.getSize(size); - - // Initialize our size based variables based on the screen resolution - numberHorizontalPixels = size.x; - numberVerticalPixels = size.y; - blockSize = numberHorizontalPixels / gridWidth; - gridHeight = numberVerticalPixels / blockSize; - - Log.d("Debugging", "In onCreate"); - newGame(); - draw(); - } - - /* - This code will execute when a new - game needs to be started. It will - happen when the app is first started - and after the player wins a game. - */ - void newGame(){ - Log.d("Debugging", "In newGame"); - - } - - /* - Here we will do all the drawing. - The grid lines, the HUD and - the touch indicator - */ - void draw() { - Log.d("Debugging", "In draw"); - printDebuggingText(); - } - - /* - This part of the code will - handle detecting that the player - has tapped the screen - */ - @Override - public boolean onTouchEvent(MotionEvent motionEvent) { - Log.d("Debugging", "In onTouchEvent"); - takeShot(); - - return true; - } - - - /* - The code here will execute when - the player taps the screen It will - calculate distance from the sub' - and determine a hit or miss - */ - void takeShot(){ - Log.d("Debugging", "In takeShot"); - draw(); - } - - // This code says "BOOM!" - void boom(){ - - } - - // This code prints the debugging text - public void printDebuggingText(){ - Log.d("numberHorizontalPixels", - "" + numberHorizontalPixels); - Log.d("numberVerticalPixels", - "" + numberVerticalPixels); - - Log.d("blockSize", "" + blockSize); - Log.d("gridWidth", "" + gridWidth); - Log.d("gridHeight", "" + gridHeight); - - //Log.d("horizontalTouched", - //"" + horizontalTouched); - Log.d("verticalTouched", - "" + verticalTouched); - Log.d("subHorizontalPosition", - "" + subHorizontalPosition); - Log.d("subVerticalPosition", - "" + subVerticalPosition); - - Log.d("hit", "" + hit); - Log.d("shotsTaken", "" + shotsTaken); - Log.d("debugging", "" + debugging); - - Log.d("distanceFromSub", - "" + distanceFromSub); - } -} \ No newline at end of file diff --git a/Chapter04/AndroidManifest.xml b/Chapter04/AndroidManifest.xml deleted file mode 100644 index eb38f1d..0000000 --- a/Chapter04/AndroidManifest.xml +++ /dev/null @@ -1,23 +0,0 @@ - - - - - - - - - - - - - - \ No newline at end of file diff --git a/Chapter04/Method Overloading/MainActivity.java b/Chapter04/Method Overloading/MainActivity.java deleted file mode 100644 index 3e50e54..0000000 --- a/Chapter04/Method Overloading/MainActivity.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.gamecodeschool.exploringmethodoverloading; - -import android.app.Activity; -import android.os.Bundle; -import android.util.Log; - -public class MainActivity extends Activity { - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - // Declare and initialize a String and an int - int anInt = 10; - String aString = "I am a string"; - - // Now call the different versions of printStuff - // The name stays the same, only the parameters vary - printStuff(anInt); - printStuff(aString); - printStuff(anInt, aString); - - } - - void printStuff(int myInt){ - Log.i("info", "This is the int only version"); - Log.i("info", "myInt = "+ myInt); - } - - void printStuff(String myString){ - Log.i("info", "This is the String only version"); - Log.i("info", "myString = "+ myString); - } - - void printStuff(int myInt, String myString){ - Log.i("info", "This is the combined int and String version"); - Log.i("info", "myInt = "+ myInt); - Log.i("info", "myString = "+ myString); - } - -} diff --git a/Chapter04/java/SubHunter.java b/Chapter04/java/SubHunter.java deleted file mode 100644 index aecdaba..0000000 --- a/Chapter04/java/SubHunter.java +++ /dev/null @@ -1,151 +0,0 @@ -// This is our package -// If you are copy & pasting the code then this line will probably be different to yours -// If so, keep YOUR line- not this one -package com.gamecodeschool.c4subhunter; - -// These are all the classes of other people's -// (Android) code that we use in Sub Hunt -import android.app.Activity; -import android.os.Bundle; -import android.view.MotionEvent; -import android.util.Log; -import android.graphics.Bitmap; -import android.graphics.Canvas; -import android.graphics.Color; -import android.graphics.Paint; -import android.graphics.Point; -import android.view.Display; -import android.widget.ImageView; -import java.util.Random; - - -public class SubHunter extends Activity { - - // These variables can be "seen" - // throughout the SubHunter class - int numberHorizontalPixels; - int numberVerticalPixels; - int blockSize; - int gridWidth = 40; - int gridHeight; - float horizontalTouched = -100; - float verticalTouched = -100; - int subHorizontalPosition; - int subVerticalPosition; - boolean hit = false; - int shotsTaken; - int distanceFromSub; - boolean debugging = true; - - /* - Android runs this code just before - the app is seen by the player. - This makes it a good place to add - the code that is needed for - the one-time setup. - */ - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - // Get the current device's screen resolution - Display display = getWindowManager().getDefaultDisplay(); - Point size = new Point(); - display.getSize(size); - - // Initialize our size based variables based on the screen resolution - numberHorizontalPixels = size.x; - numberVerticalPixels = size.y; - blockSize = numberHorizontalPixels / gridWidth; - gridHeight = numberVerticalPixels / blockSize; - - Log.d("Debugging", "In onCreate"); - newGame(); - draw(); - } - - /* - This code will execute when a new - game needs to be started. It will - happen when the app is first started - and after the player wins a game. - */ - void newGame(){ - Random random = new Random(); - subHorizontalPosition = random.nextInt(gridWidth); - subVerticalPosition = random.nextInt(gridHeight); - shotsTaken = 0; - - Log.d("Debugging", "In newGame"); - - } - - /* - Here we will do all the drawing. - The grid lines, the HUD and - the touch indicator - */ - void draw() { - Log.d("Debugging", "In draw"); - printDebuggingText(); - } - - /* - This part of the code will - handle detecting that the player - has tapped the screen - */ - @Override - public boolean onTouchEvent(MotionEvent motionEvent) { - Log.d("Debugging", "In onTouchEvent"); - takeShot(); - - return true; - } - - - /* - The code here will execute when - the player taps the screen It will - calculate distance from the sub' - and determine a hit or miss - */ - void takeShot(){ - Log.d("Debugging", "In takeShot"); - draw(); - } - - // This code says "BOOM!" - void boom(){ - - } - - // This code prints the debugging text - public void printDebuggingText(){ - Log.d("numberHorizontalPixels", - "" + numberHorizontalPixels); - Log.d("numberVerticalPixels", - "" + numberVerticalPixels); - - Log.d("blockSize", "" + blockSize); - Log.d("gridWidth", "" + gridWidth); - Log.d("gridHeight", "" + gridHeight); - - Log.d("horizontalTouched", - "" + horizontalTouched); - Log.d("verticalTouched", - "" + verticalTouched); - Log.d("subHorizontalPosition", - "" + subHorizontalPosition); - Log.d("subVerticalPosition", - "" + subVerticalPosition); - - Log.d("hit", "" + hit); - Log.d("shotsTaken", "" + shotsTaken); - Log.d("debugging", "" + debugging); - - Log.d("distanceFromSub", - "" + distanceFromSub); - } -} \ No newline at end of file diff --git a/Chapter05/AndroidManifest.xml b/Chapter05/AndroidManifest.xml deleted file mode 100644 index 643bf50..0000000 --- a/Chapter05/AndroidManifest.xml +++ /dev/null @@ -1,23 +0,0 @@ - - - - - - - - - - - - - - \ No newline at end of file diff --git a/Chapter05/Canvas Demo/MainActivity.java b/Chapter05/Canvas Demo/MainActivity.java deleted file mode 100644 index 2903ef4..0000000 --- a/Chapter05/Canvas Demo/MainActivity.java +++ /dev/null @@ -1,69 +0,0 @@ -package com.gamecodeschool.canvasdemo; - -import android.app.Activity; -import android.graphics.Bitmap; -import android.graphics.Canvas; -import android.graphics.Color; -import android.graphics.Paint; -import android.os.Bundle; -import android.widget.ImageView; - -public class MainActivity extends Activity { - - // Here are all the objects(instances) - // of classes that we need to do some drawing - ImageView myImageView; - Bitmap myBlankBitmap; - Canvas myCanvas; - Paint myPaint; - - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - // Initialize all the objects ready for drawing - // We will do this inside the onCreate method - int widthInPixels = 800; - int heightInPixels = 600; - - // Create a new Bitmap - myBlankBitmap = Bitmap.createBitmap(widthInPixels, - heightInPixels, - Bitmap.Config.ARGB_8888); - - // Initialize the Canvas and asociate it - // with the Bitmap to draw on - myCanvas = new Canvas(myBlankBitmap); - - // Initialize the ImageView and the Paint - myImageView = new ImageView(this); - myPaint = new Paint(); - - - // Draw on the Bitmap - // Wipe the Bitmap with a blue color - myCanvas.drawColor(Color.argb(255, 0, 0, 255)); - - // Re-size the text - myPaint.setTextSize(100); - // Change the paint to white - myPaint.setColor(Color.argb(255, 255, 255, 255)); - // Actually draw the text - myCanvas.drawText("Hello World!",100, 100, myPaint); - - // Change the paint to yellow - myPaint.setColor(Color.argb(255, 212, 207, 62)); - // Draw a circle - myCanvas.drawCircle(400,250, 100, myPaint); - - // Associate the drawn upon Bitmap with the ImageView - myImageView.setImageBitmap(myBlankBitmap); - // Tell Android to set our drawing - // as the view for this app - // via the ImageView - setContentView(myImageView); - - - } -} diff --git a/Chapter05/java/SubHunter.java b/Chapter05/java/SubHunter.java deleted file mode 100644 index 9f7bc3e..0000000 --- a/Chapter05/java/SubHunter.java +++ /dev/null @@ -1,210 +0,0 @@ -// This is our package -// If you are copy & pasting the code then this line will probably be different to yours -// If so, keep YOUR line- not this one -package com.gamecodeschool.c5subhunter; - -// These are all the classes of other people's -// (Android) code that we use in Sub Hunt -import android.app.Activity; -import android.os.Bundle; -import android.view.MotionEvent; -import android.util.Log; -import android.graphics.Bitmap; -import android.graphics.Canvas; -import android.graphics.Color; -import android.graphics.Paint; -import android.graphics.Point; -import android.view.Display; -import android.widget.ImageView; -import java.util.Random; - - -public class SubHunter extends Activity { - - // These variables can be "seen" - // throughout the SubHunter class - int numberHorizontalPixels; - int numberVerticalPixels; - int blockSize; - int gridWidth = 40; - int gridHeight; - float horizontalTouched = -100; - float verticalTouched = -100; - int subHorizontalPosition; - int subVerticalPosition; - boolean hit = false; - int shotsTaken; - int distanceFromSub; - boolean debugging = true; - - // Here are all the objects(instances) - // of classes that we need to do some drawing - ImageView gameView; - Bitmap blankBitmap; - Canvas canvas; - Paint paint; - - - /* - Android runs this code just before - the app is seen by the player. - This makes it a good place to add - the code that is needed for - the one-time setup. - */ - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - // Get the current device's screen resolution - Display display = getWindowManager().getDefaultDisplay(); - Point size = new Point(); - display.getSize(size); - - // Initialize our size based variables based on the screen resolution - numberHorizontalPixels = size.x; - numberVerticalPixels = size.y; - blockSize = numberHorizontalPixels / gridWidth; - gridHeight = numberVerticalPixels / blockSize; - - // Initialize all the objects ready for drawing - blankBitmap = Bitmap.createBitmap(numberHorizontalPixels, - numberVerticalPixels, - Bitmap.Config.ARGB_8888); - - canvas = new Canvas(blankBitmap); - gameView = new ImageView(this); - paint = new Paint(); - - // Tell Android to set our drawing - // as the view for this app - setContentView(gameView); - - Log.d("Debugging", "In onCreate"); - newGame(); - draw(); - } - - /* - This code will execute when a new - game needs to be started. It will - happen when the app is first started - and after the player wins a game. - */ - void newGame(){ - Random random = new Random(); - subHorizontalPosition = random.nextInt(gridWidth); - subVerticalPosition = random.nextInt(gridHeight); - shotsTaken = 0; - - Log.d("Debugging", "In newGame"); - - } - - /* - Here we will do all the drawing. - The grid lines, the HUD and - the touch indicator - */ - void draw() { - gameView.setImageBitmap(blankBitmap); - - // Wipe the screen with a white color - canvas.drawColor(Color.argb(255, 255, 255, 255)); - - // Change the paint color to black - paint.setColor(Color.argb(255, 0, 0, 0)); - - // Draw the vertical lines of the grid - canvas.drawLine(blockSize * 1, 0, - blockSize * 1, numberVerticalPixels, - paint); - - // Draw the horizontal lines of the grid - canvas.drawLine(0, blockSize * 1, - numberHorizontalPixels, blockSize * 1, - paint); - - // Re-size the text appropriate for the - // score and distance text - paint.setTextSize(blockSize * 2); - paint.setColor(Color.argb(255, 0, 0, 255)); - canvas.drawText( - "Shots Taken: " + shotsTaken + - " Distance: " + distanceFromSub, - blockSize, blockSize * 1.75f, - paint); - - - Log.d("Debugging", "In draw"); - printDebuggingText(); - } - - /* - This part of the code will - handle detecting that the player - has tapped the screen - */ - @Override - public boolean onTouchEvent(MotionEvent motionEvent) { - Log.d("Debugging", "In onTouchEvent"); - takeShot(); - - return true; - } - - - /* - The code here will execute when - the player taps the screen It will - calculate distance from the sub' - and determine a hit or miss - */ - void takeShot(){ - Log.d("Debugging", "In takeShot"); - draw(); - } - - // This code says "BOOM!" - void boom(){ - - } - - // This code prints the debugging text - public void printDebuggingText(){ - paint.setTextSize(blockSize); - canvas.drawText("numberHorizontalPixels = " - + numberHorizontalPixels, - 50, blockSize * 3, paint); - canvas.drawText("numberVerticalPixels = " - + numberVerticalPixels, - 50, blockSize * 4, paint); - canvas.drawText("blockSize = " + blockSize, - 50, blockSize * 5, paint); - canvas.drawText("gridWidth = " + gridWidth, - 50, blockSize * 6, paint); - canvas.drawText("gridHeight = " + gridHeight, - 50, blockSize * 7, paint); - canvas.drawText("horizontalTouched = " + - horizontalTouched, 50, - blockSize * 8, paint); - canvas.drawText("verticalTouched = " + - verticalTouched, 50, - blockSize * 9, paint); - canvas.drawText("subHorizontalPosition = " + - subHorizontalPosition, 50, - blockSize * 10, paint); - canvas.drawText("subVerticalPosition = " + - subVerticalPosition, 50, - blockSize * 11, paint); - canvas.drawText("hit = " + hit, - 50, blockSize * 12, paint); - canvas.drawText("shotsTaken = " + - shotsTaken, - 50, blockSize * 13, paint); - canvas.drawText("debugging = " + debugging, - 50, blockSize * 14, paint); - - } -} diff --git a/Chapter06/AndroidManifest.xml b/Chapter06/AndroidManifest.xml deleted file mode 100644 index 3ad0714..0000000 --- a/Chapter06/AndroidManifest.xml +++ /dev/null @@ -1,23 +0,0 @@ - - - - - - - - - - - - - - \ No newline at end of file diff --git a/Chapter06/java/SubHunter.java b/Chapter06/java/SubHunter.java deleted file mode 100644 index 1256099..0000000 --- a/Chapter06/java/SubHunter.java +++ /dev/null @@ -1,214 +0,0 @@ -// This is our package -// If you are copy & pasting the code then this line will probably be different to yours -// If so, keep YOUR line- not this one -package com.gamecodeschool.c6subhunter; - -// These are all the classes of other people's -// (Android) code that we use in Sub Hunt -import android.app.Activity; -import android.os.Bundle; -import android.view.MotionEvent; -import android.util.Log; -import android.graphics.Bitmap; -import android.graphics.Canvas; -import android.graphics.Color; -import android.graphics.Paint; -import android.graphics.Point; -import android.view.Display; -import android.widget.ImageView; -import java.util.Random; - - -public class SubHunter extends Activity { - - // These variables can be "seen" - // throughout the SubHunter class - int numberHorizontalPixels; - int numberVerticalPixels; - int blockSize; - int gridWidth = 40; - int gridHeight; - float horizontalTouched = -100; - float verticalTouched = -100; - int subHorizontalPosition; - int subVerticalPosition; - boolean hit = false; - int shotsTaken; - int distanceFromSub; - boolean debugging = true; - - // Here are all the objects(instances) - // of classes that we need to do some drawing - ImageView gameView; - Bitmap blankBitmap; - Canvas canvas; - Paint paint; - - - /* - Android runs this code just before - the app is seen by the player. - This makes it a good place to add - the code that is needed for - the one-time setup. - */ - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - // Get the current device's screen resolution - Display display = getWindowManager().getDefaultDisplay(); - Point size = new Point(); - display.getSize(size); - - // Initialize our size based variables based on the screen resolution - numberHorizontalPixels = size.x; - numberVerticalPixels = size.y; - blockSize = numberHorizontalPixels / gridWidth; - gridHeight = numberVerticalPixels / blockSize; - - // Initialize all the objects ready for drawing - blankBitmap = Bitmap.createBitmap(numberHorizontalPixels, - numberVerticalPixels, - Bitmap.Config.ARGB_8888); - - canvas = new Canvas(blankBitmap); - gameView = new ImageView(this); - paint = new Paint(); - - // Tell Android to set our drawing - // as the view for this app - setContentView(gameView); - - Log.d("Debugging", "In onCreate"); - newGame(); - draw(); - } - - /* - This code will execute when a new - game needs to be started. It will - happen when the app is first started - and after the player wins a game. - */ - void newGame(){ - Random random = new Random(); - subHorizontalPosition = random.nextInt(gridWidth); - subVerticalPosition = random.nextInt(gridHeight); - shotsTaken = 0; - - Log.d("Debugging", "In newGame"); - - } - - /* - Here we will do all the drawing. - The grid lines, the HUD and - the touch indicator - */ - void draw() { - gameView.setImageBitmap(blankBitmap); - - // Wipe the screen with a white color - canvas.drawColor(Color.argb(255, 255, 255, 255)); - - // Change the paint color to black - paint.setColor(Color.argb(255, 0, 0, 0)); - - // Draw the vertical lines of the grid - for(int i = 0; i < gridWidth; i++){ - canvas.drawLine(blockSize * i, 0, - blockSize * i, numberVerticalPixels, - paint); - } - - // Draw the horizontal lines of the grid - for(int i = 0; i < gridHeight; i++){ - canvas.drawLine(0, blockSize * i, - numberHorizontalPixels, blockSize * i, - paint); - } - - // Re-size the text appropriate for the - // score and distance text - paint.setTextSize(blockSize * 2); - paint.setColor(Color.argb(255, 0, 0, 255)); - canvas.drawText( - "Shots Taken: " + shotsTaken + - " Distance: " + distanceFromSub, - blockSize, blockSize * 1.75f, - paint); - - - Log.d("Debugging", "In draw"); - printDebuggingText(); - } - - /* - This part of the code will - handle detecting that the player - has tapped the screen - */ - @Override - public boolean onTouchEvent(MotionEvent motionEvent) { - Log.d("Debugging", "In onTouchEvent"); - takeShot(); - - return true; - } - - - /* - The code here will execute when - the player taps the screen It will - calculate distance from the sub' - and determine a hit or miss - */ - void takeShot(){ - Log.d("Debugging", "In takeShot"); - draw(); - } - - // This code says "BOOM!" - void boom(){ - - } - - // This code prints the debugging text - public void printDebuggingText(){ - paint.setTextSize(blockSize); - canvas.drawText("numberHorizontalPixels = " - + numberHorizontalPixels, - 50, blockSize * 3, paint); - canvas.drawText("numberVerticalPixels = " - + numberVerticalPixels, - 50, blockSize * 4, paint); - canvas.drawText("blockSize = " + blockSize, - 50, blockSize * 5, paint); - canvas.drawText("gridWidth = " + gridWidth, - 50, blockSize * 6, paint); - canvas.drawText("gridHeight = " + gridHeight, - 50, blockSize * 7, paint); - canvas.drawText("horizontalTouched = " + - horizontalTouched, 50, - blockSize * 8, paint); - canvas.drawText("verticalTouched = " + - verticalTouched, 50, - blockSize * 9, paint); - canvas.drawText("subHorizontalPosition = " + - subHorizontalPosition, 50, - blockSize * 10, paint); - canvas.drawText("subVerticalPosition = " + - subVerticalPosition, 50, - blockSize * 11, paint); - canvas.drawText("hit = " + hit, - 50, blockSize * 12, paint); - canvas.drawText("shotsTaken = " + - shotsTaken, - 50, blockSize * 13, paint); - canvas.drawText("debugging = " + debugging, - 50, blockSize * 14, paint); - - } -} diff --git a/Chapter07/AndroidManifest.xml b/Chapter07/AndroidManifest.xml deleted file mode 100644 index 36f718a..0000000 --- a/Chapter07/AndroidManifest.xml +++ /dev/null @@ -1,23 +0,0 @@ - - - - - - - - - - - - - - \ No newline at end of file diff --git a/Chapter07/java/SubHunter.java b/Chapter07/java/SubHunter.java deleted file mode 100644 index a2bec09..0000000 --- a/Chapter07/java/SubHunter.java +++ /dev/null @@ -1,279 +0,0 @@ -// This is our package -// If you are copy & pasting the code then this line will probably be different to yours -// If so, keep YOUR line- not this one -package com.gamecodeschool.c7subhunter; - -// These are all the classes of other people's -// (Android) code that we use in Sub Hunt -import android.app.Activity; -import android.os.Bundle; -import android.view.MotionEvent; -import android.util.Log; -import android.graphics.Bitmap; -import android.graphics.Canvas; -import android.graphics.Color; -import android.graphics.Paint; -import android.graphics.Point; -import android.view.Display; -import android.widget.ImageView; -import java.util.Random; - - -public class SubHunter extends Activity { - - // These variables can be "seen" - // throughout the SubHunter class - int numberHorizontalPixels; - int numberVerticalPixels; - int blockSize; - int gridWidth = 40; - int gridHeight; - float horizontalTouched = -100; - float verticalTouched = -100; - int subHorizontalPosition; - int subVerticalPosition; - boolean hit = false; - int shotsTaken; - int distanceFromSub; - boolean debugging = false; - - // Here are all the objects(instances) - // of classes that we need to do some drawing - ImageView gameView; - Bitmap blankBitmap; - Canvas canvas; - Paint paint; - - - /* - Android runs this code just before - the app is seen by the player. - This makes it a good place to add - the code that is needed for - the one-time setup. - */ - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - // Get the current device's screen resolution - Display display = getWindowManager().getDefaultDisplay(); - Point size = new Point(); - display.getSize(size); - - // Initialize our size based variables based on the screen resolution - numberHorizontalPixels = size.x; - numberVerticalPixels = size.y; - blockSize = numberHorizontalPixels / gridWidth; - gridHeight = numberVerticalPixels / blockSize; - - // Initialize all the objects ready for drawing - blankBitmap = Bitmap.createBitmap(numberHorizontalPixels, - numberVerticalPixels, - Bitmap.Config.ARGB_8888); - - canvas = new Canvas(blankBitmap); - gameView = new ImageView(this); - paint = new Paint(); - - // Tell Android to set our drawing - // as the view for this app - setContentView(gameView); - - Log.d("Debugging", "In onCreate"); - newGame(); - draw(); - } - - /* - This code will execute when a new - game needs to be started. It will - happen when the app is first started - and after the player wins a game. - */ - void newGame(){ - Random random = new Random(); - subHorizontalPosition = random.nextInt(gridWidth); - subVerticalPosition = random.nextInt(gridHeight); - shotsTaken = 0; - - Log.d("Debugging", "In newGame"); - - } - - /* - Here we will do all the drawing. - The grid lines, the HUD and - the touch indicator - */ - void draw() { - gameView.setImageBitmap(blankBitmap); - - // Wipe the screen with a white color - canvas.drawColor(Color.argb(255, 255, 255, 255)); - - // Change the paint color to black - paint.setColor(Color.argb(255, 0, 0, 0)); - - // Draw the vertical lines of the grid - for(int i = 0; i < gridWidth; i++){ - canvas.drawLine(blockSize * i, 0, - blockSize * i, numberVerticalPixels, - paint); - } - - // Draw the horizontal lines of the grid - for(int i = 0; i < gridHeight; i++){ - canvas.drawLine(0, blockSize * i, - numberHorizontalPixels, blockSize * i, - paint); - } - // Draw the player's shot - canvas.drawRect(horizontalTouched * blockSize, - verticalTouched * blockSize, - (horizontalTouched * blockSize) + blockSize, - (verticalTouched * blockSize)+ blockSize, - paint ); - - - // Re-size the text appropriate for the - // score and distance text - paint.setTextSize(blockSize * 2); - paint.setColor(Color.argb(255, 0, 0, 255)); - canvas.drawText( - "Shots Taken: " + shotsTaken + - " Distance: " + distanceFromSub, - blockSize, blockSize * 1.75f, - paint); - - - Log.d("Debugging", "In draw"); - if (debugging) { - printDebuggingText(); - } - } - - /* - This part of the code will - handle detecting that the player - has tapped the screen - */ - @Override - public boolean onTouchEvent(MotionEvent motionEvent) { - Log.d("Debugging", "In onTouchEvent"); - // Has the player removed their finger from the screen? - if((motionEvent.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_UP) { - - // Process the player's shot by passing the - // coordinates of the player's finger to takeShot - takeShot(motionEvent.getX(), motionEvent.getY()); - } - - return true; - } - - - /* - The code here will execute when - the player taps the screen It will - calculate distance from the sub' - and determine a hit or miss - */ - void takeShot(float touchX, float touchY){ - Log.d("Debugging", "In takeShot"); - - // Add one to the shotsTaken variable - shotsTaken ++; - - // Convert the float screen coordinates - // into int grid coordinates - horizontalTouched = (int)touchX/ blockSize; - verticalTouched = (int)touchY/ blockSize; - - // Did the shot hit the sub? - hit = horizontalTouched == subHorizontalPosition - && verticalTouched == subVerticalPosition; - - // How far away horizontally and vertically - // was the shot from the sub - int horizontalGap = (int)horizontalTouched - - subHorizontalPosition; - int verticalGap = (int)verticalTouched - - subVerticalPosition; - - // Use Pythagoras's theorem to get the - // distance travelled in a straight line - distanceFromSub = (int)Math.sqrt( - ((horizontalGap * horizontalGap) + - (verticalGap * verticalGap))); - - // If there is a hit call boom - if(hit) - boom(); - // Otherwise call draw as usual - else draw(); - } - - // This code says "BOOM!" - void boom(){ - - gameView.setImageBitmap(blankBitmap); - - // Wipe the screen with a red color - canvas.drawColor(Color.argb(255, 255, 0, 0)); - - // Draw some huge white text - paint.setColor(Color.argb(255, 255, 255, 255)); - paint.setTextSize(blockSize * 10); - - canvas.drawText("BOOM!", blockSize * 4, - blockSize * 14, paint); - - // Draw some text to prompt restarting - paint.setTextSize(blockSize * 2); - canvas.drawText("Take a shot to start again", - blockSize * 8, - blockSize * 18, paint); - - // Start a new game - newGame(); - } - - // This code prints the debugging text - public void printDebuggingText(){ - paint.setTextSize(blockSize); - canvas.drawText("numberHorizontalPixels = " - + numberHorizontalPixels, - 50, blockSize * 3, paint); - canvas.drawText("numberVerticalPixels = " - + numberVerticalPixels, - 50, blockSize * 4, paint); - canvas.drawText("blockSize = " + blockSize, - 50, blockSize * 5, paint); - canvas.drawText("gridWidth = " + gridWidth, - 50, blockSize * 6, paint); - canvas.drawText("gridHeight = " + gridHeight, - 50, blockSize * 7, paint); - canvas.drawText("horizontalTouched = " + - horizontalTouched, 50, - blockSize * 8, paint); - canvas.drawText("verticalTouched = " + - verticalTouched, 50, - blockSize * 9, paint); - canvas.drawText("subHorizontalPosition = " + - subHorizontalPosition, 50, - blockSize * 10, paint); - canvas.drawText("subVerticalPosition = " + - subVerticalPosition, 50, - blockSize * 11, paint); - canvas.drawText("hit = " + hit, - 50, blockSize * 12, paint); - canvas.drawText("shotsTaken = " + - shotsTaken, - 50, blockSize * 13, paint); - canvas.drawText("debugging = " + debugging, - 50, blockSize * 14, paint); - - } -} diff --git a/Chapter08/AccessScopeThisAndStatic/AccessScopeThisAndStatic/AlienShip.java b/Chapter08/AccessScopeThisAndStatic/AccessScopeThisAndStatic/AlienShip.java deleted file mode 100644 index 2e71146..0000000 --- a/Chapter08/AccessScopeThisAndStatic/AccessScopeThisAndStatic/AlienShip.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.packtpub.accessscopethisandstatic.app; - -import android.util.Log; - -public class AlienShip { - private static int numShips; - private int shieldStrength; - public String shipName; - - public AlienShip(){ - numShips++; - //Can call private methods from here because I am part - //of the class - //If didn't have "this" then this call might be less clear - //But this "this" isn't strictly necessary - this.setShieldStrength(100); - //Because of "this" I am sure I am setting the correct shieldStrength - } - - public static int getNumShips(){ - return numShips; - - } - - private void setShieldStrength(int shieldStrength){ - //"this" distinguishes between the member variable shieldStrength - //And the local variable/paramater of the same name - this.shieldStrength = shieldStrength; - } - - - public int getShieldStrength(){ - return this.shieldStrength; - } - - public void hitDetected(){ - - shieldStrength -=25; - Log.i("Incomiming: ","Bam!!"); - if (shieldStrength == 0){ - destroyShip(); - } - - } - - private void destroyShip(){ - numShips--; - Log.i("Explosion: ", ""+this.shipName + " destroyed"); - } -} diff --git a/Chapter08/AccessScopeThisAndStatic/AccessScopeThisAndStatic/MainActivity.java b/Chapter08/AccessScopeThisAndStatic/AccessScopeThisAndStatic/MainActivity.java deleted file mode 100644 index 44c2156..0000000 --- a/Chapter08/AccessScopeThisAndStatic/AccessScopeThisAndStatic/MainActivity.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.packtpub.accessscopethisandstatic.app; - -import android.app.Activity; -import android.os.Bundle; -import android.util.Log; - -public class MainActivity extends Activity { - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_main); - - //every time we do this the constructor runs - AlienShip girlShip = new AlienShip(); - AlienShip boyShip = new AlienShip(); - - //Look no objects but using the static method - Log.i("numShips: ", "" + AlienShip.getNumShips()); - - //This works because shipName is public - girlShip.shipName = "Corrine Yu"; - boyShip.shipName = "Andre LaMothe"; - - //This won't work because shieldStrenth is private - //girlship.shieldStrength = 999; - - //But we have a public getter - Log.i("girlShip shieldStrngth: ", "" + girlShip.getShieldStrength()); - Log.i("boyShip shieldStrngth: ", "" + boyShip.getShieldStrength()); - - //But we can't do this because it's private - //boyship.setShieldStrength(1000000); - - //lets shoot some ships - girlShip.hitDetected(); - Log.i("girlShip shieldStrngth: ", "" + girlShip.getShieldStrength()); - Log.i("boyShip shieldStrngth: ", "" + boyShip.getShieldStrength()); - boyShip.hitDetected(); - boyShip.hitDetected(); - boyShip.hitDetected(); - Log.i("girlShip shieldStrngth: ", "" + girlShip.getShieldStrength()); - Log.i("boyShip shieldStrngth: ", "" + boyShip.getShieldStrength()); - boyShip.hitDetected();//ahhh - Log.i("girlShip shieldStrngth: ", "" + girlShip.getShieldStrength()); - Log.i("boyShip shieldStrngth: ", "" + boyShip.getShieldStrength()); - Log.i("numShips: ", "" + AlienShip.getNumShips()); - } - -} diff --git a/Chapter08/BasicClasses/RTSActivity.java b/Chapter08/BasicClasses/RTSActivity.java deleted file mode 100644 index 11228cb..0000000 --- a/Chapter08/BasicClasses/RTSActivity.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.gamecodeschool.basicclasses; - -import android.app.Activity; -import android.os.Bundle; -import android.util.Log; - -public class RTSActivity extends Activity { - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - // first we make an object of type soldier - Soldier rambo = new Soldier(); - rambo.soldierType = "Green Beret"; - rambo.health = 150; - // It takes allot to kill Rambo - - // Now we make another Soldier object - Soldier vassily = new Soldier(); - vassily.soldierType = "Sniper"; - vassily.health = 50; - // Snipers have less health - - // And one more Soldier object - Soldier wellington = new Soldier(); - wellington.soldierType = "Sailor"; - wellington.health = 100; - // He's tough but no green beret - - Log.d("Rambo's health = ", "" + rambo.health); - Log.d("Vassily's health = ", "" + vassily.health); - Log.d("Wellington's health = ", "" + wellington.health); - - rambo.shootEnemy(); - vassily.shootEnemy(); - wellington.shootEnemy(); - - - } -} diff --git a/Chapter08/BasicClasses/Soldier.java b/Chapter08/BasicClasses/Soldier.java deleted file mode 100644 index cafc529..0000000 --- a/Chapter08/BasicClasses/Soldier.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.gamecodeschool.basicclasses; - -import android.util.Log; - -/** - * Created by johnh on 08/12/2017. - */ - -public class Soldier { - int health; - String soldierType; - - void shootEnemy(){ - //let's print which type of soldier is shooting - Log.d(soldierType, " is shooting"); - } - -} diff --git a/Chapter08/C8Pong/AndroidManifest.xml b/Chapter08/C8Pong/AndroidManifest.xml deleted file mode 100644 index 7e3e325..0000000 --- a/Chapter08/C8Pong/AndroidManifest.xml +++ /dev/null @@ -1,23 +0,0 @@ - - - - - - - - - - - - - - \ No newline at end of file diff --git a/Chapter08/C8Pong/java/Ball.java b/Chapter08/C8Pong/java/Ball.java deleted file mode 100644 index 8860150..0000000 --- a/Chapter08/C8Pong/java/Ball.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.gamecodeschool.c8pong; - -class Ball { -} \ No newline at end of file diff --git a/Chapter08/C8Pong/java/Bat.java b/Chapter08/C8Pong/java/Bat.java deleted file mode 100644 index b4b66a1..0000000 --- a/Chapter08/C8Pong/java/Bat.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.gamecodeschool.c8pong; - -class Bat { -} diff --git a/Chapter08/C8Pong/java/PongActivity.java b/Chapter08/C8Pong/java/PongActivity.java deleted file mode 100644 index 7fda723..0000000 --- a/Chapter08/C8Pong/java/PongActivity.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.gamecodeschool.c8pong; - -import android.app.Activity; -import android.os.Bundle; - -public class PongActivity extends Activity { - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - } - -} diff --git a/Chapter08/C8Pong/java/PongGame.java b/Chapter08/C8Pong/java/PongGame.java deleted file mode 100644 index b53f601..0000000 --- a/Chapter08/C8Pong/java/PongGame.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.gamecodeschool.c8pong; - -class PongGame{ -} - - - - - - - - - - diff --git a/Chapter08/InheritanceExample/InheritanceExample/AlienShip.java b/Chapter08/InheritanceExample/InheritanceExample/AlienShip.java deleted file mode 100644 index b94de1c..0000000 --- a/Chapter08/InheritanceExample/InheritanceExample/AlienShip.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.packtpub.inheritanceexample.app; - -import android.util.Log; - -public abstract class AlienShip { - private static int numShips; - private int shieldStrength; - public String shipName; - - public AlienShip(int shieldStrength){ - Log.i("Location: ", "AlienShip constructor"); - numShips++; - setShieldStrength(shieldStrength); - } - - public abstract void fireWeapon();//Ahh my body - - public static int getNumShips(){ - return numShips; - } - - private void setShieldStrength(int shieldStrength){ - this.shieldStrength = shieldStrength; - } - - - public int getShieldStrength(){ - return this.shieldStrength; - } - - public void hitDetected(){ - shieldStrength -=25; - Log.i("Incomiming: ", "Bam!!"); - if (shieldStrength == 0){ - destroyShip(); - } - - } - - private void destroyShip(){ - numShips--; - Log.i("Explosion: ", ""+this.shipName + " destroyed"); - } - -} diff --git a/Chapter08/InheritanceExample/InheritanceExample/Bomber.java b/Chapter08/InheritanceExample/InheritanceExample/Bomber.java deleted file mode 100644 index 5acd919..0000000 --- a/Chapter08/InheritanceExample/InheritanceExample/Bomber.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.packtpub.inheritanceexample.app; - -import android.util.Log; - -public class Bomber extends AlienShip { - - public Bomber(){ - super(100); - //Weak shields for a bomber - Log.i("Location: ", "Bomber constructor"); - } - - public void fireWeapon(){ - Log.i("Firing weapon: ", "bombs away"); - } -} diff --git a/Chapter08/InheritanceExample/InheritanceExample/Fighter.java b/Chapter08/InheritanceExample/InheritanceExample/Fighter.java deleted file mode 100644 index 4a0c814..0000000 --- a/Chapter08/InheritanceExample/InheritanceExample/Fighter.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.packtpub.inheritanceexample.app; - -import android.util.Log; - -public class Fighter extends AlienShip{ - - public Fighter(){ - super(400); - //Strong shields for a fighter - Log.i("Location: ", "Fighter constructor"); - } - - public void fireWeapon(){ - Log.i("Firing weapon: ", "lasers firing"); - } - -} diff --git a/Chapter08/InheritanceExample/InheritanceExample/MainActivity.java b/Chapter08/InheritanceExample/InheritanceExample/MainActivity.java deleted file mode 100644 index cc20815..0000000 --- a/Chapter08/InheritanceExample/InheritanceExample/MainActivity.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.packtpub.inheritanceexample.app; - -import android.app.Activity; -import android.os.Bundle; -import android.util.Log; - -public class MainActivity extends Activity { - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_main); - - Fighter aFighter = new Fighter(); - Bomber aBomber = new Bomber(); - - //Can't do this AlienShip is abstract - - //Literally speaking as well as in code - //AlienShip alienShip = new AlienShip(500); - - //But our objects of the subclasses can still do - //everything the AlienShip is meant to do - aBomber.shipName = "Newell Bomber"; - aFighter.shipName = "Meier Fighter"; - - //And because of the overiden constructor - //That still calls the super constructor - //They have unique properties - Log.i("aFighter Shield:", ""+ aFighter.getShieldStrength()); - Log.i("aBomber Shield:", ""+ aBomber.getShieldStrength()); - - //As well as certain things in certain ways - //That are unique to the subclass - aBomber.fireWeapon(); - aFighter.fireWeapon(); - - //Take down those alien ships - //Focus on the bomber it has a weaker shield - aBomber.hitDetected(); - aBomber.hitDetected(); - aBomber.hitDetected(); - aBomber.hitDetected(); - } - -} diff --git a/Chapter09/AndroidManifest.xml b/Chapter09/AndroidManifest.xml deleted file mode 100644 index 7980510..0000000 --- a/Chapter09/AndroidManifest.xml +++ /dev/null @@ -1,23 +0,0 @@ - - - - - - - - - - - - - - \ No newline at end of file diff --git a/Chapter09/java/Ball.java b/Chapter09/java/Ball.java deleted file mode 100644 index 7ab7e50..0000000 --- a/Chapter09/java/Ball.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.gamecodeschool.c9pong; - -class Ball { -} diff --git a/Chapter09/java/Bat.java b/Chapter09/java/Bat.java deleted file mode 100644 index eff2a44..0000000 --- a/Chapter09/java/Bat.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.gamecodeschool.c9pong; - -class Bat { -} diff --git a/Chapter09/java/PongActivity.java b/Chapter09/java/PongActivity.java deleted file mode 100644 index bbf1ae6..0000000 --- a/Chapter09/java/PongActivity.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.gamecodeschool.c9pong; - -import android.app.Activity; -import android.graphics.Point; -import android.os.Bundle; -import android.view.Display; - -public class PongActivity extends Activity { - private PongGame mPongGame; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - Display display = getWindowManager().getDefaultDisplay(); - Point size = new Point(); - display.getSize(size); - - mPongGame = new PongGame(this, size.x, size.y); - setContentView(mPongGame); - } - - @Override - protected void onResume() { - super.onResume(); - - // More code here later in the chapter - mPongGame.resume(); - } - - @Override - protected void onPause() { - super.onPause(); - - // More code here later in the chapter - mPongGame.pause(); - } -} diff --git a/Chapter09/java/PongGame.java b/Chapter09/java/PongGame.java deleted file mode 100644 index 73e5578..0000000 --- a/Chapter09/java/PongGame.java +++ /dev/null @@ -1,229 +0,0 @@ -package com.gamecodeschool.c9pong; - -import android.content.Context; -import android.graphics.Canvas; -import android.graphics.Color; -import android.graphics.Paint; -import android.util.Log; -import android.view.SurfaceHolder; -import android.view.SurfaceView; - -class PongGame extends SurfaceView implements Runnable{ - - // Are we debugging? - private final boolean DEBUGGING = true; - - // These objects are needed to do the drawing - private SurfaceHolder mOurHolder; - private Canvas mCanvas; - private Paint mPaint; - - // How many frames per second did we get? - private long mFPS; - // The number of milliseconds in a second - private final int MILLIS_IN_SECOND = 1000; - - // Holds the resolution of the screen - private int mScreenX; - private int mScreenY; - // How big will the text be? - private int mFontSize; - private int mFontMargin; - - // The game objects - private Bat mBat; - private Ball mBall; - - // The current score and lives remaining - private int mScore = 0; - private int mLives = 3; - - // Here is the Thread and two control variables - private Thread mGameThread = null; - // This volatile vaiable can be accessed - // from inside and outside the thread - private volatile boolean mPlaying; - private boolean mPaused = true; - - // The PongGame constructor - // Called when this line: - // mPongGame = new PongGame(this, size.x, size.y); - // is executed from PongActivity - public PongGame(Context context, int x, int y) { - // Super... calls the parent class - // constructor of SurfaceView - // provided by Android - super(context); - - // Initialize these two members/fields - // With the values passesd in as parameters - mScreenX = x; - mScreenY = y; - - // Font is 5% (1/20th) of screen width - mFontSize = mScreenX / 20; - // Margin is 1.5% (1/75th) of screen width - mFontMargin = mScreenX / 75; - - // Initialize the objects - // ready for drawing with - // getHolder is a method of SurfaceView - mOurHolder = getHolder(); - mPaint = new Paint(); - - // Initialize the bat and ball - - // Everything is ready so start the game - startNewGame(); - } - - // The player has just lost - // or is starting their first game - private void startNewGame(){ - - // Put the ball back to the starting position - - // Rest the score and the player's chances - mScore = 0; - mLives = 3; - - } - - // When we start the thread with: - // mGameThread.start(); - // the run method is continuously called by Android - // because we implemented the Runnable interface - // Calling mGameThread.join(); - // will stop the thread - @Override - public void run() { - // mPlaying gives us finer control - // rather than just relying on the calls to run - // mPlaying must be true AND - // the thread running for the main loop to execute - while (mPlaying) { - - // What time is it now at the start of the loop? - long frameStartTime = System.currentTimeMillis(); - - // Provided the game isn't paused call the update method - if(!mPaused){ - update(); - // Now the bat and ball are in their new positions - // we can see if there have been any collisions - detectCollisions(); - - } - - // The movement has been handled and collisions - // detected now we can draw the scene. - draw(); - - // How long did this frame/loop take? - // Store the answer in timeThisFrame - long timeThisFrame = System.currentTimeMillis() - frameStartTime; - - // Make sure timeThisFrame is at least 1 millisecond - // because accidentally dividing by zero crashes the game - if (timeThisFrame > 0) { - // Store the current frame rate in mFPS - // ready to pass to the update methods of - // mBat and mBall next frame/loop - mFPS = MILLIS_IN_SECOND / timeThisFrame; - } - - } - - } - - private void update() { - // Update the bat and the ball - } - - private void detectCollisions(){ - // Has the bat hit the ball? - - // Has the ball hit the edge of the screen - - // Bottom - - // Top - - // Left - - // Right - - } - - // Draw the game objects and the HUD - void draw() { - if (mOurHolder.getSurface().isValid()) { - // Lock the canvas (graphics memory) ready to draw - mCanvas = mOurHolder.lockCanvas(); - - // Fill the screen with a solid color - mCanvas.drawColor(Color.argb - (255, 26, 128, 182)); - - // Choose a color to paint with - mPaint.setColor(Color.argb - (255, 255, 255, 255)); - - // Draw the bat and ball - - // Choose the font size - mPaint.setTextSize(mFontSize); - - // Draw the HUD - mCanvas.drawText("Score: " + mScore + - " Lives: " + mLives, - mFontMargin , mFontSize, mPaint); - - if(DEBUGGING){ - printDebuggingText(); - } - // Display the drawing on screen - // unlockCanvasAndPost is a method of SurfaceView - mOurHolder.unlockCanvasAndPost(mCanvas); - } - - } - - private void printDebuggingText(){ - int debugSize = mFontSize / 2; - int debugStart = 150; - mPaint.setTextSize(debugSize); - mCanvas.drawText("FPS: " + mFPS , - 10, debugStart + debugSize, mPaint); - - } - - // This method is called by PongActivity - // when the player quits the game - public void pause() { - - // Set mPlaying to false - // Stopping the thread isn't - // always instant - mPlaying = false; - try { - // Stop the thread - mGameThread.join(); - } catch (InterruptedException e) { - Log.e("Error:", "joining thread"); - } - - } - - - // This method is called by PongActivity - // when the player starts the game - public void resume() { - mPlaying = true; - // Initialize the instance of Thread - mGameThread = new Thread(this); - - // Start the thread - mGameThread.start(); - } -} diff --git a/Chapter10/AndroidManifest.xml b/Chapter10/AndroidManifest.xml deleted file mode 100644 index dd7aed0..0000000 --- a/Chapter10/AndroidManifest.xml +++ /dev/null @@ -1,24 +0,0 @@ - - - - - - - - - - - - - - \ No newline at end of file diff --git a/Chapter10/java/Ball.java b/Chapter10/java/Ball.java deleted file mode 100644 index cd0d9e7..0000000 --- a/Chapter10/java/Ball.java +++ /dev/null @@ -1,127 +0,0 @@ -package com.gamecodeschool.c10pong; - -import android.graphics.RectF; - -class Ball { - - // These are the member variables (fields) - // They all have the m prefix - // They are all private - // because direct access is not required - private RectF mRect; - private float mXVelocity; - private float mYVelocity; - private float mBallWidth; - private float mBallHeight; - - // This is the constructor method. - // It is called when by the code: - // mBall = new Ball(mScreenX); - // In the PongGame class - Ball(int screenX){ - - // Make the ball square and 1% of screen width - // of the screen width - mBallWidth = screenX / 100; - mBallHeight = screenX / 100; - - // Initialize the RectF with 0, 0, 0, 0 - // We do it here because we only want to - // do it once. - // WE will initialize the detail - // at the start of each game - mRect = new RectF(); - } - - - // Return a reference to mRect to PongGame - RectF getRect(){ - return mRect; - } - - // Update the ball position. - // Called each frame/loop - void update(long fps){ - // Move the ball based upon the - // horizontal (mXVelocity) and - // vertical(mYVelocity) speed - // and the current frame rate(fps) - - // Move the top left corner - mRect.left = mRect.left + (mXVelocity / fps); - mRect.top = mRect.top + (mYVelocity / fps); - - // Match up the bottom right corner - // based on the size of the ball - mRect.right = mRect.left + mBallWidth; - mRect.bottom = mRect.top + mBallHeight; - } - - // Reverse the vertical direction of travel - void reverseYVelocity(){ - mYVelocity = -mYVelocity; - } - - // Reverse the horizontal direction of travel - void reverseXVelocity(){ - mXVelocity = -mXVelocity; - } - - void reset(int x, int y){ - - // Initialise the four points of - // the rectangle which defines the ball - mRect.left = x / 2; - mRect.top = 0; - mRect.right = x / 2 + mBallWidth; - mRect.bottom = mBallHeight; - - // How fast will the ball travel - // You could vary this to suit - // You could even increase it as the game progresses - // to make it harder - mYVelocity = -(y / 3); - mXVelocity = (y / 3); - } - - void increaseVelocity(){ - // increase the speed by 10% - mXVelocity = mXVelocity * 1.1f; - mYVelocity = mYVelocity * 1.1f; - } - - - // Bounce the ball back based upon - // whether it hits the left or right hand side - void batBounce(RectF batPosition){ - - // Detect center of bat - float batCenter = batPosition.left + - (batPosition.width() / 2); - - // detect the center of the ball - float ballCenter = mRect.left + - (mBallWidth / 2); - - // Where on the bat did the ball hit? - float relativeIntersect = (batCenter - ballCenter); - - // Pick a bounce direction - if(relativeIntersect < 0){ - // Go right - mXVelocity = Math.abs(mXVelocity); - // Math.abs is a static method that - // strips any negative values from a value. - // So -1 becomes 1 and 1 stays as 1 - }else{ - // Go left - mXVelocity = -Math.abs(mXVelocity); - } - - // Having calculated left or right for - // horizontal direction simply reverse the - // vertical direction to go back up - // the screen - reverseYVelocity(); - } -} \ No newline at end of file diff --git a/Chapter10/java/Bat.java b/Chapter10/java/Bat.java deleted file mode 100644 index b945313..0000000 --- a/Chapter10/java/Bat.java +++ /dev/null @@ -1,102 +0,0 @@ -package com.gamecodeschool.c10pong; - -import android.graphics.RectF; - -class Bat { - - // These are the member variables (fields) - // They all have the m prefix - // They are all private - // because direct access is not required - private RectF mRect; - private float mLength; - private float mXCoord; - private float mBatSpeed; - private int mScreenX; - - // These variables are public and final - // They can be directly accessed by - // the instance (in PongGame) - // because they are part of the same - // package but cannot be changed - final int STOPPED = 0; - final int LEFT = 1; - final int RIGHT = 2; - - // Keeps track of if an how the ball is moving - // Starting with STOPPED - private int mBatMoving = STOPPED; - - Bat(int sx, int sy){ - - // Bat needs to know the screen - // horizontal resolution - // Outside of this method - mScreenX = sx; - - // Configure the size of the bat based on - // the screen resolution - // One eighth the screen width - mLength = mScreenX / 8; - // One fortieth the screen height - float height = sy / 40; - - // Configure the starting locaion of the bat - // Roughly the middle horizontally - mXCoord = mScreenX / 2; - // The height of the bat - // off of the bottom of the screen - float mYCoord = sy - height; - - // Initialize mRect based on the size and position - mRect = new RectF(mXCoord, mYCoord, - mXCoord + mLength, - mYCoord + height); - - // Configure the speed of the bat - // This code means the bat can cover the - // width of the screen in 1 second - mBatSpeed = mScreenX; - } - - // Return a reference to the mRect object - RectF getRect(){ - return mRect; - } - - // Update the movement state passed - // in by the onTouchEvent method - void setMovementState(int state){ - mBatMoving = state; - } - - - // Update the bat- Called each frame/loop - void update(long fps){ - - // Move the bat based on the mBatMoving variable - // and the speed of the previous frame - if(mBatMoving == LEFT){ - mXCoord = mXCoord - mBatSpeed / fps; - } - - if(mBatMoving == RIGHT){ - mXCoord = mXCoord + mBatSpeed / fps; - } - - // Stop the bat going off the screen - if(mXCoord < 0){ - mXCoord = 0; - } - - if(mXCoord + mLength > mScreenX){ - mXCoord = mScreenX - mLength; - } - - // Update mRect based on the results from - // the previous code in update - mRect.left = mXCoord; - mRect.right = mXCoord + mLength; - } - -} diff --git a/Chapter10/java/PongActivity.java b/Chapter10/java/PongActivity.java deleted file mode 100644 index 101c6da..0000000 --- a/Chapter10/java/PongActivity.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.gamecodeschool.c10pong; - -import android.app.Activity; -import android.graphics.Point; -import android.os.Bundle; -import android.view.Display; - -public class PongActivity extends Activity { - private PongGame mPongGame; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - Display display = getWindowManager().getDefaultDisplay(); - Point size = new Point(); - display.getSize(size); - - mPongGame = new PongGame(this, size.x, size.y); - setContentView(mPongGame); - } - - @Override - protected void onResume() { - super.onResume(); - - // More code here later in the chapter - mPongGame.resume(); - } - - @Override - protected void onPause() { - super.onPause(); - - // More code here later in the chapter - mPongGame.pause(); - } -} diff --git a/Chapter10/java/PongGame.java b/Chapter10/java/PongGame.java deleted file mode 100644 index 7e40508..0000000 --- a/Chapter10/java/PongGame.java +++ /dev/null @@ -1,279 +0,0 @@ -package com.gamecodeschool.c10pong; - -import android.content.Context; -import android.graphics.Canvas; -import android.graphics.Color; -import android.graphics.Paint; -import android.util.Log; -import android.view.MotionEvent; -import android.view.SurfaceHolder; -import android.view.SurfaceView; - -class PongGame extends SurfaceView implements Runnable{ - - // Are we debugging? - private final boolean DEBUGGING = true; - - // These objects are needed to do the drawing - private SurfaceHolder mOurHolder; - private Canvas mCanvas; - private Paint mPaint; - - // How many frames per second did we get? - private long mFPS; - // The number of milliseconds in a second - private final int MILLIS_IN_SECOND = 1000; - - // Holds the resolution of the screen - private int mScreenX; - private int mScreenY; - // How big will the text be? - private int mFontSize; - private int mFontMargin; - - // The game objects - private Bat mBat; - private Ball mBall; - - // The current score and lives remaining - private int mScore = 0; - private int mLives = 3; - - // Here is the Thread and two control variables - private Thread mGameThread = null; - // This volatile vaiable can be accessed - // from inside and outside the thread - private volatile boolean mPlaying; - private boolean mPaused = true; - - // The PongGame constructor - // Called when this line: - // mPongGame = new PongGame(this, size.x, size.y); - // is executed from PongActivity - public PongGame(Context context, int x, int y) { - // Super... calls the parent class - // constructor of SurfaceView - // provided by Android - super(context); - - // Initialize these two members/fields - // With the values passesd in as parameters - mScreenX = x; - mScreenY = y; - - // Font is 5% (1/20th) of screen width - mFontSize = mScreenX / 20; - // Margin is 1.5% (1/75th) of screen width - mFontMargin = mScreenX / 75; - - // Initialize the objects - // ready for drawing with - // getHolder is a method of SurfaceView - mOurHolder = getHolder(); - mPaint = new Paint(); - - // Initialize the bat and ball - mBall = new Ball(mScreenX); - mBat = new Bat(mScreenX, mScreenY); - - // Everything is ready so start the game - startNewGame(); - } - - // The player has just lost - // or is starting their first game - private void startNewGame(){ - - // Put the ball back to the starting position - mBall.reset(mScreenX, mScreenY); - - // Rest the score and the player's chances - mScore = 0; - mLives = 3; - - } - - // When we start the thread with: - // mGameThread.start(); - // the run method is continuously called by Android - // because we implemented the Runnable interface - // Calling mGameThread.join(); - // will stop the thread - @Override - public void run() { - // mPlaying gives us finer control - // rather than just relying on the calls to run - // mPlaying must be true AND - // the thread running for the main loop to execute - while (mPlaying) { - - // What time is it now at the start of the loop? - long frameStartTime = System.currentTimeMillis(); - - // Provided the game isn't paused call the update method - if(!mPaused){ - update(); - // Now the bat and ball are in their new positions - // we can see if there have been any collisions - detectCollisions(); - - } - - // The movement has been handled and collisions - // detected now we can draw the scene. - draw(); - - // How long did this frame/loop take? - // Store the answer in timeThisFrame - long timeThisFrame = System.currentTimeMillis() - frameStartTime; - - // Make sure timeThisFrame is at least 1 millisecond - // because accidentally dividing by zero crashes the game - if (timeThisFrame > 0) { - // Store the current frame rate in mFPS - // ready to pass to the update methods of - // mBat and mBall next frame/loop - mFPS = MILLIS_IN_SECOND / timeThisFrame; - } - - } - - } - - private void update() { - // Update the bat and the ball - mBall.update(mFPS); - mBat.update(mFPS); - } - - private void detectCollisions(){ - // Has the bat hit the ball? - - // Has the ball hit the edge of the screen - - // Bottom - - // Top - - // Left - - // Right - - } - - // Draw the game objects and the HUD - void draw() { - if (mOurHolder.getSurface().isValid()) { - // Lock the canvas (graphics memory) ready to draw - mCanvas = mOurHolder.lockCanvas(); - - // Fill the screen with a solid color - mCanvas.drawColor(Color.argb - (255, 26, 128, 182)); - - // Choose a color to paint with - mPaint.setColor(Color.argb - (255, 255, 255, 255)); - - // Draw the bat and ball - mCanvas.drawRect(mBall.getRect(), mPaint); - mCanvas.drawRect(mBat.getRect(), mPaint); - - // Choose the font size - mPaint.setTextSize(mFontSize); - - // Draw the HUD - mCanvas.drawText("Score: " + mScore + - " Lives: " + mLives, - mFontMargin , mFontSize, mPaint); - - if(DEBUGGING){ - printDebuggingText(); - } - // Display the drawing on screen - // unlockCanvasAndPost is a method of SurfaceView - mOurHolder.unlockCanvasAndPost(mCanvas); - } - - } - - // Handle all the screen touches - @Override - public boolean onTouchEvent(MotionEvent motionEvent) { - - // This switch block replaces the - // if statement from the Sub Hunter game - switch (motionEvent.getAction() & - MotionEvent.ACTION_MASK) { - - // The player has put their finger on the screen - case MotionEvent.ACTION_DOWN: - - // If the game was paused unpause - mPaused = false; - - // Where did the touch happen - if(motionEvent.getX() > mScreenX / 2){ - // On the right hand side - mBat.setMovementState(mBat.RIGHT); - } - else{ - // On the left hand side - mBat.setMovementState(mBat.LEFT); - } - - break; - - // The player lifted their finger - // from anywhere on screen. - // It is possible to create bugs by using - // multiple fingers. We will use more - // complicated and robust touch handling - // in later projects - case MotionEvent.ACTION_UP: - - // Stop the bat moving - mBat.setMovementState(mBat.STOPPED); - break; - } - return true; - } - - private void printDebuggingText(){ - int debugSize = mFontSize / 2; - int debugStart = 150; - mPaint.setTextSize(debugSize); - mCanvas.drawText("FPS: " + mFPS , - 10, debugStart + debugSize, mPaint); - - } - - // This method is called by PongActivity - // when the player quits the game - public void pause() { - - // Set mPlaying to false - // Stopping the thread isn't - // always instant - mPlaying = false; - try { - // Stop the thread - mGameThread.join(); - } catch (InterruptedException e) { - Log.e("Error:", "joining thread"); - } - - } - - - // This method is called by PongActivity - // when the player starts the game - public void resume() { - mPlaying = true; - // Initialize the instance of Thread - mGameThread = new Thread(this); - - // Start the thread - mGameThread.start(); - } -} \ No newline at end of file diff --git a/Chapter11/AndroidManifest.xml b/Chapter11/AndroidManifest.xml deleted file mode 100644 index 25110f0..0000000 --- a/Chapter11/AndroidManifest.xml +++ /dev/null @@ -1,24 +0,0 @@ - - - - - - - - - - - - - - \ No newline at end of file diff --git a/Chapter11/assets/beep.ogg b/Chapter11/assets/beep.ogg deleted file mode 100644 index 16e0e30..0000000 Binary files a/Chapter11/assets/beep.ogg and /dev/null differ diff --git a/Chapter11/assets/boop.ogg b/Chapter11/assets/boop.ogg deleted file mode 100644 index e9e729a..0000000 Binary files a/Chapter11/assets/boop.ogg and /dev/null differ diff --git a/Chapter11/assets/bop.ogg b/Chapter11/assets/bop.ogg deleted file mode 100644 index 6253161..0000000 Binary files a/Chapter11/assets/bop.ogg and /dev/null differ diff --git a/Chapter11/assets/miss.ogg b/Chapter11/assets/miss.ogg deleted file mode 100644 index 0c8e4d6..0000000 Binary files a/Chapter11/assets/miss.ogg and /dev/null differ diff --git a/Chapter11/java/Ball.java b/Chapter11/java/Ball.java deleted file mode 100644 index 6493c1a..0000000 --- a/Chapter11/java/Ball.java +++ /dev/null @@ -1,127 +0,0 @@ -package com.gamecodeschool.c11pong; - -import android.graphics.RectF; - -class Ball { - - // These are the member variables (fields) - // They all have the m prefix - // They are all private - // because direct access is not required - private RectF mRect; - private float mXVelocity; - private float mYVelocity; - private float mBallWidth; - private float mBallHeight; - - // This is the constructor method. - // It is called when by the code: - // mBall = new Ball(mScreenX); - // In the PongGame class - Ball(int screenX){ - - // Make the ball square and 1% of screen width - // of the screen width - mBallWidth = screenX / 100; - mBallHeight = screenX / 100; - - // Initialize the RectF with 0, 0, 0, 0 - // We do it here because we only want to - // do it once. - // WE will initialize the detail - // at the start of each game - mRect = new RectF(); - } - - - // Return a reference to mRect to PongGame - RectF getRect(){ - return mRect; - } - - // Update the ball position. - // Called each frame/loop - void update(long fps){ - // Move the ball based upon the - // horizontal (mXVelocity) and - // vertical(mYVelocity) speed - // and the current frame rate(mFPS) - - // Move the top left corner - mRect.left = mRect.left + (mXVelocity / fps); - mRect.top = mRect.top + (mYVelocity / fps); - - // Match up the bottom right corner - // based on the size of the ball - mRect.right = mRect.left + mBallWidth; - mRect.bottom = mRect.top + mBallHeight; - } - - // Reverse the vertical direction of travel - void reverseYVelocity(){ - mYVelocity = -mYVelocity; - } - - // Reverse the horizontal direction of travel - void reverseXVelocity(){ - mXVelocity = -mXVelocity; - } - - void reset(int x, int y){ - - // Initialise the four points of - // the rectangle which defines the ball - mRect.left = x / 2; - mRect.top = 0; - mRect.right = x / 2 + mBallWidth; - mRect.bottom = mBallHeight; - - // How fast will the ball travel - // You could vary this to suit - // You could even increase it as the game progresses - // to make it harder - mYVelocity = -(y / 3); - mXVelocity = (y / 3); - } - - void increaseVelocity(){ - // increase the speed by 10% - mXVelocity = mXVelocity * 1.1f; - mYVelocity = mYVelocity * 1.1f; - } - - - // Bounce the ball back based upon - // whether it hits the left or right hand side - void batBounce(RectF batPosition){ - - // Detect center of bat - float batCenter = batPosition.left + - (batPosition.width() / 2); - - // detect the center of the ball - float ballCenter = mRect.left + - (mBallWidth / 2); - - // Where on the bat did the ball hit? - float relativeIntersect = (batCenter - ballCenter); - - // Pick a bounce direction - if(relativeIntersect < 0){ - // Go right - mXVelocity = Math.abs(mXVelocity); - // Math.abs is a static method that - // strips any negative values from a value. - // So -1 becomes 1 and 1 stays as 1 - }else{ - // Go left - mXVelocity = -Math.abs(mXVelocity); - } - - // Having calculated left or right for - // horizontal direction simply reverse the - // vertical direction to go back up - // the screen - reverseYVelocity(); - } -} \ No newline at end of file diff --git a/Chapter11/java/Bat.java b/Chapter11/java/Bat.java deleted file mode 100644 index 48f8fd0..0000000 --- a/Chapter11/java/Bat.java +++ /dev/null @@ -1,102 +0,0 @@ -package com.gamecodeschool.c11pong; - -import android.graphics.RectF; - -class Bat { - - // These are the member variables (fields) - // They all have the m prefix - // They are all private - // because direct access is not required - private RectF mRect; - private float mLength; - private float mXCoord; - private float mBatSpeed; - private int mScreenX; - - // These variables are public and final - // They can be directly accessed by - // the instance (in PongGame) - // because they are part of the same - // package but cannot be changed - final int STOPPED = 0; - final int LEFT = 1; - final int RIGHT = 2; - - // Keeps track of if an how the ball is moving - // Starting with STOPPED - private int mBatMoving = STOPPED; - - Bat(int sx, int sy){ - - // Bat needs to know the screen - // horizontal resolution - // Outside of this method - mScreenX = sx; - - // Configure the size of the bat based on - // the screen resolution - // One eighth the screen width - mLength = mScreenX / 8; - // One fortieth the screen height - float height = sy / 40; - - // Configure the starting locaion of the bat - // Roughly the middle horizontally - mXCoord = mScreenX / 2; - // The height of the bat - // off of the bottom of the screen - float mYCoord = sy - height; - - // Initialize mRect based on the size and position - mRect = new RectF(mXCoord, mYCoord, - mXCoord + mLength, - mYCoord + height); - - // Configure the speed of the bat - // This code means the bat can cover the - // width of the screen in 1 second - mBatSpeed = mScreenX; - } - - // Return a reference to the mRect object - RectF getRect(){ - return mRect; - } - - // Update the movement state passed - // in by the onTouchEvent method - void setMovementState(int state){ - mBatMoving = state; - } - - - // Update the bat- Called each frame/loop - void update(long fps){ - - // Move the bat based on the mBatMoving variable - // and the speed of the previous frame - if(mBatMoving == LEFT){ - mXCoord = mXCoord - mBatSpeed / fps; - } - - if(mBatMoving == RIGHT){ - mXCoord = mXCoord + mBatSpeed / fps; - } - - // Stop the bat going off the screen - if(mXCoord < 0){ - mXCoord = 0; - } - - if(mXCoord + mLength > mScreenX){ - mXCoord = mScreenX - mLength; - } - - // Update mRect based on the results from - // the previous code in update - mRect.left = mXCoord; - mRect.right = mXCoord + mLength; - } - -} diff --git a/Chapter11/java/PongActivity.java b/Chapter11/java/PongActivity.java deleted file mode 100644 index e7c48e3..0000000 --- a/Chapter11/java/PongActivity.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.gamecodeschool.c11pong; - -import android.app.Activity; -import android.graphics.Point; -import android.os.Bundle; -import android.view.Display; - -public class PongActivity extends Activity { - private PongGame mPongGame; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - Display display = getWindowManager().getDefaultDisplay(); - Point size = new Point(); - display.getSize(size); - - mPongGame = new PongGame(this, size.x, size.y); - setContentView(mPongGame); - } - - @Override - protected void onResume() { - super.onResume(); - - // More code here later in the chapter - mPongGame.resume(); - } - - @Override - protected void onPause() { - super.onPause(); - - // More code here later in the chapter - mPongGame.pause(); - } -} diff --git a/Chapter11/java/PongGame.java b/Chapter11/java/PongGame.java deleted file mode 100644 index e3caa42..0000000 --- a/Chapter11/java/PongGame.java +++ /dev/null @@ -1,369 +0,0 @@ -package com.gamecodeschool.c11pong; - -import android.content.Context; -import android.content.res.AssetFileDescriptor; -import android.content.res.AssetManager; -import android.graphics.Canvas; -import android.graphics.Color; -import android.graphics.Paint; -import android.graphics.RectF; -import android.media.AudioAttributes; -import android.media.AudioManager; -import android.media.SoundPool; -import android.os.Build; -import android.util.Log; -import android.view.MotionEvent; -import android.view.SurfaceHolder; -import android.view.SurfaceView; - -import java.io.IOException; - -class PongGame extends SurfaceView implements Runnable{ - - // Are we debugging? - private final boolean DEBUGGING = true; - - // These objects are needed to do the drawing - private SurfaceHolder mOurHolder; - private Canvas mCanvas; - private Paint mPaint; - - // How many frames per second did we get? - private long mFPS; - // The number of milliseconds in a second - private final int MILLIS_IN_SECOND = 1000; - - // Holds the resolution of the screen - private int mScreenX; - private int mScreenY; - // How big will the text be? - private int mFontSize; - private int mFontMargin; - - // The game objects - private Bat mBat; - private Ball mBall; - - // The current score and lives remaining - private int mScore = 0; - private int mLives = 3; - - // Here is the Thread and two control variables - private Thread mGameThread = null; - // This volatile variable can be accessed - // from inside and outside the thread - private volatile boolean mPlaying; - private boolean mPaused = true; - - // All these are for playing sounds - private SoundPool mSP; - private int mBeepID = -1; - private int mBoopID = -1; - private int mBopID = -1; - private int mMissID = -1; - - // The PongGame constructor - // Called when this line: - // mPongGame = new PongGame(this, size.x, size.y); - // is executed from PongActivity - public PongGame(Context context, int x, int y) { - // Super... calls the parent class - // constructor of SurfaceView - // provided by Android - super(context); - - // Initialize these two members/fields - // With the values passesd in as parameters - mScreenX = x; - mScreenY = y; - - // Font is 5% (1/20th) of screen width - mFontSize = mScreenX / 20; - // Margin is 1.5% (1/75th) of screen width - mFontMargin = mScreenX / 75; - - // Initialize the objects - // ready for drawing with - // getHolder is a method of SurfaceView - mOurHolder = getHolder(); - mPaint = new Paint(); - - // Initialize the bat and ball - mBall = new Ball(mScreenX); - mBat = new Bat(mScreenX, mScreenY); - - // Prepare the SoundPool instance - // Depending upon the version of Android - - 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); - } - - - - // Open each of the sound files in turn - // and load them in to Ram ready to play - // The try-catch blocks handle when this fails - // and is required. - try{ - AssetManager assetManager = context.getAssets(); - AssetFileDescriptor descriptor; - - descriptor = assetManager.openFd("beep.ogg"); - mBeepID = mSP.load(descriptor, 0); - - descriptor = assetManager.openFd("boop.ogg"); - mBoopID = mSP.load(descriptor, 0); - - descriptor = assetManager.openFd("bop.ogg"); - mBopID = mSP.load(descriptor, 0); - - descriptor = assetManager.openFd("miss.ogg"); - mMissID = mSP.load(descriptor, 0); - - - }catch(IOException e){ - Log.e("error", "failed to load sound files"); - } - - // Everything is ready so start the game - startNewGame(); - } - - // The player has just lost - // or is starting their first game - private void startNewGame(){ - - // Put the ball back to the starting position - mBall.reset(mScreenX, mScreenY); - - // Rest the score and the player's chances - mScore = 0; - mLives = 3; - - } - - // When we start the thread with: - // mGameThread.start(); - // the run method is continuously called by Android - // because we implemented the Runnable interface - // Calling mGameThread.join(); - // will stop the thread - @Override - public void run() { - // mPlaying gives us finer control - // rather than just relying on the calls to run - // mPlaying must be true AND - // the thread running for the main loop to execute - while (mPlaying) { - - // What time is it now at the start of the loop? - long frameStartTime = System.currentTimeMillis(); - - // Provided the game isn't paused call the update method - if(!mPaused){ - update(); - // Now the bat and ball are in their new positions - // we can see if there have been any collisions - detectCollisions(); - - } - - // The movement has been handled and collisions - // detected now we can draw the scene. - draw(); - - // How long did this frame/loop take? - // Store the answer in timeThisFrame - long timeThisFrame = System.currentTimeMillis() - frameStartTime; - - // Make sure timeThisFrame is at least 1 millisecond - // because accidentally dividing by zero crashes the game - if (timeThisFrame > 0) { - // Store the current frame rate in mFPS - // ready to pass to the update methods of - // mBat and mBall next frame/loop - mFPS = MILLIS_IN_SECOND / timeThisFrame; - } - - } - - } - - private void update() { - // Update the bat and the ball - mBall.update(mFPS); - mBat.update(mFPS); - } - - private void detectCollisions(){ - // Has the bat hit the ball? - if(RectF.intersects(mBat.getRect(), mBall.getRect())) { - // Realistic-ish bounce - mBall.batBounce(mBat.getRect()); - mBall.increaseVelocity(); - mScore++; - mSP.play(mBeepID, 1, 1, 0, 0, 1); - } - - // Has the ball hit the edge of the screen - - // Bottom - if(mBall.getRect().bottom > mScreenY){ - mBall.reverseYVelocity(); - - mLives--; - mSP.play(mMissID, 1, 1, 0, 0, 1); - - if(mLives == 0){ - mPaused = true; - startNewGame(); - } - } - - // Top - if(mBall.getRect().top < 0){ - mBall.reverseYVelocity(); - mSP.play(mBoopID, 1, 1, 0, 0, 1); - } - - // Left - if(mBall.getRect().left < 0){ - mBall.reverseXVelocity(); - mSP.play(mBopID, 1, 1, 0, 0, 1); - } - - // Right - if(mBall.getRect().right > mScreenX){ - mBall.reverseXVelocity(); - mSP.play(mBopID, 1, 1, 0, 0, 1); - } - - } - - // Draw the game objects and the HUD - void draw() { - if (mOurHolder.getSurface().isValid()) { - // Lock the canvas (graphics memory) ready to draw - mCanvas = mOurHolder.lockCanvas(); - - // Fill the screen with a solid color - mCanvas.drawColor(Color.argb - (255, 26, 128, 182)); - - // Choose a color to paint with - mPaint.setColor(Color.argb - (255, 255, 255, 255)); - - // Draw the bat and ball - mCanvas.drawRect(mBall.getRect(), mPaint); - mCanvas.drawRect(mBat.getRect(), mPaint); - - // Choose the font size - mPaint.setTextSize(mFontSize); - - // Draw the HUD - mCanvas.drawText("Score: " + mScore + - " Lives: " + mLives, - mFontMargin , mFontSize, mPaint); - - if(DEBUGGING){ - printDebuggingText(); - } - // Display the drawing on screen - // unlockCanvasAndPost is a method of SurfaceView - mOurHolder.unlockCanvasAndPost(mCanvas); - } - - } - - // Handle all the screen touches - @Override - public boolean onTouchEvent(MotionEvent motionEvent) { - - // This switch block replaces the - // if statement from the Sub Hunter game - switch (motionEvent.getAction() & - MotionEvent.ACTION_MASK) { - - // The player has put their finger on the screen - case MotionEvent.ACTION_DOWN: - - // If the game was paused unpause - mPaused = false; - - // Where did the touch happen - if(motionEvent.getX() > mScreenX / 2){ - // On the right hand side - mBat.setMovementState(mBat.RIGHT); - } - else{ - // On the left hand side - mBat.setMovementState(mBat.LEFT); - } - - break; - - // The player lifted their finger - // from anywhere on screen. - // It is possible to create bugs by using - // multiple fingers. We will use more - // complicated and robust touch handling - // in later projects - case MotionEvent.ACTION_UP: - - // Stop the bat moving - mBat.setMovementState(mBat.STOPPED); - break; - } - return true; - } - - private void printDebuggingText(){ - int debugSize = mFontSize / 2; - int debugStart = 150; - mPaint.setTextSize(debugSize); - mCanvas.drawText("FPS: " + mFPS , - 10, debugStart + debugSize, mPaint); - - } - - // This method is called by PongActivity - // when the player quits the game - public void pause() { - - // Set mPlaying to false - // Stopping the thread isn't - // always instant - mPlaying = false; - try { - // Stop the thread - mGameThread.join(); - } catch (InterruptedException e) { - Log.e("Error:", "joining thread"); - } - - } - - - // This method is called by PongActivity - // when the player starts the game - public void resume() { - mPlaying = true; - // Initialize the instance of Thread - mGameThread = new Thread(this); - - // Start the thread - mGameThread.start(); - } -} diff --git a/Chapter12/C12BulletHell/AndroidManifest.xml b/Chapter12/C12BulletHell/AndroidManifest.xml deleted file mode 100644 index dc37e8f..0000000 --- a/Chapter12/C12BulletHell/AndroidManifest.xml +++ /dev/null @@ -1,24 +0,0 @@ - - - - - - - - - - - - - - \ No newline at end of file diff --git a/Chapter12/C12BulletHell/assets/beep.ogg b/Chapter12/C12BulletHell/assets/beep.ogg deleted file mode 100644 index 16e0e30..0000000 Binary files a/Chapter12/C12BulletHell/assets/beep.ogg and /dev/null differ diff --git a/Chapter12/C12BulletHell/assets/teleport.ogg b/Chapter12/C12BulletHell/assets/teleport.ogg deleted file mode 100644 index fb2eb90..0000000 Binary files a/Chapter12/C12BulletHell/assets/teleport.ogg and /dev/null differ diff --git a/Chapter12/C12BulletHell/drawable/bob.png b/Chapter12/C12BulletHell/drawable/bob.png deleted file mode 100644 index 251d305..0000000 Binary files a/Chapter12/C12BulletHell/drawable/bob.png and /dev/null differ diff --git a/Chapter12/C12BulletHell/java/Bob.java b/Chapter12/C12BulletHell/java/Bob.java deleted file mode 100644 index 6f2a3ee..0000000 --- a/Chapter12/C12BulletHell/java/Bob.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.gamecodeschool.c12bullethell; - -class Bob { -} diff --git a/Chapter12/C12BulletHell/java/Bullet.java b/Chapter12/C12BulletHell/java/Bullet.java deleted file mode 100644 index f866a6a..0000000 --- a/Chapter12/C12BulletHell/java/Bullet.java +++ /dev/null @@ -1,71 +0,0 @@ -package com.gamecodeschool.c12bullethell; - -import android.graphics.RectF; - -class Bullet { - - // A RectF to represent the size and location of the bullet - private RectF mRect; - - // How fast is the bullet travelling? - private float mXVelocity; - private float mYVelocity; - - // How big is a bullet - private float mWidth; - private float mHeight; - - // The constructor - Bullet(int screenX){ - - // Configure the bullet based on - // the screen width in pixels - mWidth = screenX / 100; - mHeight = screenX / 100; - mRect = new RectF(); - mYVelocity = (screenX / 5); - mXVelocity = (screenX / 5); - } - - // Return a reference to the RectF - RectF getRect(){ - return mRect; - } - - - // Move the bullet based on the speed and the frame rate - void update(long fps){ - mRect.left = mRect.left + (mXVelocity / fps); - mRect.top = mRect.top + (mYVelocity / fps); - mRect.right = mRect.left + mWidth; - mRect.bottom = mRect.top - mHeight; - } - - // Reverse the bullets vertical direction - void reverseYVelocity(){ - mYVelocity = -mYVelocity; - } - - // Reverse the bullets horizontal direction - void reverseXVelocity(){ - mXVelocity = -mXVelocity; - } - - // Spawn a new bullet - void spawn(int pX, int pY, int vX, int vY){ - - // Spawn the bullet at the location - // passed in as parameters - mRect.left = pX; - mRect.top = pY; - mRect.right = pX + mWidth; - mRect.bottom = pY + mHeight; - - // Head away from the player - // It's only fair - mXVelocity = mXVelocity * vX; - mYVelocity = mYVelocity * vY; - - - } -} diff --git a/Chapter12/C12BulletHell/java/BulletHellActivity.java b/Chapter12/C12BulletHell/java/BulletHellActivity.java deleted file mode 100644 index 545459b..0000000 --- a/Chapter12/C12BulletHell/java/BulletHellActivity.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.gamecodeschool.c12bullethell; - -import android.app.Activity; -import android.graphics.Point; -import android.os.Bundle; -import android.view.Display; - - -// This class is almost exactly the same as the Pong project -public class BulletHellActivity extends Activity { - - // An instance of the main class of this project - private BulletHellGame mBHGame; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - // Get the screen resolution - Display display = getWindowManager().getDefaultDisplay(); - Point size = new Point(); - display.getSize(size); - - // Call the constructor(initialize) the BulletHellGame instance - mBHGame = new BulletHellGame(this, size.x, size.y); - setContentView(mBHGame); - } - - @Override - // Start the main game thread when the game is launched - protected void onResume() { - super.onResume(); - - mBHGame.resume(); - } - - @Override - // Stop the thread when the player quits - protected void onPause() { - super.onPause(); - - mBHGame.pause(); - } -} diff --git a/Chapter12/C12BulletHell/java/BulletHellGame.java b/Chapter12/C12BulletHell/java/BulletHellGame.java deleted file mode 100644 index 30757e8..0000000 --- a/Chapter12/C12BulletHell/java/BulletHellGame.java +++ /dev/null @@ -1,262 +0,0 @@ -package com.gamecodeschool.c12bullethell; - -import android.content.Context; -import android.content.res.AssetFileDescriptor; -import android.content.res.AssetManager; -import android.graphics.Canvas; -import android.graphics.Color; -import android.graphics.Paint; -import android.media.AudioAttributes; -import android.media.AudioManager; -import android.media.SoundPool; -import android.os.Build; -import android.util.Log; -import android.view.MotionEvent; -import android.view.SurfaceHolder; -import android.view.SurfaceView; -import java.io.IOException; -import java.util.Random; - -class BulletHellGame extends SurfaceView implements Runnable{ - - // Are we currently debugging - boolean mDebugging = true; - - // Objects for the game loop/thread - private Thread mGameThread = null; - private volatile boolean mPlaying; - private boolean mPaused = true; - - // Objects for drawing - private SurfaceHolder mOurHolder; - private Canvas mCanvas; - private Paint mPaint; - - // Keep track of the frame rate - private long mFPS; - // The number of milliseconds in a second - private final int MILLIS_IN_SECOND = 1000; - - // Holds the resolution of the screen - private int mScreenX; - private int mScreenY; - - // How big will the text be? - private int mFontSize; - private int mFontMargin; - - // These are for the sound - private SoundPool mSP; - private int mBeepID = -1; - private int mTeleportID = -1; - - // Up to 10000 bullets - private Bullet[] mBullets = new Bullet[10]; - private int mNumBullets = 0; - private int mSpawnRate = 1; - - private Random mRandomX = new Random(); - private Random mRandomY = new Random(); - - // This is the constructor method that gets called - // from BullethellActivity - public BulletHellGame(Context context, int x, int y) { - super(context); - - mScreenX = x; - mScreenY = y; - // Font is 5% of screen width - mFontSize = mScreenX / 20; - // Margin is 2% of screen width - mFontMargin = mScreenX / 50; - - mOurHolder = getHolder(); - mPaint = new Paint(); - - - // 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; - - descriptor = assetManager.openFd("beep.ogg"); - mBeepID = mSP.load(descriptor, 0); - - descriptor = assetManager.openFd("teleport.ogg"); - mTeleportID = mSP.load(descriptor, 0); - - }catch(IOException e){ - Log.e("error", "failed to load sound files"); - } - - for(int i = 0; i < mBullets.length; i++){ - mBullets[i] = new Bullet(mScreenX); - } - - startGame(); - } - - - // Called to start a new game - public void startGame(){ - } - - // Spawns ANOTHER bullet - private void spawnBullet(){ - // Add one to the number of bullets - mNumBullets++; - - // Where to spawn the next bullet - // And in which direction should it travel - int spawnX; - int spawnY; - int velocityX; - int velocityY; - - // This code will change in chapter 13 - - // Pick a random point on the screen - // to spawn a bullet - spawnX = mRandomX.nextInt(mScreenX); - spawnY = mRandomY.nextInt(mScreenY); - - // The horizontal direction of travel - velocityX = 1; - // Randomly make velocityX negative - if(mRandomX.nextInt(2)==0){ - velocityX = -1; - } - - velocityY = 1; - // Randomly make velocityY negative - if(mRandomY.nextInt(2)==0){ - velocityY = -1; - } - - // Spawn the bullet - mBullets[mNumBullets - 1].spawn(spawnX, spawnY, velocityX, velocityY); - } - - // Handles the game loop - @Override - public void run() { - while (mPlaying) { - - long frameStartTime = System.currentTimeMillis(); - - if(!mPaused){ - update(); - // Now all the bullets have been moved - // we can detect any collisions - detectCollisions(); - - } - - draw(); - - long timeThisFrame = System.currentTimeMillis() - frameStartTime; - if (timeThisFrame >= 1) { - mFPS = MILLIS_IN_SECOND / timeThisFrame; - } - - } - } - - - // Update all the game objects - private void update(){ - for(int i = 0; i < mNumBullets; i++){ - mBullets[i].update(mFPS); - } - } - - private void detectCollisions(){ - // Has a bullet collided with a wall? - // Loop through each active bullet in turn - for(int i = 0; i < mNumBullets; i++) { - if (mBullets[i].getRect().bottom > mScreenY) { - mBullets[i].reverseYVelocity(); - } - - if (mBullets[i].getRect().top < 0) { - mBullets[i].reverseYVelocity(); - } - - if (mBullets[i].getRect().left < 0) { - mBullets[i].reverseXVelocity(); - } - - if (mBullets[i].getRect().right > mScreenX) { - mBullets[i].reverseXVelocity(); - } - - } - } - - private void draw(){ - if (mOurHolder.getSurface().isValid()) { - mCanvas = mOurHolder.lockCanvas(); - mCanvas.drawColor(Color.argb(255, 243, 111, 36)); - mPaint.setColor(Color.argb(255, 255, 255, 255)); - - // All the drawing code will go here - for(int i = 0; i < mNumBullets; i++){ - mCanvas.drawRect(mBullets[i].getRect(), mPaint); - } - - if(mDebugging) { - printDebuggingText(); - } - - mOurHolder.unlockCanvasAndPost(mCanvas); - } - } - - @Override - public boolean onTouchEvent(MotionEvent motionEvent) { - - mPaused = false; - spawnBullet(); - - return true; - } - - public void pause() { - mPlaying = false; - try { - mGameThread.join(); - } catch (InterruptedException e) { - Log.e("Error:", "joining thread"); - } - } - - public void resume() { - mPlaying = true; - mGameThread = new Thread(this); - mGameThread.start(); - } - - private void printDebuggingText(){ - int debugSize = 35; - int debugStart = 150; - mPaint.setTextSize(debugSize); - - mCanvas.drawText("FPS: " + mFPS , 10, debugStart + debugSize, mPaint); - - } -} diff --git a/Chapter12/DynamicArrayExample/MainActivity.java b/Chapter12/DynamicArrayExample/MainActivity.java deleted file mode 100644 index adeb3f2..0000000 --- a/Chapter12/DynamicArrayExample/MainActivity.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.packtpub.dynamicarrayexample.dynamicarrayexample; - -import android.app.Activity; -import android.os.Bundle; -import android.util.Log; - - -public class MainActivity extends Activity { - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_main); - - //Declaring and allocating in one step - int[] ourArray = new int[1000]; - - //Let's initialize ourArray using a for loop - //Because more than a few variables is allot of typing! - for(int i = 0; i < 1000; i++){ - //Put the value of ourValue into our array - //At the position determined by i. - ourArray[i] = i*5; - - //Output what is going on - Log.i("info", "i = " + i); - Log.i("info", "ourArray[i] = " + ourArray[i]); - } - } - -} diff --git a/Chapter12/MultidimensionalArrayExample/MainActivity.java b/Chapter12/MultidimensionalArrayExample/MainActivity.java deleted file mode 100644 index adeb3f2..0000000 --- a/Chapter12/MultidimensionalArrayExample/MainActivity.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.packtpub.dynamicarrayexample.dynamicarrayexample; - -import android.app.Activity; -import android.os.Bundle; -import android.util.Log; - - -public class MainActivity extends Activity { - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_main); - - //Declaring and allocating in one step - int[] ourArray = new int[1000]; - - //Let's initialize ourArray using a for loop - //Because more than a few variables is allot of typing! - for(int i = 0; i < 1000; i++){ - //Put the value of ourValue into our array - //At the position determined by i. - ourArray[i] = i*5; - - //Output what is going on - Log.i("info", "i = " + i); - Log.i("info", "ourArray[i] = " + ourArray[i]); - } - } - -} diff --git a/Chapter12/SimpleArrayExample/MainActivity.java b/Chapter12/SimpleArrayExample/MainActivity.java deleted file mode 100644 index e9e24a4..0000000 --- a/Chapter12/SimpleArrayExample/MainActivity.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.packtpub.simplearrayexample.simplearrayexample; - -import android.app.Activity; -import android.os.Bundle; -import android.util.Log; - - -public class MainActivity extends Activity { - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_main); - - //Declaring an array - int[] ourArray; - - //Allocate memory for a maximum size of 5 elements - ourArray = new int[5]; - - //Initialize ourArray with values - //The values are arbitrary as long as they are int - //The indexes are not arbitrary 0 through 4 or crash! - ourArray[0] = 25; - ourArray[1] = 50; - ourArray[2] = 125; - ourArray[3] = 68; - ourArray[4] = 47; - - //Output all the stored values - Log.i("info", "Here is ourArray:"); - Log.i("info", "[0] = "+ourArray[0]); - Log.i("info", "[1] = "+ourArray[1]); - Log.i("info", "[2] = "+ourArray[2]); - Log.i("info", "[3] = "+ourArray[3]); - Log.i("info", "[4] = "+ourArray[4]); - - //We can do any calculation with an array element - //As long as it is appropriate to the contained type - //Like this: - int answer = ourArray[0] + - ourArray[1] + - ourArray[2] + - ourArray[3] + - ourArray[4]; - - Log.i("info", "Answer = "+answer); - } - - - -} diff --git a/Chapter13/AndroidManifest.xml b/Chapter13/AndroidManifest.xml deleted file mode 100644 index 8893921..0000000 --- a/Chapter13/AndroidManifest.xml +++ /dev/null @@ -1,24 +0,0 @@ - - - - - - - - - - - - - - \ No newline at end of file diff --git a/Chapter13/assets/beep.ogg b/Chapter13/assets/beep.ogg deleted file mode 100644 index 16e0e30..0000000 Binary files a/Chapter13/assets/beep.ogg and /dev/null differ diff --git a/Chapter13/assets/teleport.ogg b/Chapter13/assets/teleport.ogg deleted file mode 100644 index fb2eb90..0000000 Binary files a/Chapter13/assets/teleport.ogg and /dev/null differ diff --git a/Chapter13/drawable/bob.png b/Chapter13/drawable/bob.png deleted file mode 100644 index 251d305..0000000 Binary files a/Chapter13/drawable/bob.png and /dev/null differ diff --git a/Chapter13/java/Bob.java b/Chapter13/java/Bob.java deleted file mode 100644 index 2237b62..0000000 --- a/Chapter13/java/Bob.java +++ /dev/null @@ -1,73 +0,0 @@ -package com.gamecodeschool.c13bullethell; - -import android.content.Context; -import android.graphics.Bitmap; -import android.graphics.BitmapFactory; -import android.graphics.RectF; - -class Bob { - - RectF mRect; - float mBobHeight; - float mBobWidth; - boolean mTeleporting = false; - - Bitmap bitmap; - - public Bob(Context context, float screenX, float screenY){ - mBobHeight = screenY / 10; - mBobWidth = mBobHeight / 2; - - mRect = new RectF(screenX / 2, - screenY / 2, - (screenX / 2) + mBobWidth, - (screenY / 2) + mBobHeight); - - // Prepare the bitmap - // Load Bob from his .png file - // Bob practices responsible encapsulato - // looking after his own resources - bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.bob); - } - - boolean teleport(float newX, float newY){ - - // Did Bob manage to teleport? - boolean success = false; - - // Move Bob to the new position - // If not already teleporting - if(!mTeleporting){ - - // Make him roughly central to the touch - mRect.left = newX - mBobWidth / 2; - mRect.top = newY - mBobHeight / 2; - mRect.bottom = mRect.top + mBobHeight; - mRect.right = mRect.left + mBobWidth; - - mTeleporting = true; - - // Notify BulletHellGame that teleport - // attempt was successful - success = true; - } - - return success; - } - - void setTelePortAvailable(){ - mTeleporting = false; - } - - // Return a reference to mRect - RectF getRect(){ - return mRect; - } - - // Return a reference to bitmap - Bitmap getBitmap(){ - - return bitmap; - } - -} \ No newline at end of file diff --git a/Chapter13/java/Bullet.java b/Chapter13/java/Bullet.java deleted file mode 100644 index 100f198..0000000 --- a/Chapter13/java/Bullet.java +++ /dev/null @@ -1,71 +0,0 @@ -package com.gamecodeschool.c13bullethell; - -import android.graphics.RectF; - -class Bullet { - - // A RectF to represent the size and location of the bullet - private RectF mRect; - - // How fast is the bullet travelling? - private float mXVelocity; - private float mYVelocity; - - // How big is a bullet - private float mWidth; - private float mHeight; - - // The constructor - Bullet(int screenX){ - - // Configure the bullet based on - // the screen width in pixels - mWidth = screenX / 100; - mHeight = screenX / 100; - mRect = new RectF(); - mYVelocity = (screenX / 5); - mXVelocity = (screenX / 5); - } - - // Return a reference to the RectF - RectF getRect(){ - return mRect; - } - - - // Move the bullet based on the speed and the frame rate - void update(long fps){ - mRect.left = mRect.left + (mXVelocity / fps); - mRect.top = mRect.top + (mYVelocity / fps); - mRect.right = mRect.left + mWidth; - mRect.bottom = mRect.top - mHeight; - } - - // Reverse the bullets vertical direction - void reverseYVelocity(){ - mYVelocity = -mYVelocity; - } - - // Reverse the bullets horizontal direction - void reverseXVelocity(){ - mXVelocity = -mXVelocity; - } - - // Spawn a new bullet - void spawn(int pX, int pY, int vX, int vY){ - - // Spawn the bullet at the location - // passed in as parameters - mRect.left = pX; - mRect.top = pY; - mRect.right = pX + mWidth; - mRect.bottom = pY + mHeight; - - // Head away from the player - // It's only fair - mXVelocity = mXVelocity * vX; - mYVelocity = mYVelocity * vY; - - - } -} \ No newline at end of file diff --git a/Chapter13/java/BulletHellActivity.java b/Chapter13/java/BulletHellActivity.java deleted file mode 100644 index 2f66765..0000000 --- a/Chapter13/java/BulletHellActivity.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.gamecodeschool.c13bullethell; - -import android.app.Activity; -import android.graphics.Point; -import android.os.Bundle; -import android.view.Display; - - -// This class is almost exactly the same as the Pong project -public class BulletHellActivity extends Activity { - - // An instance of the main class of this project - private BulletHellGame mBHGame; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - // Get the screen resolution - Display display = getWindowManager().getDefaultDisplay(); - Point size = new Point(); - display.getSize(size); - - // Call the constructor(initialize) the BulletHellGame instance - mBHGame = new BulletHellGame(this, size.x, size.y); - setContentView(mBHGame); - } - - @Override - // Start the main game thread when the game is launched - protected void onResume() { - super.onResume(); - - mBHGame.resume(); - } - - @Override - // Stop the thread when the player quits - protected void onPause() { - super.onPause(); - - mBHGame.pause(); - } -} diff --git a/Chapter13/java/BulletHellGame.java b/Chapter13/java/BulletHellGame.java deleted file mode 100644 index f25cf9a..0000000 --- a/Chapter13/java/BulletHellGame.java +++ /dev/null @@ -1,381 +0,0 @@ -package com.gamecodeschool.c13bullethell; - - -import android.content.Context; -import android.content.res.AssetFileDescriptor; -import android.content.res.AssetManager; -import android.graphics.Canvas; -import android.graphics.Color; -import android.graphics.Paint; -import android.graphics.RectF; -import android.media.AudioAttributes; -import android.media.AudioManager; -import android.media.SoundPool; -import android.os.Build; -import android.util.Log; -import android.view.MotionEvent; -import android.view.SurfaceHolder; -import android.view.SurfaceView; -import java.io.IOException; -import java.util.Random; - -class BulletHellGame extends SurfaceView implements Runnable{ - - // Are we currently debugging - boolean mDebugging = true; - - // Objects for the game loop/thread - private Thread mGameThread = null; - private volatile boolean mPlaying; - private boolean mPaused = true; - - // Objects for drawing - private SurfaceHolder mOurHolder; - private Canvas mCanvas; - private Paint mPaint; - - // Keep track of the frame rate - private long mFPS; - // The number of milliseconds in a second - private final int MILLIS_IN_SECOND = 1000; - - // Holds the resolution of the screen - private int mScreenX; - private int mScreenY; - - // How big will the text be? - private int mFontSize; - private int mFontMargin; - - // These are for the sound - private SoundPool mSP; - private int mBeepID = -1; - private int mTeleportID = -1; - - // Up to 10000 bullets - private Bullet[] mBullets = new Bullet[10000]; - private int mNumBullets = 0; - private int mSpawnRate = 1; - - private Random mRandomX = new Random(); - private Random mRandomY = new Random(); - - private Bob mBob; - private boolean mHit = false; - private int mNumHits; - private int mShield = 10; - - // Let's time the game - private long mStartGameTime; - private long mBestGameTime; - private long mTotalGameTime; - - // This is the constructor method that gets called - // from BullethellActivity - public BulletHellGame(Context context, int x, int y) { - super(context); - - mScreenX = x; - mScreenY = y; - // Font is 5% of screen width - mFontSize = mScreenX / 20; - // Margin is 2% of screen width - mFontMargin = mScreenX / 50; - - mOurHolder = getHolder(); - mPaint = new Paint(); - - - // 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; - - descriptor = assetManager.openFd("beep.ogg"); - mBeepID = mSP.load(descriptor, 0); - - descriptor = assetManager.openFd("teleport.ogg"); - mTeleportID = mSP.load(descriptor, 0); - - }catch(IOException e){ - Log.e("error", "failed to load sound files"); - } - - for(int i = 0; i < mBullets.length; i++){ - mBullets[i] = new Bullet(mScreenX); - } - - mBob = new Bob(context, mScreenX, mScreenY); - - startGame(); - } - - - // Called to start a new game - public void startGame(){ - mNumHits = 0; - mNumBullets = 0; - mHit = false; - - // Did the player survive longer than previously - if(mTotalGameTime > mBestGameTime){ - mBestGameTime = mTotalGameTime; - } - - } - - // Spawns ANOTHER bullet - private void spawnBullet(){ - // Add one to the number of bullets - mNumBullets++; - - // Where to spawn the next bullet - // And in which direction should it travel - int spawnX; - int spawnY; - int velocityX; - int velocityY; - - // This code will change in chapter 13 - // Don't spawn to close to Bob - if (mBob.getRect().centerX() < mScreenX / 2) { - // Bob is on the left - // Spawn bullet on the right - spawnX = mRandomX.nextInt - (mScreenX / 2) + mScreenX / 2; - // Head right - velocityX = 1; - } else { - // Bob is on the right - // Spawn bullet on the left - spawnX = mRandomX.nextInt - (mScreenX / 2); - // Head left - velocityX = -1; - } - - // Don't spawn to close to Bob - if (mBob.getRect().centerY() < mScreenY / 2) { - // Bob is on the top - // Spawn bullet on the bottom - spawnY = mRandomY.nextInt - (mScreenY / 2) + mScreenY / 2; - // Head down - velocityY = 1; - } else { - // Bob is on the bottom - // Spawn bullet on the top - spawnY = mRandomY.nextInt - (mScreenY / 2); - // head up - velocityY = -1; - } - - // Spawn the bullet - mBullets[mNumBullets - 1] - .spawn(spawnX, spawnY, velocityX, velocityY); - } - - // Handles the game loop - @Override - public void run() { - while (mPlaying) { - - long frameStartTime = System.currentTimeMillis(); - - if(!mPaused){ - update(); - // Now all the bullets have been moved - // we can detect any collisions - detectCollisions(); - - } - - draw(); - - long timeThisFrame = System.currentTimeMillis() - frameStartTime; - if (timeThisFrame >= 1) { - mFPS = MILLIS_IN_SECOND / timeThisFrame; - } - - } - } - - - // Update all the game objects - private void update(){ - for(int i = 0; i < mNumBullets; i++){ - mBullets[i].update(mFPS); - } - } - - private void detectCollisions(){ - // Has a bullet collided with a wall? - // Loop through each active bullet in turn - for(int i = 0; i < mNumBullets; i++) { - if (mBullets[i].getRect().bottom > mScreenY) { - mBullets[i].reverseYVelocity(); - } - - if (mBullets[i].getRect().top < 0) { - mBullets[i].reverseYVelocity(); - } - - if (mBullets[i].getRect().left < 0) { - mBullets[i].reverseXVelocity(); - } - - if (mBullets[i].getRect().right > mScreenX) { - mBullets[i].reverseXVelocity(); - } - - } - - // Has a bullet hit Bob? - // Check each bullet for an intersection with Bob's RectF - for (int i = 0; i < mNumBullets; i++) { - - if (RectF.intersects(mBullets[i].getRect(), mBob.getRect())) { - // Bob has been hit - mSP.play(mBeepID, 1, 1, 0, 0, 1); - - // This flags that a hit occured so that the draw - // method "knows" as well - mHit = true; - - // Rebound the bullet that collided - mBullets[i].reverseXVelocity(); - mBullets[i].reverseYVelocity(); - - // keep track of the number of hits - mNumHits++; - - if(mNumHits == mShield) { - mPaused = true; - mTotalGameTime = System.currentTimeMillis() - mStartGameTime; - startGame(); - } - } - - } - } - - private void draw(){ - if (mOurHolder.getSurface().isValid()) { - mCanvas = mOurHolder.lockCanvas(); - mCanvas.drawColor(Color.argb(255, 243, 111, 36)); - mPaint.setColor(Color.argb(255, 255, 255, 255)); - - // All the drawing code will go here - for(int i = 0; i < mNumBullets; i++){ - mCanvas.drawRect(mBullets[i].getRect(), mPaint); - } - - mCanvas.drawBitmap(mBob.getBitmap(), - mBob.getRect().left, mBob.getRect().top, mPaint); - - - mPaint.setTextSize(mFontSize); - mCanvas.drawText("Bullets: " + mNumBullets + - " Shield: " + (mShield - mNumHits) + - " Best Time: " + mBestGameTime / - MILLIS_IN_SECOND, - mFontMargin, mFontSize, mPaint); - - - // Don't draw the current time when paused - if(!mPaused) { - mCanvas.drawText("Seconds Survived: " + - ((System.currentTimeMillis() - - mStartGameTime) / MILLIS_IN_SECOND), - mFontMargin, mFontMargin * 30, mPaint); - } - - - if(mDebugging) { - printDebuggingText(); - } - - mOurHolder.unlockCanvasAndPost(mCanvas); - } - } - - @Override - public boolean onTouchEvent(MotionEvent motionEvent) { - - switch (motionEvent.getAction() & - MotionEvent.ACTION_MASK) { - - case MotionEvent.ACTION_DOWN: - - if(mPaused){ - mStartGameTime = System.currentTimeMillis(); - mPaused = false; - } - - if(mBob.teleport(motionEvent.getX(), motionEvent.getY())){ - mSP.play(mTeleportID, 1, 1, 0, 0, 1); - } - break; - - case MotionEvent.ACTION_UP: - - mBob.setTelePortAvailable(); - spawnBullet(); - break; - } - return true; - } - - public void pause() { - mPlaying = false; - try { - mGameThread.join(); - } catch (InterruptedException e) { - Log.e("Error:", "joining thread"); - } - } - - public void resume() { - mPlaying = true; - mGameThread = new Thread(this); - mGameThread.start(); - } - - private void printDebuggingText(){ - int debugSize = 35; - int debugStart = 150; - mPaint.setTextSize(debugSize); - - mCanvas.drawText("FPS: " + mFPS , 10, - debugStart + debugSize, mPaint); - mCanvas.drawText("Bob left: " + mBob.getRect().left , 10, - debugStart + debugSize *2, mPaint); - mCanvas.drawText("Bob top: " + mBob.getRect().top , 10, - debugStart + debugSize *3, mPaint); - mCanvas.drawText("Bob right: " + mBob.getRect().right , 10, - debugStart + debugSize *4, mPaint); - mCanvas.drawText("Bob bottom: " + mBob.getRect().bottom , 10, - debugStart + debugSize *5, mPaint); - mCanvas.drawText("Bob centerX: " + mBob.getRect().centerX() , 10, - debugStart + debugSize *6, mPaint); - mCanvas.drawText("Bob centerY: " + mBob.getRect().centerY() , 10, - debugStart + debugSize *7, mPaint); - - } -} diff --git a/Chapter14/AndroidManifest.xml b/Chapter14/AndroidManifest.xml deleted file mode 100644 index 30febdb..0000000 --- a/Chapter14/AndroidManifest.xml +++ /dev/null @@ -1,24 +0,0 @@ - - - - - - - - - - - - - - \ No newline at end of file diff --git a/Chapter14/assets/get_apple.ogg b/Chapter14/assets/get_apple.ogg deleted file mode 100644 index fcea634..0000000 Binary files a/Chapter14/assets/get_apple.ogg and /dev/null differ diff --git a/Chapter14/assets/snake_death.ogg b/Chapter14/assets/snake_death.ogg deleted file mode 100644 index a4ab72b..0000000 Binary files a/Chapter14/assets/snake_death.ogg and /dev/null differ diff --git a/Chapter14/java/Apple.java b/Chapter14/java/Apple.java deleted file mode 100644 index d044113..0000000 --- a/Chapter14/java/Apple.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.gamecodeschool.c14snake; - -/** - * Created by johnh on 22/12/2017. - */ - -class Apple { -} diff --git a/Chapter14/java/Snake.java b/Chapter14/java/Snake.java deleted file mode 100644 index 67fd55f..0000000 --- a/Chapter14/java/Snake.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.gamecodeschool.c14snake; - -/** - * Created by johnh on 22/12/2017. - */ - -class Snake { -} diff --git a/Chapter14/java/SnakeActivity.java b/Chapter14/java/SnakeActivity.java deleted file mode 100644 index 4ab3367..0000000 --- a/Chapter14/java/SnakeActivity.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.gamecodeschool.c14snake; - -import android.app.Activity; -import android.graphics.Point; -import android.os.Bundle; -import android.view.Display; - -public class SnakeActivity extends Activity { - - // Declare an instance of SnakeGame - SnakeGame mSnakeGame; - - // Set the game up - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - // Get the pixel dimensions of the screen - Display display = getWindowManager().getDefaultDisplay(); - - // Initialize the result into a Point object - Point size = new Point(); - display.getSize(size); - - // Create a new instance of the SnakeEngine class - mSnakeGame = new SnakeGame(this, size); - - // Make snakeEngine the view of the Activity - setContentView(mSnakeGame); - } - - // Start the thread in snakeEngine - @Override - protected void onResume() { - super.onResume(); - mSnakeGame.resume(); - } - - // Stop the thread in snakeEngine - @Override - protected void onPause() { - super.onPause(); - mSnakeGame.pause(); - } -} \ No newline at end of file diff --git a/Chapter14/java/SnakeGame.java b/Chapter14/java/SnakeGame.java deleted file mode 100644 index 11c7e46..0000000 --- a/Chapter14/java/SnakeGame.java +++ /dev/null @@ -1,251 +0,0 @@ -package com.gamecodeschool.c14snake; - -import android.content.Context; -import android.content.res.AssetFileDescriptor; -import android.content.res.AssetManager; -import android.graphics.Canvas; -import android.graphics.Color; -import android.graphics.Paint; -import android.graphics.Point; -import android.media.AudioAttributes; -import android.media.AudioManager; -import android.media.SoundPool; -import android.os.Build; -import android.view.MotionEvent; -import android.view.SurfaceHolder; -import android.view.SurfaceView; -import java.io.IOException; - -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 - - } - - - // Called to start a new game - public void newGame() { - - // reset the snake - - - // Get the apple ready for dinner - - - // 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 - - // Did the head of the snake eat the apple? - - // Did the snake die? - - } - - - // Do all the drawing - public void draw() { - // Get a lock on the mCanvas - if (mSurfaceHolder.getSurface().isValid()) { - mCanvas = mSurfaceHolder.lockCanvas(); - - // Fill the screen with a color - mCanvas.drawColor(Color.argb(255, 26, 128, 182)); - - // Set the size and color of the mPaint for the text - mPaint.setColor(Color.argb(255, 255, 255, 255)); - mPaint.setTextSize(120); - - // Draw the score - mCanvas.drawText("" + mScore, 20, 120, mPaint); - - // Draw the apple and the snake - - // Draw some text while paused - if(mPaused){ - - // Set the size and color of the mPaint for the text - mPaint.setColor(Color.argb(255, 255, 255, 255)); - mPaint.setTextSize(250); - - // Draw the message - // We will give this an international upgrade soon - mCanvas.drawText("Tap To Play!", 200, 700, mPaint); - } - - - // Unlock the mCanvas and reveal the graphics for this frame - mSurfaceHolder.unlockCanvasAndPost(mCanvas); - } - } - - - - - - @Override - public boolean onTouchEvent(MotionEvent motionEvent) { - switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) { - case MotionEvent.ACTION_UP: - if (mPaused) { - mPaused = false; - newGame(); - - // Don't want to process snake direction for this tap - return true; - } - - // Let the Snake class handle the input - - break; - - default: - break; - - } - return true; - } - - - // Stop the thread - public void pause() { - mPlaying = false; - try { - mThread.join(); - } catch (InterruptedException e) { - // Error - } - } - - - // Start the thread - public void resume() { - mPlaying = true; - mThread = new Thread(this); - mThread.start(); - } -} \ No newline at end of file diff --git a/Chapter15/AndroidManifest.xml b/Chapter15/AndroidManifest.xml deleted file mode 100644 index 1035cdb..0000000 --- a/Chapter15/AndroidManifest.xml +++ /dev/null @@ -1,24 +0,0 @@ - - - - - - - - - - - - - - \ No newline at end of file diff --git a/Chapter15/assets/get_apple.ogg b/Chapter15/assets/get_apple.ogg deleted file mode 100644 index fcea634..0000000 Binary files a/Chapter15/assets/get_apple.ogg and /dev/null differ diff --git a/Chapter15/assets/snake_death.ogg b/Chapter15/assets/snake_death.ogg deleted file mode 100644 index a4ab72b..0000000 Binary files a/Chapter15/assets/snake_death.ogg and /dev/null differ diff --git a/Chapter15/java/Apple.java b/Chapter15/java/Apple.java deleted file mode 100644 index 9b8e0a1..0000000 --- a/Chapter15/java/Apple.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.gamecodeschool.c15snake; - -/** - * Created by johnh on 22/12/2017. - */ - -class Apple { -} diff --git a/Chapter15/java/Snake.java b/Chapter15/java/Snake.java deleted file mode 100644 index 3b9c9c5..0000000 --- a/Chapter15/java/Snake.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.gamecodeschool.c15snake; - -/** - * Created by johnh on 22/12/2017. - */ - -class Snake { -} diff --git a/Chapter15/java/SnakeActivity.java b/Chapter15/java/SnakeActivity.java deleted file mode 100644 index 156daca..0000000 --- a/Chapter15/java/SnakeActivity.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.gamecodeschool.c15snake; - -import android.app.Activity; -import android.graphics.Point; -import android.os.Bundle; -import android.view.Display; - -public class SnakeActivity extends Activity { - - // Declare an instance of SnakeGame - SnakeGame mSnakeGame; - - // Set the game up - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - // Get the pixel dimensions of the screen - Display display = getWindowManager().getDefaultDisplay(); - - // Initialize the result into a Point object - Point size = new Point(); - display.getSize(size); - - // Create a new instance of the SnakeEngine class - mSnakeGame = new SnakeGame(this, size); - - // Make snakeEngine the view of the Activity - setContentView(mSnakeGame); - } - - // Start the thread in snakeEngine - @Override - protected void onResume() { - super.onResume(); - mSnakeGame.resume(); - } - - // Stop the thread in snakeEngine - @Override - protected void onPause() { - super.onPause(); - mSnakeGame.pause(); - } -} \ No newline at end of file diff --git a/Chapter15/java/SnakeGame.java b/Chapter15/java/SnakeGame.java deleted file mode 100644 index f2d7a49..0000000 --- a/Chapter15/java/SnakeGame.java +++ /dev/null @@ -1,254 +0,0 @@ -package com.gamecodeschool.c15snake; - -import android.content.Context; -import android.content.res.AssetFileDescriptor; -import android.content.res.AssetManager; -import android.graphics.Canvas; -import android.graphics.Color; -import android.graphics.Paint; -import android.graphics.Point; -import android.media.AudioAttributes; -import android.media.AudioManager; -import android.media.SoundPool; -import android.os.Build; -import android.view.MotionEvent; -import android.view.SurfaceHolder; -import android.view.SurfaceView; -import java.io.IOException; - -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 - - } - - - // Called to start a new game - public void newGame() { - - // reset the snake - - - // Get the apple ready for dinner - - - // 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 - - // Did the head of the snake eat the apple? - - // Did the snake die? - - } - - - // Do all the drawing - public void draw() { - // Get a lock on the mCanvas - if (mSurfaceHolder.getSurface().isValid()) { - mCanvas = mSurfaceHolder.lockCanvas(); - - // Fill the screen with a color - mCanvas.drawColor(Color.argb(255, 26, 128, 182)); - - // Set the size and color of the mPaint for the text - mPaint.setColor(Color.argb(255, 255, 255, 255)); - mPaint.setTextSize(120); - - // Draw the score - mCanvas.drawText("" + mScore, 20, 120, mPaint); - - // Draw the apple and the snake - - // Draw some text while paused - if(mPaused){ - - // Set the size and color of the mPaint for the text - mPaint.setColor(Color.argb(255, 255, 255, 255)); - mPaint.setTextSize(120); - - // Draw the message - // We will give this an international upgrade soon - //mCanvas.drawText("Tap To Play!", 200, 700, mPaint); - mCanvas.drawText(getResources(). - getString(R.string.tap_to_play), - 200, 700, mPaint); - } - - - // Unlock the mCanvas and reveal the graphics for this frame - mSurfaceHolder.unlockCanvasAndPost(mCanvas); - } - } - - - - - - @Override - public boolean onTouchEvent(MotionEvent motionEvent) { - switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) { - case MotionEvent.ACTION_UP: - if (mPaused) { - mPaused = false; - newGame(); - - // Don't want to process snake direction for this tap - return true; - } - - // Let the Snake class handle the input - - break; - - default: - break; - - } - return true; - } - - - // Stop the thread - public void pause() { - mPlaying = false; - try { - mThread.join(); - } catch (InterruptedException e) { - // Error - } - } - - - // Start the thread - public void resume() { - mPlaying = true; - mThread = new Thread(this); - mThread.start(); - } -} diff --git a/Chapter15/values-de/strings.xml b/Chapter15/values-de/strings.xml deleted file mode 100644 index 58d4bd8..0000000 --- a/Chapter15/values-de/strings.xml +++ /dev/null @@ -1,4 +0,0 @@ - - - Tippen Sie, um zu spielen! - \ No newline at end of file diff --git a/Chapter15/values-es/strings.xml b/Chapter15/values-es/strings.xml deleted file mode 100644 index 47da145..0000000 --- a/Chapter15/values-es/strings.xml +++ /dev/null @@ -1,4 +0,0 @@ - - - Toque para jugar! - \ No newline at end of file diff --git a/Chapter15/values/colors.xml b/Chapter15/values/colors.xml deleted file mode 100644 index 3ab3e9c..0000000 --- a/Chapter15/values/colors.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - #3F51B5 - #303F9F - #FF4081 - diff --git a/Chapter15/values/strings.xml b/Chapter15/values/strings.xml deleted file mode 100644 index 1b00486..0000000 --- a/Chapter15/values/strings.xml +++ /dev/null @@ -1,4 +0,0 @@ - - C15Snake - Tap To Play! - diff --git a/Chapter15/values/styles.xml b/Chapter15/values/styles.xml deleted file mode 100644 index ff6c9d2..0000000 --- a/Chapter15/values/styles.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - diff --git a/Chapter16/AndroidManifest.xml b/Chapter16/AndroidManifest.xml deleted file mode 100644 index 0815cfb..0000000 --- a/Chapter16/AndroidManifest.xml +++ /dev/null @@ -1,24 +0,0 @@ - - - - - - - - - - - - - - \ No newline at end of file diff --git a/Chapter16/assets/get_apple.ogg b/Chapter16/assets/get_apple.ogg deleted file mode 100644 index fcea634..0000000 Binary files a/Chapter16/assets/get_apple.ogg and /dev/null differ diff --git a/Chapter16/assets/snake_death.ogg b/Chapter16/assets/snake_death.ogg deleted file mode 100644 index a4ab72b..0000000 Binary files a/Chapter16/assets/snake_death.ogg and /dev/null differ diff --git a/Chapter16/java/Apple.java b/Chapter16/java/Apple.java deleted file mode 100644 index b0197ef..0000000 --- a/Chapter16/java/Apple.java +++ /dev/null @@ -1,64 +0,0 @@ -package com.gamecodeschool.c16snake; - -import android.content.Context; -import android.graphics.Bitmap; -import android.graphics.BitmapFactory; -import android.graphics.Canvas; -import android.graphics.Paint; -import android.graphics.Point; -import java.util.Random; - -class Apple { - - // The location of the apple on the grid - // Not in pixels - private Point location = new Point(); - - // The range of values we can choose from - // to spawn an apple - private Point mSpawnRange; - private int mSize; - - // An image to represent the apple - private Bitmap mBitmapApple; - - /// Set up the apple in the constructor - Apple(Context context, Point sr, int s){ - - // Make a note of the passed in spawn range - mSpawnRange = sr; - // Make a note of the size of an apple - mSize = s; - // Hide the apple off-screen until the game starts - location.x = -10; - - // Load the image to the bitmap - mBitmapApple = BitmapFactory.decodeResource(context.getResources(), R.drawable.apple); - - // Resize the bitmap - mBitmapApple = Bitmap.createScaledBitmap(mBitmapApple, s, s, false); - } - - // This is called every time an apple is eaten - void spawn(){ - // Choose two random values and place the apple - Random random = new Random(); - location.x = random.nextInt(mSpawnRange.x) + 1; - location.y = random.nextInt(mSpawnRange.y - 1) + 1; - } - - // Let SnakeGame know where the apple is - // SnakeGame can share this with the snake - Point getLocation(){ - return location; - } - - // Draw the apple - void draw(Canvas canvas, Paint paint){ - canvas.drawBitmap(mBitmapApple, - location.x * mSize, location.y * mSize, paint); - - } - -} - diff --git a/Chapter16/java/Snake.java b/Chapter16/java/Snake.java deleted file mode 100644 index 4beabf5..0000000 --- a/Chapter16/java/Snake.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.gamecodeschool.c16snake; - -/** - * Created by johnh on 22/12/2017. - */ - -class Snake { -} diff --git a/Chapter16/java/SnakeActivity.java b/Chapter16/java/SnakeActivity.java deleted file mode 100644 index 7fc1134..0000000 --- a/Chapter16/java/SnakeActivity.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.gamecodeschool.c16snake; - -import android.app.Activity; -import android.graphics.Point; -import android.os.Bundle; -import android.view.Display; - -public class SnakeActivity extends Activity { - - // Declare an instance of SnakeGame - SnakeGame mSnakeGame; - - // Set the game up - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - // Get the pixel dimensions of the screen - Display display = getWindowManager().getDefaultDisplay(); - - // Initialize the result into a Point object - Point size = new Point(); - display.getSize(size); - - // Create a new instance of the SnakeEngine class - mSnakeGame = new SnakeGame(this, size); - - // Make snakeEngine the view of the Activity - setContentView(mSnakeGame); - } - - // Start the thread in snakeEngine - @Override - protected void onResume() { - super.onResume(); - mSnakeGame.resume(); - } - - // Stop the thread in snakeEngine - @Override - protected void onPause() { - super.onPause(); - mSnakeGame.pause(); - } -} diff --git a/Chapter16/java/SnakeGame.java b/Chapter16/java/SnakeGame.java deleted file mode 100644 index 33dc159..0000000 --- a/Chapter16/java/SnakeGame.java +++ /dev/null @@ -1,259 +0,0 @@ -package com.gamecodeschool.c16snake; - -import android.content.Context; -import android.content.res.AssetFileDescriptor; -import android.content.res.AssetManager; -import android.graphics.Canvas; -import android.graphics.Color; -import android.graphics.Paint; -import android.graphics.Point; -import android.media.AudioAttributes; -import android.media.AudioManager; -import android.media.SoundPool; -import android.os.Build; -import android.view.MotionEvent; -import android.view.SurfaceHolder; -import android.view.SurfaceView; -import java.io.IOException; - -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); - - } - - - // Called to start a new game - public void newGame() { - - // reset the snake - - - // 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 - - // Did the head of the snake eat the apple? - - // Did the snake die? - - } - - - // Do all the drawing - public void draw() { - // Get a lock on the mCanvas - if (mSurfaceHolder.getSurface().isValid()) { - mCanvas = mSurfaceHolder.lockCanvas(); - - // Fill the screen with a color - mCanvas.drawColor(Color.argb(255, 26, 128, 182)); - - // Set the size and color of the mPaint for the text - mPaint.setColor(Color.argb(255, 255, 255, 255)); - mPaint.setTextSize(120); - - // Draw the score - mCanvas.drawText("" + mScore, 20, 120, mPaint); - - // Draw the apple and the snake - mApple.draw(mCanvas, mPaint); - - // Draw some text while paused - if(mPaused){ - - // Set the size and color of the mPaint for the text - mPaint.setColor(Color.argb(255, 255, 255, 255)); - mPaint.setTextSize(250); - - // Draw the message - // We will give this an international upgrade soon - //mCanvas.drawText("Tap To Play!", 200, 700, mPaint); - mCanvas.drawText(getResources(). - getString(R.string.tap_to_play), - 200, 700, mPaint); - } - - - // Unlock the mCanvas and reveal the graphics for this frame - mSurfaceHolder.unlockCanvasAndPost(mCanvas); - } - } - - - - - - @Override - public boolean onTouchEvent(MotionEvent motionEvent) { - switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) { - case MotionEvent.ACTION_UP: - if (mPaused) { - mPaused = false; - newGame(); - - // Don't want to process snake direction for this tap - return true; - } - - // Let the Snake class handle the input - - break; - - default: - break; - - } - return true; - } - - - // Stop the thread - public void pause() { - mPlaying = false; - try { - mThread.join(); - } catch (InterruptedException e) { - // Error - } - } - - - // Start the thread - public void resume() { - mPlaying = true; - mThread = new Thread(this); - mThread.start(); - } -} \ No newline at end of file diff --git a/Chapter17/AndroidManifest.xml b/Chapter17/AndroidManifest.xml index 408d7ef..485270a 100644 --- a/Chapter17/AndroidManifest.xml +++ b/Chapter17/AndroidManifest.xml @@ -1,23 +1,29 @@ + xmlns:tools="http://schemas.android.com/tools"> - + + + android:exported="true"> - + + diff --git a/Chapter17/assets/gameOver.ogg b/Chapter17/assets/gameOver.ogg new file mode 100644 index 0000000..5ebd309 Binary files /dev/null and b/Chapter17/assets/gameOver.ogg differ diff --git a/Chapter17/drawable/badapple.png b/Chapter17/drawable/badapple.png new file mode 100644 index 0000000..688f879 Binary files /dev/null and b/Chapter17/drawable/badapple.png differ diff --git a/Chapter17/java/Apple.java b/Chapter17/java/Apple.java index 199f303..b1fb929 100644 --- a/Chapter17/java/Apple.java +++ b/Chapter17/java/Apple.java @@ -3,61 +3,57 @@ import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; -import android.graphics.Canvas; -import android.graphics.Paint; import android.graphics.Point; import java.util.Random; -class Apple { +public class Apple extends GameObject{ - // The location of the apple on the grid - // Not in pixels private Point location = new Point(); - - // The range of values we can choose from - // to spawn an apple - private Point mSpawnRange; - private int mSize; - - // An image to represent the apple private Bitmap mBitmapApple; - /// Set up the apple in the constructor - Apple(Context context, Point sr, int s){ - - // Make a note of the passed in spawn range - mSpawnRange = sr; - // Make a note of the size of an apple - mSize = s; - // Hide the apple off-screen until the game starts - location.x = -10; - - // Load the image to the bitmap - mBitmapApple = BitmapFactory.decodeResource(context.getResources(), R.drawable.apple); - - // Resize the bitmap - mBitmapApple = Bitmap.createScaledBitmap(mBitmapApple, s, s, false); + // Private constructor to enforce the use of the builder + private Apple(Point spawnRange, int size, Bitmap bitmapApple) { + super(spawnRange, size, bitmapApple); + location.x = -10; // Hide the apple off-screen until the game starts + mbitmapobject = bitmapApple; + mbitmapobject = Bitmap.createScaledBitmap(mbitmapobject, size, size, false); } // This is called every time an apple is eaten - void spawn(){ - // Choose two random values and place the apple + void spawn(Point spawnRange) { Random random = new Random(); - location.x = random.nextInt(mSpawnRange.x) + 1; - location.y = random.nextInt(mSpawnRange.y - 1) + 1; + location.x = random.nextInt(spawnRange.x) + 1; + location.y = random.nextInt(spawnRange.y - 1) + 1; } // Let SnakeGame know where the apple is // SnakeGame can share this with the snake - Point getLocation(){ + Point getLocation() { return location; } - - // Draw the apple - void draw(Canvas canvas, Paint paint){ - canvas.drawBitmap(mBitmapApple, - location.x * mSize, location.y * mSize, paint); - + // Builder class for Apple + public static class AppleBuilder { + private Point mSpawnRange; + private int mSize; + private Bitmap mBitmapApple; + + public AppleBuilder setSpawnRange(Point spawnRange) { + mSpawnRange = spawnRange; + return this; + } + + public AppleBuilder setSize(int size) { + mSize = size; + return this; + } + + public AppleBuilder setBitmap(Context context, int resourceId) { + mBitmapApple = BitmapFactory.decodeResource(context.getResources(), resourceId); + return this; + } + + public Apple build() { + return new Apple(mSpawnRange, mSize, mBitmapApple); + } } - } \ No newline at end of file diff --git a/Chapter17/java/AppleBuilder.java b/Chapter17/java/AppleBuilder.java new file mode 100644 index 0000000..02aab73 --- /dev/null +++ b/Chapter17/java/AppleBuilder.java @@ -0,0 +1,17 @@ +package com.gamecodeschool.c17snake; + +import android.content.Context; +import android.graphics.Bitmap; +import android.graphics.Canvas; +import android.graphics.Paint; +import android.graphics.Point; + +public interface AppleBuilder { + Point getLocation(); + public void spawn(Point spawnRange, boolean hide); + public void setSize(int size); +// public void draw(Canvas canvas, Paint paint); + public void setBitmap(Context context, int resourceId); +// public void build(); +}; + diff --git a/Chapter17/java/Audio.java b/Chapter17/java/Audio.java new file mode 100644 index 0000000..02a4e32 --- /dev/null +++ b/Chapter17/java/Audio.java @@ -0,0 +1,8 @@ +package com.gamecodeschoolc17.c17snake; + +// added the audio class for strategy implementation +public interface Audio { + void playEatSound(); + + void playCrashSound(); +} diff --git a/Chapter17/java/BadAppleBuilder.java b/Chapter17/java/BadAppleBuilder.java new file mode 100644 index 0000000..22a25ea --- /dev/null +++ b/Chapter17/java/BadAppleBuilder.java @@ -0,0 +1,67 @@ +package com.gamecodeschool.c17snake; + +import android.content.Context; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.graphics.Canvas; +import android.graphics.Paint; +import android.graphics.Point; + +import java.util.Random; + +public class BadAppleBuilder implements AppleBuilder { + private NewApple Apple; + + // constructor for BadAppleBuilder + public BadAppleBuilder(Bitmap mBitmapApple, Point location, Point mSpawnRange, int mSize){ + this.Apple = new NewApple(); + this.Apple.location = location; + this.Apple.mBitmapApple = mBitmapApple; + this.Apple.mSpawnRange = mSpawnRange; + this.Apple.mSize = mSize; + }; + + @Override + public Point getLocation() { + return this.Apple.location; + } + +// @Override +// public void draw(Canvas canvas, Paint paint) { +// canvas.drawBitmap(this.Apple.mBitmapApple, +// this.Apple.location.x * this.Apple.mBitmapApple.getWidth(), +// this.Apple.location.y * this.Apple.mBitmapApple.getHeight(), paint); +// } + + @Override + public void spawn(Point spawnRange, boolean hide) { + if (hide == true) { + this.Apple.location.x = -10; // Hide the apple off-screen until the game starts + } else { + this.Apple.mSpawnRange = spawnRange; + Random random = new Random(); + this.Apple.location.x = random.nextInt(this.Apple.mSpawnRange.x) + 1; + this.Apple.location.y = random.nextInt(this.Apple.mSpawnRange.y - 1) + 1; + } + } + + @Override + public void setSize(int size) { + this.Apple.mSize = size; + } + + @Override + public void setBitmap(Context context, int resourceId) { + this.Apple.mBitmapApple = BitmapFactory.decodeResource(context.getResources(), resourceId); + } + + public NewApple returnApple() { + return this.Apple; + } + +// @Override +// public NewApple build() { +// return Apple(Apple.mSpawnRange, Apple.mSize, Apple.mBitmapApple); +// } + +} \ No newline at end of file diff --git a/Chapter17/java/DefaultAudio.java b/Chapter17/java/DefaultAudio.java new file mode 100644 index 0000000..2d44cfb --- /dev/null +++ b/Chapter17/java/DefaultAudio.java @@ -0,0 +1,25 @@ +package com.gamecodeschoolc17.c17snake; +import android.media.SoundPool; + +// added the default audio class for strategy implementation +public class DefaultAudio implements Audio { + private SoundPool soundPool; + private int eatSoundId; + private int crashSoundId; + + public DefaultAudio(SoundPool soundPool, int eatSoundId, int crashSoundId) { + this.soundPool = soundPool; + this.eatSoundId = eatSoundId; + this.crashSoundId = crashSoundId; + } + + @Override + public void playEatSound() { + soundPool.play(eatSoundId, 1, 1, 0, 0, 1); + } + + @Override + public void playCrashSound() { + soundPool.play(crashSoundId, 1, 1, 0, 0, 1); + } +} diff --git a/Chapter17/java/GameObject.java b/Chapter17/java/GameObject.java new file mode 100644 index 0000000..7cc38b2 --- /dev/null +++ b/Chapter17/java/GameObject.java @@ -0,0 +1,47 @@ +package com.gamecodeschool.c17snake; + +import android.content.Context; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.graphics.Canvas; +import android.graphics.Paint; +import android.graphics.Point; + +public abstract class GameObject { + + + // The location of the object on the grid + // Not in pixels + protected Point location = new Point(); + + // The range of values we can choose from + // to spawn an object + protected Point mSpawnRange; + protected int mSize; + // How big is the entire grid + protected Point mRange; + + // An image to represent the object + protected Bitmap mbitmapobject; + GameObject(Point spawnRange, int size, Bitmap bitmapApple){ + + // Make a note of the passed in spawn range + mRange = spawnRange; + // Make a note of the size of an object + mSize = size; + // Hide the object off-screen until the game starts + location.x = -10; + + } + + void draw(Canvas canvas, Paint paint){ + canvas.drawBitmap(mbitmapobject, + location.x * mSize, location.y * mSize, paint); + + } + //let game know where the location of the object is + Point getLocation(){ + return location; + } + +} \ No newline at end of file diff --git a/Chapter17/java/GoodAppleBuilder.java b/Chapter17/java/GoodAppleBuilder.java new file mode 100644 index 0000000..a7ae723 --- /dev/null +++ b/Chapter17/java/GoodAppleBuilder.java @@ -0,0 +1,67 @@ +package com.gamecodeschool.c17snake; + +import android.content.Context; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.graphics.Canvas; +import android.graphics.Paint; +import android.graphics.Point; + +import java.util.Random; + +public class GoodAppleBuilder implements AppleBuilder { + private NewApple Apple; + + // constructor GoodAppleBuilder + public GoodAppleBuilder(Bitmap mBitmapApple, Point location, Point mSpawnRange, int mSize){ + this.Apple = new NewApple(); + this.Apple.location = location; + this.Apple.mBitmapApple = mBitmapApple; + this.Apple.mSpawnRange = mSpawnRange; + this.Apple.mSize = mSize; + }; + + @Override + public Point getLocation() { + return this.Apple.location; + } + +// @Override +// public void draw(Canvas canvas, Paint paint) { +// canvas.drawBitmap(this.Apple.mBitmapApple, +// this.Apple.location.x * this.Apple.mBitmapApple.getWidth(), +// this.Apple.location.y * this.Apple.mBitmapApple.getHeight(), paint); +// } + + @Override + public void spawn(Point spawnRange, boolean hide) { + if (hide == true) { + this.Apple.location.x = -10; // Hide the apple off-screen until the game starts + } else { + this.Apple.mSpawnRange = spawnRange; + Random random = new Random(); + this.Apple.location.x = random.nextInt(this.Apple.mSpawnRange.x) + 1; + this.Apple.location.y = random.nextInt(this.Apple.mSpawnRange.y - 1) + 1; + } + } + + @Override + public void setSize(int size) { + this.Apple.mSize = size; + } + + @Override + public void setBitmap(Context context, int resourceId) { + this.Apple.mBitmapApple = BitmapFactory.decodeResource(context.getResources(), resourceId); + } + + public NewApple returnApple() { + return this.Apple; + } + +// @Override +// public NewApple build() { +// return Apple(Apple.mSpawnRange, Apple.mSize, Apple.mBitmapApple); +// } + +} \ No newline at end of file diff --git a/Chapter17/java/NewApple.java b/Chapter17/java/NewApple.java new file mode 100644 index 0000000..a9a944a --- /dev/null +++ b/Chapter17/java/NewApple.java @@ -0,0 +1,19 @@ +package com.gamecodeschool.c17snake; + +import android.graphics.Bitmap; +import android.graphics.Canvas; +import android.graphics.Paint; +import android.graphics.Point; + +public class NewApple { + public Bitmap mBitmapApple; + public Point location; + public Point mSpawnRange; + public int mSize; + + public void draw(Canvas canvas, Paint paint) { + canvas.drawBitmap(this.mBitmapApple, + this.location.x * this.mBitmapApple.getWidth(), + this.location.y * this.mBitmapApple.getHeight(), paint); + }; +} diff --git a/Chapter17/java/Obstacle.java b/Chapter17/java/Obstacle.java new file mode 100644 index 0000000..037b70c --- /dev/null +++ b/Chapter17/java/Obstacle.java @@ -0,0 +1,51 @@ +package com.gamecodeschool.c17snake; + +import android.content.Context; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.graphics.Canvas; +import android.graphics.Paint; +import android.graphics.Point; + +import java.util.Random; + +public class Obstacle { + + private Point location = new Point(); + private Bitmap mBitmapObstacle; + private Point mSpawnRange; + private int mSize; + + Obstacle(Context context, Point sr, int s) { + // spawn range + mSpawnRange = sr; + // size of an obstacle + mSize = s; + + mBitmapObstacle = BitmapFactory.decodeResource(context.getResources(), R.drawable.obstacle); + mBitmapObstacle = Bitmap.createScaledBitmap(mBitmapObstacle, s, s, false); + + // initial spawn range + Random random = new Random(); + location.x = random.nextInt(mSpawnRange.x); + location.y = random.nextInt(mSpawnRange.y); + + } + + void spawn() { + Random random = new Random(); + location.x = random.nextInt(mSpawnRange.x) + 1; + location.y = random.nextInt(mSpawnRange.y - 1) + 1; + } + + Point getLocation() { + return location; + } + + // draw the obstacle + void draw(Canvas canvas, Paint paint) { + canvas.drawBitmap(mBitmapObstacle, + location.x * mSize, location.y * mSize, paint); + } + +} diff --git a/Chapter17/java/Snake.java b/Chapter17/java/Snake.java index 5c1184c..6372783 100644 --- a/Chapter17/java/Snake.java +++ b/Chapter17/java/Snake.java @@ -1,299 +1,324 @@ -package com.gamecodeschool.c17snake; - -import android.content.Context; -import android.graphics.Bitmap; -import android.graphics.BitmapFactory; -import android.graphics.Canvas; -import android.graphics.Matrix; -import android.graphics.Paint; -import android.graphics.Point; -import android.view.MotionEvent; - -import java.util.ArrayList; - -class Snake { - - // The location in the grid of all the segments - private ArrayList segmentLocations; - - // How big is each segment of the snake? - private int mSegmentSize; - - // How big is the entire grid - private Point mMoveRange; - - // Where is the centre of the screen - // horizontally in pixels? - private int halfWayPoint; - - // For tracking movement Heading - private enum Heading { - UP, RIGHT, DOWN, LEFT - } - - // Start by heading to the right - private Heading heading = Heading.RIGHT; - - // A bitmap for each direction the head can face - private Bitmap mBitmapHeadRight; - private Bitmap mBitmapHeadLeft; - private Bitmap mBitmapHeadUp; - private Bitmap mBitmapHeadDown; - - // A bitmap for the body - private Bitmap mBitmapBody; - - - Snake(Context context, Point mr, int ss) { - - // Initialize our ArrayList - segmentLocations = new ArrayList<>(); - - // Initialize the segment size and movement - // range from the passed in parameters - mSegmentSize = ss; - mMoveRange = mr; - - // Create and scale the bitmaps - mBitmapHeadRight = BitmapFactory - .decodeResource(context.getResources(), - R.drawable.head); - - // Create 3 more versions of the head for different headings - mBitmapHeadLeft = BitmapFactory - .decodeResource(context.getResources(), - R.drawable.head); - - mBitmapHeadUp = BitmapFactory - .decodeResource(context.getResources(), - R.drawable.head); - - mBitmapHeadDown = BitmapFactory - .decodeResource(context.getResources(), - R.drawable.head); - - // Modify the bitmaps to face the snake head - // in the correct direction - mBitmapHeadRight = Bitmap - .createScaledBitmap(mBitmapHeadRight, - ss, ss, false); - - // A matrix for scaling - Matrix matrix = new Matrix(); - matrix.preScale(-1, 1); - - mBitmapHeadLeft = Bitmap - .createBitmap(mBitmapHeadRight, - 0, 0, ss, ss, matrix, true); - - // A matrix for rotating - matrix.preRotate(-90); - mBitmapHeadUp = Bitmap - .createBitmap(mBitmapHeadRight, - 0, 0, ss, ss, matrix, true); - - // Matrix operations are cumulative - // so rotate by 180 to face down - matrix.preRotate(180); - mBitmapHeadDown = Bitmap - .createBitmap(mBitmapHeadRight, - 0, 0, ss, ss, matrix, true); - - // Create and scale the body - mBitmapBody = BitmapFactory - .decodeResource(context.getResources(), - R.drawable.body); - - mBitmapBody = Bitmap - .createScaledBitmap(mBitmapBody, - ss, ss, false); - - // The halfway point across the screen in pixels - // Used to detect which side of screen was pressed - halfWayPoint = mr.x * ss / 2; - } - - // Get the snake ready for a new game - void reset(int w, int h) { - - // Reset the heading - heading = Heading.RIGHT; - - // Delete the old contents of the ArrayList - segmentLocations.clear(); - - // Start with a single snake segment - segmentLocations.add(new Point(w / 2, h / 2)); - } - - - void move() { - // Move the body - // Start at the back and move it - // to the position of the segment in front of it - for (int i = segmentLocations.size() - 1; i > 0; i--) { - - // Make it the same value as the next segment - // going forwards towards the head - segmentLocations.get(i).x = segmentLocations.get(i - 1).x; - segmentLocations.get(i).y = segmentLocations.get(i - 1).y; - } - - // Move the head in the appropriate heading - // Get the existing head position - Point p = segmentLocations.get(0); - - // Move it appropriately - switch (heading) { - case UP: - p.y--; - break; - - case RIGHT: - p.x++; - break; - - case DOWN: - p.y++; - break; - - case LEFT: - p.x--; - break; - } - - } - - boolean detectDeath() { - // Has the snake died? - boolean dead = false; - - // Hit any of the screen edges - if (segmentLocations.get(0).x == -1 || - segmentLocations.get(0).x > mMoveRange.x || - segmentLocations.get(0).y == -1 || - segmentLocations.get(0).y > mMoveRange.y) { - - dead = true; - } - - // Eaten itself? - for (int i = segmentLocations.size() - 1; i > 0; i--) { - // Have any of the sections collided with the head - if (segmentLocations.get(0).x == segmentLocations.get(i).x && - segmentLocations.get(0).y == segmentLocations.get(i).y) { - - dead = true; - } - } - return dead; - } - - boolean checkDinner(Point l) { - //if (snakeXs[0] == l.x && snakeYs[0] == l.y) { - if (segmentLocations.get(0).x == l.x && - segmentLocations.get(0).y == l.y) { - - // Add a new Point to the list - // located off-screen. - // This is OK because on the next call to - // move it will take the position of - // the segment in front of it - segmentLocations.add(new Point(-10, -10)); - return true; - } - return false; - } - - void draw(Canvas canvas, Paint paint) { - - // Don't run this code if ArrayList has nothing in it - if (!segmentLocations.isEmpty()) { - // All the code from this method goes here - // Draw the head - switch (heading) { - case RIGHT: - canvas.drawBitmap(mBitmapHeadRight, - segmentLocations.get(0).x - * mSegmentSize, - segmentLocations.get(0).y - * mSegmentSize, paint); - break; - - case LEFT: - canvas.drawBitmap(mBitmapHeadLeft, - segmentLocations.get(0).x - * mSegmentSize, - segmentLocations.get(0).y - * mSegmentSize, paint); - break; - - case UP: - canvas.drawBitmap(mBitmapHeadUp, - segmentLocations.get(0).x - * mSegmentSize, - segmentLocations.get(0).y - * mSegmentSize, paint); - break; - - case DOWN: - canvas.drawBitmap(mBitmapHeadDown, - segmentLocations.get(0).x - * mSegmentSize, - segmentLocations.get(0).y - * mSegmentSize, paint); - break; - } - - // Draw the snake body one block at a time - for (int i = 1; i < segmentLocations.size(); i++) { - canvas.drawBitmap(mBitmapBody, - segmentLocations.get(i).x - * mSegmentSize, - segmentLocations.get(i).y - * mSegmentSize, paint); - } - } - } - - - // Handle changing direction - void switchHeading(MotionEvent motionEvent) { - - // Is the tap on the right hand side? - if (motionEvent.getX() >= halfWayPoint) { - switch (heading) { - // Rotate right - case UP: - heading = Heading.RIGHT; - break; - case RIGHT: - heading = Heading.DOWN; - break; - case DOWN: - heading = Heading.LEFT; - break; - case LEFT: - heading = Heading.UP; - break; - - } - } else { - // Rotate left - switch (heading) { - case UP: - heading = Heading.LEFT; - break; - case LEFT: - heading = Heading.DOWN; - break; - case DOWN: - heading = Heading.RIGHT; - break; - case RIGHT: - heading = Heading.UP; - break; - } - } - } -} +package com.gamecodeschool.c17snake; + +import android.content.Context; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.graphics.Canvas; +import android.graphics.Matrix; +import android.graphics.Paint; +import android.graphics.Point; +import android.view.MotionEvent; +import android.os.Handler; +import android.os.Looper; + +import java.util.ArrayList; + +class Snake { + + // The location in the grid of all the segments + private ArrayList segmentLocations; + + // How big is each segment of the snake? + private int mSegmentSize; + + // How big is the entire grid + private Point mMoveRange; + + // Where is the centre of the screen + // horizontally in pixels? + private int halfWayPoint; + + // For tracking movement Heading + private enum Heading { + UP, RIGHT, DOWN, LEFT + } + + // Start by heading to the right + private Heading heading = Heading.RIGHT; + + // A bitmap for each direction the head can face + private Bitmap mBitmapHeadRight; + private Bitmap mBitmapHeadLeft; + private Bitmap mBitmapHeadUp; + private Bitmap mBitmapHeadDown; + + // A bitmap for the body + private Bitmap mBitmapBody; + + // The snake's speed + private int speed; + + Snake(Context context, Point mr, int ss) { + + // Initialize our ArrayList + segmentLocations = new ArrayList<>(); + + // Set the initial speed + speed = 1; + + // Initialize the segment size and movement + // range from the passed in parameters + mSegmentSize = ss; + mMoveRange = mr; + + // Create and scale the bitmaps + mBitmapHeadRight = createVersion(context, "head"); + + // Create 3 more versions of the head for different headings + //Separated the creating of different versions to improve readability and redundancy + mBitmapHeadLeft = createVersion(context, "head"); + + mBitmapHeadUp = createVersion(context, "head"); + + mBitmapHeadDown = createVersion(context, "head"); + + // Modify the bitmaps to face the snake head + // in the correct direction + mBitmapHeadRight = Bitmap + .createScaledBitmap(mBitmapHeadRight, + ss, ss, false); + + // A matrix for scaling + Matrix matrix = new Matrix(); + matrix.preScale(-1, 1); + + mBitmapHeadLeft = modifySnakeHead(ss, matrix); + + // A matrix for rotating + matrix.preRotate(-90); + mBitmapHeadUp = modifySnakeHead(ss, matrix); + + // Matrix operations are cumulative + // so rotate by 180 to face down + matrix.preRotate(180); + mBitmapHeadDown = modifySnakeHead(ss, matrix); + + // Create and scale the body + mBitmapBody = createVersion(context, "body"); + + mBitmapBody = Bitmap + .createScaledBitmap(mBitmapBody, + ss, ss, false); + + // The halfway point across the screen in pixels + // Used to detect which side of screen was pressed + halfWayPoint = mr.x * ss / 2; + } + private Bitmap createVersion(Context context, String drawing){ + switch(drawing){ + case "head": + return BitmapFactory.decodeResource(context.getResources(), R.drawable.head); + } + return BitmapFactory.decodeResource(context.getResources(), R.drawable.body); + } + private Bitmap modifySnakeHead(int ss, Matrix matrix){ + return Bitmap.createBitmap(mBitmapHeadRight, + 0, 0, ss, ss, matrix, true); + } + + public int getSegmentSize(){ + return mSegmentSize; + } + + // Get the snake ready for a new game + void reset(int w, int h) { + + // Reset the heading + heading = Heading.RIGHT; + + // Delete the old contents of the ArrayList + segmentLocations.clear(); + + // Start with a single snake segment + segmentLocations.add(new Point(w / 2, h / 2)); + } + + + void move() { + // Move the body + // Start at the back and move it + // to the position of the segment in front of it + for (int i = segmentLocations.size() - 1; i > 0; i--) { + + // Make it the same value as the next segment + // going forwards towards the head + segmentLocations.get(i).x = segmentLocations.get(i - 1).x; + segmentLocations.get(i).y = segmentLocations.get(i - 1).y; + } + + // Move the head in the appropriate heading + // Get the existing head position + Point p = segmentLocations.get(0); + + // Move it appropriately + switch (heading) { + case UP: + p.y -= speed; + break; + + case RIGHT: + p.x += speed; + break; + + case DOWN: + p.y += speed; + break; + + case LEFT: + p.x -= speed; + break; + } + + } + + // Consume star >> get boosted + private boolean isSpeedBoostActive = false; + void applySpeedBoost() { + // Increase the speed for a brief period + speed = 2; + isSpeedBoostActive = true; + + // Using new imports to schedule a task to run after half a second of in-game time + new Handler(Looper.getMainLooper()).postDelayed(new Runnable() { + @Override + public void run() { + // Task to reset the snake's speed + speed = 1; + isSpeedBoostActive = false; + } + }, 500); // 500 = half a second + } + + boolean detectDeath() { + // Has the snake died? + boolean dead = false; + + // Hit any of the screen edges + if (segmentLocations.get(0).x == -1 || + segmentLocations.get(0).x > mMoveRange.x || + segmentLocations.get(0).y == -1 || + segmentLocations.get(0).y > mMoveRange.y) { + + dead = true; + } + + // Eaten itself? + for (int i = segmentLocations.size() - 1; i > 0; i--) { + // Have any of the sections collided with the head + if (segmentLocations.get(0).x == segmentLocations.get(i).x && + segmentLocations.get(0).y == segmentLocations.get(i).y) { + + dead = true; + } + } + return dead; + } + + boolean checkDinner(Point l) { + //if (snakeXs[0] == l.x && snakeYs[0] == l.y) { + if (segmentLocations.get(0).x == l.x && + segmentLocations.get(0).y == l.y) { + + // Add a new Point to the list + // located off-screen. + // This is OK because on the next call to + // move it will take the position of + // the segment in front of it + segmentLocations.add(new Point(-10, -10)); + return true; + } + return false; + } + + void draw(Canvas canvas, Paint paint) { + + // Don't run this code if ArrayList has nothing in it + if (!segmentLocations.isEmpty()) { + // All the code from this method goes here + // Draw the head + switch (heading) { + case RIGHT: + canvas.drawBitmap(mBitmapHeadRight, + segmentLocations.get(0).x + * mSegmentSize, + segmentLocations.get(0).y + * mSegmentSize, paint); + break; + + case LEFT: + canvas.drawBitmap(mBitmapHeadLeft, + segmentLocations.get(0).x + * mSegmentSize, + segmentLocations.get(0).y + * mSegmentSize, paint); + break; + + case UP: + canvas.drawBitmap(mBitmapHeadUp, + segmentLocations.get(0).x + * mSegmentSize, + segmentLocations.get(0).y + * mSegmentSize, paint); + break; + + case DOWN: + canvas.drawBitmap(mBitmapHeadDown, + segmentLocations.get(0).x + * mSegmentSize, + segmentLocations.get(0).y + * mSegmentSize, paint); + break; + } + + // Draw the snake body one block at a time + for (int i = 1; i < segmentLocations.size(); i++) { + canvas.drawBitmap(mBitmapBody, + segmentLocations.get(i).x + * mSegmentSize, + segmentLocations.get(i).y + * mSegmentSize, paint); + } + } + } + + + // Handle changing direction + void switchHeading(MotionEvent motionEvent) { + + // Is the tap on the right hand side? + if (motionEvent.getX() >= halfWayPoint) { + switch (heading) { + // Rotate right + case UP: + heading = Heading.RIGHT; + break; + case RIGHT: + heading = Heading.DOWN; + break; + case DOWN: + heading = Heading.LEFT; + break; + case LEFT: + heading = Heading.UP; + break; + + } + } else { + // Rotate left + switch (heading) { + case UP: + heading = Heading.LEFT; + break; + case LEFT: + heading = Heading.DOWN; + break; + case DOWN: + heading = Heading.RIGHT; + break; + case RIGHT: + heading = Heading.UP; + break; + } + } + } +} \ No newline at end of file diff --git a/Chapter17/java/SnakeActivity.java b/Chapter17/java/SnakeActivity.java index 6d4dc42..0452df3 100644 --- a/Chapter17/java/SnakeActivity.java +++ b/Chapter17/java/SnakeActivity.java @@ -23,7 +23,7 @@ protected void onCreate(Bundle savedInstanceState) { display.getSize(size); // Create a new instance of the SnakeEngine class - mSnakeGame = new SnakeGame(this, size); + mSnakeGame = new SnakeGame(this, size, null); // Make snakeEngine the view of the Activity setContentView(mSnakeGame); diff --git a/Chapter17/java/SnakeGame.java b/Chapter17/java/SnakeGame.java index 97338a3..b51bfdf 100644 --- a/Chapter17/java/SnakeGame.java +++ b/Chapter17/java/SnakeGame.java @@ -1,279 +1,434 @@ -package com.gamecodeschool.c17snake; - -import android.content.Context; -import android.content.res.AssetFileDescriptor; -import android.content.res.AssetManager; -import android.graphics.Canvas; -import android.graphics.Color; -import android.graphics.Paint; -import android.graphics.Point; -import android.media.AudioAttributes; -import android.media.AudioManager; -import android.media.SoundPool; -import android.os.Build; -import android.view.MotionEvent; -import android.view.SurfaceHolder; -import android.view.SurfaceView; -import java.io.IOException; - -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.detectDeath()) { - // Pause the game ready to start again - mSP.play(mCrashID, 1, 1, 0, 0, 1); - - mPaused =true; - } - - } - - - // Do all the drawing - public void draw() { - // Get a lock on the mCanvas - if (mSurfaceHolder.getSurface().isValid()) { - mCanvas = mSurfaceHolder.lockCanvas(); - - // Fill the screen with a color - mCanvas.drawColor(Color.argb(255, 26, 128, 182)); - - // Set the size and color of the mPaint for the text - mPaint.setColor(Color.argb(255, 255, 255, 255)); - mPaint.setTextSize(120); - - // Draw the score - mCanvas.drawText("" + mScore, 20, 120, mPaint); - - // Draw the apple and the snake - mApple.draw(mCanvas, mPaint); - mSnake.draw(mCanvas, mPaint); - - // Draw some text while paused - if(mPaused){ - - // Set the size and color of the mPaint for the text - mPaint.setColor(Color.argb(255, 255, 255, 255)); - mPaint.setTextSize(250); - - // Draw the message - // We will give this an international upgrade soon - //mCanvas.drawText("Tap To Play!", 200, 700, mPaint); - mCanvas.drawText(getResources(). - getString(R.string.tap_to_play), - 200, 700, mPaint); - } - - - // Unlock the mCanvas and reveal the graphics for this frame - mSurfaceHolder.unlockCanvasAndPost(mCanvas); - } - } - - @Override - public boolean onTouchEvent(MotionEvent motionEvent) { - switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) { - case MotionEvent.ACTION_UP: - if (mPaused) { - mPaused = false; - newGame(); - - // Don't want to process snake direction for this tap - return true; - } - - // Let the Snake class handle the input - mSnake.switchHeading(motionEvent); - break; - - default: - break; - - } - return true; - } - - - // Stop the thread - public void pause() { - mPlaying = false; - try { - mThread.join(); - } catch (InterruptedException e) { - // Error - } - } - - - // Start the thread - public void resume() { - mPlaying = true; - mThread = new Thread(this); - mThread.start(); - } -} +package com.gamecodeschool.cscfinalproject; + +import android.content.Context; +import android.content.res.AssetFileDescriptor; +import android.content.res.AssetManager; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.Paint; +import android.graphics.Point; +import android.media.SoundPool; +import android.media.MediaPlayer; +import android.view.MotionEvent; +import android.view.SurfaceHolder; +import android.view.SurfaceView; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileWriter; +import java.io.IOException; +import java.util.InputMismatchException; +import java.util.Random; +import java.util.Scanner; + + +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; + private int mGameOverID = -1; + + //for playing background music + private MediaPlayer mediaBackground; + + // 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; + private int mHighScore; + private int blockSize; + + Random random = new Random(); + + // Objects for drawing + private Canvas mCanvas; + private SurfaceHolder mSurfaceHolder; + private Paint mPaint; + + // A snake ssss + private Snake mSnake; + // And an apple + // Two Apple types + private NewApple mGoodApple; + private NewApple mBadApple; + + // adding in a star power-up! + private Star mStar; + + // adding in obstacles + private Obstacle mObstacle; + + // Apple Builders + private GoodAppleBuilder mGoodAppleBuilder; + private BadAppleBuilder mBadAppleBuilder; + + // Good/Bad Apple bitmaps + private Bitmap mBitmapGoodApple; + private Bitmap mBitmapBadApple; + + // adding reference to audio + private Audio audio; + + + // added the audio class for strategy implementation + public interface Audio { + void playEatSound(); + void playCrashSound(); + } + + + + // This is the constructor method that gets called + // from SnakeActivity + public SnakeGame(Context context, Point size, Audio audio) { + super(context); + mSP = new SoundPool.Builder().build(); + // 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 + try { + AssetManager assetManager = context.getAssets(); + AssetFileDescriptor descriptor; + + // Prepare the sounds in mem + descriptor = assetManager.openFd("get_apple.ogg"); + mEat_ID = mSP.load(descriptor, 0); + + descriptor = assetManager.openFd("snake_death.ogg"); + mCrashID = mSP.load(descriptor, 0); + + descriptor = assetManager.openFd("gameOver.ogg"); + mGameOverID = mSP.load(descriptor, 0); + + this.audio = audio; + + } catch (IOException e) { + // Error + } + + // Initialize the drawing objects + mSurfaceHolder = getHolder(); + mPaint = new Paint(); + + // Call the constructors of our two game objects + // initialize GoodApple bitmap + this.mBitmapGoodApple = BitmapFactory.decodeResource(context.getResources(), R.drawable.apple); + this.mBitmapGoodApple = Bitmap.createScaledBitmap(mBitmapGoodApple, blockSize, blockSize, false); + + // initialize BadApple bitmap + this.mBitmapBadApple = BitmapFactory.decodeResource(context.getResources(), R.drawable.badapple); + this.mBitmapBadApple = Bitmap.createScaledBitmap(mBitmapBadApple, blockSize, blockSize, false); + + + this.mGoodAppleBuilder = new GoodAppleBuilder(mBitmapGoodApple, new Point (0, 40) , new Point(NUM_BLOCKS_WIDE, mNumBlocksHigh), blockSize); + this.mGoodApple = this.mGoodAppleBuilder.returnApple(); + + this.mBadAppleBuilder = new BadAppleBuilder(mBitmapBadApple, new Point (10, 30) , new Point(NUM_BLOCKS_WIDE, mNumBlocksHigh), blockSize); + this.mBadApple = this.mBadAppleBuilder.returnApple(); + + mStar = new Star(context, + new Point(NUM_BLOCKS_WIDE, + mNumBlocksHigh), + blockSize); + mStar.spawn(); + + mObstacle = new Obstacle(context, + new Point(NUM_BLOCKS_WIDE, + mNumBlocksHigh), + blockSize); + mObstacle.spawn(); + + mSnake = new Snake(context, + new Point(NUM_BLOCKS_WIDE, + mNumBlocksHigh), + blockSize); + + //Initializing MediaPlayer + mediaBackground = MediaPlayer.create(this.getContext(), R.raw.background); + + } + + + // Called to start a new game + public void newGame() { + + // reset the snake + mSnake.reset(NUM_BLOCKS_WIDE, mNumBlocksHigh); + + // Get the apple ready for dinner + if (random.nextInt(2) + 1 > 1) { + this.mGoodApple.location.x = random.nextInt(this.mGoodApple.mSpawnRange.x - 1) + 1; + this.mGoodApple.location.y = random.nextInt(this.mGoodApple.mSpawnRange.y - 1) + 1; + this.mBadApple.location.x = -10; + this.mBadApple.location.y = 0; + } else { + this.mBadApple.location.x = random.nextInt(this.mBadApple.mSpawnRange.x - 1) + 1; + this.mBadApple.location.y = random.nextInt(this.mBadApple.mSpawnRange.y - 1) + 1; + this.mGoodApple.location.x = -10; + this.mGoodApple.location.y = 0; + } + + // Reset the mScore + mScore = 0; + Scanner input = new Scanner("highscore.txt"); + + System.out.println(new File("highscore.txt").getAbsolutePath()); + System.out.println("Testing"); + try { + + int test = input.nextInt(); + mHighScore = test; + input.close(); + }catch(InputMismatchException e){ + //discards + } + // Respawn tertiary items + mStar.spawn(); + mObstacle.spawn(); + + // 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(); + + //checking if background music is playing + if(!mediaBackground.isPlaying()){ + mediaBackground = MediaPlayer.create(this.getContext(), R.raw.background); + mediaBackground.start(); + } + + // Did the head of the snake eat the apple? + // Check if GoodApple was eaten + if(mSnake.checkDinner(this.mGoodApple.location)){ + // After eating an apple, randomly spawn a GoodApple or BadApple + // If: Spawns GoodApple hides BadApple; Else: vice versa + if (random.nextInt(2) + 1 > 1) { + this.mGoodApple.location.x = random.nextInt(this.mGoodApple.mSpawnRange.x - 1) + 1; + this.mGoodApple.location.y = random.nextInt(this.mGoodApple.mSpawnRange.y - 1) + 1; + this.mBadApple.location.x = -10; + this.mBadApple.location.y = 0; + } else { + this.mBadApple.location.x = random.nextInt(this.mBadApple.mSpawnRange.x - 1) + 1; + this.mBadApple.location.y = random.nextInt(this.mBadApple.mSpawnRange.y - 1) + 1; + this.mGoodApple.location.x = -10; + this.mGoodApple.location.y = 0; + } + // Add to 2 mScore for GoodApple + mScore = mScore + 2; + + // Play a sound + mSP.play(mEat_ID, 1, 1, 0, 0, 1); + } + + // Check if bad apple was eaten + if(mSnake.checkDinner(this.mBadApple.location)){ + // After eating an apple, randomly spawn a GoodApple or BadApple + // If: Spawns GoodApple hides BadApple; Else: vice versa + if (random.nextInt(2) + 1 > 1) { + this.mGoodApple.location.x = random.nextInt(this.mGoodApple.mSpawnRange.x - 1) + 1; + this.mGoodApple.location.y = random.nextInt(this.mGoodApple.mSpawnRange.y - 1) + 1; + this.mBadApple.location.x = -10; + this.mBadApple.location.y = 0; + } else { + this.mBadApple.location.x = random.nextInt(this.mBadApple.mSpawnRange.x - 1) + 1; + this.mBadApple.location.y = random.nextInt(this.mBadApple.mSpawnRange.y - 1) + 1; + this.mGoodApple.location.x = -10; + this.mGoodApple.location.y = 0; + } + // Add to 1 mScore for BadApple + mScore = mScore + 1; + + // Play a sound + mSP.play(mEat_ID, 1, 1, 0, 0, 1); + } + + // Did the head of the snake eat the star? + if (mSnake.checkDinner(mStar.getLocation())) { + mStar.spawn(); // spawn a new star + mSP.play(mEat_ID, 1, 1, 0, 0, 1); + mStar.applySpeedBoost(mSnake); // speed booster + mObstacle.spawn(); // spawn new obstacle >:) + } + + // Did the head of the snake eat the obstacle? + if (mSnake.checkDinner(mObstacle.getLocation())) { + mScore = mScore -1; + mObstacle.spawn(); // spawn new obstacle + } + + // Did the snake die? + if (mSnake.detectDeath()) { + // play the new crash sound using strategy ;) + if (audio != null) { + audio.playCrashSound(); + } + mSP.play(mGameOverID, 1, 1, 0, 0, 1); + if(mScore > mHighScore){ + mHighScore = mScore; + + try { + FileWriter output = new FileWriter("HighScore.txt"); + output.write(mHighScore); + } catch (IOException e) { + //in the case the save file is not present discards + } + + } + mPaused =true; + } + + } + + + // Do all the drawing + public void draw() { + // Get a lock on the mCanvas + if (mSurfaceHolder.getSurface().isValid()) { + mCanvas = mSurfaceHolder.lockCanvas(); + + // Fill the screen with a color + mCanvas.drawColor(Color.argb(255, 26, 128, 182)); + + // Set the size and color of the mPaint for the text + mPaint.setColor(Color.argb(255, 255, 255, 255)); + mPaint.setTextSize(70); + // Draw the score + mCanvas.drawText("Score: " + mScore, 20, 100, mPaint); + mCanvas.drawText("HighScore: " + mHighScore, 20, 200, mPaint); + + + // Draw the apple, snake, star, and obstacle + this.mGoodApple.draw(mCanvas, mPaint); + this.mBadApple.draw(mCanvas, mPaint); + mSnake.draw(mCanvas, mPaint); + mStar.draw(mCanvas, mPaint); + mObstacle.draw(mCanvas, mPaint); + + + // Draw some text while paused + if(mPaused){ + + // Set the size and color of the mPaint for the text + mPaint.setColor(Color.argb(255, 255, 255, 255)); + mPaint.setTextSize(250); + + // Draw the message + // We will give this an international upgrade soon + //mCanvas.drawText("Tap To Play!", 200, 700, mPaint); + mCanvas.drawText(getResources(). + getString(R.string.tap_to_play), + 200, 700, mPaint); + } + + + // Unlock the mCanvas and reveal the graphics for this frame + mSurfaceHolder.unlockCanvasAndPost(mCanvas); + } + } + + @Override + public boolean onTouchEvent(MotionEvent motionEvent) { + switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) { + case MotionEvent.ACTION_UP: + if (mPaused) { + mPaused = false; + newGame(); + + // Don't want to process snake direction for this tap + return true; + } + + // Let the Snake class handle the input + mSnake.switchHeading(motionEvent); + break; + + default: + break; + + } + return true; + } + + + // Stop the thread + public void pause() { + mPlaying = false; + try { + mThread.join(); + } catch (InterruptedException e) { + // Error + } + } + + + // Start the thread + public void resume() { + mPlaying = true; + mThread = new Thread(this); + mThread.start(); + } +} diff --git a/Chapter17/java/Star.java b/Chapter17/java/Star.java new file mode 100644 index 0000000..429d2ea --- /dev/null +++ b/Chapter17/java/Star.java @@ -0,0 +1,54 @@ +package com.gamecodeschool.c17snake; + +import android.content.Context; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.graphics.Canvas; +import android.graphics.Paint; +import android.graphics.Point; + +import java.util.Random; + +public class Star { + + private Point location = new Point(); + private Bitmap mBitmapStar; + private Point mSpawnRange; + private int mSize; + + Star(Context context, Point sr, int s) { + // spawn range + mSpawnRange = sr; + // size of a star + mSize = s; + + mBitmapStar = BitmapFactory.decodeResource(context.getResources(), R.drawable.star); + mBitmapStar = Bitmap.createScaledBitmap(mBitmapStar, s, s, false); + + // initial spawn range + Random random = new Random(); + location.x = random.nextInt(mSpawnRange.x); + location.y = random.nextInt(mSpawnRange.y); + } + + void spawn() { + Random random = new Random(); + location.x = random.nextInt(mSpawnRange.x) + 1; + location.y = random.nextInt(mSpawnRange.y - 1) + 1; + } + + Point getLocation() { + return location; + } + + // draw the star + void draw(Canvas canvas, Paint paint) { + canvas.drawBitmap(mBitmapStar, + location.x * mSize, location.y * mSize, paint); + } + + // consume star >> get boosted! + void applySpeedBoost(Snake snake) { + snake.applySpeedBoost(); + } +} diff --git a/Chapter17/java/highscore.txt b/Chapter17/java/highscore.txt new file mode 100644 index 0000000..9a03714 --- /dev/null +++ b/Chapter17/java/highscore.txt @@ -0,0 +1 @@ +10 \ No newline at end of file diff --git a/Chapter16/drawable/apple.png b/Chapter17/res/drawable/apple.png similarity index 100% rename from Chapter16/drawable/apple.png rename to Chapter17/res/drawable/apple.png diff --git a/Chapter17/res/drawable/badapple.png b/Chapter17/res/drawable/badapple.png new file mode 100644 index 0000000..43315b9 Binary files /dev/null and b/Chapter17/res/drawable/badapple.png differ diff --git a/Chapter17/res/drawable/bapple.png b/Chapter17/res/drawable/bapple.png new file mode 100644 index 0000000..81115b6 Binary files /dev/null and b/Chapter17/res/drawable/bapple.png differ diff --git a/Chapter16/drawable/body.png b/Chapter17/res/drawable/body.png similarity index 100% rename from Chapter16/drawable/body.png rename to Chapter17/res/drawable/body.png diff --git a/Chapter16/drawable/head.png b/Chapter17/res/drawable/head.png similarity index 100% rename from Chapter16/drawable/head.png rename to Chapter17/res/drawable/head.png diff --git a/Chapter17/res/drawable/ic_launcher_background.xml b/Chapter17/res/drawable/ic_launcher_background.xml new file mode 100644 index 0000000..07d5da9 --- /dev/null +++ b/Chapter17/res/drawable/ic_launcher_background.xml @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Chapter17/res/drawable/ic_launcher_foreground.xml b/Chapter17/res/drawable/ic_launcher_foreground.xml new file mode 100644 index 0000000..2b068d1 --- /dev/null +++ b/Chapter17/res/drawable/ic_launcher_foreground.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/Chapter17/res/drawable/obstacle.png b/Chapter17/res/drawable/obstacle.png new file mode 100644 index 0000000..c10b22e Binary files /dev/null and b/Chapter17/res/drawable/obstacle.png differ diff --git a/Chapter17/res/drawable/star.png b/Chapter17/res/drawable/star.png new file mode 100644 index 0000000..c10f4b1 Binary files /dev/null and b/Chapter17/res/drawable/star.png differ diff --git a/Chapter17/res/mipmap-anydpi-v26/ic_launcher.xml b/Chapter17/res/mipmap-anydpi-v26/ic_launcher.xml new file mode 100644 index 0000000..6f3b755 --- /dev/null +++ b/Chapter17/res/mipmap-anydpi-v26/ic_launcher.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Chapter17/res/mipmap-anydpi-v26/ic_launcher_round.xml b/Chapter17/res/mipmap-anydpi-v26/ic_launcher_round.xml new file mode 100644 index 0000000..6f3b755 --- /dev/null +++ b/Chapter17/res/mipmap-anydpi-v26/ic_launcher_round.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Chapter17/res/mipmap-hdpi/ic_launcher.webp b/Chapter17/res/mipmap-hdpi/ic_launcher.webp new file mode 100644 index 0000000..c209e78 Binary files /dev/null and b/Chapter17/res/mipmap-hdpi/ic_launcher.webp differ diff --git a/Chapter17/res/mipmap-hdpi/ic_launcher_round.webp b/Chapter17/res/mipmap-hdpi/ic_launcher_round.webp new file mode 100644 index 0000000..b2dfe3d Binary files /dev/null and b/Chapter17/res/mipmap-hdpi/ic_launcher_round.webp differ diff --git a/Chapter17/res/mipmap-mdpi/ic_launcher.webp b/Chapter17/res/mipmap-mdpi/ic_launcher.webp new file mode 100644 index 0000000..4f0f1d6 Binary files /dev/null and b/Chapter17/res/mipmap-mdpi/ic_launcher.webp differ diff --git a/Chapter17/res/mipmap-mdpi/ic_launcher_round.webp b/Chapter17/res/mipmap-mdpi/ic_launcher_round.webp new file mode 100644 index 0000000..62b611d Binary files /dev/null and b/Chapter17/res/mipmap-mdpi/ic_launcher_round.webp differ diff --git a/Chapter17/res/mipmap-xhdpi/ic_launcher.webp b/Chapter17/res/mipmap-xhdpi/ic_launcher.webp new file mode 100644 index 0000000..948a307 Binary files /dev/null and b/Chapter17/res/mipmap-xhdpi/ic_launcher.webp differ diff --git a/Chapter17/res/mipmap-xhdpi/ic_launcher_round.webp b/Chapter17/res/mipmap-xhdpi/ic_launcher_round.webp new file mode 100644 index 0000000..1b9a695 Binary files /dev/null and b/Chapter17/res/mipmap-xhdpi/ic_launcher_round.webp differ diff --git a/Chapter17/res/mipmap-xxhdpi/ic_launcher.webp b/Chapter17/res/mipmap-xxhdpi/ic_launcher.webp new file mode 100644 index 0000000..28d4b77 Binary files /dev/null and b/Chapter17/res/mipmap-xxhdpi/ic_launcher.webp differ diff --git a/Chapter17/res/mipmap-xxhdpi/ic_launcher_round.webp b/Chapter17/res/mipmap-xxhdpi/ic_launcher_round.webp new file mode 100644 index 0000000..9287f50 Binary files /dev/null and b/Chapter17/res/mipmap-xxhdpi/ic_launcher_round.webp differ diff --git a/Chapter17/res/mipmap-xxxhdpi/ic_launcher.webp b/Chapter17/res/mipmap-xxxhdpi/ic_launcher.webp new file mode 100644 index 0000000..aa7d642 Binary files /dev/null and b/Chapter17/res/mipmap-xxxhdpi/ic_launcher.webp differ diff --git a/Chapter17/res/mipmap-xxxhdpi/ic_launcher_round.webp b/Chapter17/res/mipmap-xxxhdpi/ic_launcher_round.webp new file mode 100644 index 0000000..9126ae3 Binary files /dev/null and b/Chapter17/res/mipmap-xxxhdpi/ic_launcher_round.webp differ diff --git a/Chapter17/res/raw/background.ogg b/Chapter17/res/raw/background.ogg new file mode 100644 index 0000000..44199be Binary files /dev/null and b/Chapter17/res/raw/background.ogg differ diff --git a/Chapter17/res/values/colors.xml b/Chapter17/res/values/colors.xml new file mode 100644 index 0000000..f8c6127 --- /dev/null +++ b/Chapter17/res/values/colors.xml @@ -0,0 +1,10 @@ + + + #FFBB86FC + #FF6200EE + #FF3700B3 + #FF03DAC5 + #FF018786 + #FF000000 + #FFFFFFFF + \ No newline at end of file diff --git a/Chapter17/res/values/strings.xml b/Chapter17/res/values/strings.xml new file mode 100644 index 0000000..5627111 --- /dev/null +++ b/Chapter17/res/values/strings.xml @@ -0,0 +1,4 @@ + + My Application + Tap to Play + \ No newline at end of file diff --git a/Chapter17/res/values/themes.xml b/Chapter17/res/values/themes.xml new file mode 100644 index 0000000..e48770a --- /dev/null +++ b/Chapter17/res/values/themes.xml @@ -0,0 +1,5 @@ + + + +