ENGLISH

Learning Java by Building Android Games

Book information

Publisher
Packt Publishing
Year
2018
ISBN
1788839153, 978-1788839150
Language
english
Format
PDF
Filesize
8 MB (8090878 bytes)
Edition
2
Pages
775\775
Time added
2019-07-16 00:08:21

Description

Cover......Page 1 Copyright......Page 3 Contributors......Page 5 Table of Contents......Page 8 Preface......Page 24 Chapter 1: Java, Android and Game Development......Page 34 Why Java, Android and Games?......Page 35 Java: The first stumbling block......Page 36 Sub' Hunter......Page 37 Pong......Page 38 Snake Clone......Page 39 Scrolling Shooter......Page 40 Open-World Platformer......Page 41 How Java and Android work......Page 42 Setting up Android Studio......Page 43 Final step......Page 50 Starting the first project: Sub' Hunter......Page 51 Android Studio and our project – A very brief guided tour......Page 57 The Project panel......Page 58 The Editor window......Page 59 Locking the game to full-screen and landscape orientation......Page 61 Deploying the game so far......Page 62 Running the game on an Android emulator......Page 63 Running the game on a real device......Page 65 Summary......Page 66 Planning the Sub' Hunter game......Page 68 Actions flowchart/diagram......Page 71 Code comments......Page 72 Mapping out our code using comments......Page 73 Introduction to Java methods......Page 76 Structuring Sub' Hunter with methods......Page 78 Introduction to Object Oriented Programming......Page 80 Classes, objects, and instances......Page 81 Final word on OOP, Classes, and Objects – for now......Page 82 Using Java packages......Page 84 Adding classes by importing packages......Page 85 Linking up our methods......Page 86 Summary......Page 92 Handling syntax and jargon......Page 94 Java Variables......Page 95 Primitive types......Page 97 Reference variables......Page 99 Initializing variables......Page 101 Most used operators in this book......Page 102 Concatenation......Page 104 Planning the variables......Page 105 Declaring the variables......Page 106 Handling different screen sizes and resolutions......Page 107 Handling different screen resolutions part 1: Initialising the variables......Page 109 Errors, warnings, and bugs......Page 112 Printing Debugging Information......Page 113 Testing the game......Page 114 Summary......Page 115 Methods......Page 116 Methods revisited and explained further......Page 117 The method signature......Page 118 Modifier......Page 120 Return type......Page 121 A closer look at method names......Page 122 Parameters......Page 123 Doing things in the method body......Page 124 Creating a new project......Page 125 Coding the method overloading mini-app......Page 126 Running the method overloading mini-app......Page 128 Scope: Methods and Variables......Page 129 Revisiting the code and methods we have used already......Page 130 The Random class and the nextInt method......Page 131 Adding Random based code to newGame......Page 132 Testing the game......Page 133 Summary......Page 134 Understanding the Canvas class......Page 136 Paint......Page 137 Canvas, Bitmap, Paint and ImageView quick summary......Page 138 Preparing the objects of classes......Page 139 Setting the Activity content......Page 140 Creating a new project......Page 141 Coding the Canvas demo app......Page 142 Drawing on the screen......Page 144 Android Coordinate system......Page 146 Plotting and drawing......Page 147 Preparing to draw......Page 148 Initializing a Canvas, Paint, ImageView, and Bitmap......Page 149 Drawing some grid lines......Page 151 Drawing the HUD......Page 153 Upgrading the printDebuggingText method......Page 155 Summary......Page 157 Decisions, decisions......Page 158 More operators......Page 159 Loops......Page 161 While loops......Page 162 Breaking out of a loop......Page 163 Do while loops......Page 164 Nested loops......Page 165 Using for loops to draw the Sub' Hunter grid......Page 166 Summary......Page 169 If they come over the bridge shoot them......Page 170 Else do this instead......Page 171 Switching to make decisions......Page 174 Switch example......Page 175 Combining different control flow blocks......Page 176 Making sense of the screen touches......Page 177 Coding the onTouchEvent method......Page 180 Coding the takeShot method......Page 182 Explaining the takeShot method......Page 183 Coding the boom method......Page 185 Drawing the shot on the grid......Page 186 Running the game......Page 188 Summary......Page 189 Basic Object-Oriented Programming......Page 190 Introducing OOP......Page 191 Inheritance......Page 192 Why we do it like this?......Page 193 Looking at the code for a class......Page 194 Declaring, initializing and using an object of the class......Page 195 Creating your first class......Page 199 More things we can do with our first class......Page 203 Remember that encapsulation thing?......Page 204 Controlling class use with access modifiers......Page 205 Controlling variable use with access modifiers......Page 206 Methods have access modifiers too......Page 207 Method access summary......Page 208 Accessing private variables with getters and setters......Page 209 Setting up our objects with constructors......Page 212 Static methods......Page 214 Encapsulation and static methods mini-app......Page 216 OOP and inheritance......Page 221 Inheritance mini-app......Page 223 Polymorphism......Page 227 Abstract classes......Page 228 Interfaces......Page 230 Starting the Pong game......Page 231 Setting up the Pong project......Page 232 Summary......Page 233 Chapter 9: The Game Engine, Threads, and The Game Loop......Page 234 Coding the PongActivity class......Page 235 Coding the PongGame class......Page 238 Coding the PongGame class......Page 240 Adding the member variables......Page 241 Coding the PongGame constructor......Page 244 Coding the startNewGame method......Page 245 Coding the draw method......Page 246 Adding the printDebuggingText method......Page 247 Understanding the draw method and the SurfaceView class......Page 248 The game loop......Page 250 Problems with threads......Page 252 Java try, catch exception handling......Page 256 Implementing Runnable and providing the run method......Page 257 Starting and stopping the thread......Page 258 Activity lifecycle......Page 259 Lifecycle phases: What we need to know......Page 260 Lifecycle phases: What we need to do......Page 262 Coding the run method......Page 263 Running the game......Page 267 Summary......Page 268 The Ball Class......Page 270 Representing rectangles and squares with RectF......Page 272 Coding the Ball constructor......Page 273 Coding the RectF getter method......Page 274 Coding the Ball update method......Page 275 Coding the Ball helper methods......Page 276 Coding a realistic-ish bounce......Page 278 Using the Ball class......Page 280 The Bat class......Page 283 Coding the Bat variables......Page 284 Coding the Bat constructor......Page 285 Coding the Bat's update method......Page 287 Using the Bat Class......Page 289 Coding the Bat input handling......Page 290 Summary......Page 292 Chapter 11: Collisions, Sound Effects and Supporting Different Versions of Android......Page 294 Rectangle intersection......Page 295 Radius overlapping......Page 297 Crossing number algorithm......Page 298 Neighbour checking......Page 299 Best options for Pong......Page 300 Handling different versions of Android......Page 301 The Soundpool class......Page 302 Back to initializing SoundPool (the new way)......Page 303 Initializing SoundPool the old way......Page 304 Loading sound files into memory......Page 305 Playing a sound......Page 306 Generating sound effects......Page 307 Adding the sound variables......Page 310 Initializing the SoundPool......Page 311 The bat and the ball......Page 313 The four walls......Page 314 Playing the game......Page 315 Summary......Page 316 Chapter 12: Handling Lots of Data with Arrays......Page 318 Planning the project......Page 319 Starting the project......Page 320 Creating the classes......Page 321 Coding the BulletHellActivity......Page 322 Coding the BulletHellGame class......Page 323 Coding the member variables......Page 324 Coding the BulletHellGame constructor......Page 325 Coding the BulletHellGame methods......Page 326 Coding draw and onTouchEvent......Page 327 Coding pause, resume, and printDebuggingText......Page 328 Testing the Bullet Hell engine......Page 329 Coding the Bullet class......Page 330 Spawning a bullet......Page 333 Java Arrays......Page 335 Arrays are objects......Page 337 Simple array example mini-app......Page 338 Dynamic array example......Page 340 Multidimensional Array mini app......Page 342 Spawning an array of bullets......Page 346 Running the game......Page 351 Summary......Page 352 The Bob (player's) class......Page 354 Coding the Bob class......Page 355 Using the Bob class......Page 359 Adding Bob to the collision detection......Page 360 Drawing Bob to the screen......Page 362 Adding the sound effects......Page 363 Activating Bob's teleport......Page 364 Coding the printDebuggingText method......Page 365 Coding the spawnBullet method (again)......Page 366 Coding the startGame method......Page 368 Summary......Page 369 Chapter 14: The Stack, the Heap, and the Garbage Collector......Page 372 Variables revisited......Page 373 The stack and the heap......Page 374 A quick break to throw out the trash......Page 375 Introduction to the Snake game......Page 376 Make full screen and landscape......Page 378 Coding SnakeActivity......Page 379 Coding the members......Page 381 Coding the constructor......Page 383 Coding the run method......Page 385 Coding the updateRequired method......Page 386 Coding the update method......Page 387 Coding the draw method......Page 388 Coding onTouchEvent......Page 389 Coding pause and resume......Page 390 Summary......Page 391 Making the snake game Spanish, English and German......Page 392 Adding German support......Page 393 Add the string resources......Page 394 Amending the code......Page 395 Run the game in German or Spanish......Page 396 Summary......Page 397 Adding the graphics......Page 398 Coding the apple......Page 399 The Apple constructor......Page 400 Using the apple......Page 402 Running the game......Page 403 ArrayLists......Page 404 Arrays and ArrayLists are polymorphic......Page 406 Enumerations......Page 408 Summary......Page 410 Rotating Bitmaps......Page 412 What is a Bitmap exactly......Page 413 Inverting the head to face left......Page 414 Rotating the head to face up and down......Page 416 Coding the Snake class......Page 417 Coding the constructor......Page 419 Coding the reset method......Page 421 Coding the move method......Page 422 Coding the detectDeath method......Page 424 Coding the checkDinner method......Page 425 Coding the draw method......Page 426 Coding the switchHeading method......Page 428 Using the snake class and finishing the game......Page 429 Running the completed game......Page 432 Summary......Page 433 Chapter 18: Introduction to Design Patterns and much more!......Page 436 Introducing the Scrolling Shooter project......Page 437 Game programming patterns and the structure of the Scrolling Shooter project......Page 440 Starting the project......Page 441 Code the GameActivity class......Page 442 Getting started on the GameEngine class......Page 443 Controlling the game with a GameState class......Page 446 Communicating from GameState to GameEngine......Page 447 Interface refresher......Page 448 2. Implementing the interface......Page 449 3. Passing a reference to the interface into the class that needs it......Page 450 Coding the GameState class......Page 451 Saving and loading the high score- forever......Page 452 Finishing off the GameState class......Page 455 Using the GameState class......Page 458 Adding the sound files to the project......Page 459 Coding the SoundEngine class......Page 460 Using the SoundEngine class......Page 461 Building a HUD class to display control buttons and text......Page 462 Coding the prepareControls method......Page 464 Coding the draw method of the HUD class......Page 466 Coding drawControls and getControls......Page 468 Building a Renderer class to handle the drawing......Page 469 Using the HUD and Renderer classes......Page 471 Running the game......Page 472 Summary......Page 473 Chapter 19: Listening with the Observer Pattern, Multitouch and Building a Particle System......Page 474 The Observer pattern in the Scrolling Shooter project......Page 475 Coding the Broadcaster interface......Page 477 Making GameEngine a Broadcaster......Page 478 Coding a Multitouch UI controller and making it a listener......Page 479 Coding the required handleInput method......Page 480 Using the UIController......Page 483 Running the game......Page 484 Implementing a particle system explosion......Page 485 Coding the Particle class......Page 486 Coding the ParticleSystem class......Page 488 Adding a particle system to the game engine and drawing it with the Renderer......Page 492 Building a physics engine to get things moving......Page 495 Running the game......Page 497 Summary......Page 498 Chapter 20: More Patterns, a Scrolling Background and Building the Player's ship......Page 500 Reminder of how all these objects will behave......Page 501 The first coding nightmare......Page 502 Using a generic GameObject for better code structure......Page 504 Composition over inheritance......Page 505 The Simple Factory pattern......Page 506 At last some good news......Page 508 Summary so far......Page 509 Coding the ObjectSpec parent class......Page 510 AlienChaseSpec......Page 512 AlienLaserSpec......Page 514 AlienPatrolSpec......Page 515 BackgroundSpec......Page 516 PlayerLaserSpec......Page 517 Coding the component Interfaces......Page 518 InputComponent......Page 519 SpawnComponent......Page 520 StdGraphicsComponent......Page 521 PlayerMovementComponent......Page 522 PlayerInputComponent and the PlayerLaserSpawner interface......Page 523 LaserSpawnComponent......Page 525 BackgroundGraphicsComponent......Page 526 BackgroundSpawnComponent......Page 527 Every GameObject has a transform......Page 528 Every object is a GameObject......Page 535 Completing the StdGraphicsComponent......Page 539 Completing the PlayerMovementComponent......Page 541 Completing the PlayerSpawnComponent......Page 543 Completing the PlayerInputComponent......Page 544 Completing the LaserMovementComponent......Page 548 Completing the LaserSpawnComponent......Page 550 Coding a scrolling background......Page 551 Completing the BackgroundGraphicsComponent......Page 552 Completing the BackgroundMovementComponent......Page 556 GameObject/Component reality check......Page 557 Building the GameObjectFactory......Page 558 Coding the Level class......Page 564 Updating GameEngine......Page 567 Updating PhysicsEngine......Page 570 Updating Renderer......Page 571 Running the game......Page 572 Summary......Page 573 Adding the alien's components......Page 574 AlienChaseMovementComponent......Page 575 Implement the Interface in GameEngine......Page 581 AlienDiverMovementComponent......Page 582 AlienHorizontalSpawnComponent......Page 584 AlienPatrolMovementComponent......Page 585 AlienVerticalSpawnComponent......Page 589 Updating GameEngine......Page 590 Updating Level......Page 591 Updating GameObjectFactory......Page 592 Detecting collisions......Page 593 Summary......Page 597 Chapter 22: Exploring More Patterns and Planning the Platformer Project......Page 598 Platform Game: Bob Was in A Hurry......Page 599 Level design files example......Page 602 The graphics......Page 604 Cameras and the real world......Page 605 The slightly modified ObjectSpec......Page 606 Project patterns......Page 607 Levels and LevelManager......Page 608 Creating the project and adding the assets......Page 609 Specifying all the game objects with GameObjectSpec classes......Page 610 GameObjectSpec......Page 611 BackgroundCitySpec......Page 613 BackgroundMountainSpec......Page 614 BrickTileSpec......Page 615 CoalTileSpec......Page 616 CollectibleObjectSpec......Page 617 DeadTreeTileSpec......Page 618 GrassTileSpec......Page 619 MoveablePlatformSpec......Page 621 PlayerSpec......Page 622 ScorchedTileSpec......Page 623 SnowyTreeTileSpec......Page 624 StalagmiteTileSpec......Page 625 Coding the component interfaces......Page 626 GraphicsComponent......Page 627 Coding the other interfaces......Page 628 InputObserver......Page 629 Summary......Page 630 Chapter 23: The Singleton Pattern, Java HashMap, Storing Bitmaps Efficiently and Designing Levels......Page 632 The Singleton code......Page 634 More Java Collections – Meet Java Hashmap......Page 637 Coding the BitmapStore class......Page 639 Coding the basic transform......Page 644 InanmiateBlockGraphicsComponent......Page 648 InanimateBlockUpdateComponent......Page 651 Create the levels......Page 652 LevelCity......Page 653 LevelMountains......Page 655 LevelUnderground......Page 656 Coding a stripped down GameObjectFactory......Page 657 Coding a slightly commented-out game object......Page 660 Coding the GameState......Page 662 Code the SoundEngine......Page 668 Coding the physics engine (without collision)......Page 671 Coding a Renderer......Page 672 Coding the camera......Page 673 Testing the formulas with hypothetical coordinates......Page 677 Coding the Hud......Page 680 Coding the UIController class......Page 685 Code the Activity......Page 687 Code the GameEngine......Page 688 Coding the LevelManager class......Page 693 Running the game......Page 699 Summary......Page 701 Chapter 24: Sprite-sheet animations, Controllable Player and Parallax Scrolling Backgrounds......Page 702 Coding the Animator......Page 703 PlayerTransform......Page 708 AnimatedGraphicsComponent......Page 714 PlayerInputComponent......Page 717 PlayerUpdateComponent......Page 721 Coding a parallax background......Page 726 Coding the BackgroundTransform......Page 727 Coding the BackgroundGraphicsComponent......Page 728 Coding the BackgroundUpdateComponent......Page 731 Updating the levelManager, GameObjectFactory and GameObject......Page 732 Summary......Page 739 Coding the moving platform component class......Page 740 MoveableBlockUpdateComponent......Page 741 Update LevelManager and GameObjectFactory......Page 743 Coding the detectCollisions method: Part 1......Page 744 Explaining part 1......Page 746 Coding the detectCollisions method: Part 2......Page 749 Explaining part 2......Page 751 Summary......Page 754 Publishing......Page 756 Future learning......Page 757 Thanks......Page 758 Index......Page 764

Similar books