Class Help

  • Thread starter Thread starter Brian
  • Start date Start date
B

Brian

Hello..

After many books and self study, I think I am finally starting to grasp the
basics of classes. However, I can't quite grasp how to use them after I
design them.

For example, I want to practice class development and basic VB.net design by
making a text adventure of sorts.

So, I start with a location. And in that location I will have a description
and a location number (integer) to let the program know where I am at. So,
would I design the class like this

Public Class Location

Private desc as String
Private loc as Integer

End Class

If that is it, then how do I call it..let's say, I press the button to go
WEST and I want it to then know the location and show the description. How
do I call this class or any class for that matter. I hope I am just not
totally lost or making it harder than it really is.

Thanks,
Brian
 
Brian,

There are objects and there are classes.

From a class you make an object.
The object is what you use in your program

dim MyObject as New MyClass("Here",10)

This has a class that can looks without the properties
\\\
Public Class Location
Private desc as String
Private loc as Integer
Sub new (byval a as String, Byval b as Integer)
desc = a
loc = b
end sub
End Class
///
This is a senseless class, because you cannot access it and therefore you
need the properties.

Therefore have a look at properties from a class as well.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcn7/html/vbconProperties.asp

I hope this gives an idea (this is not for a "shared" class however forget
that for the moment)

I hope this helps,

Cor
 
I think your problem is that you're starting with a Location object, which
is really an abstract notion. I would start by modeling the concrete
objects, such as the Player, Enemy, Treasure, etc. Write out a list of
everything you need to describe these entities (Color, Size, Hit Points,
Value, etc.) These become your Properties. Then decide what each one is
capable of doing (Shoot, Move, Explode, etc.) These become your Methods.

In your situation, for the game, I would probably create classes like this:

Public Class Player
' Public Properties
Public Property LocationX
Public Property LocationY
Public Property Name
' Public Methods
Public Sub Move (Direction, Distance)
' Maybe some private methods to handle single
' distance movement. I.e., if you are moving
' 5 blocks east, call MoveEast() 5 times
Private Sub MoveEast()
Private Sub MoveWest()
' ... you get the idea
End Class

And as far as locations, you can make a location class like you were saying,
although I would probably use it to define a grid. You can keep whatever
info you want in the class:

Public Class Location
Public Property Desc
Public Property EnemyPresentFlag
Public Property ContainsTreasureFlag
Public Property TerrainType
' etc.
End Class

Then create a Grid made up of Locations. A two-dimensional array would work
well for this:

Dim WorldMap(99, 99) As Location

Then populate the WorldMap (set all the properties of the grid squares to
their appropriate values), like this:

For X As Integer = 0 to 99
For Y As Integer = 0 to 99
WorldMap(X, Y).Desc = "Default Description"
WorldMap(X, Y).TerrainType = "Grassland"
' etc.
Next
Next

Then create your Player and assign default values:

Dim MyPlayer as New Player
MyPlayer.Name = "Joe"
MyPlayer.LocationX = 50
MyPlayer.LocationY = 50

To access the player's current WorldMap Location Object:

WorldMap(MyPlayer.LocationX, MyPlayer.LocationY)

Instead of plain Integers you can use Enumerations which will help you write
self-documenting code:

Public Enum Direction
East = 1
West = 2
North = 3
South = 4
End Enum

Finally, whenever you move, just call the Move() method of MyPlayer:

MyPlayer.Move(Direction.East, 3) ' Move three squares East
MyPlayer.Move(Direction.North, 5) ' Move five squares North

All the code to check boundaries (make sure the player isn't off the map),
make sure we haven't run into an enemy during a 'move' operation, etc. would
be included in your private MoveXXXX methods.

As you can see, basically to call methods of a Class, we have to create an
Object out of the class (the Dim WorldMap and Dim MyPlayer statements above
do this for us). Then just access the public methods and properties via the
object.

Hope that helps.
 
Interesting problem. However, your location class is incomplete because you
are storing a "loc" number, rather than a position relative to other
locations. If your movement can be north, east, west or south from the
current location, then your class can encapsulate this information by making
reference to other "nodes", for example:

Public Class Location

Public Description as String
Public North, South, East, West As Location

End Class


.... now, when you initially setup your "map", you "connect" nodes together,
like this:

Dim theLocations(4) As New Location

' Location 1 is north of location 0

theLocations(0).North = theLocations(1)

' Location 2 is south of location 0

theLocations(0).South = theLocations(2)

' Location 0 is south of location1

theLocations(1).South = theLocations(0)

' Location 3 is west of location 1

theLocations(1).West = theLocations(3)

etc.....

A "player" has a current location:

public CurrentLocation As Location

to move west:

CurrentLocation = CurrentLocation.West

to move north:

CurrentLocation = CurrentLocation.North


..... and so on........................




Hope this helps.
 
Yes..this gives me a start..will study it and apply it...I know I am making
it harder than it really is..I already designed a text adventure that didn't
utilize classes like they should be so I wanted to start over and I am lost.
This may help..

Thanks,
Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top