Visual Basic.NET
  Home arrow Visual Basic.NET arrow Page 2 - Movement and Player Statistics 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

Movement and Player Statistics in a VB.NET Text-Based Game
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 4
    2008-07-15

    Table of Contents:
  • Movement and Player Statistics in a VB.NET Text-Based Game
  • Adding Movement
  • Player Statistics
  • Displaying Properties

  • 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


    Movement and Player Statistics in a VB.NET Text-Based Game - Adding Movement


    (Page 2 of 4 )

    Now it's time to add movement. We need to first set up a function, TryMove, that will try to move an Entity (not necessarily the player) a certain number of spaces. This function will accept the entity to be moved, the change in the entity's X value, and the change in the entity's Y value. It will then check to see if the move is valid. If it is valid, then the movement will be made, and True will be returned. If the movement cannot be made, then False will be returned. We need TryMove to return these values because we don't want to end the player's turn if he tries to make an invalid move. Rather, we want him to be able to try again before the other entities get their turns.

    So far, we've never actually worked with functions before. They're very simple, however. A Func, as it is termed in the language, is similar to a Sub, only we must return a value, and we can explicitly specify what type that value will be.

    We're also going to need to create yet another method called RedrawTile. DrawTile will draw a tile, but since it was originally built to be used to draw the entire map, it assumes that the cursor is already in the correct position. RedrawTile will manually set the cursor in the proper position and then call DrawTile. First, though, here is TryMove, which calls RedrawTile:

    Function TryMove(ByVal toBeMoved As Entity, ByVal xChange As Integer, ByVal ychange As Integer) As Boolean

     Dim x As Integer = toBeMoved.X + xChange

     Dim y As Integer = toBeMoved.Y + ychange

     If map(x, y).Passable = True Then

    RedrawTile(toBeMoved.X, toBeMoved.Y)

    toBeMoved.X = x

    toBeMoved.Y = y

    DrawEntity(toBeMoved)

     Return True

     End If

     Return False

    End Function

    The function accepts, as I said before, the change in the entity's X value and the change in the entity's Y value. So, if the entity is trying to move right, then the change in the X value is 1, and the change in the Y value is 0. If the entity is trying to move up, then the change in the X value is 0, and the change in the Y value is -1. The changes are added to the player's current location to determine the destination tile. The tile is then checked to see if the player can walk on it, and if the player can, then the player's old location is redrawn, and the player is drawn on the new location.

    Now let's write RedrawTile, which is very simple, as was already noted:

    Sub RedrawTile(ByVal x As Integer, ByVal y As Integer)

    Console.SetCursorPosition(x, y)

    DrawTile(x, y)

    End Sub

    Great. Now the methods required for movement are in place, and we can wire them up to our loop. Recall how I said that the player's turn will only be used up if the player has made a move. This is determined by what TryMove returned. We're going to need a Boolean variable to store the value returned by TryMove. Add this definition before the Select statement:

    Dim playerMoved As Boolean = False

    We assume that the player has not made a move. If the player does indeed made a move, then we can change this. This also eliminates the need for the Case Else block, so you can go ahead and delete it. It will be replaced by a conditional that checks to see whether or not the player has moved. If the player has not moved, then we'll need to immediately move to the next iteration, just like we did in the Case Else block. Put the conditional below End Select:

    If Not playerMoved Then

     Continue While

    End If

    Now we can check for movement keys and respond accordingly. We'll use the numpad for movement. Here's what the entire Select block should now look like:

    Select Case input.Key

     Case ConsoleKey.Escape

     Exit While

     Case ConsoleKey.NumPad8

    playerMoved = TryMove(player, 0, -1)

     Case ConsoleKey.NumPad2

    playerMoved = TryMove(player, 0, 1)

     Case ConsoleKey.NumPad4

    playerMoved = TryMove(player, -1, 0)

     Case ConsoleKey.NumPad6

    playerMoved = TryMove(player, 1, 0)

    End Select

    Above, we check for movement keys and then call TryMove, passing the appropriate values. The result of the move is stored in playerMoved. Run the program and press the directional keys on the numpad. The player should now move around the map.

    More Visual Basic.NET Articles
    More By Peyton McCullough


       · Hello, all,This is a continuation of my series on learning Visual Basic through...
     

    VISUAL BASIC.NET ARTICLES

    - LINQ to XML Programming Using Visual Basic.N...
    - 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...

     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     




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