ebook img

Beginning Java Game Programming Second Edition PDF

359 Pages·2007·43.758 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Beginning Java Game Programming Second Edition

BEGINNING JAVA™ GAME PROGRAMMING 1 WzECOND EDITION -~l ARBOUR ©2008ThomsonCourseTeclmology.adivisionofThomsonLearning PublisherandGeneralManager) Inc. All rightsreserved. No partofthisbook may be reproduced or ThomsonCourseTechnologyPTR: transmitted in any form orby any means, electronicor mechanical. StacyL.Hiquet includingphotocopying, recording, or byany informationstorageor retrieval system without written permission from Thomson Course AssociateDirectorofMarketing: TeclmologyPTR,exceptfortheinclusionofbriefquotationsinareview. SarahO'Donnell TheThomsonCourseTechnologyPTRlogoandrelatedtradedressare ManagerofEditorialServices: trademarksofThomson CourseTechnology.adivisionofThomson HeatherTalbot LearningInc.,andmaynotbeusedwithoutwrittenpermission. MarketingManager: JavaisatrademarkofSunMicrosystems,Inc.intheUnitedStatesand JordanCasey olher countries. All other trademarks are the property of their respectiveowners. SeniorAcquisitionsEditor: Jlllporfclllt:ThomsonCourseTechnologyPTRcannotprovidesofhvare EmiSmith support. Please contact the appropriate software manufacturer's technicalsupportlineorWebsiteforassistance. ProjectEditor/CopyEditor: CathleenD.Small Thomson Course Technology PTRand the author have attempted throughout this book to distinguish proprietary trademarks from TechnicalReviewer: descriptive terms by following the capitalization style used bythe DustinClingman manufacturer. PTREditorialServicesCoordinator: Information contained in thisbookhasbeenobtained byThomson ErinJohnson CourseTechnolo!:,'YPTRfromsourcesbelievedtobereliable.However, becauseofthepossibilityofhumanormechanicalerrorbyoursources, InteriorLayoutTech: ThomsonCourseTechnologyPTR,orothers,thePublisherdoesnot rccMacmillanInc. guaranteetheaccuracy,adequacy,orcompletenessofanyinformation and is not responsible for any errors Of omissions or the results CoverDesigner: obtainedfromuseofsuchinformation.Readersshouldbeparticularly MikeTanamachi awafeofthefactthattheInternetisanever-changingentity.Somefacts mayhavechangedsincethisbookwenttopress. CD-ROMProducer: BrandonPenticuff Educational facilities, companies, and organizations interested in multiplecopiesorlicensingofthisbookshouldcontactthePublisher Indexer: forquantitydiscountinformation.Trainingmanuals,CD-ROMs,and KatherineStimson portionsofthisbookarealsoavailableindividuallyorcanbetailored forspecificneeds. Proofreader: KateShoup ISBN-l0:1-59863-476-3 ISBN-13:978-1-59863-476-1 LibraryofCongressCatalogCardNumber:2007938236 PrintedintheUnitedStatesofAmerica 0809 10II 12TW10987654321 ThomsonCourseTechnologyPTR, THOMSON adivisionofThomsonLe:uningInc. • ,~ 25ThomsonPlace COURSETECHNOLOGY Boston,MA022\0 Professional_Technical.Reference http://www.courseptr.com ForKaitlyn Faye ACKNOWLEDGMENTS IthankGodforthemanyopportunitiesthathavecomemywaythisyear,suchas the chance to write this book, and for the apparent talent needed to make somethingtangibleofthese opportunities. Iam gratefulto myfamilyfor their ongoingencouragement:Jennifer,Jeremiah,Kayleigh, Kaitlyn,Kourtney,Mom andDad,GrandmaCremeen, DaveandBarbara,myextendedfamilyatVision Baptist Church, and Pastor Michael Perham and his family-Jennifer, Ashley, Bryce,andSage-whohavebeensuchablessingthispastyear. Thank you to thestudents, faculty, and staffatUATfor contributingto such a wonderfullycreativeenvironment for learning. Iwould like to thank the Alpha Squadteam,whohadsomeinfluenceonthisbook(andevenhelpedtosolveafew codingproblemswithGalacticWar):RoyEvans,StewartJohnston,PeterPascoal, TravisEddlemon,DanielMuller,DanielStirk,PatrickCissarz,DavidCoddington, MarcKirschner,JeffreyWoodard,JonathanAllmen, Levi Bath,DouglasCannon, Joshua Gertz, Justin Hair, Adam Knight, Eric Lacerna, Daryl Lynch, and Kevin McCusker; and the faculty sponsors: Rebecca Whitehead, Michael Eilers, and Arnaud Ehgner. I also owe my thanks to students Mark Walker and Andrew HawkenforintroducingmetotheangularvelocitycodeusedinGalacticWar. Iamalsoverythankfulfortheartworkfeaturedinthisbook,providedbyAriFeldman (www.flyingyogi.com) and Reiner Prokein (www.reinerstiJeset.de). Without their wonderfulgraphics,GalacticWarwouldhavefeaturedprogrammerart(cringen. I offer my sincere thanks to the editors at Course Technology PTR and the freelanceeditorswhoputthisbooktogether:EmiSmith,CathleenSmall,Dustin Clingman,andKateShoup. ABOUT THE AUTHOR Jonathan S. Harbour is an Associate Professor ofGame Development at the University of Advancing Technology in Tempe, Arizona. His current game project,Starflight:TheLostColony(www.starflightgame.com).willbereleasedin late2007.HelivesinArizonawithhiswife,Jennifer,andfourchildren:Jeremiah, Kayleigh, Kaitlyn,and Kourtney. Hecanbereachedatwww.jharbouLcom. CONTENTS Introduction .. xiii PARTI JAVAFOR BEGINNERS . Chapter1 Getting Startedwith Java ................. 3 JavaandtheWeb.. 3 StudyingtheMarket .. 4 DesignRules.. ......•.......•.......•.......•. 5 TheCasualGamesMarket. 6 NoManualRequired .. .....•......••.... 7 CasualGarners.. ...........••................. 8 CasualGames. 8 InstallingandConfiguringJava. .....••.. 9 InstallingJava ..........••.. 10 ConfiguringJava 11 JavaVersion Numbers ......... 14 YourFirstJavaProgram .. ......•.. 1S JavaApplication..... 15 JavaApplet...... .....••.. 17 WhatYouHaveLearned. ......•.. 20 Review Questions. 21 OnYourOwn. 22 Exercise1.. 22 Exercise2.....................••................ 22 vi Contents vii Chapter2 Java Programming Essentials ..................... 23 JavaApplets... . . 24 WebServerTechnologyExplained 24 HostingJavaApplets .. ............•......•...... 24 CompilingJavaCode .. 25 TheJavaLanguage.. 25 JavaDataTypes.... ........•.................... 27 TheEssenceofClass. 37 ThernainFunction.. 39 Object-OrientedProgramming . 40 WhatYou HaveLearned.... ........••.. 46 ReviewQuestions. 47 OnYourOwn. 48 Exercise 1. 48 Exercise2.. 48 Chapter3 Creating YourFirstJavaGame . 49 AbouttheGameProject. 49 CreatingtheGame... 52 CreatingtheProject.. 53 TheBaseVectorshapeClass. 53 TheShipClass 55 TheBulletClass . 56 TheAsteroidClass...................•. 57 TheMainSourceCodeFile . 58 AppletinitO Event. ........•. 59 AppletupdateOEvent ... .......•. 60 DrawingthePlayer'sShip.......••.............•..... 61 DrawingtheBullets. 61 DrawingtheAsteroids. 62 ScreenRefresh....... ......•.•.....••..... 63 ThreadEventsandtheGameLoop . 63 GameLoopUpdate .......•. 64 UpdatingtheShip...........••.......... ..•...... 65 UpdatingtheBullets .. .....••... 65 UpdatingtheAsteroids 66 TestingforCollisions. 68 Keyboard Events 69 CalculatingRealisticMotion. 71 viii Contents WhatYouHaveLearned. 73 ReviewQuestions 73 OnYourOwn. ......•........ 74 Exercise1 . 74 Exercise2. 74 PARTII Java Game Programming .. ...................... 75 Chapter4 Vector-Based Graphics .......................... 77 ProgrammingVectorGraphics . 77 WorkingwithShapes.. 78 WorkingwithPolygons 80 Rotating andScalingShapes . 83 WhatYou HaveLearned. 86 ReviewQuestions. . . 87 OnYourOwn. .•........•........ 87 Exercise 1.. 87 Exercise2 . 88 Chapter5 Bitmap-BasedGraphics . 89 ProgrammingBitmappedGraphics...............•....... 89 Loadingand Drawing Images. . . 90 ApplyingTransformstoImages. 92 Transparency..... .....•.......•.•... 94 OpaqueImages ......••.......... 9S TransparentImages ...............••. 97 Working SomeMaskingMagic.........•... 99 WhatYou HaveLearned. 104 ReviewQuestions. 104 OnYourOwn. 104 Exercise 1. lOS Exercise2. lOS Chapter6 SimpleSprites . 107 ProgrammingSimpleSprites......... . . 107 ThePoint2DClass. 109 BasicGameEntities 110 TheImageEntityClass . 111 CreatingaReusableSpriteClass 114 Contents ix CollisionTesting...........•. 114 SpriteClassSourceCode......•.............••....... 114 TestingtheSpriteClass. ......••......••....... 118 WhatYouHaveLearned. 121 ReviewQuestions... 121 OnYourOwn.. .......•......••.....••....... 122 Exercise 1........... 122 Exercise2. 122 Chapter7 SpriteAnimation . 123 SpriteAnimation... 123 AnimationTechniques 124 Drawing Individual Frames 124 KeepingTrackofAnimationFrames. 126 TestingSpriteAnimation 127 EncapsulatingSpriteAnimation inaClass........•....... 132 TestingtheAnimatedSpriteClass. 13S WhatYouHaveLearned.. 137 ReviewQuestions... . . 138 OnYourOwn. 138 Exercise1.. 138 Exercise2.... 139 Chapter8 Keyboard and MouseInput . 141 ListeningtotheUser . 141 KeyboardInput... 142 ListeningforKeyboardEvents 142 TestingKeyboardInput ... 143 MouseInput... 145 ReadingMouseMotion 146 DetectingMouseButtons. 147 Testing MouseInput 148 WhatYou HaveLearned.................•............ 152 ReviewQuestions.. 153 OnYourOwn.. 153 Exercise 1 . 153 Exercise2... 154 Chapter9 Sound Effectsand Music 1SS PlayingDigitalSampleFiles . 155

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.