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

Drawing a tiled map flickering

$
0
0
Hi.

I'm making a game that draws tiles in a picturebox to form a map, then draws an image with a transparent background, representing the character. The problem is, when the player moves, the character image flickers. Can anyone advise me on how to resolve this?

The program checks to see what is on the square, then calls the following subroutine to draw the tiles on the screen:

Code:

    Sub Draw(X As Integer, Y As Integer, Sel As Integer, Trans As Boolean)
        Dim g As Graphics
        Dim Img As New System.Drawing.Bitmap(picTiles.Image)

        Dim src_rect As New Rectangle(Sel * 64, 0, 64, 64)
        Dim dst_rect As New Rectangle((X - 1) * 64, Y * 64, 64, 64)

        If Strt = False Then g = Graphics.FromImage(picScreen.Image) Else g = picScreen.CreateGraphics()
        If Trans = True Then Img.MakeTransparent(System.Drawing.Color.White)
        g.DrawImage(Img, dst_rect, src_rect, GraphicsUnit.Pixel)

        g.Dispose()
    End Sub

After all the tiles have been printed this subroutine is called to draw the character.

Code:

    Sub Playr()
        Dim g As Graphics
        Dim Img As New System.Drawing.Bitmap(picTiles2.Image)

        Dim src_rect As New Rectangle(Player.Direction * 64, 0, 64, 64)
        Dim dst_rect As New Rectangle(256, 256, 64, 64)

        If Strt = False Then g = Graphics.FromImage(picScreen.Image) Else g = picScreen.CreateGraphics()
        Img.MakeTransparent(System.Drawing.Color.Magenta)
        g.DrawImage(Img, dst_rect, src_rect, GraphicsUnit.Pixel)

        g.Dispose()
    End Sub

Then, the value Strt is switched to true so that when the player moves, and the tiles are redrawn, it uses the g = picScreen.CreateGraphics() method.

Any ideas?

Viewing all articles
Browse latest Browse all 27208

Trending Articles



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