+ All Categories
Home > Documents > learn VB .NET Through Game Progranvning978-1-4302-0808-2/1.pdfTechnical Reviewer: Mike Burgess...

learn VB .NET Through Game Progranvning978-1-4302-0808-2/1.pdfTechnical Reviewer: Mike Burgess...

Date post: 27-Mar-2018
Category:
Upload: duongminh
View: 213 times
Download: 0 times
Share this document with a friend
14
learn VB .NET Through Game Progranvning Matthew Tagliaferri APress Media, LLC
Transcript

learn VB .NET Through Game Progranvning

Matthew Tagliaferri

APress Media, LLC

learn VB .NET Through Game Programming Copyright © Matther Tagliaferri 2003 Originally published by Apress 2003

All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher.

ISBN 978-1-59059-114-7 ISBN 978-1-4302-0808-2 (eBook) DOl 10.1007/978-1-4302-0808-2

Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.

Technical Reviewer: Mike Burgess

Editorial Board: Dan Appleman, Craig Berry, Gary Cornell, Tony Davis, Steven Rycroft, Julian Skinner, Martin Streicher, Jim Sumser, Karen Watterson, Gavin Wray, John Zukowski

Assistant Publisher: Grace Wong

Project Managers: Laura Cheu and Nate McFadden

Copy Editor: Kim Wimpsett

Production Manager: Kari Brooks

Production Editor: JanetVail

Proofreader and Indexer: Carol Burbo

Compositor: Kinetic Publishing Services, LLC

Artist: Dina Quan

Cover Designer: Kurt Krames

Manufacturing Manager: Tom Debolsld

In the United States: phone I-S00-SPRINGER, email [email protected]. or visit http://www.springer-ny.com. Outside the United States: fax +49 6221 345229, email orders@springer . de, or visit http://www.springer.de.

For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley, CA 94710. Phone 510-549-5930, fax 510-549-5939, email [email protected], or visit http://www.apress.com.

The information in this book is distributed on an "as isn basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work.

The source code for this book is available to readers at http://www.apress.comin the Downloads section.

Learn VB .NET Through Game Programming Copyright © Matther Tagliaferri 2003 Originally published by Apress 2003

All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher.

ISBN 978-1-59059-114-7 ISBN 978-1-4302-0808-2 (eBook) DOI 10.1007/978-1-4302-0808-2

Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.

Technical Reviewer: Mike Burgess

Editorial Board: Dan Appleman, Craig Berry, Gary Cornell, Tony Davis, Steven Rycroft, Julian Skinner, Martin Streicher, Jim Sumser, Karen Watterson, Gavin Wray, John Zukowski

Assistant Publisher: Grace Wong

Project Managers: Laura Cheu and Nate McFadden

Copy Editor: KimWimpsett

Production Manager: Kari Brooks

Production Editor: Janet Vail

Proofreader and Indexer: Carol Burbo

Compositor: Kinetic Publishing Services, LLC

Artist: Dina Quan

Cover Designer: Kurt Krames

Manufacturing Manager: Tom Debolski In the United States: phone 1-800-SPRINGER, email [email protected], or visit http://www.springer-ny.com. Outside the United States: fax +49 6221 345229, email [email protected], or visit http://www.springer.de.

For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley, CA 94710. Phone 510-549-5930, fax 510-549-5939, email [email protected], or visit http://www.apress.com.

The information in this book is distributed on an "as is" basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work.

The source code for this book is available to readers at http://www.apress.com in the Downloads section.

To my mom and dad, who got me here

Contents at a Glance

About the Author ................................................... xi

About the Technical Reviewer .................................... xii

Acknowledgments ................................................... xiii

Introduction ....................................................... xv

Chapter 1

Chapter 2

Chapter 3

Chapter 4

Chapter 5

Chapter 6

Chapter 7

Chapter 8

Chapter 9

Developing Your First Game ......................... l

Writing Your First Game, Again ................... 43

Understanding Object-Oriented Programming

from the Start ...................................... 89

More OOPing Around ................................ 123

Understanding Polymorphism ....................... 177

Using Polymorphism via Interfaces ............... 209

Creating Multiplayer Games ....................... 247

Using DirectX ...................................... 279

Learning other Object-Oriented

Programming Topics ................................ 319

Appendix A The Basics of Visual Basic ....................... 341

Appendix BUsing POV-RAY and Moray .......................... 347

