0% found this document useful (0 votes)
11 views

Beginning Java Game Programming 2nd Edition Jonathan S. Harbour instant download

Beginning Java Game Programming 2nd Edition by Jonathan S. Harbour is a comprehensive guide aimed at teaching readers how to develop games using Java. The book covers essential programming concepts, game design principles, and practical examples to help beginners create their own games. It includes information on Java applets, compiling code, and the Java language itself, making it a valuable resource for aspiring game developers.

Uploaded by

vrabomesur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Beginning Java Game Programming 2nd Edition Jonathan S. Harbour instant download

Beginning Java Game Programming 2nd Edition by Jonathan S. Harbour is a comprehensive guide aimed at teaching readers how to develop games using Java. The book covers essential programming concepts, game design principles, and practical examples to help beginners create their own games. It includes information on Java applets, compiling code, and the Java language itself, making it a valuable resource for aspiring game developers.

Uploaded by

vrabomesur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

Beginning Java Game Programming 2nd Edition

Jonathan S. Harbour download

https://ebookultra.com/download/beginning-java-game-
programming-2nd-edition-jonathan-s-harbour/

Explore and download more ebooks or textbooks


at ebookultra.com
Here are some recommended products for you. Click the link to
download, or explore more at ebookultra.com

Multi Threaded Game Engine Design 1st Edition Jonathan


S.(Jonathan S. Harbour) Harbour

https://ebookultra.com/download/multi-threaded-game-engine-design-1st-
edition-jonathan-s-jonathan-s-harbour-harbour/

Beginning C Game Programming 2nd ed. Edition John Horton

https://ebookultra.com/download/beginning-c-game-programming-2nd-ed-
edition-john-horton/

XNA Game Studio 4 0 for Xbox 360 Developers 1st Edition


Jonathan S. Harbour

https://ebookultra.com/download/xna-game-studio-4-0-for-
xbox-360-developers-1st-edition-jonathan-s-harbour/

Advanced Java Game Programming Wallace Croft

https://ebookultra.com/download/advanced-java-game-programming-
wallace-croft/
Beginning Direct3D Game Programming Prima Tech s Game
Development 1st Edition Wolfgang Engel

https://ebookultra.com/download/beginning-direct3d-game-programming-
prima-tech-s-game-development-1st-edition-wolfgang-engel/

Killer Game Programming in Java 1st Edition Andrew Davison

https://ebookultra.com/download/killer-game-programming-in-java-1st-
edition-andrew-davison/

Beginning Programming with Java For Dummies 4th Edition


Barry Burd

https://ebookultra.com/download/beginning-programming-with-java-for-
dummies-4th-edition-barry-burd/

Beginning Programming with Java for Dummies 5th Edition


Barry Burd

https://ebookultra.com/download/beginning-programming-with-java-for-
dummies-5th-edition-barry-burd/

Beginning OpenGL Game Programming Second Edition Luke


(Luke Benstead) Benstead

https://ebookultra.com/download/beginning-opengl-game-programming-
second-edition-luke-luke-benstead-benstead/
Beginning Java Game Programming 2nd Edition
Jonathan S. Harbour Digital Instant Download
Author(s): Jonathan S. Harbour
ISBN(s): 9781598636499, 1598636499
Edition: 2
File Details: PDF, 3.26 MB
Year: 2007
Language: english
Beginning
JavaTM Game
Programming
Second Edition

Jonathan S. Harbour
ß 2008 Thomson Course Technology, a division of Thomson Learning Publisher and General Manager,
Inc. All rights reserved. No part of this book may be reproduced or Thomson Course Technology PTR:
transmitted in any form or by any means, electronic or mechanical, Stacy L. Hiquet
including photocopying, recording, or by any information storage or
retrieval system without written permission from Thomson Course Associate Director of Marketing:
Technology PTR, except for the inclusion of brief quotations in a review. Sarah O’Donnell

The Thomson Course Technology PTR logo and related trade dress are Manager of Editorial Services:
trademarks of Thomson Course Technology, a division of Thomson Heather Talbot
Learning Inc., and may not be used without written permission.
Marketing Manager:
Java is a trademark of Sun Microsystems, Inc. in the United States and
Jordan Casey
other countries. All other trademarks are the property of their
respective owners. Senior Acquisitions Editor:
Important: Thomson Course Technology PTR cannot provide software Emi Smith
support. Please contact the appropriate software manufacturer’s
technical support line or Web site for assistance. Project Editor/Copy Editor:
Cathleen D. Small
Thomson Course Technology PTR and the author have attempted
throughout this book to distinguish proprietary trademarks from Technical Reviewer:
descriptive terms by following the capitalization style used by the Dustin Clingman
manufacturer.
PTR Editorial Services Coordinator:
Information contained in this book has been obtained by Thomson Erin Johnson
Course Technology PTR from sources believed to be reliable. However,
because of the possibility of human or mechanical error by our sources, Interior Layout Tech:
Thomson Course Technology PTR, or others, the Publisher does not ICC Macmillan Inc.
guarantee the accuracy, adequacy, or completeness of any information
and is not responsible for any errors or omissions or the results Cover Designer:
obtained from use of such information. Readers should be particularly Mike Tanamachi
aware of the fact that the Internet is an ever-changing entity. Some facts
may have changed since this book went to press. CD-ROM Producer:
Brandon Penticuff
Educational facilities, companies, and organizations interested in
multiple copies or licensing of this book should contact the Publisher Indexer:
for quantity discount information. Training manuals, CD-ROMs, and Katherine Stimson
portions of this book are also available individually or can be tailored
for specific needs. Proofreader:
Kate Shoup
ISBN-10: 1-59863-476-3
ISBN-13: 978-1-59863-476-1
eISBN-10: 1-59863-649-9
Library of Congress Catalog Card Number: 2007938236
Printed in the United States of America
08 09 10 11 12 TW 10 9 8 7 6 5 4 3 2 1

Thomson Course Technology PTR,


a division of Thomson Learning Inc.
25 Thomson Place
Boston, MA 02210
http://www.courseptr.com
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.reinerstileset.de). Without their
wonderful graphics, Galactic War would have featured programmer art (cringe!).
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.jharbour.com.
Contents

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii

PART I JAVA FOR BEGINNERS . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

Chapter 1 Getting Started with Java . . . . . . . . . . . . . . . . . . . . . . . . 3


Java and the Web . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Studying the Market . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Design Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
The Casual Games Market . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
No Manual Required . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Casual Gamers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Casual Games. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Installing and Configuring Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Installing Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Configuring Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Java Version Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Your First Java Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Java Application. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Java Applet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
What You Have Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
On Your Own . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Exercise 1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Exercise 2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

vi
Contents vii

Chapter 2 Java Programming Essentials . . . . . . . . . . . . . . . . . . . . . 23


Java Applets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Web Server Technology Explained . . . . . . . . . . . . . . . . . . . . . . 24
Hosting Java Applets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Compiling Java Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
The Java Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Java Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
The Essence of Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
The main Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Object-Oriented Programming . . . . . . . . . . . . . . . . . . . . . . . . . 40
What You Have Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
On Your Own . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Exercise 1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Exercise 2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48

Chapter 3 Creating Your First Java Game . . . . . . . . . . . . . . . . . . . . 49


About the Game Project . . . . . . ..... . . . . . . . . . . . . . . . . . . . . . 49
Creating the Game . . . . . . . . . . ..... . . . . . . . . . . . . . . . . . . . . . 52
Creating the Project . . . . . . . ..... . . . . . . . . . . . . . . . . . . . . . 53
The BaseVectorShape Class . . ..... . . . . . . . . . . . . . . . . . . . . . 53
The Ship Class . . . . . . . . . . . ..... . . . . . . . . . . . . . . . . . . . . . 55
The Bullet Class . . . . . . . . . . ..... . . . . . . . . . . . . . . . . . . . . . 56
The Asteroid Class . . . . . . . . ..... . . . . . . . . . . . . . . . . . . . . . 57
The Main Source Code File . . ..... . . . . . . . . . . . . . . . . . . . . . 58
Applet init() Event . . . . . . . . ..... . . . . . . . . . . . . . . . . . . . . . 59
Applet update() Event . . . . . ..... . . . . . . . . . . . . . . . . . . . . . 60
Drawing the Player’s Ship. . . ..... . . . . . . . . . . . . . . . . . . . . . 61
Drawing the Bullets . . . . . . . ..... . . . . . . . . . . . . . . . . . . . . . 61
Drawing the Asteroids . . . . . ..... . . . . . . . . . . . . . . . . . . . . . 62
Screen Refresh . . . . . . . . . . . ..... . . . . . . . . . . . . . . . . . . . . . 63
Thread Events and the Game Loop . . . . . . . . . . . . . . . . . . . . . . 63
Game Loop Update . . . . . . . ..... . . . . . . . . . . . . . . . . . . . . . 64
Updating the Ship . . . . . . . . ..... . . . . . . . . . . . . . . . . . . . . . 65
Updating the Bullets . . . . . . ..... . . . . . . . . . . . . . . . . . . . . . 65
Updating the Asteroids . . . . ..... . . . . . . . . . . . . . . . . . . . . . 66
Testing for Collisions . . . . . . ..... . . . . . . . . . . . . . . . . . . . . . 68
Keyboard Events . . . . . . . . . ..... . . . . . . . . . . . . . . . . . . . . . 69
Calculating Realistic Motion . ..... . . . . . . . . . . . . . . . . . . . . . 71
viii Contents

What You Have Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73


Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
On Your Own . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Exercise 1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Exercise 2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74

PART II Java Game Programming . . . . . . . . . . . . . . . . . . . . . . . . 75

Chapter 4 Vector-Based Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . 77


Programming Vector Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Working with Shapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
Working with Polygons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
Rotating and Scaling Shapes. . . . . . . . . . . . . . . . . . . . . . . . . . . 83
What You Have Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
On Your Own . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Exercise 1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Exercise 2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88

Chapter 5 Bitmap-Based Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . 89


Programming Bitmapped Graphics. . . . . . . . . . . . . . . . . . . . . . . . . 89
Loading and Drawing Images . . . . . . . . . . . . . . . . . . . . . . . . . . 90
Applying Transforms to Images. . . . . . . . . . . . . . . . . . . . . . . . . 92
Transparency. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
Opaque Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
Transparent Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
Working Some Masking Magic . . . . . . . . . . . . . . . . . . . . . . . . . 99
What You Have Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
On Your Own . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
Exercise 1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
Exercise 2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105

Chapter 6 Simple Sprites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107


Programming Simple Sprites . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
The Point2D Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
Basic Game Entities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
The ImageEntity Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Creating a Reusable Sprite Class . . . . . . . . . . . . . . . . . . . . . . . . . 114
Contents ix

Collision Testing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114


Sprite Class Source Code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
Testing the Sprite Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
What You Have Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
On Your Own . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Exercise 1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Exercise 2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122

Chapter 7 Sprite Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123


Sprite Animation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Animation Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
Drawing Individual Frames . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
Keeping Track of Animation Frames . . . . . . . . . . . . . . . . . . . . 126
Testing Sprite Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Encapsulating Sprite Animation in a Class . . . . . . . . . . . . . . . . 132
Testing the AnimatedSprite Class . . . . . . . . . . . . . . . . . . . . . . 135
What You Have Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
On Your Own . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Exercise 1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Exercise 2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139

Chapter 8 Keyboard and Mouse Input . . . . . . . . . . . . . . . . . . . . . 141


Listening to the User. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
Keyboard Input. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
Listening for Keyboard Events . . . . . . . . . . . . . . . . . . . . . . . . 142
Testing Keyboard Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
Mouse Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
Reading Mouse Motion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
Detecting Mouse Buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Testing Mouse Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
What You Have Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
On Your Own . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
Exercise 1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
Exercise 2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154

Chapter 9 Sound Effects and Music . . . . . . . . . . . . . . . . . . . . . . . 155


Playing Digital Sample Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
x Contents

Getting Started with Java Sound . . . . . . . . . . . . . . . . . . . . . . 157


Playing Sounds. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
Playing MIDI Sequence Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
Loading a MIDI File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
Playing Music. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
Reusable Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
The SoundClip Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
The MidiSequence Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
What You Have Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
On Your Own . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
Exercise 1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
Exercise 2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176

Chapter 10 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
Recovering Long-Lost Applet Methods . . . . . . . . . . . . . . . . . . 183
Stepping Up to Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
Starting and Stopping the Thread. . . . . . . . . . . . . . . . . . . . . . 184
The ThreadedLoop Program . . . . . . . . . . . . . . . . . . . . . . . . . . 185
Examining Multithreading . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
What You Have Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
On Your Own . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
Exercise 1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
Exercise 2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190

PART III THE GALACTIC WAR PROJECT . . . . . . . . . . . . . . . . . . . . 191

Chapter 11 Galactic War: From Vectors to Bitmaps . . . . . . . . . . . . . 193


Improving the Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
Generalizing the Vector Classes . . . . . . . . . . . . . . . . . . . . . . . 194
The Main Source Code File: GalacticWar.java . . . . . . . . . . . . . 197
What You Have Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
On Your Own . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
Contents xi

Chapter 12 Galactic War: Sprites and Collision Boxes . . . . . . . . . . . 207


Creating the Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
The Galactic War Bitmaps . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
The New and Improved Source Code . . . . . . . . . . . . . . . . . . . 210
What You Have Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
On Your Own . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225

Chapter 13 Galactic War: Squashed by Space Rocks . . . . . . . . . . . . 227


Being Civilized about Collisions . . . . . . . . . . . . . . . . . . . . . . . . . . 227
What You Have Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
On Your Own . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234

Chapter 14 Galactic War: Entity Management . . . . . . . . . . . . . . . . 235


Adjusting to Event-Driven Programming . . . . . . . . . . . . . . . . . . . 236
Exploring the Class Library . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
Building the New Game Class . . . . . . . . . . . . . . . . . . . . . . . . . 237
Enhancing Galactic War. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
Exploring the New Galactic War Source Code . . . . . . . . . . . . . 249
What You Have Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
On Your Own . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274

Chapter 15 Galactic War: Finishing the Game . . . . . . . . . . . . . . . . . 275


Let’s Talk about Powerups. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
Ship and Bonus-Point Powerups . . . . . . . . . . . . . . . . . . . . . . . 276
Weapon Upgrades . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
Enhancing Galactic War. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
New Sprite Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
New Game States . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
New Sprite Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282
Health/Shield Meters, Score, Firepower, and
Game State Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
New Input Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
Sound and Music Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
Loading Media Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
Game State Issue—Resetting the Game. . . . . . . . . . . . . . . . . . 287
Detecting the Game-Over State . . . . . . . . . . . . . . . . . . . . . . . 288
Screen Refresh Updates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
xii Contents

Preparing to End . . . . . . . . . . . . ........ . . . . . . . . . . . . . . 291


Updating New Sprites. . . . . . . . . ........ . . . . . . . . . . . . . . 292
Grabbing Powerups . . . . . . . . . . ........ . . . . . . . . . . . . . . 294
New Input Keys . . . . . . . . . . . . . ........ . . . . . . . . . . . . . . 296
Spawning Powerups . . . . . . . . . . ........ . . . . . . . . . . . . . . 299
Making the Shield Work. . . . . . . ........ . . . . . . . . . . . . . . 301
Making Use of Weapon Upgrade Powerups . . . . . . . . . . . . . . 302
Tallying the Score . . . . . . . . . . . ........ . . . . . . . . . . . . . . 305
What You Have Learned . . . . . . . . . ........ . . . . . . . . . . . . . . 306
Review Questions . . . . . . . . . . . . . . ........ . . . . . . . . . . . . . . 306
On Your Own . . . . . . . . . . . . . . . . . ........ . . . . . . . . . . . . . . 307

Chapter 16 Galactic War: Web Deployment . . . . . . . . . . . . . . . . . . 309


Packaging an Applet in a Java Archive (JAR) . . . . . . . . . . . . . . . . 309
Using the jar.exe Program . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
Packaging Galactic War in a Java Archive . . . . . . . . . . . . . . . . 312
Creating an HTML Host File for Your Applet . . . . . . . . . . . . . . . . 315
A Simple HTML File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
Testing the Deployed Applet Game . . . . . . . . . . . . . . . . . . . . 317
What You Have Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
Epilogue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320

Appendix A: Chapter Quiz Answers. . . . . . . . . . . . . . . . . . . . . . . . . . 321


Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
Introduction

This book will teach you how to write Java games that will run as applets in a web
browser. The goal is to develop games for the casual game market. Game pro-
gramming is a challenging subject that is not just difficult to master—it is
difficult just to get started. This book takes away some of the mystery of game
programming by explaining each step along the way, from one chapter to the
next. I assume that you have a little Java programming experience, but even if you
have never used Java before, you should be able to keep up.
This book reads like a hobby book, with no pressure and limited goals, because the
primary purpose of this book is to help you have fun learning how to program web-
based games. Typing in long source code listings out of a book is not fun, so I don’t
ask you to do that in every single chapter in this book. Instead, you will learn to write
short programs to demonstrate the major topics, and over time you will get the hang
of it. There is no memorization required here, because I’m a firm believer that
repetition—practice—is the best way to learn, not theory and memorization.
Note
Java is a programming language invented by Sun Microsystems. The primary goal of Java’s
developers was to be able to compile a program once and have it run on many different computer
systems. The Java compiler creates a ‘‘bytecode’’ file containing virtual machine instructions that
the JRE (Java Runtime Environment ) can execute on any computer system upon which it is
installed.

You will learn how to write a simple Java program in the first chapter. From
there, you will learn the details of how to write games that will run in a web
xiii
xiv Introduction

browser. We cover source code at a pace that will not leave you behind. After you
have learned enough, you will write your first web-based game, and you will then
learn new subjects in each chapter to increase your game programming skills. By
the end of this book, you will have learned to create a complete web-based game
called Galactic War, and you will be able to deploy it to your website in a Java
Archive (JAR) file. And I’m not talking about some half-baked simulation posing
as a game; I’m talking about a real game that is retail quality, suitable for pub-
lishing in the casual game market. There are thousands of casual gamers who are
paying to download games of this type from the many casual game sites on the
web today—such as Real Arcade (www.realarcade.com). By learning how to
create a casual game, you may even be preparing for a career in the game
industry, developing games for Microsoft Xbox Live Arcade and other com-
mercial endeavors.
Note
Web-based games are video games that are installed on a website and run in a web browser, so
that end users do not need to install the game. Some games are able to store high-score lists and
player data on the web server. The most popular type of web game is a ‘‘casual game.’’

While we’re on the subject of casual games, you can even program your own
Xbox 360 games, distributed on Xbox Live Arcade, using Microsoft’s free XNA
Game Studio Express software. Although this subject is beyond the scope of this
book, I bring it up because Microsoft’s C# language is unabashedly similar to
Java. Thomson published a book on this subject by Joseph Hall, titled XNA Game
Studio Express: Developing Games for Windows and the Xbox 360 (Thomson
Course Technology PTR, 2007).
It all begins here! Are you serious about this subject and willing to learn? As a
senior instructor of game development, I am scrutinized daily by students who
eat, drink, and breathe video games. I cannot create something that stinks, or I’ll
never hear the end of it! So I am as motivated to teach you cutting-edge game
development techniques here as I am in a real classroom setting, by students who
are paying a lot more than the retail cost of this book to learn these concepts. I
have used this book in several Java courses already, so you are guaranteed high-
quality material in these pages that will not be a waste of your time.

What Will You Learn in This Book?


