Introducing the World PDF Print E-mail
Written by Joris Bijvoets   
Friday, 26 August 2011 12:31

Introducing the World in the XNA for VB.Net game

Introduction of the game

This page is the fourth in a series about programming a game in XNA for Visual Basic.Net. The game is awful simple, just move a block around in the World without colliding into obstacles. Playing the game is not the goal, it's the road to the game that's my goal. You can read more about my intentions in the introductory page, about the design on the Design page. The third page describes the starting of the project.

Defining the objects in the World

The first success I could have is some screen, showing blocks in a virtual world, eventually in different colours.

To make this happen, I'll need to define the world and the obstacles.

These classes are quite simple:

World class

Public Class World1

'The World consists of 100 * 100 squares, free, occupied by an Obstacle or occupied by the Car
Private Squares(100, 100) As Integer

Private colObstacles As Collection

Public Const FREE_SQUARE = 0
Public Const OBSTACLE_SQUARE = 1
Public Const CAR_SQUARE = 2

Public Sub New()

'Create between 10 and 20 obstacles
'The first obstacles will be just one square large
'Make sure each obstacle is placed on a free square

Dim FoundFreeSquare As Boolean = False
Dim NewX As Integer, NewY As Integer

For i As Integer = 0 To CInt(Rnd()) * 10 + 10

FoundFreeSquare = False
Do While Not FoundFreeSquare
NewX = CInt(Rnd()) * 98 + 1 'Don't place these obstacles on the border
NewY = CInt(Rnd()) * 98 + 1 'Don't place these obstacles on the border
FoundFreeSquare = (Squares(NewX, NewY) = FREE_SQUARE)
Loop

colObstacles.Add(New Obstacle1(NewX, NewY))
Squares(NewX, NewY) = OBSTACLE_SQUARE

Next

'Who said the World isn't flat, anyway?
'To prevent the car from falling of the World, make a wall around the world

For i As Integer = 0 To 99
NewX = 0
NewY = i
colObstacles.Add(New Obstacle1(NewX, NewY))
Squares(NewX, NewY) = OBSTACLE_SQUARE

NewX = 99
NewY = i
colObstacles.Add(New Obstacle1(NewX, NewY))
Squares(NewX, NewY) = OBSTACLE_SQUARE

NewX = i
NewY = 0
colObstacles.Add(New Obstacle1(NewX, NewY))
Squares(NewX, NewY) = OBSTACLE_SQUARE

NewX = i
NewY = 99
colObstacles.Add(New Obstacle1(NewX, NewY))
Squares(NewX, NewY) = OBSTACLE_SQUARE
Next

End Sub
End Sub

End Class

Obstacle class

Public Class Obstacle1


Private _X As Integer, _y As Integer

Public Sub New(x As Integer, y As Integer)

_X = x
_y = y

End Sub

End Class

Loading the World

As I've defined the World class, and made sure that with creating the World (in only two hours, notice that!) all obstacles are created also, I can now try to draw My World.

On the next page, I'll describe my efforts.