5 Replies - 2752 Views - Last Post: 24 February 2016 - 10:36 AM

#1 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1505
  • View blog
  • Posts: 3,809
  • Joined: 01-February 13

Drawing and moving a game sprite image on a form

Posted 23 February 2016 - 06:52 PM

I decided to post this because i often see many beginners trying to use PictureBoxes or other controls to represent characters when creating games with VB.Net which is not what controls are meant for. Controls have many downfalls when used this way such as slow game movement and higher levels of unneeded memory usage. The proper way to create and display a game object in VB.Net is to use a Bitmap image and a Rectangle to indicate the location and size to draw the Bitmap image on the form.

The Rectangle has a few very useful methods that can be used for collision detection in games too. The Rectangle.IntersectsWith and Rectangle.Contains are 2 such methods that make collision and location checking a fairly easy thing to do in a game.

Actually game characters and objects should be made by creating a class or a few classes that hold the game object`s specific data such as the Location Rectangle, Powers, Colors, Images, Drawing method, and/or whatever else each one needs. I wont get into all that here though, just figured it should be mentioned.

This example only concentrates on drawing and moving a simple Bitmap image on the form using the 4 Arrow keys. It uses the Win32 GetAsyncKeyState Api function to detect the keys being pressed. This is the most commonly used method to detect keys in a game because of the ability to detect more than 1 key being pressed at a time.

This will enable you to not only move in the 4 Left, Right, Up, and Down directions but, also in any of the 4 diagonal direction too.
Imports System.Runtime.InteropServices

Public Class Form1
    Private PlayerImg As New Bitmap("C:\TestFolder\Sprite.png") 'the image used for the game character
    Private PlayerRect As Rectangle 'a rectangle used to draw the PlayerImg bitmap on the form
    Private MoveStep As Integer = 3 'this is used to control the speed of the PlayerImg bitmap movement

    'a Win32 Api function used to get the State of a Key being pressed or not pressed
    <DllImport("user32.dll", EntryPoint:="GetAsyncKeyState")> Public Shared Function GetAsyncKeyState(ByVal vKey As Integer) As Short
    End Function

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        PlayerImg.Dispose() 'always dispose of any New Bitmaps you created at a class scope when the form is closing
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        PlayerRect = New Rectangle(CInt((Me.ClientSize.Width / 2) - (PlayerImg.Width / 2)), CInt((Me.ClientSize.Height / 2) - (PlayerImg.Height / 2)), PlayerImg.Width, PlayerImg.Height)
        Me.DoubleBuffered = True 'make the form DoubleBuffered to help stop any flickering that may accure when redrawing the form
        Timer1.Interval = 30 'a Timer will only tick at a maximum of about 20 to 30 millisecond intervals so, 30 is a good place to start
        Timer1.Start()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim repaint As Boolean = False 'used to detect if a key was pressed and the form needs to repaint itself

        If GetAsyncKeyState(Keys.Left) <> 0 Then 'check if the Left Arrow key is or has been pressed
            If PlayerRect.X - MoveStep < 0 Then 'check if the new X position will be outside the client area of the form
                PlayerRect.X = 0 'if it will be outside the client area then set the X location to 0 instead
            Else
                PlayerRect.X -= MoveStep 'if it wont be ouside the client area then subtract the MoveStep value from X location
            End If
            repaint = True 'indicates that a key was detected and the form should repaint itself
        End If

        If GetAsyncKeyState(Keys.Right) <> 0 Then 'check if the Right Arrow key is or has been pressed
            If PlayerRect.X + MoveStep + PlayerRect.Width > Me.ClientSize.Width Then 'check if the new X position will be outside the client area of the form
                PlayerRect.X = Me.ClientSize.Width - PlayerRect.Width 'if it will be outside the client area then set the X location to the client width minus the PlayerImg bitmap width
            Else
                PlayerRect.X += MoveStep 'if it wont be outside the client area then add the MoveStep value to the X location
            End If
            repaint = True 'indicates that a key was detected and the form should repaint itself
        End If

        'these repeate the same steps but, for the Up and Down Arrow keys and the Y location of the PlayerRect
        If GetAsyncKeyState(Keys.Up) <> 0 Then
            If PlayerRect.Y - MoveStep < 0 Then
                PlayerRect.Y = 0
            Else
                PlayerRect.Y -= MoveStep
            End If
            repaint = True
        End If

        If GetAsyncKeyState(Keys.Down) <> 0 Then
            If PlayerRect.Y + MoveStep + PlayerRect.Height > Me.ClientSize.Height Then
                PlayerRect.Y = Me.ClientSize.Height - PlayerRect.Height
            Else
                PlayerRect.Y += MoveStep
            End If
            repaint = True
        End If

        If repaint Then Me.Refresh() 'if a key was detected then force the Form to repaint itself immediately
    End Sub

    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        e.Graphics.DrawImage(PlayerImg, PlayerRect) 'draw the PlayerImg bitmap at the location and size of the PlayerRect when the form is repainted
    End Sub
End Class


This post has been edited by IronRazer: 24 February 2016 - 10:22 AM


Is This A Good Question/Topic? 3
  • +

Replies To: Drawing and moving a game sprite image on a form

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14175
  • View blog
  • Posts: 56,782
  • Joined: 12-June 08

Re: Drawing and moving a game sprite image on a form

Posted 23 February 2016 - 07:50 PM

Lol! OOP with Video Game Basics Part 1 :D
Was This Post Helpful? 1
  • +
  • -

#3 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1505
  • View blog
  • Posts: 3,809
  • Joined: 01-February 13

Re: Drawing and moving a game sprite image on a form

Posted 24 February 2016 - 09:44 AM

Yes, that is a great tutorial that explains pretty much the whole concept. 8)

I just wanted something basic i could point beginners to so that they can get the basic concept of drawing and moving a Bitmap image on the Form using a Rectangle.
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14175
  • View blog
  • Posts: 56,782
  • Joined: 12-June 08

Re: Drawing and moving a game sprite image on a form

Posted 24 February 2016 - 09:45 AM

I hate the use of bitmaps as 'sprints' so.. frigg'n.. much.

---
I'm a dink and can't get what I want to say out.

This post has been edited by modi123_1: 24 February 2016 - 10:36 AM

Was This Post Helpful? 0
  • +
  • -

#5 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1505
  • View blog
  • Posts: 3,809
  • Joined: 01-February 13

Re: Drawing and moving a game sprite image on a form

Posted 24 February 2016 - 10:31 AM

Well, i edited out all the 'Sprites' in my post, just for you. However, i can`t edit the Title of the thread. If you feel like it, change the title to 'Bitmap' instead of 'Game Sprite'. It would be more appropriate. 8)
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14175
  • View blog
  • Posts: 56,782
  • Joined: 12-June 08

Re: Drawing and moving a game sprite image on a form

Posted 24 February 2016 - 10:36 AM

Sorry.. I am a dink. Multitasking too much this morning.

What I meant to say is I hate PICTUREBOX objects as sprites and love BITMAP+RECT as sprites.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1