Appendix C Using the BMPStitch Utility ...................... 357

Index .............................................................. 367

v

Contents

About the Author ................................................... xi About the Technical Reviewer .................................... xii Acknowledgments ................................................... xiii Introduction ....................................................... xv

Chapter 1 Developing Your First Game .................... 1

Learning the Basics of Object-Oriented Programming ............ 2 Overview of the .NET Framework ................................... 5 Class Is in Session ................................................ 6 Designing the Die Roller Form ................................... 12 Coding the Die Roller ............................................ 17 Adding Your OWn Property ......................................... 19 Setting Up Events, or Making the Program Do Something ........ 21 Setting Up Methods ................................................ 24 Rolling the Die ................................................... 24 Where to Draw the Die? ........................................... 32 Putting the Die in Its Place .................................... 37 Moving the Die .................................................... 38 Tying the Functionality Together ................................ 40 The End? ........................................................... 42

Chapter 2 Writing Your First Game, Again ............. 43

Understanding the Benefits of Rewriting ........................ 44 Creating the Die Class ........................................... 45 Putting the Die Class to Work ................................... 60 Version 3: Creating the DicePanel Class ........................ 64 Testing the Classes ............................................... 81 Adding the DicePanel Class to the Toolbox ..................... 83 Now That's Reusable Stuff 1 ....................................... 86

Chapter 3 Understanding Object-Oriented Programming from the Start ................... 89

Starting the NineTiles Project .................................. 90 Looking Back on the Design ...................................... 118

vii

Contents

Chapter 4 More DOPing Around ............................ 123

Setting Up the Graphics ......................................... 124 Creating the Base Tile Class ................................... 125 Developing Brain Drain Concentration .......................... 139 Developing DeducTile Reasoning ................................. 146 Developing Lose Your Mind ....................................... 163 Summary ............................................................ 176

Chapter 5 Understanding Polymorphism .................. 177

Seeing Polymorphism in Action .................................. 178 Understanding Life and Something Like It ...................... 179 Developing Conway's Game of Life ............................... 187 Developing Rainbow Life ......................................... 194 Developing the Voting Game ...................................... 199 Building the Main Cellular Automaton Program ................. 203 Expanding the Cellular Automaton Games ........................ 207

Chapter 6 Using Polymorphism via Interfaces ........ 209

Seeing an Example Interface in Action ......................... 210 Using More Complex Interfaces .................................. 215 Finally, Seeing the Polymorphism in Action ................... 243 Additional Interface Topics ..................................... 245

Chapter 7 Creating Multiplayer Games .................. 247

Understanding Network Communication ............................ 247 Creating the Player Class and Subclasses ...................... 248 Developing the ReversiPiece Class .............................. 256 Developing the ReversiGame Class ............................... 257 Setting Up the Game .............................................. 262 Sending Game Data ................................................ 272 Receiving Game Data .............................................. 274 Debugging Network Code .......................................... 275 Moving Forward ................................................... 276

viii

Contents

Chapter 8 Using DirectX ................................... 279

Installing DirectX 9 ............................................. 280 Understanding DirectDraw Basics ................................ 280 Building an Arcade Game ......................................... 289 Summary ............................................................ 318

Chapter 9 Learning Other Object-Oriented Programming Topics ............................ 319

Understanding Structured Exception Handling .................. 319 Understanding Garbage Collection ............................... 323 Understanding Object Serialization ............................. 328 Understanding Threading ......................................... 332

Appendix A The Basics of Visual Basic ................. 341

Getting Started with Basic Programming ........................ 342 Looking at an Example Project .................................. 346

Appendix BUsing POV-RAY and Moray ...................... 347

Introducing Persistence of Vision .............................. 347 Creating the Ray-Traced Die ..................................... 348 Summary ............................................................ 355

Appendix C Using the BMPStitch Utility ................ 357

Using BMPStitch .................................................. 358 Using the BMPStitch Code ........................................ 360 Summary ............................................................ 366

Index ............................................................. 367

ix

About the Author

matt tagliaferrl has been developing soft­ware since his high school obtained its first computers-six TRS-80 Model 3s-in 1982. matt (who prefers his name in lowercase) has developed software in sev­eral industries, including retail, insurance, corporate finance, and trucking before (somewhat miraculously) landing a senior analyst position with the Cleveland Indians baseball organization. matt has

