vb animation background

PhilH 1 Reputation point
2022-04-04T10:22:06.793+00:00

I'm trying to write some basic maze games and got to the point where I'm doing the graphics.
What I'd like to do is draw a random maze (add a bunch of 'wall' bitmaps to a graphics object, or similar), then 'save' the image I've just created and use it as the background for playing the game.
Effectively, the image would have three 'layers'; a basic background, then the maze, then the things moving around the maze.
Saving the multiple 'drawn' images and using the save as a single background image for a graphics object would be much more efficient than re-drawing the maze every move, but I can't see how to do it at all.
I've chased down various rabbit holes on this site, but they all seem to start off very 'Janet and John' and the suddenly switch to a bunch of impenetrable jargon....
Any help /ideas will be much appreciated

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,755 questions
{count} votes

4 answers

Sort by: Most helpful
  1. PhilH 1 Reputation point
    2022-04-05T09:44:48.813+00:00

    Hi, my apologies for not being clear. Yes, you're correct, the code for things would look something like this;

    g = Graphics.FromImage(Back_Bitmap)

    For loop1 as Int16 = 0 to Num_Wide
    For loop2 as Int16 = 0 to Num_Across
    If Rand.Next(0,4) = 3 Then g.DrawImage(Wall_Bitmap, loop1 * Wall_Bitmap.Width, loop2 * Wall_Bitmap, r, GraphicsUnit.Pixel)
    Next
    Next

    ' Save image in g as 'Background' <= This is the bit I have a problem with

    Do Until End_of_Game
    Calculate(Player_Pos)
    Calculate(Enemy_Pos)
    g = Graphics.FromImage(Background)
    g.DrawImage(Player_Bitmap, Player.X_Pos, Player.Y_Pos, r, GraphicsUnit.Pixel)
    g.DrawImage(Enemy_Bitmap, Enemy.X_Pos, Enemy.Y_Pos, r, GraphicsUnit.Pixel)
    Loop

    I have all the basic 'mechanics' (how things move, start and end conditions, etc.) worked out, but it's (only :) !) the graphics I'm having trouble with. If I can separate the 'fixed' parts of the maze from the moving things, it will (obviously) be a lot more efficient.
    Thank you for your interest


  2. Jiachen Li-MSFT 32,851 Reputation points Microsoft Vendor
    2022-04-06T08:25:40.167+00:00

    Hi @PhilH ,
    You can define a new bitmap to store the drawn maze.

    Dim Background As New Bitmap = Back_Bitmap  
    

    Then use it when drawing in the game.
    Best Regards.
    Jiachen Li

    ----------

    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  3. PhilH 1 Reputation point
    2022-04-06T09:00:52.403+00:00

    Hi, My apologies, I'm apparently still not being clear.

    I'm aware that I could (theoretically) use a bitmap, save it, etc., etc..

    However, I don't know how to get the image held in 'g' (the picture I've built up of the basic background and the maze) into the bitmap called Background.

    There isn't an 'image' property on g, or 'contents', so it's not obvious (*to me) how to do it).

    That is what my issue is, please.

    Thank you for your time (so far) on this


  4. Leon Stanley 96 Reputation points
    2022-04-10T10:36:27.337+00:00

    Are you using e.Graphics in the Paint event to do your drawing?

    in the paint event : -
    e.Graphics.DrawImage(Back_Bitmap, 0, 0)

    ' then draw on the moving sprites

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.