This book will teach you the difference between Java applications and applets
(which run in a web browser). You will then learn about Java’s graphics classes
Random documents with unrelated
content Scribd suggests to you:
too—it is wonderful where the driving force comes from to keep
things going on.
"But still—I don't want to convey a false impression. If you took
my last letter by itself you might think things were very terrible
out here all the time. They are not. On the whole it is not a bad
time at all. The life is full of interest, and the discomforts are
few and far between. Bad times do come along occasionally, but
they are by way of exceptions. It is most like a long picnic in all
sorts of places with a sort of constraint and uneasiness in the
air. This last is purely mental, and the less one worries about it
the less it is, and so one can contrive to be light-hearted and
happy through it all—unless one starts to get depressed and
moody. And it is just that which has happened to Laws and
Fletcher and one or two others. They had been out long and
had seen unpleasant times and without an occasional rest; none
but the very thick can stand it."

"Saturday, 26 June 1915, 6.40 p.m.


"Here I am installed in the school [Machine Gun] which is, or
was, a convent. Fine large place and grounds. Two officers per
bedroom and a large Mess-room; about twenty officers up for
the course (or more) which starts to-morrow (Sunday). Your
solution of the Thompson acrostic [St. Omer] was perfectly
right, we are far back. This convent is about two miles from that
town.
"I am so pleased to be in the 'pleasant, sunny land of France,'
amid absolute peacefulness. We had a curious journey. Last
night I slept at our transport (and had a bath!). I got up soon
after six, mounted a horse just before eight (after breakfast).
My servant and my valise, also a groom to bring my horse back,
came in a limber. And that excellent man Polchet rode all the
way to Divisional Headquarters with me, although it was about
six miles out of his way. We got to Headquarters at a quarter to
ten—a motor-bus was to start at ten for here. It started at
10.30 with me, my luggage, and my servant (I don't know why
he comes last) in it. The Harborne motor-buses in the Harborne
High Street weren't in it. We got shaken to a jelly—we were on
top. We went back about two miles to pick up some of our
Division, and having done so, we set off to pick up some of the
14th Division, at a point carefully specified in our driver's
instructions. This was about five miles away, in our proper
direction. But when we got to the spot we discovered they (the
Division) had left it a week ago and gone to a point quite close
to where we had just picked up the 3rd Division men. I
telephoned in vain; we had to go all the way back. We found
the place with difficulty (we found all our places with difficulty
as we had no maps), collected the men, and came all the way
out again. Then we came straight here, which was about fifteen
miles at least. We got here at 4.30 p.m.! Six hours'
motorbussing! and the bus's maximum was 25 m.p.h. at least, I
should judge. Luckily it was a glorious day, and I sat in front
with the driver and enjoyed it all....
"I told you leave was starting—well, it has now started. Three of
our officers have gone—and all together! They are only getting
three clear days in England—but still!
"I am going to find out when this course finishes—I think it lasts
for sixteen days—and then I am going to apply for my leave to
follow on. I wish—oh, how I wish—I may get it; but of course
many things may intervene.
"If it does come off I hope there will be a representative
gathering to meet me at dinner. That is, I hope Violet will be
back from Edinburgh, Lorna and Norah from Coniston, and
perhaps Oliver and his Winifred will pay a flying visit from
Cardiff. Haven't I got an enlarged opinion of my own
importance? I suppose it is too much to expect the offices to
have a whole holiday!"
"Monday, 28 June 1915, 6.15 p.m.
"The enemy's lines round here do not appear to be strongly
held, in fact quite the reverse—that is, the front lines. But
attacks on our part don't always pay—even so. Their method, as
I understand it, is simply to lose less men than we do.
Accordingly, they leave very few men in their front trench, but
what there are have a good supply of machine guns and are
well supported by artillery. We precede our attacks by heavy
shelling, and the few men get into well-built dug-outs until it is
over, then they come out and get to work with their machine
guns on the attacking infantry. The trench ultimately falls after
rather heavy loss on our side (especially if the wire isn't properly
cut) and the few defenders hold up their hands. Some are made
prisoners—some are not. If the enemy want the trench very
badly they try and retake it by means of a strong counter-
attack, trusting that our men and arrangements are in sufficient
confusion to prevent adequate support. That is why our attacks
are so expensive and why we aren't constantly attacking. The
alternative plan is, I think, simply to shell them heavily—in all
their lines—and leave out the actual attack in most cases....
"I was so interested to hear that Alec had applied for me to
come back. It is not at all impossible, because I have known
two or three cases where officers have been recalled—one was
chief chemist (or so he said) at Brunner Mond's. He was
returning as I came out, and tried to make one's flesh creep by
his tales of war. But I don't think it is likely to happen in my
case. I only wish it would. I should love to come home again,
although I don't feel as if I had done my bit yet—really. I
haven't been in any big scrap, and I haven't killed my man
even....
"I had a ripping time at the transport; I hope they enjoyed the
peas—they deserved to. They were hospitality itself. They
welcomed me, gave me three meals, lent me anything I
wanted, made room for me to sleep in their large room (this
necessitated the Quartermaster-Sergeant moving his bed into
another room), gave me a warm bath, and generally made me
feel quite at home. They have a ripping dug-out. Rooms half
underground, 7 feet high, plenty of ventilation, boarded floor
and walls, and a wooden roof supported on square wooden
pillars and covered in earth well sodded on top....
"Talking about the Major (Major Cotton), he used to be our
Adjutant at Crosby—he was Captain then. He came out as
second in command and has now got the Battalion while our
Colonel (Colonel Dudgeon) is away sick. The latter got his C.B.
in the last honours list. He is an excellent man. Lieut. Burlton,
too, got a Military Cross. He has now been wounded twice; he
was the moving spirit of the hockey matches at Crosby in the
old days, and, when he was recalled to the Front, his mantle fell
upon me....
"All the officers here are from different regiments with a very
few exceptions. It is most interesting. At meals, Way and I sit
among the Cavalry, Dragoons and Lancers, etc. They are fine
chaps—the real Army officers of which there are now all too
few."

"Machine-Gun School, G.H.Q.,


