android game programming by example horton 2015 06 30 Lập trình android

409 34 0
android game programming by example horton 2015 06 30 Lập trình android

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

CuuDuongThanCong.com Table of Contents Android Game Programming by Example Credits About the Author About the Reviewers www.PacktPub.com Support files, eBooks, discount offers, and more Why subscribe? Free access for Packt account holders Preface What this book covers What you need for this book Who this book is for Conventions Reader feedback Customer support Downloading the example code Downloading the color images of this book Errata Piracy Questions Player UP A closer look at the games Tappy Defender Tough retro platformer Asteroids simulator Setting up your development environment Installing the JDK Installing Android Studio Summary Tappy Defender – First Step Planning the first game Backstory The game mechanics Rules for the game The design Control Model View Design pattern reality check CuuDuongThanCong.com The game code structure The Android Activity lifecycle The Android Studio file structure Building the home screen Creating the project What we did Building the home screen UI What we did Coding the functionality Creating GameActivity What we did Configuring the AndroidManifest.xml file What we did Coding the game loop Building the view Creating a new class for the view What we did Structuring the class code The game activity The PlayerShip object Drawing the scene Plotting and drawing Drawing PlayerShip The Canvas and Paint objects Controlling the frame rate Deploying the game Debugging on an Android device Summary Tappy Defender – Taking Flight Controlling the spaceship Detecting touches Adding boosters to the spaceship Detecting the screen resolution Building the enemies Designing the enemy Spawning the enemy Making the enemy think The thrill of flight – scrolling the background Things that go bump – collision detection Collision detection options Rectangle intersection Radius overlapping The crossing number algorithm CuuDuongThanCong.com Optimizations Multiple hitboxes Neighbor checking Best options for Tappy Defender Summary Tappy Defender – Going Home Displaying a HUD Implementing the rules Ending the game Restarting the game Adding sound FX Generating the FX The SoundPool class Coding the sound FX Adding persistence Iteration Multiple different enemy graphics An exercise in balance Format time Handle the back button The finished game Summary Platformer – Upgrading the Game Engine The game The backstory The game mechanics Rules for the game Upgrading the game engine The platform activity Locking the layout to landscape The PlatformView class The basic structure of PlatformView The GameObject class The view through a viewport Creating the levels The enhanced update method The enhanced draw method Summary Platformer – Bob, Beeps, and Bumps The SoundManager class Introducing Bob Multiphase collision detection Player input CuuDuongThanCong.com Animating Bob Summary Platformer – Guns, Life, Money, and the Enemy Ready aim fire Pickups The drone The guard Summary Platformer – Putting It All Together Bullet collision detection Adding some fire tiles Eye candy The new platform tiles The new scenery objects Scrolling parallax backgrounds Pause menu with moveable viewport Levels and game rules Traveling between levels The level designs The cave The city The forest The mountains The HUD Summary Asteroids at 60 FPS with OpenGL ES Asteroids simulator The game controls Rules for the game Introducing OpenGL ES Why use it and how does it work? What is neat about Version 2? How we will use OpenGL ES 2? Preparing OpenGL ES Locking the layout to landscape Activity The view A class to manage our game Managing simple shaders The game's main loop – the renderer Building an OpenGL-friendly, GameObject super class The spaceship Drawing at 60 + FPS CuuDuongThanCong.com Summary 10 Move and Draw with OpenGL ES Drawing a static game border Twinkling stars Bringing the spaceship to life Rapid fire bullets Reusing existing classes Adding the SoundManager class Adding the InputController class Drawing and moving the asteroids Scores and the HUD Adding control buttons Tally icons Life icons Declaring, initializing, and drawing the HUD objects Summary 11 Things That Go Bump – Part II Planning for collision detection Colliding with the border The first phase of border collision detection Colliding with an asteroid The crossing number The first phase and overview of asteroid collision detection The CollisionPackage class Adding collision packages to the objects and making them accessible Adding a collision package to the Bullet class Adding a collision package to the SpaceShip class Adding a collision package to the Asteroid class The CD class outline Implementing radius overlapping for asteroids and ships Implementing rectangle intersection for the border Performing the checks Helper methods Destroying a ship Destroying an asteroid Testing for collisions in update() Precise collision detection with the border Precise collision detection with an asteroid Finishing touches Summary Index CuuDuongThanCong.com Android Game Programming by Example CuuDuongThanCong.com Android Game Programming by Example Copyright © 2015 Packt Publishing All rights reserved No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews Every effort has been made in the preparation of this book to ensure the accuracy of the information presented However, the information contained in this book is sold without warranty, either express or implied Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals CuuDuongThanCong.com However, Packt Publishing cannot guarantee the accuracy of this information First published: June 2015 Production reference: 1250615 Published by Packt Publishing Ltd Livery Place 35 Livery Street Birmingham B3 2PB, UK ISBN 978-1-78528-012-2 www.packtpub.com CuuDuongThanCong.com Credits Author John Horton Reviewers Håvard Kindem José Rodriguez-Rivas Commissioning Editor Nadeem N Bagban Acquisition Editor Tushar Gupta Content Development Editor Siddhesh Salvi Technical Editor Prajakta Mhatre Copy Editor Charlotte Carneiro Project Coordinator Nidhi Joshi Proofreader Safis Editing Indexer Tejal Soni Production Coordinator CuuDuongThanCong.com (int) ((worldUnrotatedX - cp2.worldLocation.x) * sinAngle2 + (worldUnrotatedY - cp2.worldLocation.y) * cosAngle2); Here, we detect if the current vertex from either the ship or a bullet crosses the line formed by the current vertex pair of the asteroid If it does, we increment numCrosses // And now we can test the rotated point from cp1 against the // rotated points which form a side from cp2 if (((cp2.currentPoint.y > cp1.currentPoint.y) != (cp2.currentPoint2.y > cp1.currentPoint.y)) && (cp1.currentPoint.x < (cp2.currentPoint2.x cp2.currentPoint2.x) *(cp1.currentPoint.y cp2.currentPoint.y) / (cp2.currentPoint2.y cp2.currentPoint.y) + cp2.currentPoint.x)){ numCrosses++; } Finally, we use the modulus operator to determine if numCrosses is odd or even As discussed, we return true (collision) for odd and false (no collision) for even } } // So we have a collision? if (numCrosses % == 0) { // even number of crosses(outside asteroid) collided = false; } else { // odd number of crosses(inside asteroid) collided = true; } }// end if You can now fly your ship right up to the asteroids and only get hit when it really looks like you should Refer to the following screenshot: CuuDuongThanCong.com Now, all of our collision detection and our Asteroids simulator game is done! CuuDuongThanCong.com Finishing touches We can continue to improve our game For example, it wouldn't be too hard to spawn two or three smaller asteroids when the current asteroid is destroyed We just need an array to hold the smaller asteroids When we deactivate the regular asteroid, the array activates some previously instantiated smaller ones at the same location as the regular one We can then make some minor modifications to the way we count asteroids, and we will have a neat new feature The arcade classic, Asteroids, had a mean UFO that would turn up occasionally It would be simple to design a UFO shape from lines, and have it randomly proceed from left to right, or right to left, moving up and down a bit as well Finally, we can add a hyperspace button This is a kind of last resort for the player when they are sure that death is imminent Tap the hyperspace button and the ship will respawn in a random location We will just need to add a button to the array in the InputController class and a call to a new, simple randomHyperspaceJUmp method in the Ship class We can also add Google Play achievements and leaderboards and then publish the game If you publish a game that uses OpenGL, you need to add this declaration to the AndroidManifest.xml file: Try and add some of the improvements we talked about and perhaps some more of your own If you publish your game or even if you don't, I would love to hear your ideas or see a link to your projects on gamecodeschool.com I think we are done! Buy Tablet PC online CuuDuongThanCong.com Summary I hope you have enjoyed our whirl-wind tour, making games for Android, and I hope you keep making lots of new games! CuuDuongThanCong.com Index A Android Activity lifecycle about / The Android Activity lifecycle reference link / The Android Activity lifecycle Android device Tappy Defender, debugging on / Debugging on an Android device Android Studio installing / Installing Android Studio URL / Installing Android Studio Android Studio file structure about / The Android Studio file structure asteroid collision detection, precising with / Precise collision detection with an asteroid Asteroid class collision package, adding to / Adding a collision package to the Asteroid class asteroid collision detection about / Colliding with an asteroid crossing number / The crossing number phase / The first phase and overview of asteroid collision detection overview / The first phase and overview of asteroid collision detection asteroids drawing / Drawing and moving the asteroids moving / Drawing and moving the asteroids Asteroids' Simulator game checks, performing / Performing the checks helper methods / Helper methods ship, destroying / Destroying a ship asteroid, destroying / Destroying an asteroid collisions, testing in update() / Testing for collisions in update() improving / Finishing touches asteroids simulator CuuDuongThanCong.com about / Asteroids simulator game controls / The game controls rules / Rules for the game asteroids simulator project about / Asteroids simulator features / Asteroids simulator B backstory, Tappy Defender game / Backstory enhanced draw method / The backstory BFXR URL / Generating the FX Bob functionality, adding to / Introducing Bob animating / Animating Bob border collision detection, precising with / Precise collision detection with the border border collision about / Colliding with the border border collision detection phase / The first phase of border collision detection Bullet class creating / Rapid fire bullets collision package, adding to / Adding a collision package to the Bullet class bullet collision detection about / Bullet collision detection C CD class about / The CD class outline radius overlapping, implementing for asteroid / Implementing radius overlapping for asteroids and ships CuuDuongThanCong.com radius overlapping, implementing for ships / Implementing radius overlapping for asteroids and ships rectangle, implementing for border / Implementing rectangle intersection for the border code structure, Tappy Defender game about / The game code structure Android Activity lifecycle / The Android Activity lifecycle collision detection about / Things that go bump – collision detection options / Collision detection options planning for / Planning for collision detection precising, with border / Precise collision detection with the border precising, with asteroid / Precise collision detection with an asteroid collision package adding, to objects / Adding collision packages to the objects and making them accessible access, providing to / Adding collision packages to the objects and making them accessible adding, to Bullet class / Adding a collision package to the Bullet class adding, to SpaceShip class / Adding a collision package to the SpaceShip class adding, to Asteroid class / Adding a collision package to the Asteroid class CollisionPackage class about / The CollisionPackage class control buttons, HUD object / Adding control buttons CopyOnWriteArrayList reference link / Ready aim fire crossing number algorithm about / The crossing number algorithm D design pattern, Tappy Defender game about / The design control / Control model / Model view / View reality check / Design pattern reality check CuuDuongThanCong.com development environment setting up / Setting up your development environment drone about / The drone E endianness reference link / Building an OpenGL-friendly, GameObject super class enemies building / Building the enemies designing / Designing the enemy spawning / Spawning the enemy update method, handling / Making the enemy think existing classes reusing / Reusing existing classes SoundManager class, adding / Adding the SoundManager class InputController class, adding / Adding the InputController class F 60 + FPS drawing at / Drawing at 60 + FPS fire tiles adding / Adding some fire tiles Flappy Bird apps, Google Play URL / The game mechanics flight, Tappy Defender game background, scrolling / The thrill of flight – scrolling the background fragment shader about / What is neat about Version 2? G CuuDuongThanCong.com game engine, Tappy Defender game upgrading / Upgrading the game engine platform activity / The platform activity layout, locking to landscape / Locking the layout to landscape PlatformView class / The PlatformView class game loop, Tappy Defender coding / Coding the game loop view, building / Building the view new class, creating for view / Creating a new class for the view, What we did class code, structuring / Structuring the class code game activity / The game activity GameObject class about / The GameObject class functionality, adding to / Bringing the spaceship to life GameObject super class building / Building an OpenGL-friendly, GameObject super class GL Shader Language (GLSL) / What is neat about Version 2? GLSL about / Managing simple shaders guard about / The guard route, generating for / The guard H home screen, Tappy Defender game building / Building the home screen project, creating / Creating the project UI, building / Building the home screen UI functionality, coding / Coding the functionality GameActivity, creating / Creating GameActivity AndroidManifest.xml file, configuring / Configuring the AndroidManifest.xml file, What we did HUD displaying / Displaying a HUD HUD objects CuuDuongThanCong.com about / Scores and the HUD control buttons, adding / Adding control buttons tally icons / Tally icons life icons / Life icons declaring / Declaring, initializing, and drawing the HUD objects initializing / Declaring, initializing, and drawing the HUD objects drawing / Declaring, initializing, and drawing the HUD objects I identity matrix reference link / Building an OpenGL-friendly, GameObject super class InputController class adding / Adding the InputController class installing JDK / Installing the JDK Android Studio / Installing Android Studio iteration, Tappy Defender game about / Iteration enemy graphics, adding / Multiple different enemy graphics exercise, in balance / An exercise in balance format time / Format time back button, handling / Handle the back button J Java SE Downloads URL / Installing the JDK JDK installing / Installing the JDK L level designs, Platformer game about / The level designs cave / The cave CuuDuongThanCong.com city / The city forest / The forest mountains / The mountains HUD / The HUD life icons, HUD objects about / Life icons M machine gun building, with variable rate of fire / Ready aim fire MachineGun class implementing / Ready aim fire matrices about / How we will use OpenGL ES 2? reference link / Building an OpenGL-friendly, GameObject super class mechanics, Tappy Defender game / The game mechanics, The game mechanics MotionEvent class reference link / Detecting touches multiphase collision detection about / Multiphase collision detection O objects collision package, adding to / Adding collision packages to the objects and making them accessible OpenGL working / Why use it and how does it work? reasons, for using / Why use it and how does it work? OpenGL ES about / What is neat about Version 2? OpenGL ES about / Introducing OpenGL ES CuuDuongThanCong.com benefits / What is neat about Version 2? using / How we will use OpenGL ES 2? preparing / Preparing OpenGL ES layout, locking to landscape / Locking the layout to landscape Activity class / Activity view / The view class, used for managing game / A class to manage our game simple shaders, managing / Managing simple shaders renderer / The game's main loop – the renderer optimizations, collision detection multiple hitboxes / Multiple hitboxes neighbour checking / Neighbor checking options, collision detection rectangle intersection / Rectangle intersection radius overlapping / Radius overlapping crossing number algorithm / The crossing number algorithm P persistence, Tappy Defender game adding / Adding persistence pickups about / Pickups collecting / Pickups drone / The drone guard / The guard Platformer game aesthetic props, adding / Eye candy new platform tiles, adding / The new platform tiles new scenery objects, adding / The new scenery objects scrolling parallax backgrounds / Scrolling parallax backgrounds pause menu, with moveable viewport / Pause menu with moveable viewport levels / Levels and game rules game rules / Levels and game rules travelling, between levels / Traveling between levels level designs / The level designs PlatformView class about / The PlatformView class CuuDuongThanCong.com basic structure / The basic structure of PlatformView view, through viewport / The view through a viewport levels, creating / Creating the levels enhanced update method / The enhanced update method enhanced draw method / The enhanced draw method player input about / Player input PlayerShip object about / The PlayerShip object drawing / Drawing PlayerShip Q quadrants reference link / Bringing the spaceship to life R radius overlapping about / Radius overlapping rectangle intersection about / Rectangle intersection route generating, for guard / The guard rules, Tappy Defender game / Rules for the game, Rules for the game implementing / Implementing the rules S scene, Tappy Defender game drawing / Drawing the scene plotting / Plotting and drawing PlayerShip, drawing / Drawing PlayerShip Canvas object / The Canvas and Paint objects Paint object / The Canvas and Paint objects CuuDuongThanCong.com frame rate, controlling / Controlling the frame rate scores about / Scores and the HUD shader program about / What is neat about Version 2? sound FX, Tappy Defender game adding / Adding sound FX generating / Generating the FX SoundPool class, used for playing sounds / The SoundPool class coding / Coding the sound FX SoundManager class about / The SoundManager class adding / Adding the SoundManager class spaceship about / The spaceship bringing, to life / Bringing the spaceship to life spaceship, controlling about / Controlling the spaceship touches, detecting / Detecting touches boosters, adding to spaceship / Adding boosters to the spaceship screen resolution, detecting / Detecting the screen resolution SpaceShip class collision package, adding to / Adding a collision package to the SpaceShip class Star class update method, adding / Twinkling stars static game border drawing / Drawing a static game border T tally icons, HUD object / Tally icons Tappy Defender game planning / Planning the first game backstory / Backstory, The backstory CuuDuongThanCong.com mechanics / The game mechanics, The game mechanics rules / Rules for the game, Rules for the game design pattern / The design code structure / The game code structure home screen, building / Building the home screen game loop, coding / Coding the game loop PlayerShip object / The PlayerShip object scene, drawing / Drawing the scene deploying / Deploying the game debugging, on Android device / Debugging on an Android device best options / Best options for Tappy Defender rules, implementing / Implementing the rules ending / Ending the game restarting / Restarting the game sound FX, adding / Adding sound FX sound FX, generating / Generating the FX persistence, adding / Adding persistence iteration / Iteration finishing / The finished game about / The game game engine, upgrading / Upgrading the game engine Tappy Defender project about / Tappy Defender tough retro platformer project about / Tough retro platformer features / Tough retro platformer V vertex shader about / What is neat about Version 2? CuuDuongThanCong.com ... touches Summary Index CuuDuongThanCong.com Android Game Programming by Example CuuDuongThanCong.com Android Game Programming by Example Copyright © 2015 Packt Publishing All rights reserved No... Handle the back button The finished game Summary Platformer – Upgrading the Game Engine The game The backstory The game mechanics Rules for the game Upgrading the game engine The platform activity... suited for existing Android or Java programmers, who want to adapt their skills to make exciting Android games The book is also for readers who might have no Android, game programming, or even

Ngày đăng: 29/08/2020, 16:35

Mục lục

  • Android Game Programming by Example

    • Table of Contents

    • Android Game Programming by Example

    • www.PacktPub.com

      • Support files, eBooks, discount offers, and more

        • Why subscribe?

        • Free access for Packt account holders

        • Preface

          • What this book covers

          • What you need for this book

          • Who this book is for

          • Customer support

            • Downloading the example code

            • Downloading the color images of this book

            • 1. Player 1 UP

              • A closer look at the games

                • Tappy Defender

                • Setting up your development environment

                  • Installing the JDK

                  • 2. Tappy Defender – First Step

                    • Planning the first game

                      • Backstory

                      • Rules for the game

                      • Design pattern reality check

                      • The game code structure

                        • The Android Activity lifecycle

                        • The Android Studio file structure

                        • Building the home screen

                          • Creating the project

                            • What we did

                            • Building the home screen UI

                              • What we did

                              • Creating GameActivity

                                • What we did

                                • Configuring the AndroidManifest.xml file

                                  • What we did

Tài liệu cùng người dùng

Tài liệu liên quan