BEGINNING JAVA™ GAME PROGRAMMING WzECOND EDITION
1-~l
ARBOUR
© 2008 Thomson Course Teclmology. a division of Thomso...
640 downloads
2941 Views
44MB Size
Report
This content was uploaded by our users and we assume good faith they have the permission to share this book. If you own the copyright to this book and it is wrongfully on our website, we offer a simple DMCA procedure to remove your content from our site. Start by pressing the button below!
Report copyright / DMCA form
BEGINNING JAVA™ GAME PROGRAMMING WzECOND EDITION
1-~l
ARBOUR
© 2008 Thomson Course Teclmology. a division of Thomson Learni ng Inc. All rights reserved. No pa rt of this book may be reproduced or transmitted in any form or by any means, electronic or mechanical. includi ng photocopying, record ing, or by any informa tion storage or retrieval system without written permission from Thomson Course Teclmology PTR, except for the inclusion of brief quotations in a review.
Publisher and General Manager) Thomson Course Technology PTR: Stacy L. Hiquet
The Thomson Course Technology PTR logo and related trade dress are trademarks of Thomson Course Technology. a div ision of Tho mso n Learning Inc., and may not be used without written permission.
Manager of Editorial Services: Heather Talbot
Java is a trademark of Sun Mic rosystems, Inc. in the Un ited States and olher countries. All other trademarks are the property of their respective ow ners. Jlllporfclllt: Thomson Course Techn ology PTR canno t provide sofhvare
support. Please co ntact the appropriate softwa re manufacturer's techni cal support lin e or Web site for assistance. Thomson Course Technology PTR and the author have attempted throughout this book to distinguish proprietary trademarks from descriptive terms by following the cap italization style used by the manufacturer. Information contained in this book has been obta ined by Thomson Co urse Technolo!:,'Y PTR from so urces believed to be reliable. However, because of the possibil ity of human or mecha nical error by our sources, Thomson Course Technology PTR, or othe rs, the Publisher does not guarantee the accuracy, adequacy, or completeness of any information and is not responsible for any erro rs Of omissions or the results obtained from use of such information. Readers should be particularly awafe of the fact that the Internet is an ever-cha nging entity. Some facts may have changed since this book went to press. Educatio nal facilities, compa nies, and organizations interested in multiple copies or licensing of this book should contact the Publisher for quantity discount information. Tra inin g ma nuals, CD-ROMs, and portions of this book are also available individua lly or can be tailored for spec ific needs. ISBN-l0: 1-59863-476-3 ISBN - 13: 978 - 1-59863-476-1 Library of Co ngress Catalog Card Number: 2007938236 Printed in the Un ited States of America 0809 10 II 12 TW 1098765432 1
Thomson Course Technology PTR, THOMSON a division of Thomson Le:uning Inc. • ,~ 25 Thomson Place COURSE TECHNOLOGY Boston, MA 022\0
Professional_ Technical. Reference
http://www.courseptr.com
Associate Director of Marketing: Sa rah O'Donnell
Marketing Manager: Jordan Casey Senior Acquisitions Editor: Emi Sm ith Project Editor/Copy Editor: Cath leen D. Small Technical Reviewer: Dusti n Clingman PTR Editorial Services Coordinator: Erin Johnson Interior Layout Tech: rcc Macm illan Inc. Cover Designer: Mike Ta namachi CD-ROM Producer: Brando n Penticuff Indexer: Katherine Stimson Proofreader: Kate Shoup
For Kaitlyn Faye
ACKNOWLEDGMENTS
I thank God for the many opportunities that have come my way this year, such as the chance to write this book, and for the apparent talent needed to make something tangible of these opportunities. I am grateful to my family for their ongoing encouragement: Jennifer, Jeremiah, Kayleigh, Kaitlyn, Kourtney, Mom and Dad, Grandma Cremeen, Dave and Barbara, my extended family at Vision Baptist Church, and Pastor Michael Perham and his family-Jennifer, Ashley, Bryce, and Sage-who have been such a blessing this past year. Thank you to the students, faculty, and staff at UAT for contributing to such a wonderfully creative environment for learning. I would like to thank the Alpha Squad team, who had some influence on this book (and even helped to solve a few coding problems with Galactic War): Roy Evans, Stewart Johnston, Peter Pascoal, Travis Eddlemon, Daniel Muller, Daniel Stirk, Patrick Cissarz, David Coddington, Marc Kirschner, Jeffrey Woodard, Jonathan Allmen, Levi Bath, Douglas Cannon, 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 Hawken for introducing me to the angular velocity code used in Galactic War. I am also very thankful for the artwork featured in this book, provided by Ari Feldman (www.flyingyogi.com) and Reiner Prokein (www.reinerstiJeset.de). Without their wonderful graphics, Galactic War would have featured programmer art (cringen. I offer my sincere thanks to the editors at Course Technology PTR and the freelance editors who put this book together: Emi Smith, Cathleen Small, Dustin Clingman, and Kate Shoup.
ABOUT THE AUTHOR
Jonathan S. Harbour is an Associate Professor of Game Development at the University of Advancing Technology in Tempe, Arizona. His current game project, Starflight: The Lost Colony (www.starflightgame.com). will be released in late 2007. He lives in Arizona with his wife, Jennifer, and four children: Jeremiah, Kayleigh, Kaitlyn, and Kourtney. He can be reached at www.jharbouLcom.
CONTENTS
PART I Chapter 1
Introduction ..
xiii
JAVA FOR BEGINNERS
.
Getting Started with Java
. . . . . . . . . . . . . . . .. 3
Java and the Web . . Studying the Market . . Design Rules. . . . . . . . •. . . . . . . •. . . . . . . •. . . . . . . •. The Casual Games Market.
3 4
5 6 No Manual Required . . .. . ..•.. . .. .••.. . . 7 Casual Garners. . .. ... .. . ... ••. .. ... .. .. . ... .. . 8 Casual Games. 8 Installing and Configuring Java. .. ...••.. 9 Installing Java . . . . . . . . . . • • .. 10 Configuring Java 11 Java Version Numbers ........ . 14 Your Fi rst Java Program . . . . . . . . • .. 1S Java Application. . . . . 15 Java Applet . . . . . . .. .. .••. . 17 What You Have Learned. ......•.. 20 Rev iew Questions . 21 On Your Own. 22 22 Exercise 1 . . Exercise 2 .... . .. . ... . . .. . .... .• •. . . . .. . . . .. .. . . . 22
vi
Contents
Chapter 2
Java Programming Essentials . . . . . . . . . . . . . . . . . . . ..
23
Java Applets . . .
24
.
.
Web Server Technology Explained Hosting Java Applets . .
24
. ... . ... .. . .• .. ... . • . .. . . .
The Java Language .. Java Data Types. . . .
. . . .. . . . • . .. . .. . ... . . . . . . .. ..
The Essence of Class. The rnain Function . .
27
37
39
Object-Oriented Programming What You Have Learned . . . .
. . . ...... • • . .
Review Questions.
On Your Own. Exercise 1. Exercise 2. .
Chapter 3
24
25 25
Compiling Java Code . .
40 46 47 48 48 48
. 49
Creating Your First Java Game About the Game Project.
49
Creating the Game . ..
52
Creating the Project ..
53
The BaseVectorshape Class.
53 55
The Ship Class The Bullet Class
.
56
The Asteroid Class ................... • .
57
The Main Source Code File Applet initO Event. . ..... . . • . Applet updateO Event . . .
.
. ..... . •.
Drawing the Player's Ship . ... . . .• • . ... . .... .. . . • . . . .. Draw ing the Bullets. Draw ing the Asteroids. Screen Refresh. . . . . . .
. . . .. . •. • .... . • • . . ...
Thread Events and the Game Loop Game Loop Update ... . . ..• .
.
Updating the Ship. . . . . . . . . . . • • . . . . . . . . .. Updating the Bullets . .
. ... . • •. . .
Updating the Asteroids Testing for Collisions. Keyboard Events Calculating Realistic Motion .
. . • . .. . ..
58 59 60 61 61 62 63 63 64 65 65 66 68 69 71
vii
viii
Contents
What You Have Learned. Review Questions On You r Own.
73 73 74 74 74
. . . .. . • .. .. . . . .
Exercise 1
.
Exercise 2.
PART II
Java Game Programming .. . . . . . . . . . . . . . . . . . . . . .. 75
Chapter 4
Vector-Based Graphics . . . . . . . . . . . . . . . . . . . . . . . . .. Programming Vector Graphics
.
Working w ith Shapes ..
80 83 86
.
What You Have Learned. . . . • . ..... . .•... . ... .
87 87
Exercise 1. . Exercise 2
Chapter 5
87
.
88
.
89
Programming Bitmapped Graphics ... . ..... . .... .• .. . .. . . Load ing and Drawing Images. . .
89 90 92 94 9S 97 99 104 104 104
Bitmap-Based Graphics
Applying Transforms to Images. Transparency. . . . .
. ....• . . .... .•. • . . .
Opaque Images ......••...... .. .. Transparent Images ............... •• . Working Some Masking Magic .... . .. . .• ... What You Have Learned. Review Questions . On Your Own.
Chapter 6
77 78
Working w ith Polygons Rotating and Scaling Shapes Review Questions. On Your Own.
77
Exe rcise 1.
lOS
Exercise 2.
lOS
Simple Sprites
.
Programm ing Simple Sprites. . . . . . . . . The Point2D Class. Basic Game Entities The ImageEntity Class Creating a Reusable Sprite Class
.
.
.
107 107 109 110 111 114
Contents
Collision Testing. . . . . . . . . . . • .
114
Sprite Class Source Code . . . . . . • . . . . . . . . . . . . . • • . . . . . . . Testing the Sprite Class. What You Have Learned.
118 121
. . . . . . . •. . . . . . •• . . . . . •• . . . . . ..
122
Review Questions. . .
121
On Your Own . .
Exercise 1. . . . . . . . . . . Exercise 2.
Chapter 7
122 122
Sprite Animation
.
123
Sprite Animation ...
123 124
Animation Techniqu es Drawing Individual Frames
124 126
Keeping Track of Animation Frames. Testing Sprite Animation Encapsulating Sprite An imation in a Class ..... .. .•. . . . . .. Testing the AnimatedSprite Class. What You Have Learned . . Review Questions. . . On Your Own.
.
.
Exercise 1..
Exercise 2.. ..
Chapter 8
Keyboard and Mouse Input Listening to the User
.
127 132 13S 137 138 138 138 139
141
.
141 142
Listening for Keyboard Events
142
Keyboard Input... Testing Keyboard Input . . . Mouse Input ...
143
Reading Mouse Motion
146 147
145
Detecting Mouse Buttons. Testing Mouse Input What You Have Learned . .. ... . . ... .. . . ..•. ... ... . . . . .
148
Review Questions . .
153 153
On Your Own . . Exercise 1
153
154
Sound Effects and Music Playing Digital Sample Files
152
.
Exercise 2...
Chapter 9
114
. . . . . . • • . . . . . . • • . . . . . ..
1SS .
155
ix
x
Contents
Getting Started with Java Sound ... . . .•.... . .• .
157
Playing Sounds ..
164 166
Playing MIDI Sequence Files .. Loading a MIDI File
.
166
Playing Music. ....... . .. ....•..... •. ......•.. .
167
Reusable Classes. The SoundClip Class.
169 170
The MidiSequence Class ......•• . . .. .•.. . ... .•....... What You Have Learned ... Review Questions. On Your Own. Exercise 1
.
Exercise 2
Chapter 10
.
172 175 175 176 176 176
Timing and the Game loop. . . . . . . . . . . . . . . . . . . . .
177
The Potency of a Game Loop . .
178
A Simple Loop . . .
..........
178
Overriding Some Default Applet Behaviors. . . . . . . . • . . . . ..
181
Feeling Loopy . . . . . . . . . . . . . . . Recovering Long·Lost Applet Methods. . .
182 183
Stepping Up to Threads
184
Starting and Stopping the Thread. . The ThreadedLoop Program. .
184
Examining Multithreading
189
What You Have Learned.
185
........•...
... ...•.. .. .. ... .. .. .• • .. ..
Review Questions . . . .
189
.......•.
190
.......•. On Your Own. Exercise 1. . . . . . . . . . . . . . . . . • . . . . . . . . . . . . . . . . . . . . .. Exercise 2. . . . . . . . • • . . . . . . . . . .
PART III
THE GALAalC WAR PROJEa
Chapter 11
Galactic War: From Vectors to Bitmaps
.......•....
190 190
191 .
Improving the Game
.
Generalizing the Vector Classes
193 193 194
The Main Source Code File: GalacticWar.java
197
What You Have Learned .
204
Review Questions .......... .. . •• ... . ... On Your Own. . .
190
.
.
204 204
Contents
Chapter 12
Galactic War: Sprites and Collision Boxes Creating the Project. . . . . . . . . . . . . .
. . ... .. . . •.
207
The Galactic War Bitmaps. The New and Improved Source Code
Chapter 13
208 210
What You Have Learned.
224
Review Questions .
225
On Your Own . . . .
225
Galactic War: Squashed by Space Rocks
.
227
What You Have Learned . ......... . •. . . . . . •. . . .. ... . ..
233
Review Questions.
233
On Your Own.
234
Galactic War: Entity Management
.
Adjusting to Event-Driven Programming
235 236
Exp loring the Class Library . .
236
Building the New Game Class .............. ...... •. ..
237
Enhancing Galactic War. .... .. •.•.. .. Exploring the New Galactic War Source Code ...
249
What You Have Learned
273
Review Questions. . . . . .
. .
Galactic War: Finishing the Game Let's Talk about Powerups . Ship and Bonus-Point Powerups . . Weapon Upgrades. Enhancing Galactic War.
249
.
274 274
.
275
. . •.. ... . ••... .. .• . . . . ..•. . .
On Your Own..
Chapter 15
227
. ....•....
Being Civilized about Collisions.
Chapter 14
207
. . . . ..•. .... .. . .
275 276 276
. . . . .. .. . . .•. .. . . .• .... ..•...
281
New Sprite Types ...
281
New Game States.
281
New Sprite Images. . . . . . . . . . . . . Health/Shield Meters, Score, Firepower, and
.
282
Game State Variables.
283
New Input Keys . . ..
283
Sound and Music Objects . . .. . . . . . . . . . .. •• .... ....... Loading Media Files. . . ... ... . ... . • . ..
284 284
Game State Issue-Resetting the Game.
287
Detecting the Game-Over State .... . . . . .. • . . . .. .•. . .
288
Screen Refresh Updates. .
289
. ....• .. . . ..
xi
xii
Contents
Preparing to End .. Updating New Sprites. Grabbing Powerups. . .. . . . .. .... ... ..• . . ... ...... New Input Keys. Spawning Powerups . . . Making the Shield Work. . Making Use of Weapon Upgrade Powerups ..
.
Ta llying the Score What You Have Learned . . .. . .... . .• . . .. ... . .. . . . .• . .. Review Questions On Your Own
Chapter 16
. .
Galactic War: Web Deployment
.
Packaging an Applet in a Java Archive (JAR) Using the jar.exe Program Packag ing Galactic War in a Java Archive . . Creating an HTML Host File for Your Applet ... . . . • . . A Simple HTML File. .
. ... ..... . . ..•.
Testing the Deployed Applet Game . What You Have Learned. . ........ ... . . .• • . . .. ... Review Questions. Epilogue
. ...... . •. .
Appendix A: Chapter Quiz Answers Index
291 292 294 296 299 301 302 30S 306 306 307
309 309 310 312 315 315 317 319 3 19 320
321 337
INTRODUCTION
This book willicach you how to write JaVil games that will run as applcts in a wcb bT'mt..a. l"hc goal is to &vriop gal"l'lCS ror the casual gal'DC market. Gal'DC pro· gramming is a challmging subject that is not jusl difficult 10 IfU.SItt-it is difficuh just to get started. This book takes aW3Y somc or thc m)')tc-ry or gamc prog11lmming by expbining each step along the w~y, rrom one chapler to the next I assume Ihat you havea litlle la\"a programming experience, but c\"en ir you hal'C I"IeI'tt usal )aVil herorc. you should Ix- ablc 10 k ~ Oue 10 secunl\' lie system on a user's PC Iibo a J.wa applic.allOn,
~ 10
",ftw~.
Becaust this book is dedicale
CaS
Exercise 2 See whether rou can figurc OUI "here System,oul.prinlln oUlput goes whcn run from an applel project, SIllCC lhe It"xl is nOl displayed ill tht" applt"1 window.
CHAPTER
2
.JAVA PROGRAMMING ESSENTIALS
/3V:l is a matuu programming 13ngu3ge thai offers many good features 3nd Q.pabilitiC$ that make it p Method
Description
contains "odsWllh "QU' 1S 19nOreC,se
Roltm< _
len~tn
replace ld.
ReIlJms _
if .... a new class in your program. I'm nQt lalking about typing in a new claM, but when a class is instan· tiated into an object at runtime. When a new class is created (with the new operator). the class dmnition is wed to construct an object, ~ where the keyword comes in herr. 111eO t return tn9lne: )
public .old ICUll9lntl Strl ng IItWt1lglnt) I tllglnt· ~Int: ptlbllc Int getTowlngCipiclty() I return towlngcipaclty: )
I*bllc void setTowingCipiclty{lnt ulue) ( towtngcapiClty· ..lue:
Now Itt's make some changes to the main source code in the SimpleC1ass.java file. This is the part of the program that consumes, o r uSC$, the vehicle and truck classes. Here is the complete listing: 101portjna,lang,": 1.port java.a ppl et,"; 1.port jiva,a"t. "; publlc clUS SI~leChss utends ....pplet 1 vehlcle car; truck llthtntng; ptIbllC void InitO t lIinl thl he i vehicle object Cir - new vehiclel); Clr.utMahl ·ford· ): clr.seU~heeh(4):
Illnltlallze. tlvck object "II", I constructor llthtnlng - new trvcuoford SVYo. · f ,ISO lIthtnlng·. ·S,4l Triton '8 ' . 7700): )
4S
46
Chapter 2 • Java Progr
I ha"e added the truck class to the Simpll"Class program, which is where the vehtcle class may also be found. You can open the Simpll"Class project from Ihe CD-HOM in the \sources\chapterQ2 folder. Figu re 2.3 shows the oulpul from the current version of the program up to lhis point.
What You Have Learned This chaplCr providt."d an o,'ervi...w of th... b"sks of Jav" programming. You le"rnt."d about th... differences between a Java :JpplicatiOl1 "nd" J"v" applet, and how to write programs of e"ch type and then compile "nd run them. You kMl1ed the basics of object-orienled programming "nd many other J"va programming issues th"t will be hdpfuJ in later ch"pters. Sp«ific:Jlly, this dmpter co\"ered: • How to write a Jav" application • How 10 write" J"va applet • How 10 compile a Java program
Re~jew
-..........
Questions
_,_..
c.......,. ... ~
' ..........."
".
'"
,''''''''_'',>0''''_ ..... ,
,,_ 10. What progr:amming language was lava OOSW o n1
On Your Own Use Ih~ follo...;ng aercises to test your grasp of th~ material chapter.
CO\~
in this
inh~rited class 10
try out
Exercise 1 Writt your own la"" class and then use it to almd an tht concepts of inhC'ritana and mapsulllion.
Exercise 2 Modify your new dass by addingsomc methods Ihat dcmonstratc the concepl of polymorphism by wriling sever-II versionsofth~ SOIm~ mdhod with different sets of panmders.
CHAPTER 3
CREATING YOUR FIRST .JAVA GAME
This chapter will give you a glimpse of whal's coming in the ne:xt few chapters while teaching you some of the basia of game creation. The gam{'" featured here was inspired by the classic Atari game of A5leroids. My first video game syslem wa, an At ari 2600, and [spent many hours with il. In this chapter, you'll learn how to creall' a varimion of this classic game, which will be th{'" basis of a more advanced game later on when we get to Part 111. Here arc the key topia in this chapter: • Creating an A5leroid5-slyle game • Writing key classes: BaseYectorShape, Ship, Asteroid, and Bull et • Writing Ihe main source code • Calculating velocilies on the fly
About the Game Project Our game project in this chapter will rlln in a web browser window wi lh a resolution of 640 x 480 and will be done entirely using vector graphics. It will h,we some features that you have not yet learned aoout, bu t the exposure to this code will be helpful to you. I want to introduce routo some of the concepts early on, before you have learned all oflhe prerequisiks (otherwise we wouldn't be able to creale a game until about halfway through the book!). You may not
"
50
Chapter 3 • (reating Your First
Ja~a
Game
figure 3,1 This Asteroids done is the basis for a much more .lITIbitious game.
understand e~erything in the soun:e code for the game at this point, but you will learn how it works in time. Figure 3.1 shows the completed game you will build in this chapter. As mentioned, the game is cntirely based on vc"C'nitnt support for the JDK.. ....'here you ClJl compile your Java program with QrI+1 and run it with Ctrl+3, Just note that in order to run an appkt-bued prognom, you must creale an HTML ronlainer file, as described in the fi1'$t chapter,
The BaseVectorShape Class The three main objects in the game (the asteroids, the bullets, and the player's shi p) are all derived from the BueYectorSh.pe class. I origi nally wrote this game without the base dass, and in the end, all three of the game objects (the player's ship, the bullets, and the asteroids) ended up sharing most of their properties and methods, so the 8ase~ectorSh.pe class was a way to clean up the code. In the end, I put a lot of usefu l methods in this class for handling the needs of thi5 vector graphics game. lIy doing this, I have usai the object-orienled fealure called inhtritllnu."The Asteroid, Ship, and lullet classes are all derived from 8i1se~ectorShilpe, which contains code thai is sham! by all three. As a result, the co& for the three su~ is quite shon in each C2St.
53
54
Ch apter]. Creating Your Fi rst Java Game This g:lme detects w llisioll s betwee n the a~teroids. bu llets. and player's ship, so each vecto r shape in the game includ es it s own bou'lding rectan gle. While the getBounds() method is not found in the BaseVectorShape class for reasons I'll explain in a moment, this method docs use the geU< l and gety() methods from the basr.ptlfc$2Dg2d: fltoggle for dr.wlng
bou~dlng
bOU$
boolean sho"Bounds· false: /fernte the astero1d arr.y Int ASTEROlOS· 20: Anerold[) .st • new ,l,$teroid[ASHROIDS1;
bullet arr.y Int 8lJUHS· 10; Blll1et{] bul1e~. _ Bu'let(BUltCS): 1nt clirrent8ll11 et • 0; flcrute the
Keyt'Hener I
CrUl ing the G.""", IIt~e pl.yer'S s~lp 5111p skip - nev 5blpO;
Ife r ntt the Id ent ity t ..nsfor. 10.0) M f lneTr ans f Orll Ide" tl t y - newAfflneT r~ nHor.(} ; Ilerntt a ran dOil nu~r generator hndOil rand - newRand.:-():
Applet i nit(} Event ~
appkt tnttO f'Vm l i$ run wbm the appkt fi rst sian s up and is us.ed 10 initialize the game. The code first crcatda double buffeT. upon which all gr.lphias will be rendered in order 10 produce a smOOlh screen refresh WilhoUl flicker. The player's ship and the asleroids are iniliali'£Cd, and then the key listener is st:lrtcd.
I·····················································
....................................................., • apple t Inlt nent
public wold InitO I /luntt ne bilek buffer for s_t~ grapllics baekbuffer - nev hfferMINge(6'lO. 480. hffer~INge. TYPLUCRGB): g2d - backbuffer .createG , a ph Ics( }; Il set up t he shi p shlp . seU(310) : slllp . setYdhhlp.getxO) .... . .. .... tll.«!un(ltllllp.'etH}l. S. 10); t2d.clrawStrlngt ' Mawt Ingle: " .. Kath.round! slllp.gtUlo.t....,gleO)+90. S. 25); g2d dra.Strlngl "Flct Ingle:' ......th.roun(l( slllp .gtHlctAngleOl. 5. aD};
Creating the Game
IIdr'll t~e g•• gr.phlcs drawShlp( I; drall6ulleh(); dr'llA.sterolds( l;
II rep.a ln t the .pplet 1l111llow p.alntlgl;
Drawing the Player's Ship Tht drall$hl p() mtthod is callffi by the updatel I tvent to draw tht player's ship onlO Iht back bufftr altht COITtCI X and Y location. !kfore drawing, tht idtntily trall$fonn i$ stt so thattht $hip's local coordinalt syuem i$ usN, rathtr than tht prnious v«tor'scoordinatts. Remember, a tnnsfonn afftcts an obj«t's X and Y position. The idtntity is tilt staning point {O,O}. When the ship is first dnwn, il is actuallyctntered at tht origin, with tht shape of the sh ip being drawn from --6 to +6 in the X and Y axes, So tht ship is abou t 12 pixels square in size. If you don't draw a vector around the origin, then rotation will nOI work al all, becau.soe rolatioJ1$ occur al the origin-or rather, at the idmtity Ioation.
.
/
• dr.IlShlp called by .pplet upd.te ewent
.....................................................
/
publ1cyolddruShlp(11 g2d. set Trans for.( Ident I tyl ; g2d. tr.nshte(shlp.geUI I. shlp.getll I); gld. roUtelllulI. tllRlidl ans (5111 p. 911!tfaceMgl e( ) II ; g2d. setCa Ior(Col or .OIWlIiE 1; g2d . fill (sill p. getSII.pt!:( II ;
Drawing the Bullets Tht drawllulletsO method goes through the array of bullets and draws any ~ that nted to bt drawn. Thi$ only occurs if a bulkt is alivt using the hA.llveO mtIhod. Then tht bulla is transfolllltd to iLS position on tlit Kt'ttfI, and tht shape {which is a tiny rt'CIangk} i$ drawn.
61
62
Chapt"r 3 • Cr"ating Your First Java Gam"
I········
• drawB" 11 ets ca II ed by app 1et upda te event
.
............................................·········1
public void
dr~1I6ul
lets() (
Illtera te th rough the ~rray of bull eh for (Int n - 0 ; n < BULLETS ; n++l I Ills thh bullet cu rrentl y In use? If (bullet["].1s~live()11 Ildra w the bull et
g2d. se t Trans f or.( Ident i ty) ; g2d. trans late(bullet{n],geU() , bullet[n]. getY(»; g2d. setCo 1or (Co lor, MAGENT ~) ; g2d . drall( bu l let {n], getSh~pe( ) ) ;
Drawing the Asteroids The dr allAsteroids() method draws all of the asteroids in the ast[] array. depending on whether they are ali,·c. When the playcr fin'S a bullet and it hits an asteroid, that asteroid's alive variable is set to false, SO the asteroid is no longer drawn to the st11'l9 u~ed? f (b"lletln).I~ ...lhelll flupdUe bullet ' $ x 1'0$1 llon bullet(n]. lncl( bulltt[n] .get Vtllll) : II bullet dlsappeHs at lef t /right edge If (bull et(n ].gttX() (0 II bulltt[n] .geU() ) getShe() .wld t h)
lIupdate bullel's J po$ltlOfl b"llet[n] . tncYlb"l·et{nJ.getYelYl I: Ibullet d sappun at top/llolt.. tdge fibullet:n).geU()ullet[n J.geU t) ) getS1ltt) .lIelg~t) bJllet[n]. HtAl het hheJ:
Updating the Asteroids The updateAs terol ds() mdhod upda les the X a lld Y position of each asteroid lhat is currently alive based on the vel(>(it y variables. These X and Y values and velQCities are all sel to rnndom values when the game slans up. The asteroid§ are wuped around the edges of the screen. One inlcresting thing about Ihe ;lStcroids that differs from the ship and bulJeu is tt.;.t the ;lSteroids are rotated b) d r.mdom
Creating the Game
number of degrees cach frame, causing them to spin 011 thc sen."cn. Th is is a prrtly nice effe.::t that adds to the quality of the game.
f····················································· ..................................................... • Update the asteroids based on velocity
/
pub I i c vo; d upda te~. terol ds () { fhl<Jve and rotate the asteroids for (Int n - 0, n
The ea l eAngleMo~eX() method uses cosine 10 cakulate the update value for X, returned as a double. The clleAngle"o~eY(l melhod uses sine 10 calculate the update value for Y. also returned as a double. These small methods accept a single pal1lmder (the angle IMt a game object is facing) and rdum an cstimalN X and Yupdate value in pixels based on tlut angle. I can't S1rcss enough how wonderful these two methods are! In the past, I M\"C miN mainly on the brute force (and imprecise) method to move galm objects (usually called sprires) o n the SCr«fl. I would set the ¥elocltyl to I and ¥elocltyY to 0 to cause an object to mo\'e 10 the right. Or, I would SCI ~elocltylloO and ~cloeltyY 10 -ilocause the game objelt.~.·; IlIpOrt jua.llttl. · ;
public class
Rande-S~.pes utends Appl~t
lI~er~'s t~~ s~ape
private
I
used for drawIng
S~.pe lh.p~;
fi9ure 4.1 The
~
Jlf09rJfII ilusums Iht 'NplIlcslD ~
/
Progr,mrrung Vector Gr,phics fllpplet hit e>ent pUblit >old init() l shipe-new Rectlngle20.00uble(·1 O. ·j .O. 1.0,1.0): fllpplet paint e>tl\t p"blit >oid palnt(Grlphles gl I Ifereate In instlnee of Grlphits20 &rlphles20 g2d - (&rlphle s20Ig: lisave the Identity tr~n$for. Aff1neTrinsfol'llldent1t1 - new Affindrlnsfol'll(); Ilcreite I rlndo- n~r gen.eutor Rlnda. rind - nl!W R.l1\dooI1 I: IIsue the window wldthlheight lnt width · ge tS lu( l. wl dth: i nt hel ght • getS he ( ) . hei ght : IfUll the background with blld ,2d.utColor(Color.Iucrl: ,2d.f111It1ctlO. O. wI4th. lltl,htl: for(lntn-O:n
,) Wr-Wpod
~
. ..........................................................,
,
• RoUUPolJgon progra•
l-slort I.port l-slcrt illpOrt ll1pOrt
java .awt .·; java,awt.event. Jav a.a pplet.·; J av,) . ut 11 . ' ; JaW,). , .. t. geOll, • ;
pubHc chn Rot,)uPolygon utt1lds Applet ll1plmenu leyllsteMr. Mo"uLlsteMr
I prlutelnt[]xpolnU-{ 0,-10, -1, 1,101; prlute lnt(] ypolnts - (-10, ,2, 10, 10. -21: Ilhe r e 's the snape used for dralling prlute Polnon poly;
lipolygon rClt,tlon W,)rhble lntroUtlO1l-0; Ilapple t inlt event public void lnltl) I Ilcrute tne polygon
Progr(lmming Vector Gr.phks
Ill nlth l la th e l lHene rs Iddltrlhunlr{thh l : Iddllolltell st_r(tll1 s ):
/lt pp le t pt ln t eve nt pu~1ic ~ol d pt l nUGr aph1cs g) I /lcreUe an ins ta nce of Gnpll1cs2D Grapll1cs2D gld.· ( &rtpll1cslD ) 'I: /lsue th e Iden t ity trans fo r. AfflneTransfor1l Identity· new AfflneTransfor.():
IIsue the window wldth/lleight Int width · getSlze( ).wldth: Int height· ge t Slze( 1.height; Ilfi 11 t he bac kg round with bla ck g2d.setColor(Color.BLACK); g2d..tlllRecUO. O. width. hel'lht);
/IrIOn . rotlte . l lId selle the shape rlnde-ly gZd.transhu(wldth I Z. height 12); gN.sclle(20.20); g2d. rohU(Nlth. tobdl Ins ( roht1 Oft) 1;
IIdr .. the sh.pe wi th. r.ndo- color gZd.ntColor (Color .RED): g2d. til Hpoly): g2d.ntColor(Color.ILUE1; g2d.drlw (poly l ; II hand le keyboard events publ Ic voi d keyReleas ed (KeyE vcn t kl ( public void keyType
Figure 5.1 The DrlWlmage progo-am loads a bitmap file and drirW$ it.
Programming Bitmapped Graphic;
This ~ily castle image was rendtfed by Reiner "'olein using Caligari trueSi»Ce. He oIlm a large amount of roya1ly.fref game artwor'C itthtoughoul Ihe game; there are many options. Let's take a look at how this class works; Toolkit tk - 100lkit .1letDefaultToolk lt() ; I.~ge
shl p - tk. getl lllage( ·sur_destroyer. png'):
First, I created a Toolkit object by returning the object passed l>3ck from Too Hit.getOefau 1tToolkitO . This method returns a Toolkit object that represents the state of the Java program or applet. You can then usc this Toolkit object's getlm~ge() method to load a bitmap file . Since we want our applets to be JAR-friendly so games will run on the web as efficiently as possible, I will use the getURL() melhod again; l.~ge
shIp - n.get llllage(getURL< ' star_destroyer .png·)):
Traospareocy
Opaque Images L.et'$ $Ian with what you have already ltamed up to this point-how to load and draw a bilmap wilhout any tralUpolrmcy. At this poinl il doesn'l mailer whether you use the Applet or the Tool kt t 10 load a bitfllid would ~ considered the Utranparmt ZO~" of the image. This trnll$parmt color is black in the exampleshown here (with an RGB value 0(0,0,0), but other colors can be used (or the transparent color too-the color pink (255,0,255) is often used for the transparent color bQOe
b4tmap Image contains no
tr~~rl'l'q
i1tormation.
97
98
Chapter S • Bitmap-Based Graphics
Most Jav;l progr
Figure 5.8
The cuter edge of Iht ~leroid ilflo1!ll'
-
- .... .~
Figure 5.9 Pl'!piriog to inven Iht wlection.
~
been ...lected willi Iht M. Wand IClOl.
101
102
Chapter 5 • Bitmap-Based Graphics
"':' . """ , ,;";,,,'~~~--- -- '''4 ~ "
III
..
r
- .,-.
J
.~
•
'------~ " .~
.... _.
."' Figure 5.10
- .....-
The boond.Jry of the asteroid is IlOW Sl'lected.
selection you've made in the image. To do this, open the Layers menu, select New Mask L1yer, and then Show Selection, as shown in Figure 5.11.
>I, Although I am basing mis Morial on the ex(ellent grapl1k ed~or, Paint Silop ~ most professional graphic editors support layers and prol'icle similar fcatur,..; to mose found in PSP. The GIMP, for instance, is a freeware griljlhk editor wim (omjlilrable features and is available on many platforms (Windows, linux, and 50 on). Download The GIMP (GNU Image Manipulation Program) from www.gimp.org.
In Figure 5.12, the transparency has been created based 011 the masked selection, The result looks very nice; this asteroid is ready for primetime! You can load this image into your Java applet and draw it, and it will automatically be drawn with transparency so the outer edges of the image (where the black pixels used to be) will not overwrite the background of the screen.
Trarnparency
.....
.
•
. .. •
f igure S.11 CrNting ~
new mom IIy8 out of !he wIection.
figure S.12 The flteroid image now lias a masffil
tr~ency
1Iy8.
103
104
Chapt~'
5 • Bitmap-Based Graphics
What You Have learned W~ \'Iill wnlinue to \'I~rl \'lith tr;lIlspu~nt imagu from this point fO""'3rd. so you ha\'~ Ieuncd a \Try irnponant tool in thisdupterthat will make it pol&ibl~ 10 crC'at~ utrmlely atll'3eti\'~ pffieS. Spt'CifiooIl\'. you kamed:
• How to dl'3w bitmap images • How to trJnsJal~, rotal~, and scale bitmap imagl!S • How to draw bitmaps with transparency
Review Questions Th~
following questions will MIl' you 10 determin~ how well }~U ha\'e Ie:lrned subjccu discussed in this chapter. n.e answ~rs u~ provided in Ap~ndil[ A. "Q,..aptCT QUil AIlSI'I'CTS." th~
I. What is the primary clus. wc'\'~ bttn using to manipulate bitmappcd graphics in this chapt~ 2. What method initializes
Ih~
keybo;!rd listener interface?
3. What Graphics20 method is used to draw an image? 4. Which Ja\'a cia$:> contains the get!luge() metho·d? 5. What cia" makes it possible to perform translation. rotation, Jnd SClling of imagesl 6. Whigle( I I return ent itl.tetFaceMgle( I: I publ ic .oid setFace gle(double anglel I ent Itl . setFace gI e( ang Ie) : I public . oid se tFaceAngle(float angle) l ent! tl . setFaceAng let (double) angle): I public .oid setFace.....gle(lnt angle) I ent1 t, . setfaceAngl e( (double) angle):
11.,.e angle l/ldicates direct lOll sprite Is .,.'ng
publl c doubl e .a.eAng1eO I return ent Itl .getllo.ung1 eO : publ i c .oi d setHoweAllgl e( double Ingl e) I en t lty. sHMoweAngle(angle) :
I public . oid setllo.e gle{float angle) I ent Itl .seUlowe gle( (double I al\91 e):
I public .oid setHoweAngle(lnt angle I I ut I ty .setKo>eAn gIe ( (double) ang1 e) :
llrehrns the source Illige Ifldt~//lelg~t pub lie Int Illigellldt~() I return ent It)' .If' dt~(): I public lnt ladgeHeightIJ (return entlt)' . ~elg~t(J: for coil islon If l tn a rectangul ar shape public boolean collideslli t h(Rectangle rect) I return (rect. Inter~ectS(get80u/lds(J II:
Ilt~eck
I I fc ~ etk for coli i sion with another sprl te pub11 c boo1e. n co1li des ll l Ul (Sprtte spd t e) I return (getBou"lls( I. Intersects (sprl te. getBounds ( ) 11 :
I IIcheek for coli h i OIl with a poi nt publ ie boolean coil idnll HMPolntZO point) I retu rn (getBo un d1( ). con t ai nl (poi n1. X( ). poi nt
Y()) 1:
118
Chaplft 6 • Simple Sprites
publiC ~pplet IppletO 1 returne"tlty.lpplet; 1 pub II c Graph 1cs2D grlph I cs(} I return e"t 1ty . g2d; publIc luge l..get)I return entity. luge; 1 PIlbl1c wold setlu9t( luge 'ugt" I ent 1ty. set lugetluge);
Tip 6 II feJtI.ft nssing &om I!le Sprl te diss..: this pcwot we wiI 90 _lhil next dIIpIer.
.........._
.. _
sW!ecl
Testing the Sprite Class Let's gi\~ 1M Ill!'\'> cbsl;cs ..~\~ dc\"dopro in ltus d1iptn I tal run. The follo..ing program (shown in Figurt' 6.1) dr;tws a badcgmund inlaU:and then driwS a ~pril{' randomly on the screen. This tcst progrlUlI uses a thread and the Runnable interface in order to draw a sprit{' repeatedly on th{'SCIl'('ll without user inJluL W{"U study this kalurt' more lhoroughly in Olaptn 10. "Timing and ttlt- Gaml' loop." when w{'
r19uA! 6.1 1hlo SpitloTest
~ogram
demonstrates haw to 1M lhf
Sp'H~
cliss.
Creating a Reusable Sprite dass
kam more about threads and the pmt' loop. Sl:udy this short demo prosnm wdI, bcaUK it ckmonstrates ~ps the lim high-speed enmplt you.~ $em thus far.
I ····································· · ··· ·~·········· p rg gr~•
• Spr l teTnt
•••••••••••••••• u
/
1l1port j~u . llf t.·; tllPllrt JUI.llIt. i-.lge • • ll11Pl1rt jUI,lppleL ' : '.rtjul,,,ttl, · : tlllPllrt jlu.net. · : publlc cllst SprlteTest extends "'PIll et 1.,.1e.ents Runnable ( lnt scree nll id t h - 640: l nt sc ree nHe19ht - 480: IIdouble buffer objects hffertdl-.lge bACkbuffer; 6npkics2D 92d; Sprite utero1d: 1-.l9tht1tJ' back!lround: ThrUd IIll1t l oop: Rand a" rind - new Rando-() ; public voId i nit() { IIcrute the back buffer for s.xlth gr aphics INlckb
()o,-er
124
o..pter 7 • Sprite Aoim;,tion
Animation Techniques First, there is the ~"or
animaled. ~ AnimalionTest program loads a massive &I-frame animation St'quenct (shown in Figure 7.4) and animates it on lhe streen while moving the sprite :1Tound at the sallie time. Since we are slicking to lhe subj«l of animation in this chapter, the program doesn't attempt to do any transforms, such as rotation. But can you imagine lbe resull of an animated sprite Ihal can IlOO be rotated? This program will help 10 determine ....tLat we need to do in the upcoming animation class. The output from the program is shown in Figure 7.5, where the single animated sprite is bring drawn over a b."kground image. Following is the cooe listing for the AnimationTesl prognm. I havc highlighted Uy portiol15 of code (Ihat are new to this chapler) in bold lexl.
. ..................................................... /
/
IlIpOrt JilYi. i.t . ': IlIpOrt jiIYiI.i1pplet.·: IlIpOrt JilYiI .ut 11.':
I.port
jIVII . awt
. I.age
pUbllc clin AAI ..ItIO~Tut ute~ds .l.pplet stitle l~t SCRHIlIIIDTH ~ MO:
l..,l_~ts
RuMilble I
_, _. ....
figure 1.S 1hlo AAunationTel program. stattc tM SCR[[NHElGHT - 480; Thrud gllle1 oop; Rando. rand· new Randall(); IIdoubh buffer objects Buffered I.age blckbuffer; Graphtcs2D g2d ; I/background tllolge lllolge background;
IIsprtte nrllbles IlIolge blIll: 1nt balll· 300. blIllY" 200: 1nt speedl. spHdY: lIutllolttOll nr11bhs 1nt currentfrl." D: tnt totalfr. .I" 54: tnt u1l1olt1onD1rKttOll- I: tnt fr..count .. 0: tnt fra.tlell, .. 5:
Sprite Animation
129
130
Chapter 7 • Sprite An imation private URl
Rlget"Rl(Str;~g fllen,W)
I
~rl a n~ll;
'" urI -
t~h.gftCllSsl)
.9ftllesourcelfl 1eg fllen_1 1 URlurl-null;
133
134
Chapter 7 • Sprite Animation tryl
uri· thl s. getC Iass () geUesource( HI enalle1: )
citch (Exceptlc.n e) II return uri ;
publtc lold load(Strlll9 fllen••• Int col ...s. Int rOOlS. Int width . Int height) J/IOid the tiled .nINt1on bll.llap Tooltlt It - Tooltlt.9t!tllefi\lltTooltltO; anl~Noe· tt.oeUN,e(9t!tURl(flleni.ll; ntCal ...s(cal.-s); setTaulfra.s(colu.ns·rows); setfr••1l1dth(wldth) ; seU ralleHel,ht (hel oht l; flfr• • IN,e Is pissed to parent class far dra"lng t"pIN,e - nelf BufferedIN,e(,,14th. height. Bufferedlaage. TTPE.JIlT..AR6Bl; tNpSurhce· teepINoe.crnte6r.phlcs(); super .utlaage(teIlpINge):
Intcurre ntF rillle() I return currfraltt : ) Claid setCurrentFra.( Int (ra.) ( currFralle· frallf';
p~bllc p~bll
public Int fra.1l14thO 1 return fril l 4th: I public lold setfra.llldth(int "Idth) r frllidth - "Idth: publiC Int frilleHelght() I return frHelght: I publlc lold seU. . .Height( tnt height) 1 frHelght -height: publtc Int totilFra.s() I return totf..lles; I public lold setTaulFr_s( Int tot.l) I totfra.s • toul; publiC Int inl..t1onOirectlon(} I return ,nllOlr; I public laid setAniutlonOlrect lon( Int dl r) I anllOl r· dlr; I public Int fra.-(ountO I return frCount: I public lal4 setF...-(o~nt(tnt COllntl t frtaunt· COllnt; I
Sprite Animation public Int fra~eDelay() I return frOehy; I public vOid setFra~eDe1aY(Int delay) {frOehy - delay; public 1nt columns() I return cols. I public void setColwllns(1nt IIU ~ ) {cols - nu... pub1Ic vol d updateAn lll~ t Ion(} I frCount++; I f (frameCountl) ) fr~IIleOel ~y()} setFrallleCounHO) ; Ilupdate the ~nl.~tfon fraille setCurren tF rame( currentF rame () + anlllM t Ion01 rect Ion ( ) ) • if (currentfr~IIleO ) toUlFramesO - 1) I setCurrentF rame (0) • I else if (currentFrameO (0) ( setCurrentFrame(to t~ IFraooes() - 1);
public void draw() I /lc~lcuhte the current fraDe'S X~nd Yposit10n 1nt fralleX • (currentFraeO S co IullllS () * fra_eWldth () ; lnt fralleY· (currentFraDe() I colu..,sO} * fraIleHelght(); /lcopy the frallle onto t ne tellp
i~age
O. O. fraIleWldth()-l. fr n eHelght( )'1. fralleX. fralleY. fUIleX+fraIleWldth(). fraIleY+fr neHel ght(). applet() 1:
te_pSurflce.drawlnge(an\.1~ege.
/lpass the temp I!lage on to the parent chs\ and draw It super. set Inge(te_pluge) : super. transforll(); super .draw();
Testing the AnimatedSprite Class I'm not going to list the complete source code for the AnimationCla;s exa mple program, because it is essentially the same as the AnimationTest program, except it uses the new AnlmatedSpr1te class. There is one difference, though. By
13S
136
ChapI~r
7 a
Spril~
Animation
_. -'.'.
figure 7.6 Testing lht AntalttdSprite daiS.
inheriling from Sprite, the Anl ..tedSprlte class allows yo u 10 rolatc Ihe animated sprileS ...itik Ihq arc drawing! This is duc to thc functionalily provided by l..geEnt 1t)', a core support d~ for sprite drawing. I will show you the key portion of lhe program that differs from the AnirrulllionTCSI program (covered cartier in the chapter). Figure 7.6 shows the output of the pr~m. which you can open up and run from the CD-ROM if you wish. Hcre is a list ofall the dasses used in the AnimationClass program. You can copy theSt: files from the previous chapter. The new projttt is called AnimationClass.
• Anl.. ttdSpri te.jeva •
Anl.. t ionClns.java
• Basee;..Entt t)'.jeva • '..geEnl II)' .Java •
PoinllO.Jau
•
Sprl Ie . java
What You Have LearnlJ'd Here is the source code for the fnlt() event method in the AnimationOass program. The TC$t of the program is similar to the AnimationTcst program, so I won't repeat the entin: code listing hen:. The important thing is that you undcrsland how to load an animatkm using the An1 .. tedSpr1te class. Go ahead and open the Animal>OnOass.java file: to sec the complete code listing. Illp rfte urhbles Anf ..ttdSprltt' tNll; publiC yo1d hftO I IIcrute the back buffer for s..oth grapll!cs tNckb\lffer - new Bufferedl..ge(SCRHIIVIDTH. SCRE[.I+EI&HT. Bufferedlaage. UPE....lNT_RGB); g2d - back buffer . ereateGraph! cs t ); If\Md toe background huge Too 1~ it tk - Too \kit . getDeflu I tTao 1kIt() ; b,,~ground - t~.getl ..ge(getURLt ·.oadgra!n.png·); IIload the ball an1..t10n strip ball. new Anl..tedSpritelthis. g2d); tN 11. lOld( ·xb.l l. OIIg". 8. 8. 64. 64); tNll.ntPoslt10l1(new Poht20(300.200); tNll.SltFr.lIeOch,(l); tNll.SltYelocit,("", Potnt20
Testing Mouse Input I wouMlikt to show rou a program ulled MouseTest that demonstrates all o((~ mouse tvmlS that )'Ou Iu..t just lamed about. To build this program, ~'Ou should creatt a ntW "'eb applet proj«t ulkd MouseTest, :;md ther! remo"t all o( the automatically gencrated cod~ to })(' rq>l.1ced with tht (ollo..ing rod~ listing inst~ad. This program uses the Graphics dass' d'''wStrlng method alld a bunch of variables to dispkly the status of all the mouse ~"Ven(s individually. Figure 8.2 shows what the program output looks like. Note the important p.arts of the code listing in bold. Tht first part of tht program includes the applet's dass ddinition (with the needed int~rfaces (ollowing tm, 1..,Ie.ents keyword) and ... riabl~ dfdaratiol1$. -ousttest: IlIPOrt JUI.awt.": l.,mrt JUI.awt.event.", I_port Ja... applet.":
packl~
publiC cl iSS HouseTut utends Apple! IlIIll_nts MouseLhtlMr. MousiYlDtlonltstener I l6echre s.-e a:l~se e,ent v..hotes lnt el1cb'.. cllck:f: lnt presu. pren:!,: lnt relusu. releuey: Int enurx. entery:
Mouse Input
:::=";:~m _ _ II:lllO,301 _lftOOOd _ _ 1I11:'1.156 II: J ICI,.Z)l _ < t__
II:'I~
_ _ 4l161.Z!17
lftt ultJ'. exltJ';
tnt drlgJ'. drlQJ'; lnt ... vex. -ov.,; lnt llOustbutton;
The lnl t( 1 ('Vent method is the first method that gelS run in an applel. So .his is where)'Ou would initialize )'Our game objectsand variables, and this isalso where )'Ou add !he listeners lOr any input devicf$ 1M prog.ram ~ to ~. If your program f:¥er seems to be ignoring the ~rd or moUK, check 1nIt( 1 10 make sure you have added the appropriate listener. III.HIlllle the applet pUbl1c voId lnHO I
IddMoueLlstener(th1s) ; IcldJIoIIselkltl onL1stener ( UIs):
The pllntO
~t method is CotIled whrnevCT the appkt needs to refresh the window. Since pllnt() comes with a parametCT ( Grlphics gl, we can usc this
149
ISO
Chapter 8 •
Keyboard and Mouw Input
Objecl 10 draw onto the screen. 'Ibis program uses the Graphics dr!wStrlnli() melhod 10 display texl on lhe applet window. IIrednw t~e applet wlr>dow wotd Pllnt(GTlp~ics g) t 9.drnStrtn'l( "House clicked " • -ousebutton .. . at " • c)tckx. ". ·.clicky. 10. 10}; 9 d.. "Strtng("Ilo~se entered It ... enten. ".· .. enter)'. 10. 251; g.drlwStrtl\9( "House exited It " .. exlh .. "." . exit)', 10 . 401; g.drawStrtng(·IIO~le pressed". -ousebutton •• at •• pressx. ".•• press)'. 10. 551; g.drlwStrtng( "House relelSed " • -ousebutton" " at ... relelSu" ".... relelSe)', 10. 70); g.d.."Strtng("House dragged at ... d..gx . ". · .. drag)'. 10. 8S); g.dra"String( ·House -owed at . .. -owex .. . ... -owe)'. 10.1001;
p~b1ic
The next portion of code includes the checkButton() method, which I have wriuen 10 support lhe mouse event handler in lhe program. This c~eckButton() method chcclu lhe current button that is being pressed and KlS a variable (-o~sebutton) to a V;t.1ue representing the pressed button. IlcustOll .ethod called b)' -ouse ewents to report but ton status private wotd checkButton(MouseEvent e) ( lichee! the -ouse buttons swttch(e. get8uttOllO) { Clse HouseEwent.BUnOMI: -ousebutton -I; bruk; Clse lIouseEwent. BUTTON2: ..unbutton - 2: bruk; CI se lIouseEwent. BUTTON3: lIOusebutton - 3; brUk; default: ..llSebuttool - 0:
Moose Input The -ouseCl icked() event is part of the ~ouseLtstener interface. When you implement this inlerface, you must include all of the mouse events defined in the interface. or the compiler will generate some errors about the missing events. This evenl is called whelK'VeT you click the mouse bUllon on the applel window-in which case both a press and rdease ha.s OCCUfTed. This event is nol usually needed when you program -ousePreued() and -ouseRelused() yoursel f. publ ie yoid 8OusC'Cllcked(lIousehent el { IIsne the 80ua position y'luu el1eu. - e.getI() : el1ekJ- e .geU(): Ilget ,n updlte on buttons
e!IKkButtOll(e): IIrefrUh the
5cr~n
(call the paint eyent)
repli ntO: The ncxl two mous.e event methods, mIlseEnteredO and 8lluse£J.ltedO. an" called whenever the mouse (Ul'$Or f'rlteTS or leaves the apple! window. Th~ events arc nol often needed in a game. public votd
8Ou5eEntered(~euseEYent
e) (
- e.geUO: entery - e.getnl: relNl1ntO: enter~
I publ ic Yeld lIIOuseE~lted(~euseE.enl e) I ulh-e.gelX(l; ulty-e.9ttY(l; reINl1nt/): The lIOusePressed() and lIlQuseRelused(} evenl methods are called whenever you cl ick and release the mouse bulton, respectively. When these (vents occur. you can get the CUlTf'rlt position of tile mouse a.s well a.s the bullon being prascd or released. publ1c Yeid 8OusePrused(lIousehent el I pruu-e.geU(); pressy - e.getYO:
151
152
Chapter 8 •
Keyboard and Mouse Input
ehtekButton(e): replht(}: I pybllt vOId llOuseRelused(llouseE.ent el I rellun - e.geU( I: relnuj' -1.geUO: clllckBulton(e) : rlpahtO:
1hcl'lousdiot lonllstener inlerf.1a defines the next lwocvcnes-useClugged() and lIOuseKoved( l. 'These ...·mtsan helpful when you just want to know when the mouse: is moving o"cr the apple! window (and when it is moving while the bUllon is being MId dmo.n). public void llOuuClriqqedlMotlseE.ent el I drag.- e.geU(}: dragy-e.getY(): repaht(}: I p~b 11 e vol d llOuseHored(ll(lu~eE.e"t e I I llO.u-e .geU(): IlOvey- • .geU(); repalntO:
What You Have learned This ehapterexplaincd how to lap inlO the keyboard and mouse listeners in order to add user input 10 )"Our Java programs. • You lC'arncd how 10 detect key prascs.
• You learned about kcy cod~ and character values. • You lC'arned how 10 mid the mouse's motion and buttons.
On Your Own
Review Questions The following questions will help you to doetermine how wt'll you have learnM the subje.;ts d iscussed in this chapter. The answers are provided in Appendix A, "Chapler Q uit Answers." I. What is the name of the method us.ed to enable keyboard events in your prognm~
2. What is the name of the keyboard event interbee? 3. What is the virtual key code for the Enter
key~
4. Wh ich keyboard event willldl yOIl Ihe code of a pressed
key~
5. Which ke}-board event wiD tdl you when a key has been
rdeased~
6. Which keyboard event will tell you the dW'2CtCl" of a p~ key? 7. Which XeyEyent method returns a key code value? 8. Whal is the name of the method used to enable mouse motion
events~
9. Whal is the name of the d;lS$ used as a parameter for all mousc event methods~
10. Which mouse event reporu the actual movement of the mouse?
On Your Own Use the following exercises to test your grasp of the material covered in this chapter. Arc you rearlylO put mouse and keyboard input to the test in a real game yd~ These exercises will dWlenge your understanding of this chapter.
Exercise 1 Modify the KeyboardTcst program so that pressing numeric keys I to 9 will change the font site used to display the key code and character values. To do this,
IS3
154
Ch3pter 8 •
Keybo3rd lind Mou~ Input
U5e the Gr.phlcs class in the plint event, which has a method called sttFont that you can impltmmt like thi$: g. $etFont( new Font( · Ar leI'. Font. NOJlllAl. iiI ue II , I will gi\'e you a hint: The ktyoode for I i$49, so you can subtrac! 40 from tht" k~ coot" to arrive at a good font site.
Exercise 2 Mooify tht" MOUKTest program so thaI a point is drawn whenever the user presK$ a lllOU5e button. You can U5e tht" Grlphics e!au's ftll Reet Jm1.hod and tht" lllOU5e position ",n..bles. (Just draw a r«tangle ..ith four oomtl'S that au one pixel aran.) If you au {ming confidml with your llC'W }a", programming skills. try using the setColor method to change the oolor of the points.
CHAPTER
9
SaUNa EFFECTS AND MUSIC
java has a rich sct of features for recording, mixing, and pbying sound sampk-s and MIDI sequences using a variety of classes that you will learn about in this chapter. You will learn about Java's rich set of sound support classes for loading and playing audio files in a variety of formats through Java's sound mixer. You will then learn about MIDI files and how to load and play them through Java's MIDI sequencer. Here is a rundown of the key topics in this chapter: • Loading and playing digital files • Loading and playing MIDI files • Writing some reusable audio classes
Playing Digital Sample Files Java's Sound API provides a package for working Wilh digital sample files, which has methods for loading a sample file (AIFF, AU, or WAY) and playing it through the sound mixer. The package is called javax.sound.sampled and includes numerous classes, most of which we will ignore. Some of these classes provide support for recording sound and manipulating samples, so you could write a complete sound editing program in Java that is similar to full-blown sound editing programs. One good ex-lmple is Audacity-a freeware, open-source
,ss
156
Chapter 9 • Sound EHe
""*
Since we don't need. to pass a par:am\"ler to getCI t p, you might be wondering how this obj«t knows whal to play. Thne:'s aetuaUy one: mor\" step involvtd becaU5C al this point, aU you have: is a sound dip obj«t wilh the: cllJHIbility to Iood and play an audio file: or S1r1"am. This method actually rdurns a sound dip obj«t from the: default s~lc:'m milCT.
Loading
th~
Sound Clip
At this point, you have an AudtolnputStre•• and a Cll p, so you just need to open the audio file and play it. These steps;\rC bo th performed by Ihe CI 1p class. First, let's open the sound file; cllp.open( ullple);
Pfilying the SOund Oip Next, there arc: two "'Irs to playa dip, using the Clip class. You can usc: the start() method or th\" loopC) method to playa sample. The stirtC) method simply plays the sound dip. narrator .stnt
--
Tip
ACIIl'Ilpifte pnljKI ~ flsdlss is . . . . . OIllhe (I)-l()M illhe ~
IlIJIOrt JilYl. Ia. " ; IlIJIOrt jiIYl.net."; I~art JilYu.saund,.1dl,-; public class M1dlSequence I IIprlrury .I ~ I sequencer object pr1v.te Seq uencer sequencer; flprowl~e Sequence 15 • reld-anly property prlvatt Seiluence song; public SequenctgetSong() I return song;
IIfllen•• property h reld·anly prlute Strl", fllen ; publlc Strl", getFil O I rehrn fil ...... ; 1I100ping property for lOOpl1l9 contlnUl;l\lsly prhlte boolu" looping - fllse; public boolun getlaoplng() I rehrn looping; I public void setlooplnglboolun _looping) I looping
~
_100pln9;
IIrepe.t property for looping. fl~ed nu.ber of tl.s prlute Int repeat - 0; public void setReputl Int _repe.t} I repeat - _repeat; publIc 1nt getRepeat(ll return repeat; I IIreturns whether tne sequence h readjr for actIon public boole.n hLo.ded() I return lbooleanllsequencer ,I sOpen(»);
IIprl ..ry constructor publlcMldlSeqllencel)I try t IIfire up the SeillIef\cer
173
174
Chapter 9 • Sound EHero and Ml>sic sequencer · MldlSystea.lIetSeql>enctr( I : I cltch (MldlUnnal1ablehceptlon el ( I
I ((over 1olded constructor accept s al dl fl1 enallf public MldISequence(Str1n'l aldl file) I thls(): f/cill debult constrlOctor first 10ld(aldlfl1e): f/load thealdl file
prlnte URllletURUStrlnll tll""a.) I URlurl-nul1: try I uri • thi s .IIHCI iSS( ) .lIeUesourcel fll ft'allf): I cltch ([ueption e) I return url:
f/l Old a aid 1 fl1 e 1nto
I seqllence public boolean 10adlStrlnll .ldlfl1e) 1 try ( Ilioad the.l dl file Into the seq~encer filenaltf· .idl file; sonll • IIi dl Systfll.lIttSequenct( lIetURl( fil enaltf I ) ; stq ~ encer . setSeq~enc e (song) ; seq~encer •open ( ) ; r etur n t rue ; I catch (lnvalldllidlDahhceptlon ell r eturn hhe: I catch (1I1diUn"ulhbleE:
The Potency of a Game Loop
Overriding Some Default Applet Behaviors lbtre in $Crious problem with this program b«2use it was supposed to just adda new rectangle every time the user presses a key or 1llOU§C' bUIIOn, not rtdraw tM entire appld window-with a single r«Ungle left OVtt. lbtre is definitely some-thing odd going on b«2U§C' this program shoWJ J.D,,!, worUd as cxpro.cd. Well, it tul'JU out that Java has b«n ~ng with the screen without pennissionor ~thcr, by difaw( The Applet class, which is the basis for the SimpleLoop program (rec:aIl that it ati'7UU Applet), provides many default cvmt methods that do artain things for)'Ou. You don·t even nm:Ito implement pllnt{) if you don't wanl to, and theApplet baseclass will provide il for your program. Granted, nothing will be drJwn on the window as a rcsult, but the compiler won't give you an error. This differs from an interfuce class (such as Keyll stener) that IIUl1IdllleS that you must implement all of its abstruct methods. So it's prelly obvious by this difference in functionality Ihat Applet is not an interface class, but a fully functioning class. What happens. then, whrn you implement an Applet clasli method such as 1nlt(} or p.intl l? These methods aT(' essentially empty inside the Applet class.. ah, they exist and are not abstract, but they don't doanyrhing. The Applet claS$ defines Ihese md.hods in such a way thai you can O'Wf'T"i,kthl'lTl in your ..ppld.. For instance, in the SimpleLoop progr.un, SimpkLoop is aaually the RanK' of the class, and it inherits from Applet. 1lJnrl"0f"C', Simpldoop has the opportunity to override any ohhc methods in Applet that il wa.ni.S to. including hilt( I and pllnt( I. Howt'Ytt, tMre's anothtt method that we haven't wed)'l!'1 aUed upodate(). Aha! No. I wasn't holding out on you-you've aaually wed Ihis method before. in the game project back in Chapltt 3. 1bt updlte() method actually II«s do something as coded in the Applet cbJs-i t caIls replintO 10 refresh the appld. window. (Light-bulb momen!.) Since the default upd.teO method has been refreshing the sen"en for our programs, we have had ab$Qlutely no c01l1rol over this process. That explains why only one rectangle was being drawn at a time in the Simplel.oop program updlle(} was refreshing the sereen on its own. In a sense, your Java program is a slUl'e to the master Applet class until you override the functionality by rewriting its methods. It's time 10 break tht bonds of objoxl-Oritnled slavery. Let 's add the update() method to the program: puMlc void updlte(Grap~jcs 9) I pa 1nt(t);
18\
182
Chapter 10 • Tim ing and the Game Loop
Figure 10.2 The SimpleI.oop progrilm now dlaws rna." ~Ifs.
As you can 5C'C, the: upchte ( 1 mdhod is bearing a single: line: of cOOc:, a call to the:
pal nt ( 1 mc:thod. This giVt:$ you compk1e: control over the: scrttn rdroll bc:causc: the default updH ell would root just rqxtint the: scrttn, it woukl abo dmr 1M scr«n. Now you can run the: SimpleLoop program and Stt a bunch of rectangles as originally expected, as shown in Figure 10.2. I think this example makes it pretty ckar that any serious game ...~1l nte:'d to override the update() method as wdlas paint( 1. But that doesn't rally address the subject at hand- what about the loop? I've been calling this program SimpleLoop when no loop is even being used. Let's gettD that now.
Feeling loopy There are quite a few Applet methods available that we haven't implemented yet, in addi tion to the standard methods you've snon.so far. I'll go over the remaini ng kq Appl et methods at the end of this S«tion to make you aware of them, For now, I'd like to introduce you to the: next Appl e t mdhod: stutO , The sUrt( I mdhod is invoked in your Java appltt by the web browser right after calling
The Potency of a Game Loop
in1t( l. So, you can use in1t() to get the game ready to go, and then use Hart() to get thing!i moving. Now you finally haY< an opportunily to add a real loop to this progr.tm. BUI just for kicks, what do you think would happm if you added a III1i le() loop to the In1tD method? I tried it, so you should try it too. Doing this will lock up the applet, which will not evcn be disp layed. Why! Ik
lbe core of the threld loop includes a call to Tllrud.sIHp(OJ, whkh is a place-holder for slowing the game down 10 a consistent frame n le. Righi now it's running as fasl as pos.sibk because the sleep( ) method is bting passed a O. This single method call requires an ClTor handler because: it throws an [nterruptedE.cepticn if the
187
188
Chllpler 10 • Timing and the Game Loop
fi9ure 10.3
The 1'hrNd..t.oop pnIpll
Exercise 2 The ThreadedLoop program jusl draws V«!or graphics. Modify the program so that it draws animated sprites instead.
PART III
THE GALACTIC WAR PROJECT
This final part of t~ book is devoted 10 th~ dtvelop~nt of a compll'1~ gam~ called Galactic War, built entirely in Java as a web appld. By the lime you have finished reading the book, you will have lC'3rned how to create this game from scratch and deploy it to your website. Here are the chapters in Part Ill: • Chapler II: Galactic War: From Vectors to Bitmaps • Chapler 12: G~laclic War: Sprites and Collision Boxes • Chapter 13: Galactic War: Squashed by Spacc Rocks • Chapter 14: Galact ic War: Entity Management • Chapter 15: Galactic War: Finish ing the Game • Chapter 16: Galactic War: Web iA-ploymmt
CHAPTER
11
GALACTIC WAR: FROM VECTORS TO B,TMAPS
The G~I~ctic War proiect will demonstrJte just one type of game that can be created in Java. This game is compln, but that complexity is hidden inside a game engine that, once written, does not n~'\'d to be opened again. You will write an al'pkt that will inherit from the game engine, and then the vast majority of the core code for the game will be handled behind the scenes. We'll build the game step by step, beginning with the simplistic Asteroids-style game from Chapter 3, gradually improving the game until it is finished and ready to be put up on your website. The first step to building Galactic War is to begin converting the original project from an entirely vector-based game into a bitmap-based game. We'll start with a partial conversion in this chapter, retaining some of the vector shapes but replacing the player's ship with a bitmap. Here are the key topics: • Improving the game • Generalizing the vector classes
Improving the Game Chapter 3 gave you an example of a semi-complete A>leroids-style game to show you what would be covered in the upcoming chapters. You have now learned enough about game programming in Java to greatly enhance the game. That original game featured a class called BaseVectorShape, which contained
'"
194
',,",pter 11 •
GiI~k
W"r: From 'leeton to Bitmaps
all of the properties needed to manipulate game obje'C1s on the scm:n (the asteroids, bullets, and ship). In this chapter, we'll make a few changes to add sprite support, and in the next chapter we'll move the game entirely over to bitmaps. By upgrading t~ spaceship to an illl Creating the Project
f igure 12.2 The badgroood image used in Galactic War.
fig ure 12.3 The five uniqIJe asteroids featured ;n the game. Figure 12.3 were rendered by a talented 3D artist by the name of Edgar Ibarra, who actually created these asteroid models for a project I was working on several years ago. I have converted the asteroid bitmaps to the PNG format using Paint Shop Pro, as well as applied a transparency mask in the process. The spaceship has also been upgraded significantly from the version presented in the previous chapter. I based the spaceship on a design by Reiner Prokein and significantly modified it to give it a more distinct look with a pseudO-3D appearance. (Note the four guns, top and bottom.) Figure 12.4 shows the ship sprite. This is a great-looking ship, if I do say 00 myself. I'd like to add a small fire
209
210
Chapter 12 • Galactic War:
,-
and Collision Boxes
_------ ..
. "' .....l;I"" ll". b
Sprit~
~;::=
1'"
.... _
_j["ijr__
4
!iI._.
---....,
_. .
~ "-f""'=":J
.~ .,
~·iI
••
•. _._........... _-
--"'._-~
fig ure 12.4 The player's spaceship used in Galactic War.
-
...... ...... --~
animation coming out of the engine nozzles when you press the Up arrow key to apply thrust (fulUre enhancement!).
The New and Improved Source Code There's a lot ofsource code here for the new version of the game. Th is is necessary be
lnt frav.Count - D. frlv.Rltf - 0: , ong s t.rtTille - Sys te•• currentT l11tlll111 s ( ): Thc sho~Bounds and colli s 1onTest I ng va riables arc used to draw some helpful information on ,he scrcen, which is invaluable while develop ing a complex S'lme. Figu re IZ5 shows the game with bounding bo~es and collision testing turned on. When a collision occurs, the bounding boxes of the two objects arc drawn in red instead of blue. You can turn ofT collision testing altogether, if needed for testing.
Cruting tho! Project
Figure 12.5 80lIlding boxe and mIisions are IoggIfd with Ihr Band C keys.
This brings up an imponanl point aboUIIM currenl Slate of 1M game. ~ au a lot of new featu res in the game, and il prt'lty much look$the WoIy it will whl'll il is finished-acept for a scrolling background and a few olhorr tidbits. The game really docsn'ttake any action atlhis point based On a collision. Instead, collisions arc dt'lSCREElIVl DTH) bullet[n).uUl1wetfilse) :
lIupdate bullet's y position bull et[n] . up4ttePol 1tl one ) : IIbullet diupptars It topfbottol edge If (bullet [n].pos ltlon ( ).YO SClEEIlKEl6HTl bul let(n].seUlln{fllse) :
bu I \ et[n] .s'tSti let SPRITUORHAL l :
219
220
Chapter 12 • Galactic Wilr: Sprites and Collision Bo~es
, . ....................................................., • UpdUe the nterolds t>.sed on .elocHy
public .01d
~pdntAsterolds() I
1I~.e
,ncr route the nterolds for (lilt n - 0: n < ASTEflOIOS: tt+ +) I if hst(nl.JlheO) I lI~pdne the n t erald's position Jncr rau l 10n ISt[n]. opdettPos It 1on e); ISt(n). apdlteRout ton ( J :
1nt II· ut(n] .1MgellldtMJ- l; tnt h - ut(n] .IMgtHelghUJ - l; do~ble ntwX· ut{n] .poslt10n( I.I( I: do~ble n~ - ut{n] ./lOsl t10n(). Y(): I/•.,ep t he ~st e r old ~ ro~n d the sc reen edges if (ISt[ n] .poslt l onf).If)
< '11 )
newx· SCRHHWIOTM + w: else if (lSt{n].pos1tionf).X{) >SCRHHIIIOTK+II) newx· ow; 1f (ut[ n].poslt10n O .YO (-h) newy- SCREEWKEI&HT + h: else 1f (ut(n] . poslt1onO. YO > SCREENKEIGKT + II) newy· -h: ut{n].setPosltlon(new PolntlO{ntwX.n~): ut(n] •setSute( SPIIT[JIORlIAL);
I···································
• Test ut'ralds for co III s 1OfIS with shl p or
b~ llets
.
.....................................................,
pobll c .olel checkeo III s Ions Cl I IIcheck fo r coll 1$100 bet ~e n nteral ds end bulle ts for (Int _ · 0: -BULUTS - I ) currentlul1et - 0; bullet[ currenUullet]. setAll ve( true );
IIset bull et •s start lng pot nl lnt . - bullet[currenUullet].lugelltdth(); lnt h - bull et[currentBullet].lugeHetght(): ~oub lex - sht p. centerO •Xll - ./2: doubley-shtp.centerO.YO - h/2: bul l et[currentBull etl. setpos t t lon( new Pot nt20(x ,y) ) ; llpotnt bullel tn u. dt r«tton shtp t s faclng
223
224
Chapter 12 •
Galactic War: Sprite
251
252
Chapter 14 • Galactic War: Entity Management
,
.
........................
..,
publiC (>,l.cticll.r() I IIcal1 b.se G•• ,1 us' con~tructor iUper(fRAllHATE, SCREEHIIIDTH, SCREEHHEIGHT);
.
/
,
... yold ,1.Startup() 111 old the b.ckground lalge blckground· new laigeEntlty(thl~); blc kground. 1Old( ' b1uesp.ee. pn9 ' ) ; /lerute the ship sprite- f irst in the sprite list ~hiplalge[D]· new laigeEntlty(this); shi plalge(D]. 1old ("Sill cuM p. png-); sMlllalge(l] .. new laigeEntlty (th1s); sh1 Illalge( I]. 1old( "sh1 p_thrust. png") ;
M1 ..teclSprlte ship" new M1.teclSpr1te(t~ls. grlpllles(»; sh1 p. setSprl teT;rpe (SPklTl-SIlI' ) ; sh1 p. set1.ge(sh1 pl.ge[O]. get I..,e() ); shl p. setfra.1I1 dth ( sh1 p. l.gelll dthE}); ship.setfr••Helgh! ( shlp.1 ..geHelgh!(» ; shlp.setPosltlon(new Polnt20(SCREEHIIID1HI2. SCkEEHHU6HT/2); sh 1p. setA11 vee true) ; sh1 p. setState( STATl-MORItAll ; sprltes( ). Idd (sh1 p) ;
111 old the bUllet sprite '.ge !luI letl.ge" _ l.geEntlty(this); bulletlalge. 1old("p las_shot. png"); Illoid the uploslon ~prlte '.ge nploslons[O] .. new l.geEntlty(thh}; e~p I os I ons(O]. \old( "e~p' os, on .png"} ; e~p 1os1ons(l] .. new 1.lgeEntity (this) ; up1os1ons(l]. 1oad( " e~p 1os 1on2 .png-) ;
Enhancing Gala(ti( War IO.d th~ blq tst~rojd I.. ~U 5 tot.ll for 11nt n-O; n
.....................................................
/
public .Gld sprlttO,IIIIJ(AnI..t
The m()$t important ""ent in the game is proNbly!he sprlt~GlllStGn() \'\'mt, ..ilkh is passed from the engine to your source eode automaticaUy. "The engine gots through the list ofactive sprites (when' the II tyt property is true) and teslli each one for collision with all the other spri tes in the list (ac""t for the same one-we don't want to have objects blow themselves up for no reason!).
Tah a look at figure 14.5, which shows the ship firing several volleys of flaming plasma bolts toward asteroids. There are several bolts traveling away from the
F;gur. I ..,S I"rojKliIes fired from the Vip ~ get pmed!llrol.9h the ~ __ wIwn they a6»
...........
259
260
Chapter 14 • Galactic War: Entity Management sh ip at various angles and ~ explosions thai are animating on the- S(r('('1l. Figure 14.5 shows sprite collision testing in progress.. Thce-nginc,as implemenled in the 6ne class and inhm led by 6i1hctlcllilr, goes through the- sprites and tests thtrn.a11 for collision-but that's ail it does. Look at the spritt countt"r in this figure-132 sprito! This number includes lht ship, bulku, asteroids, and explosions, and the- nurnbcr ~ and falls dynamically with the- gamtpby. (lbc: mgine- dears out unused sprites that havc " dial" ODCC' C'Vn')' second whm lht frame rate is calculated.) Thcu is no more logic going on behind the S(cn('$ than the- test for collision itself. The engi ne sim ply passes both sprites that have interacted to thc sprl teCo111· s 1on() method. At thi s poi nt, it's entirely up to you to decide what to do with the conflicting sprites. You can destroy them , have them bounce away from each other, or anything else. So all you have to do is figure out what kind s of spri tes have been passed to you through the spr1teColl1s10n() event. You can do this with your own predefined constants (defined as a static int in Java). Forexample, I have defined SPRITE-EXPLOSION for all explosions. Yes, explosion sprites are tC'$too for collision like C'·trything else-but we simply ignore them, let them play out, and then disappear. lbc value of each constant is nOI imponant, as long as each one is dilTerent. 5iIlCC' I defined each oflhe four types of asteroids wi th a stparate constant, I had to write a hdpc1" mnhod called 1s"sterold() to handle them all in tht same way whm they arc pas,soed through tht C'VCIlt handlers. Now, howabout that coot I promistd! WlItn I gt't inlO a lough spot in the-galne.
I hold down the kft or right arrow kty to spin while hitting bolh fire buttons (the Ctrl keys). This launches twice- as many volleys of plasma bolts, as Figure- 14.6 shows. {
. .....................................................
•
~prl
teCo111 s1on eve-nt
p~ssed
by gnt eng Ine
/
pub l iC voId sprl teColl151on(AnllMtedSprlte spr l. AnllatedSpr lte sprZ} I IIjullp out quickly 1f collistons are off if (lcol1 hlonTntlng) return: Iffigure out whilt type of sprite hilS collided swltcMsprl.sprlteTJpot(J) { cilSe SPRITE,..8ULLEi: IIdld bullet hit i1n nteroid? 1f (1 sAsterold(spr2. sprl teTypot( ) J) I sprl .uUlln{hlu):
Enhancing Galactic War
F;gu.. 1•.6 ~ IN>d> lwice
YlIU
a5
many boilS lMlg botIl CIr1 keys!
spr2.utAl he{f,l se): brukAste r old{spr2) : )
bru t; CHe SPRllLSHIP: IIdld Harold crHh 1nto the ship? If (l sJ.ste~l d ( lpr2.lpr1hTYPt()1l { If (spr I . IUh( I -- STATLlIOftIlAll { call1slonTl _r - SystIM.earrffttTllldtlll1l( I ; sprl. seth I11(:1 t1lftew Polnt20 (0. 01 1; doubl e x - Iprl.pollt1on O .Ill • 10; double y - Iprl.poslt1on O .YO • 10; stirtBlgEll.plos1on(new Polnt20 (x. y)l ; sprl. set5u1l (STATLEIP LOOI ~Gl ; sp r2.set.i.lln(hlse) : brukAste rold(spr2) : )
II-a t e ship tellPOrully Invulner' ble else if (sprl.lhh( 1-- STATLEIPLOOING ) { if (call1 l lonTI_r + 3000
36(}) shl p. setFlceMg1e( SHI PROfAT 10.) ;
11 Ue)'llp) ( lup Irrow Ipplles t~ ..... st til ship dt p. set 'lIge( lhl pl..,e[l).,etlllge( II ; IpplyThrustll;
I else Iiset s~ip iatge to noratl non-t~rU~I iatge shl p. set IlIge( shl pllNge[O]. get I., ge( I) ;
Moving the Spaceship
The sp.1uship in Gabelie War is rol3lffi usi ng the lrit and righl Jrrow keys, and thrusl is appliffi by pressing 1M up arrow by_ The IpplyThrust( Illt1.hod handle$
I~
acedenlion of t~ ship while k«ping I~ ship within a reasonabk
,docilY threshold•
...........
,
public ~llid applyl~ru~tl) ( lithe ~hip Is always the firs t ~prite in the 1in~ed li~t AnilNtedSpri te shlp· (AnllNtedSpritt)spri t es () .getIO); Ilup ,rrow add~ thrust III s~ip (1/ 10 nil .... ) speedl shlp.seUlllveAngll(shtp.hciWIgl,()· gO);
269
210
Chapter 14 • Galactic War: Entity Management fl calcuhte thl I aoo Y vel ocltl ba sld on angle double velK - shlp.velocltl( 1.1( I; velK +- calcAnglellovel(ihlp...,eAngleO) * ACCELERATION; If (nlK < ·10) nb.- ·10; lin H (nix> 10) velK -10; double nll - shlp.nlocltl( I. H I ; vel, +- calcAnglellonHlhlp...,eAn9leO) * ACCELERATlOI; If (nl, < ·10) nl, - ·10; IhlH (nl, > 10) nl, -10; sh1p.nUelocltl(new PolntlO(velx. vel,»);
Firing Weapons
The mOSl signifkanl area of improvement for Ihe game is in the weaponry dep.1Ttment, so some lime will be spent in Ihe ne~l ehapler adding powerups 10 the game. You will be able 10 grab powerup icons lhal arc dropped by exploding asteroids, which will Ihen enhaoce the ship in various ways. The (lIfTenl version of the game here has ll(){ changed from the previous chapter, except lhal il now fuJlctlons with the game engine. The Orl key is ~ to fire weapons, bul you can change this 10 ;lll(){!ler key if you want by eumining the key handlers.
.
/
• flrl a bullet trOll the shlp's poslt1011 aoo orlenut1on •• • ••• • •
~
••••u
uuu
"
u ,
public ,old flrellulletO I lithe ship 1s al n ,s the fIrst spr i te In the Il nkiKIl1 H AniutiKISpr1te ship - (AnlutiKISprltelsprltu( ).gltlO); IIc r eate t he new bullet spri te AnlutiKISprlte bUlllt - new AnlutedSprlte(thIS,grapMes(»: bull It. set luge (bull It luge. getluge( ) : bullet. setfrnellldtMbulletluge.wldtM I): bull et. setfruHlHII ght( bullet 'uge .hel gM( l ) ; bull It. sltSprl teT7pe( SPRITUULLETl : bullet. setAllle(true); buliit. sltLlfespan(200); bullet. setfaceAngle( shl p. hemgle( I ): bullet.setlloveAntle(shlp. hemgleO - 90); llse t t he bullet's stlrtll1<j posltlOll double x - shlp.cllIter O .10 . bal1el.lu9ltlldtMlIZ:
Enhancing Galactic War
double , . shlp.eenUr(). Y( I • bullet. 1..geHe19ht( lIZ: IMlllet.utPos1t1on(new Pol ntZDll.1I l: IIsft the IMlll et ' s ye1oc1tT double Ingle - ballet ..enAIl91e( I: dOllble s.. _ ealc:AnglellClnX(uglel* IULLE'CSPEEO: double SfY- ealc:AnglellClnY(uglel* IUlLET_SPEECl: bullet.seUeloelty(new 'OlntZO(SfX. Sfy)): lIadd IMlllet to the sprite list spr1 tts( l .Idd (1Ml1 1et):
Giv~ M~
Something to Blow Upl
There 3re two main methods for sta rling explosions. 1could hnve come up with a craftier way to do this, but I decidcd 10 just write two similar methods: one for initiating large explosions and another for smaller explosions. They usc images stored in the explosions array (of '_lgeEntlt1 objeclJl ). There are currently only two explosion animalio ns. The large explosion is used when you hit a large asltroid. The smaU explosion is drawn when you hil smallCT a5leroids.1 think the result looks prdty good. Because 1!lCTe are Q lot of mWI asteroids and bullets flying evay whidl .... y. you don'l ....nl too oomplex of an explO$ion sucking up lhe game's resources ..nrn Ibm: oould be a couple dottn such explosions animaling at a lime. QIeck out Figure .4.8. The large explosion's frames are 96)( 96 pixels in size and 1!lCTe are
LARGE EXPUlSION
SMALl. EXPLOSION
ftgure 1....
The two eq:hion .-a\lO'IS ~
_
br -.
271
272
Chapter 1
273
274
Chapter 14 •
Galactic War: Ent ity Management
Review Questions The following questions will help you to determine how well you have learned the subjects diseW items shown in bold tUI. IIsprtte t ypes tnt SHlTE.5WIP -I: tnt SP.IH....ASHROIO_RI6 -10, tnt SP.ITE",lSHItOIOJlEDlLAt-ll, tnt SPRlTEJ.SJEIlOIO_SllAtl -12, fl~11 tnt SPRlTEJoSTEROIO_TlNY - 13: fl~ll tnt SHlTL8ULLET - 100: flnll tnt SPRITLEXPlOSION· 200: flnll tnt SnITLPOIfEIW'_SHIELD - 300: fhal In! S'UTLI'OIIEIWPJjUlTM· 301; flulln! snlTLPOVuuPJSO-302: ftnll tnt SPR ITE..-POIfERU L SOO· 303; flul t nt SPR ITLPOIIERUP_1000 _ 304: flul In! SPIITLJ'(lII(lU' _6UN _ 305 :
flnll flnll flnll flnll
New Game States To give Ihe g~ ll1 e Ihe ab ilily to start, play, and cnd (with the option 10 restart), we need 10 add some cond itional gameplay stales and make usc of Ihe pau$C pro~rty in the sprite engiDe (found in the 61.... class). Add the following lines jusl below the J>eW sprile definitions, above tnc toggle V;lI.ubles. ~ J>eW code is shown in bold. Jlgl.... I tltu
fl nil tnt lWlEJtENU• 0;
28\
282
Chapter IS • Galaaic War: finishing the l ; ff relluce 5hlp health Ifter a hit health·-l: if (health (0) ( galleStite - WE...OVER; )
Illou f1 repower when)'011 get hIt
flrepower-; 11 (ffreJlllftr (1) f1rej)Olf1!r - I;
I spr2 . setAl heC h l se} : b rea kA5tero i dC sp r2}:
IllOi ke s ~ i p t e"llo r l rl ))' 1 nval nerl b)e else if (spr l. stlteO -- STAiE...EX'LOOI NG) I 11 (coillsionlille r" J.OOO ( S)'ste..currentTi...lllis(») I sprl. setStateCSiATE....llRllAtl ;
I bre~k:
cue SPRITU'OMERU'_SIIIELO: 1f (spr2.sprlteTJpeC )--SPRITE...SIIIP I I shield"- S; If Cshield) to ) shield - to; sprl. setAl I u Cfa I $II; )
break; clle SPRI TE.,POWERUP~HEAL TH: 1f Csprt.sprltlTJpeC)--SPRITLSHIP) ( hulth"-S; If C/Iotalth) to) hulth - to; sprl.$ItAI1u(flhal; )
break:
295
296
Chapter lS • Ga lactic War: Finishing the Game CGSf SPRITLPOWERUP_250: 1f (spr2.spr1teT~ pe{}--SPRITE...SHIP) { bu.pScore( 250} ; sprl.setA11 ve(hlse): )
break; cue SPR ITE...POWERUP_500; If (spr2.spr\teT~pe()--SPRITE...SHIP) { bu . pScore (500) ; sprl. setA1he(hl se}: )
brfGk: case SPRITLPOWERUP_1000: lf (spr2.sprlteType(J --SPRITE...SHIP) { bu.pScore< 1000}; sprl. seUI hevel I, and then add an additional big asteroid to each level the player completes. Although the game can handle an unlimited numocr of sprites, I would end the game at level 10 to keep it reasonable. Since the game currently just throws 10 asteroids at the player from the start, switching to a level-based system would greatly improve the fun factor! For the ultimate version of Galactic War, I refer you to a derivative applet available at www.starnightgame.com. where the team has posted a "space combat demo" of the gameplay in Swrflight: TIle Lost CO/OilY using the source code for Galactic War as a basis. You will find many aspects of this game that are £1miliar after working through the Galactic War project.
307
CHAPTER 16
GALACTIC WAR: WEB DEPLOYMENT
This chapter finishes the book by explaining the all-importan t subjegl'S and other media files in th;s book. The method you must use when a game is deoployed in ~ JAIl uses the J~va. net. URL dass and !he getResource() method to aute a URl that \'011 can pass to !he appropiaIe .... l_• • •• • •
~:::l;;:; .,'1 _ ,,,,,..
_
0;,
....
",
.. r'-' . -____ . ._.r
• m"o>
".'.'
".,... ...,"
-~
.
,-
_~
figure 16.2 Listiog the contents of the GaladicWar Iokle,. files are located. I've copied all of Ihe class and media files to a subfolder called project to keep things tidy. So, all I h~ve in this m~in G~laclicWar folder are index.htm!, manifesuxt. and the projecl subfolder (see Figure 16.2). The manifesl.lxt file for Galaclic War conlains this line: Mal n·Cl ass, G!la el i cWH This tells the jRE which of the .class files 10 open up and start runn ing after opening the JAR file. (Ik sure to include a blank line after the Mai n·Class property line.) You will need to use an optional parameter of the JAR program that lets you specify a subfolder where the actual files are located. You don't want to just tell it to include .Iprojectl·.· bc.~ng
the deployable JAR role
to add all files in Ihat subfolder to the JAR file. Figure \6.3 shows the output of the command. If thes ctHle>Thl$ 1$ ., giwclt HIe> clMid> (bod,)
(ipplu tode e glw.cl.lSS wldtll-flOO helght.ofiOO) (1Ippltt> (lbod,)(lht.l)
The key to running an applet inside a Java archive is 10 add anolheroplion within the
(/Ipplet>
1be wdlpa~ file is usualIyCl1kd inda..html ~\l.S(' that is w roune-of a 6.~ that wdl .sc.-rven will send the: wdl broWStt autolNtically if you don't specify the: HTML file directly. For inslalKe, when you go to www.jharbour.tom, W ..-cb ~r...er deli...ers index.html automat ically. You ClIn create Ihis simple HTML 6.~ using a text editor such as Notepad (as shown in Figure 16.4). If you want your applet to IX' stored with your other web files, induding your already existing index file, then just use a different name, suth as GalaclicWar.hlml.
~ ~~
>
_.
U.I~>G.oh.tl
-.
. ... ~ loy
wer: modulus 9. Whal is a good cbss 10 use whm you n«d 10 CTeate a billl1J.P in memory! An~wer: 8ufferedlaage
10. Which
A.~1I1:HedSprite
method d raws lhe current frame of animalion?
Answer: drawl l
Chapter 8 I. Whm is lhe name of the method used to enable keyboard events in your
progrom! Answer: .dd~eyL i
ste~er(
I
2. Whal ~ lhe n.aIDe of the keyboard ~'enl inlerface-!
Answer. (eytlstener J. Whal is the 'inual k~ code for lhe Enter ~ Answer: Y(..EntR 4. Which keyboard ",,'ent ..ill ldl you lhe code of.t pressed
key!
Aru....cr: Teehnic.aUy, any of lhe folJowing three (teyPreued(), keyReleised(), and keyTYl)edl) )
5. Which keyboard event willlell you when a key h"s been relcaS«!! Answer: keyPressed( l or keyTyped( l 6, Which keyboard eve nt will tdl you the character of" preSSl...d key! Answer: Techn ically, any of lhe following three (keyPreuell{ l, keyReleasell{), and keyTypedl) )
7. Which (eyEven! melhod relurns a key Galo11
< "RJ. S3 d"wA"mRd,{l n,,,,It.>d kor. ~ d"wRnll«.{j """"'nl ~". 61-61 d"wd for,61 ..,.....t'pdaI4 ...... b,_
-_
....--......I.r
''Pb'> of.
_\"'k>f5hartdooo ..... ...... d-. Sl>-S1
~
~.
.......... ! ... ! m l l _ .... la-I. ~
7J.s..-.'",~
1'0..........
" _ _ a,S7
_ _ _ s... 01.., " ' " " _....
«&Dod.
--._m
1IfdIj... " " )1.
............ s... "'",.......... . - : G.laip_b,~lo6
Ihrnd.-. b. 6J "I'dooe-91 n".bel PSG i""lI'. , ...."i"g, 9')-102 OMU' im>.gcs. 95--97 p"'l!"'mm ing, 89-90 TooIIO' , .... fo,. "" ,,,",fo,m, .pplying, 92--'14 '''''''I''"n, i""S'" 901. 97_99 8it_pT.., propom. 95--97 block .. lran'P"",n' «>101'" ,Lus. ~l-55 io 1,:. . inhcri...... in, 4l iaitO _booi. 145 wilh.Nd~ mtthod. IC-I~5
""""'"""" ........ "" w.
... MJO-"'-'I4,J
...fI:fI'grn""ning fo' tho Ab.alat, B'''' P.ge. (151'), 24 J Sound API, IS5-IS7 J S"nd.>nl Edi'ion 6 (J ..... SE 6), 10 ul>J".. '0, 11
J..... Wd> ScfYl ""ys,
M
'~7
Moc OS, C++ .nd. 26 moeros in T""tr,..{, 18 M.gic Woad ,001, 11'-102 ""';n ,Iaso d1\«1;on .nd. 26-27
.i"....
Bla,t, 9
Index
-
............ Ikt ./... op«ifo< - - - . Iloolt.- b. )2-JJ
-"-'--
.. OOP,......--.:t ..""
........:rtl'-., .""'""""'"
~s....J..,I C~.
_
-'
.... ~
~",""711
_ _ .1_14•
,
~
'-_~71_7}
c
~
_bln'o.lS
1 _ ~ Sti0llo In-17~ Mid".. y. ~ MIN V"IUfrrormy.~ MIolOIlPG ( ,..;...11 molti"""" 0fI11... rok-plaji p_~ 7 ............ lr\< pml
.. {.....,'" 1'0 .. 1""IfCI. l:I4
....0...-.
.,..,..... """""'' ' _)(11 'l'ril
&. 101. S« .1 ~1010
.n,,,,,',,,,, (
sm.cc:lo-.,»-JZ
-..."
S - M ~............
P...... II
T T.i,
~
s.... al... colli.ion ckt",,'inn
,...j
~h
I"" •.1Il-Jlb 1411-1~Z
........,
.uo--n·
,10K _
....... ll
S<tp« Mona 8"",_ 4 ~' ..... l.lI ~ ", ilily. "'1-' Coo>lrtIl
...... - " " '.... 11. fnr ( ......~" \'i.r rn-t. 2In ~["",.
,'l
Stn.c "'PI : , . . - " . "
_
4
,.,
.."....-
~1~160
"fM« I~"""""
t.-Mna."
~U9
........N.- 'f'I'I'< dooo.~ II. _ . od fnr. H __W I~hn"'y
,
II~
..
fnr. Ill- III
phl&'.,nm,"S
,,"'r'< >P"i'' ' . 101_1[1'/ 114
~mpk 'p,n... ID7-111 ....i'