简体   繁体   中英

What's the best way to draw graphics for a video game in Visual Basic .NET?

I know that VB.NET is bad for video games, but I'm stuck with it for the next little while because that's what my high school computer science course is using.

I know that there are many ways to draw graphics in Visual Basic. Ones I've heard of are using Me.CreateGraphics , using the onPaint event handler, and using a back buffer (though I'm not sure what's the best way to use those methods). I'm mostly interested in creating simple 2D games, by the way. Also, it needs to be made using the default VB.NET library, so I can't install XNA or something of the sort.

For simple games GDI+ (the CreateGraphics way) is ok. Add a Picturebox to your form. This Picturebox will show each rendered frame. However you do not draw directly onto it, but onto another image - or backbuffer. Basically you would create a game loop. Here you handle user inputs, perform game logic, and render a new frame:

Sub GameLoop()
  Do
    HandeUserInput() 'Handles keys or mouse movement
    PerformGameLogic() 'Move NPCs, etc.
    RenderNewFrame() 'Redraw the new state of the game
  Loop
End Sub

Userinput and game logic are up to you. Rendering will be done by creating a new bitmap in the size you want, and then use a GDI+ graphics object to draw stuff on it. Then you show this new bitmap in the picturebox once it is done.

Sub RenderNewFrame()
  Dim NewFrame as New Bitmap(640, 480) 'Or whatever resolution you want
  Using g as Graphics = Graphics.FromImage(NewFrame)
    DrawWorld(g)
    DrawPlayer(g)
  End Using
  If Picturebox1.Image IsNot Nothing Then Picturebox1.Image.Dispose()
  Picturebox1.Image = NewFrame
End Sub

I dispose the previous frame, since the bitmaps are not managed and would pile up in memory quickly.

This is the basic framework for a game and works actually quite nicely in VB.NET with GDI+. You may want to add small delays between each frame, 1..2 ms. This will prevent much of the processor load while not really affecting the game performace.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM