You need to start by placing an picture box on your form, make it 300 by 300 pixels, now change the name to display.
We are also going to need a timer, it is located in the components section of your IDE. Drag this on to your form, now it wont work straight away, we need to enable it in the properties pane, set enabled to True, and the interval to 10, this means every 0.01 of a second (My maths isnt so good so maby not) our timer will "Tick".
Now for some code.
First we need to create some variables:
Dim output As New Bitmap(300, 300) Dim gfx As Graphics = Graphics.FromImage(output) Dim SpriteX As Integer = 135 Dim SpriteY As Integer = 135 Dim moveU As Boolean = False Dim moveR As Boolean = False Dim moveD As Boolean = False Dim moveL As Boolean = False Dim moveStep As Integer = 4
The first one is our bitmap, we will draw to that using gfx and then display it in the picture box we made earlyer.
gfx is the thing we use to draw to the bitmap.
SpriteX and SpriteY will be the positions of out well you can probably guess, sprite, a sprite is just an object really.
The other four will be used in a second when we want to move our sprite...or box in this case.
And the moveStep will be how much our sprite moves every frame.
Now we need our function that draws and displays our data.
Sub refreshScreen() Handles Timer1.Tick End Sub
So we now have a function or sub called refreshScreen, because we have a handle, Timer1.Tick, it will happen every 0.01 seconds.
The following code will go in our refreshScreen function.
gfx.FillRectangle(Brushes.White, 0, 0, 300, 300) gfx.FillRectangle(Brushes.Blue, SpriteX, SpriteY, 25, 25) display.Image = output
The first line makes our whole output bitmap white.
The second draws our sprite using our coordinates and a size of 25 by 25.
And the third displays our output bitmap in out picture box that we named display.
How interesting is that...yes a blue box...well this blue box likes to move so we better give him what he wants.
Private Sub startMoving(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown Select Case e.KeyCode Case Keys.Up moveU = True Case Keys.Right moveR = True Case Keys.Down moveD = True Case Keys.Left moveL = True End Select End Sub Private Sub stopMoving(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp Select Case e.KeyCode Case Keys.Up moveU = False Case Keys.Right moveR = False Case Keys.Down moveD = False Case Keys.Left moveL = False End Select End Sub
Theese two functions turn our moving booleans between true and false when up, down, left or right are pushed down or up.
Now we need some more code in our refreshScreen function, this will check if the moving booleans are set to true, if so it will move the sprite, but only if it is not going to go thru the wall, this will make it go outside the bounds of the array, in other words 301 dose not fit into 300.
I reccomend placing this after your other code in this function.
If moveU = True And SpriteY > 0 Then SpriteY -= moveStep ElseIf SpriteY < 0 Then SpriteY = 0 End If If moveR = True And SpriteX < 275 Then SpriteX += moveStep ElseIf SpriteX > 275 Then SpriteX = 275 End If If moveD = True And SpriteY < 275 Then SpriteY += moveStep ElseIf SpriteY > 275 Then SpriteY = 275 End If If moveL = True And SpriteX > 0 Then SpriteX -= moveStep ElseIf SpriteX < 0 Then SpriteX = 0 End If
Compile and done, thats it, you should have about 64 lines, if not you might have missed somthing.
Hope this help a few people.
And heres the full code anyway...
Public Class Form1 Dim output As New Bitmap(300, 300) Dim gfx As Graphics = Graphics.FromImage(output) Dim SpriteX As Integer = 135 Dim SpriteY As Integer = 135 Dim moveU As Boolean = False Dim moveR As Boolean = False Dim moveD As Boolean = False Dim moveL As Boolean = False Dim moveStep As Integer = 4 Sub refreshScreen() Handles Timer1.Tick gfx.FillRectangle(Brushes.White, 0, 0, 300, 300) gfx.FillRectangle(Brushes.Blue, SpriteX, SpriteY, 25, 25) display.Image = output If moveU = True And SpriteY > 0 Then SpriteY -= moveStep ElseIf SpriteY < 0 Then SpriteY = 0 End If If moveR = True And SpriteX < 275 Then SpriteX += moveStep ElseIf SpriteX > 275 Then SpriteX = 275 End If If moveD = True And SpriteY < 275 Then SpriteY += moveStep ElseIf SpriteY > 275 Then SpriteY = 275 End If If moveL = True And SpriteX > 0 Then SpriteX -= moveStep ElseIf SpriteX < 0 Then SpriteX = 0 End If End Sub Private Sub startMoving(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown Select Case e.KeyCode Case Keys.Up moveU = True Case Keys.Right moveR = True Case Keys.Down moveD = True Case Keys.Left moveL = True End Select End Sub Private Sub stopMoving(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp Select Case e.KeyCode Case Keys.Up moveU = False Case Keys.Right moveR = False Case Keys.Down moveD = False Case Keys.Left moveL = False End Select End Sub End Class



Back to top









