Visual Basic.NET
  Home arrow Visual Basic.NET arrow Page 2 - Entity Creation and Messaging in a VB.NET ...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
VISUAL BASIC.NET

Entity Creation and Messaging in a VB.NET Text-Based Game
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 3
    2008-07-22

    Table of Contents:
  • Entity Creation and Messaging in a VB.NET Text-Based Game
  • Implementing Movement
  • Checking for Entity Collision
  • Implementing a Message System

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Entity Creation and Messaging in a VB.NET Text-Based Game - Implementing Movement


    (Page 2 of 4 )

    The next step is to add some life to PacingManEntity. Each time the player makes a move, PacingManEntity (and other entities, for that matter) needs to make a move as well. The easiest way to implement this is to add a Move method to Entity. In Entity, this method will be blank. Then, entities can override this method with their own behaviors. After each move of the player, the Move method will be called for each entity, allowing each entity to perform some sort of action.

    The first step, then, is to add a Move procedure to the Entity class. Since the intent here is to have the method be overridden by subclasses, we need to modify it with the Overridable keyword:

    Public Overridable Sub Move()


    End Sub

    In the PacingManEntity class, we need to override this procedure and provide functionality. Let's make a Pacing Man pace back and forth, from right to left. He will reverse direction whenever he hits an obstruction. This will require a way to keep track of what direction a PacingManEntity object is currently going. So, we'll need to first add a field to the PacingManEntity class. A Boolean called goingRight should do the job. If it's set to True, then the entity is moving to the right. If it's set to False, then the entity is moving to the left. Let's set it to True initially, causing him to move right from the start:

    Private goingRight As Boolean = True

    Now it's time to override Move. In order to override a procedure, the Overrides keyword is used:

    Public Overrides Sub Move()

     MyBase.Move()

    End Sub

    Visual Studio will automatically call the parent class's method first. In our case, the parent class's method has nothing in it, though.

    Remember, we need to change directions if there is an obstruction in the way (if TryMove returns False). However, since a Pacing Man only moves from right to left, if there are obstructions in both directions, then we'll have him simply forfeit his turn. In this situation, he can wait until the next turn to see if one of the obstructions has cleared. Here are the contents of PacingManEntity's Move procedure:

    ' Determine the change in X based on direction

    Dim changeX As Integer = 1

    If Not goingRight Then

    changeX = -1

    End If


    ' Move, or else change direction and move, or else quit

    If Not TryMove(Me, changeX, 0) Then

    changeX *= -1

     goingRight = Not goingRight

     TryMove(Me, changeX, 0)

    End If

    One of the things you should notice is the Me keyword. The Me keyword in Visual Basic refers to the current object. It's equivalent to the “this” or “self” operator in other languages.

    Now we just need to call this new method from within Main. Entities should only move if the player has made a move. So, after this:


    If Not playerMoved Then

        Continue While

    End If


    Place this:


    For Each toMove As Entity In entities

        toMove.Move()

    Next


    This will loop over all of the entities and give each an opportunity to make a move.

    Run the program. Each time the player makes a move, the pacing entity should make a move. When the entity hits a wall, he should reverse direction.

    More Visual Basic.NET Articles
    More By Peyton McCullough


       · Hello, all,This is yet another article in my series introducting Visual Basic...
       · In the article you skipped over making the entities move. Learning from the article...
       · Goodness, Charles, thanks again! As with last time, my version of the code is...
     

    VISUAL BASIC.NET ARTICLES

    - Understanding Delegates using Visual Basic.N...
    - Create a Sudoku Puzzle Generator using VB.NET
    - Entity Creation and Messaging in a VB.NET Te...
    - Movement and Player Statistics in a VB.NET T...
    - Creating and Drawing a Game Map in VB.NET (F...
    - Working with Classes and Properties for Game...
    - Working with Loops, Arrays, and Collections ...
    - Learning Loops in VB.NET for Game Development
    - Learning VB.NET: Working with Variables, Con...
    - The Basics of VB.NET Through Text Game Devel...
    - Learning VB.NET Through Text Game Development
    - Types of Operators in Visual Basic
    - Operators
    - Understanding Custom Events using Visual Bas...
    - Polymorphism using Abstract Classes in Visua...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT