Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Visual basic game programming for teens
Nội dung xem thử
Mô tả chi tiết
Visual Basic
R
Game
Programming for
Teens, Third
Edition
Jonathan S. Harbour
Course Technology PTR
A part of Cengage Learning
Australia . Brazil . Japan . Korea . Mexico . Singapore . Spain . United Kingdom . United States
Visual BasicR
Game Programming for Teens,
Third Edition
Jonathan S. Harbour
Publisher and General Manager,
Course Technology PTR: Stacy L. Hiquet
Associate Director of Marketing:
Sarah Panella
Manager of Editorial Services:
Heather Talbot
Marketing Manager: Jordan Castellani
Senior Acquisitions Editor: Emi Smith
Project Editor: Jenny Davidson
Technical Reviewer: Joshua Smith
Interior Layout Tech: MPS Limited, a Macmillan
Company
Cover Designer: Mike Tanamachi
Indexer: Larry Sweazy
Proofreader: Sara Gullion
© 2011 Course Technology, a part of Cengage Learning.
ALL RIGHTS RESERVED. No part of this work covered by the copyright
herein may be reproduced, transmitted, stored, or used in any form or
by any means graphic, electronic, or mechanical, including but not
limited to photocopying, recording, scanning, digitizing, taping, Web
distribution, information networks, or information storage and retrieval
systems, except as permitted under Section 107 or 108 of the 1976
United States Copyright Act, without the prior written permission of the
publisher.
For product information and technology assistance, contact us at
Cengage Learning Customer & Sales Support, 1-800-354-9706
For permission to use material from this text or product,
submit all requests online at www.cengage.com/permissions
Further permissions questions can be emailed to
Visual Basic is a registered trademark of Microsoft Corporation. Cover
art design created by Eden Celeste. All other trademarks are the
property of their respective owners.
All images C Cengage Learning unless otherwise noted.
Library of Congress Control Number: 2010936643
ISBN-13: 978-1-4354-5810-9
ISBN-10: 1-4354-5810-9
Course Technology, a part of Cengage Learning
20 Channel Center Street
Boston, MA 02210
USA
Cengage Learning is a leading provider of customized learning solutions
with office locations around the globe, including Singapore, the United
Kingdom, Australia, Mexico, Brazil, and Japan. Locate your local office at:
international.cengage.com/region
Cengage Learning products are represented in Canada by Nelson
Education, Ltd.
For your lifelong learning solutions, visit courseptr.com
Visit our corporate website at cengage.com
Printed by RR Donnelley. Crawfordsville, IN. 1st Ptg. 12/2010
Printed in the United States of America
1 2 3 4 5 6 7 12 11 10
eISBN-10: 1-4354-5811-7
For Kourtney
Many thanks to Emi Smith, Jenny Davidson, and Joshua Smith for their editing
and production efforts that led to this completed book. I am especially thankful to
Emi for giving me extra time to make this a labor of love rather than just another
book. I absolutely enjoyed this subject and trust the end result reflects the
attention that went into it.
Thanks to my wife, Jennifer, and our little ones, for your wonderful distractions.
When the first edition was published in 2004, we had only two: Jeremiah and
Kayleigh. By the time the second edition went to print in 2007, we had added two
more to our family: Kaitlyn and Kourtney. Some day when you read this, know
that you have filled my life with joy.
Much appreciation goes out to Eden Celeste for her beautiful character art featured on the cover and in several chapters—all of the hero and monster illustrations are hers. See her work at www.edenceleste.com. It seems like she can draw
anything I can imagine and always surprises me with her work.
I am grateful to Reiner Prokein for allowing me to use his extraordinary artwork,
which has made the examples in this book possible. His website, Reiner’s Tilesets,
is located at www.reinerstileset.de. I would not have even attempted to create a
role-playing game for this book had Reiner not made his artwork available in the
first place. The game artwork is all from Reiner.
Acknowledgments
Jonathan S. Harbour is a freelance writer, teacher, and indie game developer,
who has been programming video games since the 1980s. His first video game
system was an Atari 2600, which he disassembled on the floor of his room at age 9.
He has written on languages and subjects that include Cþþ, C#, Basic, Java,
DirectX, Allegro, Lua, DarkBasic, XNA Game Studio, Pocket PC, Nintendo GBA,
and game console hacking. He is the author of Visual C# Game Programming for
Teens; Beginning Java Game Programming, Third Edition; Beginning Game
Programming, Third Edition; Multi-Threaded Game Engine Design; and
Advanced 2D Game Development. His next project is The Complete XNA 4.0,
expected in early 2011. Visit his website at www.jharbour.com, which includes a
blog and game development forum.
About the Author
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiv
PART I BUILDING BLOCKS . .................................. 1
Chapter 1 Getting Started ................................. 3
What Is Game Programming Really All About? . . . . . . . . . . . . . . . . . 4
Get Your Feet Wet First, Ask Questions Later . . . . . . . . . . . . . . . 5
On Programming Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Let Your Creativity Fly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
On Creativity, Talent, and Hard Work . . . . . . . . . . . . . . . . . . . . . . . 10
The Sky’s the Limit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Learn the Tricks of the Trade . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Taking a Look at Celtic Crusader . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Building a Role-Playing Game . . . . . . . . . . . . . . . . . . . . . . . . . . 14
The Story . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Describing the Player’s Character . . . . . . . . . . . . . . . . . . . . . . . . 16
Adventure Game or Dungeon Crawler? . . . . . . . . . . . . . . . . . . . 18
Level Up! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Chapter 2 Visual Basic Primer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
What Is Visual Basic? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
The .NET Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Windows Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Contents
vi
Writing Your First Visual Basic Program . . . . . . . . . . . . . . . . . . . . . 24
Say Hello . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Code Editor Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Variables of All Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Using Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
What’s So Special about Strings? . . . . . . . . . . . . . . . . . . . . . . . . 35
Telling Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Branching/Conditional Statements . . . . . . . . . . . . . . . . . . . . . . . . . 40
The If. . .Then Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
The Select. . .Case Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Guessing Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Subroutines and Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Using Subs and Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Returning a Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Mathematical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Converting Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Addition and Subtraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Multiplication and Division . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Math Quiz . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Relational Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Looping Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
For Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Do Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
Object-Oriented Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Encapsulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Level Up! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Chapter 3 Forms and Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Dice War . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Form Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Contents vii
Block Buster . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Form Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Tic-Tac-Toe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Button Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
Level Up! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
Chapter 4 Drawing with GDIþ . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Drawing Lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
PictureBox Is Our Friend . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
Surfaces and Devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
4D Programming with a Timer . . . . . . . . . . . . . . . . . . . . . . . . . 93
Drawing Rectangles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
Drawing Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
Level Up! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
Chapter 5 Bitmaps: Our First Building Block . . . . . . . . . . . . . . . . . . . 99
Dissecting Bitmaps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Loading a Bitmap File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Drawing a Bitmap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Rotating and Flipping a Bitmap . . . . . . . . . . . . . . . . . . . . . . . . 103
Accessing Bitmap Pixels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
Creating a Game Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
Game Class Constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
Game Class Destructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
Game Updates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
Framework Demo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
Level Up! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Chapter 6 Sprites and Real-Time Animation . . . . . . . . . . . . . . . . . . 113
What Is a Sprite? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
Animating a Sprite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
Sprite Animation Theory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
Creating a Sprite Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
Sprite Drawing Demo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
Improving the Game Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Separating Form and Module Code . . . . . . . . . . . . . . . . . . . . . 134
viii Contents
Adding a Real-Time Game Loop . . . . . . . . . . . . . . . . . . . . . . . 135
Gameplay Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Level Up! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
Chapter 7 Collision Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
Reacting to Solid Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
Rectangle Intersection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
Collision Demo Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
Sprite Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
Game Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
Form1 Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
Level Up! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Chapter 8 Playing Sound Effects and Music . . . . . . . . . . . . . . . . . . 153
Programming Audio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
Playing Wave Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
Playing Wave Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
The Audio Demo Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
Using the Media Player Control . . . . . . . . . . . . . . . . . . . . . . . . . . 163
Referencing the Media Player . . . . . . . . . . . . . . . . . . . . . . . . . 163
Playing MP3 and MIDI Files . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
Level Up! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
PART II GAME WORLD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
Chapter 9 Designing the Game of Celtic Crusader . . . . . . . . . . . . . 169
The Quest-Based Storyline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
Designing the RPG World . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
Map of the World . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
Regions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
The Player’s Character . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
Character Attributes (Stats) . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
Character Status . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
Character Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
Non-Player Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
Weapons and Armor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
Magic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
Contents ix
Communication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
Combat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
Level Up! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
Chapter 10 Creating the Level Editor. . . . . . . . . . . . . . . . . . . . . . . . 195
Designing Our Own Level Editor . . . . . . . . . . . . . . . . . . . . . . . . . 196
Building the Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
Creating a New Tilemap Level . . . . . . . . . . . . . . . . . . . . . . . . . 199
Loading and Saving Level Files . . . . . . . . . . . . . . . . . . . . . . . . 199
Level Up! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
Chapter 11 Rendering a Game Level . . . . . . . . . . . . . . . . . . . . . . . . 205
Mapping the Game World . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
Tile-Based Ireland . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
Loading and Drawing Level Files . . . . . . . . . . . . . . . . . . . . . . . 209
Introduction to Tiled Scrolling . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
Constructing the Tiled Image . . . . . . . . . . . . . . . . . . . . . . . . . 214
Tile Buffer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
Stepping Stones of the World . . . . . . . . . . . . . . . . . . . . . . . . . 215
Tile Rendering Theory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
Per-Tile Scrolling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
Full Tile Scrolling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
Full-Tile Smooth Scrolling . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
Per-Pixel Scrolling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
Sub-Tile Scrolling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
Drawing the Scroll Buffer . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
Aligning Tiles to the Scroll Buffer . . . . . . . . . . . . . . . . . . . . . . 228
Sub-Tile Scroll Buffering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
Sub-Tile Smooth Scrolling . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
Level Up! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
Chapter 12 Adding Objects to the World . . . . . . . . . . . . . . . . . . . . 237
Adding Scenery to the Game World . . . . . . . . . . . . . . . . . . . . . . . 238
A New Game Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
New Level Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
Adding Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
x Contents
Adding an Animated Character . . . . . . . . . . . . . . . . . . . . . . . . . . 253
Level Up! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
Chapter 13 Using Portals to Expand the World . . . . . . . . . . . . . . . . 261
Updating the Level Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
Perusing New Editor Features . . . . . . . . . . . . . . . . . . . . . . . . . 262
Creating a Portal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
It’s a Data-Driven Game World . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
Level Class Modifications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
Teleporting to Another Dimension . . . . . . . . . . . . . . . . . . . . . 272
Looking for Tile Collisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
Portal Demo Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
Adding Lua Scripting Support to the Game . . . . . . . . . . . . . . . . . 281
Installing LuaInterface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
Testing LuaInterface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282
Sharing Tile Data with Lua . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
Level Up! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
PART III GAMEPLAY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
Chapter 14 Creating the Character Editor . . . . . . . . . . . . . . . . . . . . 291
Character Classes and Attributes . . . . . . . . . . . . . . . . . . . . . . . . . 291
Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292
Gaining Experience and Leveling Up . . . . . . . . . . . . . . . . . . . . 294
The Base Character Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . 294
Peasants as Quest-Giving NPCs . . . . . . . . . . . . . . . . . . . . . . . . 299
The Enemy/Monster Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . 300
The Character Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303
Loading Character Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
The Character Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
The Animated Character Artwork . . . . . . . . . . . . . . . . . . . . . . 319
Character Demo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322
Level Up! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324
Chapter 15 Dialogue: Talking with NPCs . . . . . . . . . . . . . . . . . . . . . 325
Talking with NPCs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326
Creating the “Vendor” NPC . . . . . . . . . . . . . . . . . . . . . . . . . . . 326
Starting a Conversation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326
Dialogue Choices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
Contents xi
Creating the Dialogue System . . . . . . . . . . . . . . . . . . . . . . . . . . . 333
Making Eye Contact . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334
Dialogue GUI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
Final Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345
Level Up! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
Chapter 16 Fighting, Getting XP, and Leveling Up . . . . . . . . . . . . . . 355
Preparing for Combat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356
Starting an Attack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
Combat Demo 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358
Character Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363
Animations : Player Characters (PCs) . . . . . . . . . . . . . . . . . . . . 364
Animations : Hostile NPCs . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
Animations : Friendly NPCs . . . . . . . . . . . . . . . . . . . . . . . . . . . 375
Creating the Combat System . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376
Making Up Our Own Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . 377
Spawning Enemy NPCs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377
Attack Rolls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378
Rolling for Damage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
Attack Roll Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382
Dealing with the Player’s Death . . . . . . . . . . . . . . . . . . . . . . . 384
Combat Demo 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385
Facing Your Enemies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391
Which Way Did He Go? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392
A Change of Character . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393
State-Based Combat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394
Dialogue Improvements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
Plugging in Attack State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396
Dealing Permanent Damage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399
Gaining Experience . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402
Level Up! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404
Chapter 17 Creating the Item Editor . . . . . . . . . . . . . . . . . . . . . . . . 405
Item Editor Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406
Item Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407
Looking Up Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408
Item Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408
Item Editor Source Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 414
Level Up! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419
xii Contents
Chapter 18 Looting Treasure and Managing Inventory . . . . . . . . . . 421
Looting Treasure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422
Preparing to Loot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422
Stumbling Upon Loot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424
Items Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428
Character Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 430
Dropping Loot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432
Managing Inventory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436
Inventory Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437
Player Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446
Level Up! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447
Chapter 19 Creating the Quest Editor . . . . . . . . . . . . . . . . . . . . . . . 449
Quests as Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 450
Pushing or Pulling? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451
Quest Condition Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451
Quest Rewards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453
Quest Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454
Quest Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457
Quests as Story Driver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460
Quests Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461
Starting a Quest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463
Quest Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464
Completing a Quest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465
Level Up! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470
Chapter 20 So You Want to Be a Hero? . . . . . . . . . . . . . . . . . . . . . 471
Rolling Your Player Character . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472
Scripting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472
Binding Basic Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473
Binding Lua Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
Loading and Saving the Game . . . . . . . . . . . . . . . . . . . . . . . . . . . 478
Saving . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 478
Loading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480
Level Up! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 482
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483
Contents xiii
xiv
Imagine this: You create a new paladin character with random stats and hit
points, receive your first quest, and begin exploring the game world. You stumble
upon a skeleton warrior pacing near a pile of treasure. You attack the skeleton,
and it hits you back! You roll 1D20 and score a critical hit! The 20-point die, plus
dexterity, against the skeleton’s armor class, nets a guaranteed hit. Your damage
roll is 1D8 plus your strength and weapon modifier, which is 1D4, more than
enough to kill the monster! It falls to the ground, a jumble of bones. Lying next to
the unmoving skeleton is chain mail armor, which you pick up and equip, giving
your character a huge boost in armor points. You also pick up 10 gold coins and
gain 25 experience points. A magic key is also found, completing the first quest!
Now you dream of finding a better sword. Off to the right, you spot a whole group
of undead, the remains of a cursed Viking army, which surely have gear and gold
you could use . . . and you head toward them to battle!
This is no fictional narrative. You will learn to build a game with every feature just
described in detail! This book will teach you how to create your own fully
functional role-playing game (RPG) using Visual Basic. You will learn step-bystep how to construct each part of the game engine using Windows Forms and
GDIþ, including a tiled scroller, game editors, and scripting. If you think roleplaying games are fun to play, wait until you start working on your very own
adventure! Constructing an RPG is more fun than playing one, because you are in
complete control over the game world, and you can let your imagination loose to
create adventures for others to enjoy. However, it is not easy! Just as your game
character must gain experience and level up, so must you level up by reading each
chapter to learn new skills!
Introduction