Wednesday, 7 July 1915, 5 p.m.
"Here I am getting towards the end of my little holiday, only five
more days to go. No word has reached me from my Battalion on
the subject of leave, or of anything else for that matter....
"If this threatened push on Calais is real, or if the higher
commands have got 'wind up' about it, they will very likely stop
all leave, and then I shall just have to wait until it starts
again....
"I am sure that the fact of our nation being 'down' and
preparing for a winter campaign will materially assist in
shortening the war and rendering that preparation unnecessary.
"We have an awfully amusing chap here who is in the Grenadier
Guards. He is always imitating Harry Tate. A great big hefty
chap, in great big sloppy clothes (including what are known as
'Prince of Wales' breeches). He gets his mouth right over to the
side of his face and says 'You stupid boy!' in Harry Tate's voice.
He does this in the middle of our instructional squads when
some wretched person does something wrong with the gun, and
sends every one into fits of laughter.... [A lot more about a
motor that wouldn't go.]
"My M.G. course is going on very nicely. I have learnt a very
great deal, have been intensely interested, and am very keen on
the work. My function as a reserve machine-gunner should
really be to train the reserve team and such parts of the main
team as are not actually required in the trenches, in a safe spot
behind the lines! It sounds 'cushy,' but those in authority over
us are not sufficiently enlightened, I am afraid, to adopt such a
plan. The object of course is to prevent your reserve men from
being 'used up' as riflemen, as otherwise when you want them
to take the place of the others they are casualties and all their
training goes for nothing.
The Cavalry officers here are a great joke. They find this life
very tiring. They are quite keen to get back again and have
been from the beginning. We, on the other hand, fairly enjoy it
and are not at all anxious to go back to our regiments. That
shows the difference between the lives we lead. Of course they
have been in the trenches and have had some very bad times
there, but they only go in in emergencies and at long
intervals....
"Another difference between us is that they keep their buttons
as bright as possible and themselves as spick and span as can
be. The infantry officer gets his buttons as dull as possible, and
if they are green so much the better, as it shows he has been
through gas. He likes his clothes and especially his puttees to be
rather torn, and his hat to be any old sloppy shape. If he gets a
new hat he is almost ashamed to wear it—he is terrified of
being mistaken for 'Kitcheners'!
"Lord Kitchener and Mr. Asquith came here last evening. Here,
to this convent. I don't know what for; but there was of course
a good deal of stir here.
"Way and I went into the town last night. We hired a fiacre for
the return journey. It came on to rain, so it was just as well we
had a hood. We both thoroughly enjoyed the journey. The fiacre
was what would be dignified by the name of 'Victoria' in
England. But in France, where it seems to be etiquette not to
take any trouble over carriagework, fiacre is the only word you
could apply, and it just fits it. It expresses not only its
shabbiness but also hints at its broken-backed appearance.
"We went into some stables and inquired about a fiacre, and a
fat boy in a blue apron with a white handkerchief tied over one
eye said we could have one. So I said, 'Où est le cocher?' and
he pointed to his breast and said, 'C'est moi!'
"The fare, he said, would be six francs and the pourboire.
Thoughtful of him not to forget that. We agreed, and he
eventually produced the usual French horse.
"The fiacre was very comfortable and we were awfully tickled
with the idea of us two in that absurd conveyance, especially
when we passed staff officers, which was frequently. Altogether
we were quite sorry when our drive was over."

NOTE BY O. J. L.
On 16 July 1915, Raymond came home on leave, and he had a great
reception. On 20 July he went back.
"Sunday, 25 July 1915, 7.30 p.m.
"I have got quite a nice dug-out, with a chair and table in it.
The table was away from the door and got no light, so I have
spent about two hours to-day turning things round. I went to
bed about three this morning (just after 'stand-to') and slept till
nearly twelve. Then I had breakfast (bacon and eggs). As my
former platoon Sergeant remarked: 'It is a great thing to have a
few comforts, it makes you forget there is a war,'
"So it does until a whizz-bang comes over.
"I have just seen an aeroplane brought down (German luckily).
I missed the first part, where one of ours went up to it and a
flame shot across between them (machine gun, I expect). I ran
out just in time to see the machine descending on fire. It came
down quite steadily inside our lines (about a mile or more
away), but the flames were quite clearly visible,"

"Thursday, 29 July 1915, 7.35 p.m.


"Here I am in the trenches again, quite like old times, and quite
in the swing again after the unsettling effect of coming home!
You know I can't help laughing at things out here. The curious
aspect of things sometimes comes and hits me, and I sit down
and laugh (not insanely or hysterically, bien entendu; but I just
can't help chuckling). It is so absurd, the reasons and causes
that have drawn me to this particular and unlikely field in
Belgium, and, having arrived here, that make me set about at
once house-hunting—for all the world as if it was the most
natural thing in life. And having selected my little house and
arranged all my belongings in it, I regard it as home and spend
a few days there. And then one morning my servant and I, we
pack up everything once more and hoist them on to our backs
and set off, staff in hand, like a pair of gipsies to another field a
mile or so distant, and there make a new home....
"I was very loth to leave my front line dug-out, because I had
arranged things to my liking—had moved the table so that it
caught the light, and so on. It had a built-in table (which took a
lot of moving), a chair and a sandbag bed. Quite small and
snug.
"But still—this new dug-out back here is quite nice. Large and
roomy, with windows with bars in them (but no glass)—a proper
square table on four legs—three chairs and a sandbag bed. So I
am quite happy. The sandbag bed is apparently made as
follows: Cover a portion of the floor, 6 feet 6 inches by 3 feet 6
inches, with a single layer of sandbags filled with earth. Over
these place several layers of empty sandbags, and the bed is
finished. If the hollows and lumps are carefully placed, the
former in the middle and the latter at the head, the result is
quite a success. Of course one sleeps in one's clothes covered
by a coat and with an air pillow under one's head.
"We have had a very gay time in the trenches. I think I told you
how I saw a hostile aeroplane brought down on fire in our lines.
That was on Sunday, and the official report says both pilots
killed. On Monday I went down to a support trench to have
meat tea and a chat with Holden and Ventris (two of C
Company officers). At a quarter to ten there was a loud
rumbling explosion and the dug-out we were in rocked for
several seconds. The Germans had fired a mine about 60 feet in
front of our trench to try to blow in some of our workings.
"I rushed to my guns—both were quite safe. You should have
heard the noise. Every man in the place got up to the parapet
and blazed away for all he was worth. It was exciting! One
machine gun fired two belts (500 rounds), and the other fifty
rounds. I heard afterwards that several of the enemy were seen
to leap their parapets, but turned back when they heard the
machine guns open fire. It took a good while for things to
quieten down. Some of our miners were at work when it went
off, but their gallery was some way off and they were quite all
right.
"Last night they actually exploded another one! Aren't they
keen? This was a much smaller affair, but closer to our trench. It
shook down a portion of our parapet, which was easily rebuilt
and entombed temporarily two of our miners. In neither case
were there any casualties....
"I am so sorry the date of the wedding had to be altered, but I
agree it was for the best. I only hope you remembered to
inform the bridegroom—he is often forgotten on these
occasions, and I have known a lot of trouble caused by just this
omission."

Letter from Raymond to Mrs. Fred Stratton,


formerly Miss Marjorie Gunn

"1 August 1915, Sunday, 11.20 p.m.


"I am not actually in the trenches at the moment, though most
of the Battalion is. I was in for five days, and then I was
relieved about four days ago by another officer (Roscoe), who
shares with me the duties of machine-gun officer. So I am in a
dug-out about three-quarters of a mile behind the firing line
while he is taking his turn in that line. (A mine has just gone off
and shaken the ground, followed by a burst of heavy rifle firing.
This makes the fourth mine this week! Two went off while I was
up there, and the whole earth rocked for several seconds. The
first three mines were theirs, this last may be ours, I don't
know; we had one ready!)
"We have been at Hill 60 and also up at Ypres. At present we
are south of that appalling place, but I learn with regret that to-
morrow we are moving again and are going up north of Ypres.
We are all depressed in consequence.
"What an awfully good letter you have written me; but, do you
know, it makes me ache all over when you write like that about
the car. You have only to mention you have got a Rover, and I
am as keen as mustard to come and tinker with it! Aren't I
young?
"But you must know I want to come to New Park in any case. I
am awfully keen to stay there and see it from inside, and see its
inmates again after many years (it feels like). So after the war
(may it be soon!) I am just going to arrive. I may let you know!
"Your remarks on weddings in general depress me very much! I
hope the bridegroom's lot is better than the poor bride's.
Because my turn is bound to come!
"I am so glad Hester gave a good account of my appearance. I
am very fit, it is the only way to exist here. Once you begin to
get 'down' and to worry, it is all up with you. You go into a rapid
decline, and eventually arrive home a wreck! But as long as you
smile and don't care a hang about anything, well the war seems
to go on quite all right!
"I enjoyed my few days' leave very much indeed. I had five
days in England and three full days and four nights at home. I
dropped into my old life just as if no change had occurred. And
the time was not long enough to make the getting back difficult.
"This life is a change for me, as you say. I haven't done
laughing at its humorous side yet. In some ways we get treated
like schoolboys. More so at Crosby than here, however."

"Saturday, 7 August 1915, 7.30 p.m.


"I have been having rather a bad time lately,—one of those
times that reminds one that it is war and not a picnic,—but,
thank goodness, it is all over now.
"I think I told you that we were about to move up north of
Ypres, to St. Julien or thereabouts. Well, just before we handed
over these trenches to one of Kitchener's Battalions, the
Germans went and knocked down a lot of our parapet, and also
sent over some appalling things that we call 'sausages,' or
'aerial torpedoes,' though they are not the latter. They are great
shell-shaped affairs, about 3 feet along and 9 inches in
diameter, I should think. They are visible during the whole of
their flight. They are thrown up about 100 yards into the air and
fall down as they go up, broadside on—not point first. A few
seconds after they fall there is the most appalling explosion I
have ever heard. From a distance of 100 yards the rush of air is
so strong that it feels as if the thing had gone off close at hand.
Luckily there is a slight explosion when they are sent up, and,
as I said, they are visible all the time in the air. The result is our
men have time to dodge them, provided they are not
mesmerised as one man was. He got stuck with his mouth
open, pointing at one! A Corporal gave him a push which sent
him 10 yards, and the 'sausage' landed not far from where he
had been. Although they have sent more than twenty of these
things over altogether, we have only had one casualty, and that
a scratch. Their effect is to terrify every one and keep them on
tenterhooks watching for them. Their purpose is to destroy mine
galleries, I believe....
"Monday, August the 2nd, was the day we should have been
relieved, and that night I went up from headquarters and
relieved Roscoe, who had had a bad time in the fire trenches....
"They were firing armour-piercing shells that go right in and
blow the parapet to blazes; dug-outs too, of course, if they
happen to be near. After punishing the right end of the left-hand
bit of trench, they traversed along, laying waste the whole of
our bit.
"I was in my dug-out with Hogg, another officer. I was trying to
make tea, but every shell blew out the Primus, and covered us
in dust. I made it, however, eventually, and we had just drunk it
when a shell blew the parados of the trench down, not far from
our door, and the next wrecked the dug-out next door to mine
(a man who happened to be inside having a miraculous
escape). We judged it was time to clear (the machine guns had
already been withdrawn to safety), and got away as best we
could through and over the debris that had been a trench.
"Later in the day I made my way back, and recovered my pack
and most of my belongings. It was exciting work getting back,
because they were sending whizz-bangs through the gaps in the
parapet, and the communication trenches in the rear were
blocked in places, so that you had to get up on top and 'scoot'
across and drop in the trench again.
"That evening they gave us a second shelling, and one hit my
dug-out fair and square (I had quarters in a support trench).
When I returned next day for the rest of my things—my
equipment and some provisions—I had to put two men on to
dig them out. It took three-quarters of an hour to get at them,
through the wreckage of timber, corrugated iron, and earth....
"On Tuesday afternoon they sent off another mine,—about the
seventh since we have been in,—but they are all well in front of
our parapet. And on Wednesday they gave us twelve sausages
—the first I had seen.
"The trouble is, we have a number of mine shafts under the
ground between our trenches and theirs, and they are fearfully
'windy' about them. They keep trying to stop us mining them,
and their shelling is with the object of blowing down our sap-
heads. Their mines, too, go up short, because they are trying to
blow in our galleries; or else they are so scared they send them
off before they are ready. I think the last explanation is probably
more near the truth, because when one of their mines went up
recently a lot of Germans went up with it!...
"We have been in here a fortnight to-night. You can imagine
how we long for clean clothes. Most of the officers have not
been out of their clothes all that time, but I have been very
lucky. I had two good cold baths when I was down here before,
and to-day I had a lovely hot one in a full-length wooden bath.
A tremendous luxury! Also I had some clean socks to put on....
"On the day I was shelled out of my dug-out my servant, Bailey,
was hit on the leg by a piece of shell and has gone down the
line wounded, not very seriously, I think. He is a great loss to
me, but I have got another one now, Gray, who shapes very
well. He is young and willing, and quite intelligent.
"You ask whether that time when the mine went off was the
first time I had used these guns. Yes, absolutely. The plan
adopted in trench warfare is to place your guns in position with
a good wide loophole in front of them, then block this up and
keep a sharp look-out. When the enemy attacks, you blaze away
at them, and then shift hurriedly to another gun-position and
watch the old one being shelled to blazes.
"If you fire on other occasions you are rather apt to have your
guns knocked out, and we can't afford to lose any. That is why I
was rather horrified to find one gun had fired 500 rounds the
other night. However, it was not discovered. I think the long
grass in front hid the flashes....
"Yes, the sandbags might be damp when used for a bed, and I
always lay my waterproof ground-sheet on top of them. I either
sleep on that or on some new clean bags laid above that again.
It is not only dampness, though, that one fears!
"As a matter of fact, one is not very sensitive to damp when
living so much out of doors. It is common to get one's feet
slightly wet and go for about four days without removing one's
boots—most unpleasant, but not in the least damaging to
health."
"Monday, 16 August 1915, Noon
"We are now out and resting after doing a long spell. I did
nineteen days, and some did a few more days than that. Three
weeks is a long time to live continuously in clothes, boots, and
puttees....
"I came out of the trenches on Thursday night, and was really a
day too soon, because on Friday we were having Orderly-Room
right in the country, in front of the C.O.'s tent; the Colonel was
there surrounded by most of the officers, when we heard a
shell. Well, that's nothing unusual, but this one got crescendo,
and we all looked up in alarm. Then it got very crescendo, and
finally cleared us and landed with a loud explosion about 50
yards beyond us, and not far from several groups of men. It
was an 8-inch 'crump.' One man only was killed, but we knew
that more were likely to come over, and so we gradually spread
out to the sides. Four came altogether at two-minute intervals,
but we only had two casualties. Rather upsetting when we were
supposed to be resting. I don't know whether they could see
our (officers') white tents, or whether they saw the cricket
match that took place on the day before.
"Anyway we moved our tents slightly—every one put their tents
where they pleased, and then the Pioneer Sergeant came and
amused himself daubing green paint on them in patches. Ours
(three of C Coy.) was the best; the splodges looked just like
hazel nuts (?) when there are three together in their little green
cases, and they were interspersed with a kind of pansy-shaped
flower. Altogether a very tasteful and pleasing effect....
"A couple of gun stocks have come. They arrived from Walker's,
the makers, and I should very much like to know who had them
sent. They are ripping, sniping attachments with periscopes for
use with the ordinary rifle. I shall stick to one, and unless I hear
otherwise I shall present the other one to our sniping officer
(honorary rank)."[5]
"Wednesday, 25 August 1915, 3 p.m.
"I am in the trenches once more. We marched in (about 10
miles) last night. We had a meal at 3 p.m., and marched off
soon after six. Our rations (officers') went astray, because they
were on a hand-cart in charge of our servants, who missed their
way, so we have had practically nothing to eat since late lunch
yesterday, and are pretty hungry. I have had a piece of
chocolate, and my water-bottle was nearly full of lemon
squash....
"We are in support trenches at Hooge, just on the left of our
former position up here. Except for some shelling (chiefly ours),
things are fairly quiet.
"Since we were here last the position is greatly improved; the
Germans have been driven over the ridge in front (during the
recapture of trenches here), and the whole place is much
'healthier' in consequence....
"I have been out here five calendar months to-day, and in the
Army just over eleven months. They will be pensioning me off
soon as an old soldier."

"29 August 1915, 11.30 a.m.


"I am having a very quiet and lazy time at the moment, and feel
I deserve it. We went into support trenches for three days, and
worked two nights from 7.30 p.m. till 3 a.m. building and
improving the fire trench. Then on the third night we had a
most exciting time. One company, under Captain Taylor, was
sent up right in front to dig a new fire trench to connect with
another on our left. We had to go up a trench which ran right
out into space, and which had only just been built itself, and
when there we had to get over the parapet and creep forward
to the new line we were to dig. Of course we had to be dead
quiet, but there was a big moon, and of course they saw us.
Most of the way we were not more than 30 yards away from
their front position (and they had bombing parties out in front
of that). While we were digging we had one platoon with bombs
to cover us, and some of this party were as close as 25 yards to
their front position. It was awful work, because they kept
throwing bombs at us, and what was almost worse was the
close-range sniping.
"'Very' lights were going up from the German lines all the time,
and you could see the bullets kicking up the dust all around.
When we first got out there I picked out my ground pretty
carefully before lying down (because the recent scrap there was
much in evidence), but when the snipers got busy I didn't worry
about what I was on, I just hugged the ground as close as I
could. They would put the 'Very' lights right into us, and one
just missed me by a yard. If they are not spent when they come
down, they blaze fiercely on the ground, and when they finish,
they look like a little coke fire. They would burn you badly if
they fell on you. I have seen a dead man that one had fallen on
afterwards. His clothes were fearfully burned.
"The Germans were on the edge of a wood and our ground was
tipped towards them, so it was extremely difficult to get cover.
Shell holes were the best. Soon the men got their trenches
down, and things were a little better. The men worked
extremely well, and the Wilts were working on our left, and we
eventually joined up with them. After about five hours' work,
the trenches were fit to hold, and we filed out and the new
garrison filed in. Our casualties were much lighter than I should
have thought possible. The Colonel came along the new
trenches just before we left, and he was most awfully pleased
with C Company, and so is the General. Captain Taylor is very
bucked about it.
"The scene of this affair was right against the Château of
Hooge, and close to the mine crater. We found a German
machine gun half buried, but in good condition, and any
number of souvenirs. The Captain has got a helmet—a dirty
thing; he had to have it cleaned out, because part of the owner
was still inside it! It is a rummy shape, so flat-topped and
square, with a brass spike and a gold band down the back. I
expect it was an officer's.
"Oh! I have seen my first German (not counting prisoners). I
was standing up and a 'Very' light went up, so I kept perfectly
still. I was looking towards the wood where the Germans were
(I was 40 or 50 yards away), and I saw one quite distinctly
walking into the wood.
"Our men that were killed (sniped) were buried just behind,
within a quarter of an hour of being hit. Rather awful.
"The actual digging was rather trying in places, and in one case
they actually came on a horse!—which dates it back to
November, when we were pushed back to these positions in the
first battle of Ypres.
"The men in such places work with their respirators on and are
often actually sick. I have had whiffs of the smell since in my
food. Once smelt never forgotten. I can tell the difference
between a man and a horse, but I don't know which I like least.
"Rather a morbid topic, I am afraid. Well, after leaving the
scene of our labours (and glad to get out), we called for our
packs and had to march about two and a half miles. We were
dead beat when we arrived here (nice safe dug-outs—roomy
and comfortable—with our valises ready to sleep in when we
arrived), but we found a good meal awaiting us, and about half-
past four we 'got down to it' and slept till noon. Holden and I
share a palatial dug-out, and we had breakfast in bed, and I did
not get up till just before our evening meal at 7. I washed and
dressed in slacks—had a meal, and later on went to bed again.
This morning we had breakfast in bed again about 9.30, and
then I got up, washed and shaved, dressed, and am now sitting
on my bed, leaning against the wall writing my letters.
"The General let us off 'stand-to' because he knew we were
fagged out; and it is a great mercy. Turning out fully dressed at
about 2.30 a.m. and remaining up for an hour does not improve
one's night's rest. I suppose, though, that we shall have to start
it soon—perhaps to-night.
"We are here till to-morrow night, I believe, and then we go to
some fairly nice trenches near the ones we were in last. We are
short of subalterns—rather—and they have taken me off
machine guns for the time being. I am sick, but I get a bit in
when I can. In the last trench we built (I and my platoon), not
the exposed one, there was a machine-gun position, and I took
great pleasure in building it a really good emplacement....
"Are you doing anything about getting me back for Munitions? I
don't know what you think about it, and whether you think I
ought to carry on out here. I am sure that after six months I
shall be just about fed-up with this business, but am not sure
that after a couple of months at home I shan't be wanting to
come out again."

"Wednesday, 1 September 1915, 4.45 p.m.


"I will just write you a short letter to let you know I am still well
and happy, and still leading the strange life of the picnic-hermit.
"When I last wrote to you I believe I was in the very same spot
as now, namely, support trenches in the neighbourhood of a
now famous château. Last time we were in for three days, and
on the night we left we had a very blood-curdling experience
digging a trench which was to bring us closer to our friends the
enemy. But they were inclined to resent our advances, and they
welcomed us, not with open arms, but with lighted bombs.
However, having completed our work to the great satisfaction of
those in authority over us (namely, the Colonel and the General
[Brigadier]), we made good our escape.
"Then for three blissful days we lived (with our valises) in some
magnificent dug-outs in one of the safest spots in this accursed
though much improved neighbourhood. These days we spent
competing who could sleep furthest round the clock (if that is a
permissible expression). I think I won, and on my record day I
got up and dressed for dinner at about 7.30 p.m., made my bed
afterwards, and got back into it again. This halcyon period was
only interrupted once, when we all had to go out and dig a
trench one night long. However, the worst feature of this
expedition was the rain, which made 'going' very difficult, and
things in general rather uncomfortable (especially for the men),
so we hadn't much to grumble about.
"Then we came back here and the first night we slept in peace,
getting up at about 3 a.m. ostensibly for the purpose of 'stand-
to,' but really to brew ourselves some cocoa. Then sleep till 9,
10, or 11, I forget which. I crawl to the door of my dug-out and
shout for Gray, who lives just opposite. 'Breakfast!' I say, and he
invariably asks, 'What will you have, sir?' just as if he could
command the larders of the Carlton or the Linga.
"Knowing my rations, and that an attempt at humour would
only put me off my plat du jour or daily round, I usually think
for a few moments and then order eggs and bacon, and face
the common task. The only variation I permit myself is that on
one or two days in the week I funk the bacon and have boiled
eggs. Where do the eggs come from? They are purchased out
of the Mess fund by our Mess cook who lives with the Transport
when we are in the trenches, and brings them up personally
when the rations arrive at night. Yes, he has a 'cushy' time of it,
does our Mess cook; and how can he avoid being happy, living
as he does in a perpetual transport?
"What of the days when no eggs are available? Why, then,
horrible dictu, I have fried cheese and bacon!
"It occurs to me here, although all this was not written with
intention, that this could be a good place to ask whether
sausages are yet in season. If they are, a few cooked ones (or
half cooked) sent out now and again would make a splendid
variant for our menu.
"The meat season is hard to follow out here. Bully beef is such a
hardy perennial. (This does not mean that we live on it—I never
eat it, there is always a good supply of fresh beef.)
"Blackberries are coming on, I notice with pleasure, and I can
usually tell what shells are in season (the season for sausages in
this department is, let us hope, mercifully short. I believe we
are now in the middle of the close-time for this sturdy little
fellow, I trust he is not utilising it to increase and multiply).
"I am sorry I have had rather a sharp attack of parentheses
lately, the touch of winter in the air cramps my style. And I
really did think this was going to be quite a short letter. I cannot
divine my moods, I find, I did not feel like writing until I got
going.
"Please thank father very much indeed for the sniperscopes. I
have given one to the Captain of D Company, who is keen on
everything. He is an engineer (civil), and is a most useful man
out here. I have not tried mine yet, as I haven't been in a fire
trench, and it would hardly be fair to use it in a support trench,
the backs of our infantry in the trench in front being too easy a
target to give the thing a fair trial.
"Oh! I was telling you about my work in this trench but got
switched off on to food. Last time I was here I (and my platoon)
worked for two nights from 7.30 till 3 improving the parapets.
Well, the second night of this period (last night) I had got all
sorts of plans ready and was going to have a thoroughly good
night building dug-outs, draining the trench, and building a
second machine-gun emplacement (not my job really at the
moment). However, word came along that the platoon was
wanted to dig another trench right in front again and near the
other one. They said, 'A covering party with bombs will be
provided, and send in your casualty report in the morning!' So I
asked if they were supplying stretchers and all complete! But
they were not. It is a most cheering way of sending you off, is it
not? It is a wonder they did not make us take up our own grave
crosses, just in case.
"(By the way, it is most impressive to meet two men walking
along at night and one carrying a large white cross. The burying
and decking of the graves is done very well here, and
conscientiously. There is a special organisation for making the
crosses, lettering them and putting them up. The position of the
grave is reported to them, with the particulars, and they do the
rest.)
"The great difference in last night's job was that I only had a
platoon to deal with, while before the Captain had a whole
company. Also I was not quite so close to the enemy (we were
30 yards off, and less, before), and the moon was mostly
obscured. I determined not to let them know we were working,
so I crept out and explored the ground with the Corporal of the
covering party (this was the worst part of the job, because you
did not know when you might not come across a party of the
enemy in the many shell holes and old trenches with which the
ground was covered). I had my large revolver in my pocket, but
I did not want to use it, as it would have given our game away.
"All went well, and I got the men placed out in absolute silence,
with the covering party pushed out in front to listen and watch.
The men worked very quietly, and when a light went up they
got down and kept still. Lights were very few, because the
enemy had got a working party out too—at one side, and we
could occasionally hear them driving in stakes for wire.
"We had to use picks in some places where the ground was
stony, and these are the hardest to keep quiet. We got through
it all right, and only one shot, I think, was fired all the time. It
came fairly close, too. I am sure they guessed we were out,
because when one light went up I hadn't time to get down, so I
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.

Let us accompany you on the journey of exploring knowledge and


personal growth!

ebookultra.com

You might also like