been with the Indians for six years and is now the manager of application development.

Currently, matt lives in the Cleveland area with his wife and two daughters. He enjoys collecting The Simpsons action figures and "family stuff" such as American Idol and Friday Pizza Nights.

xi

xii

About the Technical Reviewer

MIKE BURGESS started writing software on computers back when they took up whole rooms and had less memory than an average digital watch today. He has worked for small to large corporations (including Microsoft) and has been work­ing with Visual Basic since the beta of version 1. He's written many different types of software including small business accounting and inventory, real estate, emer­gency management, corporate communication, multimedia, medical diagnosis, and some hush-hush stuff for the government.

He currently resides in northern Utah with his wife and five (yes, five) chil­dren. He enjoys Family Game Night, Mountain Dew, and a good round of Ghost Recon with his kids and brothers.

Acknowledgments

WRITING A BOOK IS DEFINITELY not a solo project-and there are numerous people to thank for the opportunity and the work put into this project. I'd like to thank the entire Apress team. This is my first Apress title, and I've found it to be a first­class organization. Individually, my list of acknowledgments probably reads like the internal Apress organizational chart: thanks to Gary Cornell for listening to and shaping my initial pitch, thanks to Dan Appleman for some overall direction in finding the correct audience, and thanks to Laura Cheu and Nate McFadden for serving as project managers and keeping the project on track. Mike Burgess served as technical editor for the book and did a great job not only making sure the code was complete and that everything compiled but also making sugges­tions to help improve code clarity and readability. Kim Wunpsett filled the role of editor, keeping my i's dotted and my gerunds gerunding. Finally, Beth Christmas filled a multitude of roles from making sure I had the correct screen-capture soft­ware to getting me advance copies of Visual Studio. I thank all of them for turning a simple idea into the organized pile of pages you now hold in your hands.

On the home front, no project gets far without the understanding of my wife, Janet, who has to put up with me running to the computer to check email as soon as I get home from work and with the clack clack of the keyboard as she tries to read every evening. I hope she doesn't need to read this to know how much her love and support mean to me.

xiii

Introduction

A FEW YEARS AGO, Microsoft raised a few eyebrows (my own included) when it announced that the next version of Visual Basic (VB) would not be backward compatible with the current version, VB 6. There were many valid reasons for breaking the compatibility, but many people still thought Microsoft was taking a considerable gamble. After all, there were thousands of VB programmers crank­ing out millions of lines of code in the modem workforce, and suddenly Microsoft was announcing that this mountain of code would someday be considered "old" technology.

Introducing the New VB

Would the development community take to the new VB? The answer to that, of course, depended on what the new language had to offer. Community revolt would be the result if it were perceived that VB was changing simply for the sake of change. No, Microsoft had to make sure that the newVB was bringing enough to the table to get developers to want to make the change and make a concerted effort to plan upgrade paths for their production VB code.

Fortunately (for Microsoft and its stockholders), the newVB delivered against these goals. The new VB, called Vzsual Basic .NET, supported a full object-oriented paradigm-much better than the object -oriented features "bolted on" to VB along its prior development cycle. Furthermore, VB was only one of many possible languages that built upon the .NET Framework-an enormous library of classes from which your new programs would be based. Browser-based development took a major leap forward in the new release, as well-allowing the developer to do much of the devel­opment outside of the Web page in "standard" classes that could be accessed from the browser. These classes could be accessed through standard means or referenced remotely using Web Services, which allow full.NET Framework objects to be passed via Extensible Markup Language (XML) across the Internet. So much to leam! Where to start?

I've had many different people ask me about the best way to get into pro­gramming or the best way to learn a new language such as VB .NET. I've always found that I can't learn a new language unless I have a specific task that I want to solve by writing a program. I'm not of the camp that believes one can sit down with the compiler, the development environment, and the help file and then stand up many hours later an "expert" in the language. I need to learn by doing.

With that in mind, an important choice becomes the type of program to develop. Choose a program too small, and you won't have enough opportunity

xv

Introduction

xvi

to learn anything. A program too large doesn't work, either-you end up spend­ing too much time bogged down in the complexity of the task, which takes away time from learning the features of the language. like Goldilocks, you're looking for the program not too small, not too big, but instead "just right."

