Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27349

Help using tiles - tile mapping and events for them

$
0
0
Im currently doing my A level Computing project F454 (OCR) and need some help regarding the game im trying to make.

Its going to be pacman but with an educational part to it too, the user controls the pacman, if the pacman moves over a fruit, the user has to answer a question, based on the answer, you gain points or some extra game time.

What i can do:
  • Move a picture box ontop of my tile map
  • Customize the tile map with different tiles
  • Use a double buffer for the graphics
  • Using a GameLoop "isRunning" boolean

What I can't do/Want to do:
  • Make tiles interact with events on the form, eg, if the above tile is a wall then the pacman cannot move up
  • Have an adjustable game speed, eg, the difficulty can be set to easy medium and hard and the pacman will move at a higher speed
  • Implement AI - the ghosts, i have no idea where to start
  • Have a counter that when time runs out, the game ends and records the user's score

Any help would be appreciated because i can't seem to find many guides that help in this case unless it just provides a working program but i do not wish to cheat on this project

Most of the comments within the code are from a previous teacher who has left the school at this point and out current teacher knows nothing about prgramming and using VB.net

The following link is the source code but in its normal format:
http://txtup.co/5WBfL

Example output screen, yes im aware it isnt pacman at the moment, the tiles used are just for an example for me to learn using tiles:
http://imageupload.co.uk/files/c0dwynb3bx7ci1uiv8fw.png

Quote:

Public Class Form1
Dim isRunning As Boolean = True
Dim currentLevel As Integer = 1 'start with level 1
'TREE = 0
'WATER = 1
'HOUSE = 2
'DIRT = 3
'GRASS = 4
'FENCE = 5
Dim level1(,) As Integer = {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{2, 3, 3, 3, 3, 3, 3, 3, 0, 0},
{5, 5, 4, 4, 4, 4, 4, 3, 0, 0},
{4, 4, 4, 4, 4, 4, 4, 3, 0, 0},
{4, 4, 4, 4, 4, 4, 4, 3, 0, 0},
{4, 4, 4, 4, 4, 4, 4, 3, 0, 0},
{1, 1, 1, 4, 4, 4, 4, 3, 0, 0},
{1, 1, 1, 3, 3, 3, 3, 3, 0, 0},
{1, 1, 1, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}} 'Level 1

Const TILE_SIZE = 32 'how big the tiles are
Const LEVEL_TILE_WIDTH = 10 'The level is 10 tiles across
Const LEVEL_TILE_HEIGHT = 10 'The level is 10 tiles down
Dim tileBitmap As Bitmap 'Bitmap to hold the current tile
Dim levelBitmap As Bitmap 'Bitmap to hold the whole level

Private Sub Form1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown

Dim moveAmount As Integer
moveAmount = 32

Select Case e.KeyValue

Case Keys.Escape
isRunning = False
Case Keys.Up
PictureBox1.Top -= moveAmount
Case Keys.Down
PictureBox1.Top += moveAmount
Case Keys.Left
PictureBox1.Left -= moveAmount
Case Keys.Right
PictureBox1.Left += moveAmount

End Select

End Sub

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load


Me.Show() ' make sure the form actually appears
Me.Focus() 'make sure key presses get to form
CreateBitmaps() 'load the tile bitmap
GameLoop() ' start game loop

End Sub

Private Sub GameLoop()

Dim x, y As Integer

Do While isRunning = True
DrawLevel(currentLevel)
'1. check for user input
Application.DoEvents()
'2. Run AI for enemies
'3. Update object data, positions on screen ect
'4. Draw graphics to screen
DrawAll()
Me.Text = ("X: " & Me.Width & " " & "Y: " & Me.Height)
'5. Play sounds/music
Loop

Me.Close()

End Sub

Private Sub DrawAll()

Dim currentContext As BufferedGraphicsContext
Dim myBuffer As BufferedGraphics

currentContext = BufferedGraphicsManager.Current

myBuffer = currentContext.Allocate(Me.CreateGraphics, Me.DisplayRectangle)
' Me is the form so we are creating graphics the size of the form
myBuffer.Graphics.DrawImage(levelBitmap, New Rectangle(0, 0, LEVEL_TILE_WIDTH * TILE_SIZE, LEVEL_TILE_HEIGHT * TILE_SIZE), New Rectangle(0, 0, LEVEL_TILE_WIDTH * TILE_SIZE, LEVEL_TILE_HEIGHT * TILE_SIZE), GraphicsUnit.Pixel) 'copy from levelBitmap to graphics on form
myBuffer.Render(Me.CreateGraphics)
myBuffer.Dispose()
'dispose of the graphics buffer now we are done

End Sub

Private Sub CreateBitmaps()
tileBitmap = New Bitmap(My.Resources.New_Tile_Map)
levelBitmap = New Bitmap(LEVEL_TILE_WIDTH * TILE_SIZE, LEVEL_TILE_HEIGHT * TILE_SIZE)
End Sub

Private Sub DrawLevel(ByVal lvlNumber)
'This Subroutine draws the level tiles to a bitmap image.

Dim X, Y As Integer 'for looping through tiles

Dim levelGraphics = Graphics.FromImage(levelBitmap)

'need a Graphics to draw to for the level bitmap

Dim sRect As Rectangle

'source rectangle for the LEVEL tiles
Dim dRect As Rectangle
'destination rectangle to paint tiles onto level bitmap
For Y = 0 To LEVEL_TILE_WIDTH - 1 '0 to 9 is 10 high
For X = 0 To LEVEL_TILE_HEIGHT - 1 '0 to 9 is 10 wide

If (level1(X, Y).ToString() = "0") Then
sRect = New Rectangle(0, 0, TILE_SIZE, TILE_SIZE)
'TREE

ElseIf (level1(X, Y).ToString() = "1") Then
sRect = New Rectangle(TILE_SIZE, 0, TILE_SIZE, TILE_SIZE)
'WATER

ElseIf (level1(X, Y).ToString() = "2") Then
sRect = New Rectangle(TILE_SIZE * 2, 0, TILE_SIZE, TILE_SIZE)
'HOUSE

ElseIf (level1(X, Y).ToString() = "3") Then
sRect = New Rectangle(TILE_SIZE * 3, 0, TILE_SIZE, TILE_SIZE)
'DIRT

ElseIf (level1(X, Y).ToString() = "4") Then
sRect = New Rectangle(TILE_SIZE * 4, 0, TILE_SIZE, TILE_SIZE)
'GRASS

ElseIf (level1(X, Y).ToString() = "5") Then
sRect = New Rectangle(0, TILE_SIZE, TILE_SIZE, TILE_SIZE)
'FENCE

End If
dRect = New Rectangle(X * TILE_SIZE, Y * TILE_SIZE, TILE_SIZE, TILE_SIZE)
levelGraphics.DrawImage(tileBitmap, dRect, sRect, GraphicsUnit.Pixel) 'copy from tiles to level bitmap
Next
Next
levelGraphics.Dispose()
End Sub

End Class
Attached Images
 
Attached Files

Viewing all articles
Browse latest Browse all 27349

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>