I discovered a few years ago that the "small game" program fit nicely into the "just right" category for learning the basics of a programming language. A small game is one that fits on a single screen and can usually be played by a single player in a few minutes. Examples of a small game include card games, dice games, and some simple board games. Many qualities make programs such as these good learning subjects. Programs of this type usually need to keep track of one or more types of game piece, and these pieces are often required to change state (such as a die rolling or a board element changing color). These game elements often come in groups (52 cards in a deck, five dice for Yahtzee) , so you'll have to learn how the language stores a group of similar elements. The user interface requirements of such games are usually more interesting (and therefore more challenging) than the usual button/listboxl combo box interface. Finally, when you've succeeded and completed the program, you've got a fun game to play.

The goal of this book is to teach you how to get started programming in VISual Basic .NET by developing games. This book doesn't intend to be a complete treatise of all the features and capabilities of this latest version of VB. Instead, it provides a series of example programs that illustrate basic features of the language and begins the huge task of introducing you to the contents of the .NET Framework. In addition, I introduce one more important programming concept-the concept of design. Now that object-oriented languages are more common, people have found that the vari­ous objects in a program can be designed to interact with each other in different ways; these interactions can make the program more or less complex or more or less adaptable to future features or enhancements. To illustrate the importance of design on a program, I've taken the time to walk you through the development process of several of these programs rather than simply presenting you with the final version of the game. By learning how to solve a program one way, then improving upon that design in a second or third version of the program, you'll begin to recognize when your own designs might be headed down a wrong path and some redesign is in order.

Where Are You Now?

This book assumes you've had at least some experience in software development before diving into the first chapter and that you now want to learn Visual Basic .NET. Perhaps you're a VB 6 programmer, for example, in which case this book will help describe the syntactical differences between the languages, as well as introduce you to the .NET Framework classes that will be new to you. Perhaps you're experienced in an older technology such as mainframe programming and

are looking to update your skill set to something more modem. Or perhaps you've picked up software development as a secondary skill at your job (you're the office "Excel macros guru"), and now you want to learn something more for­mally. Whatever the case, my assumption is that as you crack open Chapter 1, "Developing Your First Game," you've written programs in some other language so that you have a jumping-off point. Specifically, the following concepts should be familiar to you:

• Simple variables such as integers and strings to hold pieces of information

• Manipulation of data through expressions using elements such as mathe­matical operators (plus, minus, multiply, divide) or string operations (left, right, substring, uppercase/lowercase)

• Flow statements such as If .. Then •. Else blocks, For loops, and While statements

• The use of procedures and functions and how to get information into and out of them

• The event -driven nature ofWmdows programming and how many programs remain in an "idle state" until the user does something, at which point some type of code runs

Getting Up to Speed

If the previous little review list gives you a queasy feeling, or you simply think you might need a refresher on some of these topics, then you'll be happy to know that Appendix A, "The Basics of Visual Basic" contains some introductory material. Specifically, it covers these topics while describing how to write a simple Visual Basic .NET program. Becoming familiar with the topics in the appendix should give you enough background to dive into Chapter 1, "Developing Your First Game," and start the game writing.

Downloading the Code

If you want to follow along with the examples in the book, you can download the code for all the games developed in the book. It's available from the Downloads section of the Apress Web site (http://www.apress.com) . The code is divided into the chapter folders described in Table 1.

Introduction

xvii

Introduction

xviii

Table 1. Download the Source Code

FOLDER NAME Art

BMPStitch

CellularAutomata

Cellul~tomata~thSave

Common

DicePanel

DicePanelNew

DirectXDemo

FirstApplication

GarbageDemoOne

GarbageDemoTwo

GuessTheDieRoll

GuessTheDieRoll2

GuessTheDieRoll3

lnterfaceEbGinlple

NetReversi

NineTiles

PCOpponent

PolymorphismEbGinlple

ShapeTileGames

SpaceRocks

ThreadParametersOne

ThreadParametersThree

ThreadParameters1\vo (broken)

Yahtzee

USED IN Graphics/sounds used throughout the book

AppendixC

ChapterS

Chapter 9

Modules/ classes used by multiple projects

Chapter 2

Bonus, used by the Yahtzee game

ChapterS

Appendix A

Chapter 9

Chapter 9

Chapter 1

Chapter 2

Chapter 2

Chapter 6

Chapter 7

Chapter 3

Chapter 6

ChapterS

Chapter 4

ChapterS

Chapter 9

Chapter 9

Chapter 9

Bonus game


Recommended