tags:

views:

80

answers:

5

I have the mission to make a small game for a school project. Pictures boxes, moved by a timer for walking enemies.If there are around 5 or 6 moving picture boxes at the form, my application get troubles and lags. After I kill some enemies (remove them from the Controls Collection of the Form/Panel) It come back smooth.

I think the loop of the enemy movement is too complicated but I don't know how to make that simpler.

Private Sub TimerEnemyMovement_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerEnemyMovement.Tick
    For Each Enemy As Control In PanelBackground.Controls
        If Enemy.Name.Substring(0, 5) = "Enemy" Then
            _enemy.MoveEnemy(Enemy, 2)
        End If
    Next
End Sub

I also thought about Multithreading but not sure this would solve the problem and there is also the problem that I can't access the Controls of my mainform.

You see, I don't have much knowledge about vb.net

Any ideas how to fix that lag?

A: 

VB.NET WinForm apps aren't the ideal setup to create games or moving objects. The Painting of the form uses to much performance as you have found out.

Try moving to a VB.NET WPF (Windows Presentation Framework) application it handles graphics a lot better.

rdkleine
I possibly wouldn't advocate using WPF for a "beginner" school project. It introduces a whole lot of other complexities that probably aren't related to the exercise in hand. WPF has quite a steep learning curve compared to WinForms.
chibacity
I know it's not made for games. But thats my excercise.But after I posted my question I saw that not the moving objects are the problem. It's the background. I have a background image and the enemies are animated gifs with transparent background. It seems that WinForms can't handle this that good.
René
I used this technique (moving PictureBoxes) in VB6 many times and it was pretty good, for its simplicity. Last time I tried it was VB.NET 2003. It was OK, but the performance wasn't quite as good (but maybe our old computers had to work harder to run .NET?). I haven't tried it since then but I'm surprised it would have gotten worse (we had many more than 5-6 sprites, but maybe our logic was simpler...).
FrustratedWithFormsDesigner
I just read that you're using animated GIFs for the sprites. Have you tried using a static sprite sheet and simply changing the picture of the PictureBox? I suspect the animated GIFs are what's really hurting you. If you use sprite sheets, 6 on-screen sprites shouldn't be a problem.
FrustratedWithFormsDesigner
It seems that the animated gifs alone aren't the problem. Just in the same scope with a background image (for example of the battlefield). After deleting the background of my form and just set it to black, there aren't anymore performance problems. Bad I didn't tried this before I posted my questions ;). However: In fact WinForms can't handle transparent and moving graphics on a background image that good and I have to search an other way to add a game map
René
Ah, sprites: That was my first idea. But I thought that could take more resources than a pre animated gif. Otherwise I didn't know exactly how to do that (because I'm a CSS freak and sometimes it's hard to think the .net way).
René
@René: A sprite sheet gives you much more control over the appearance of an on-screen character than an animated GIF ever could. The code is admittedly more complicated than animated GIFs, but once you understand the idea (and it sounds like you already do), it's qutie straight-forwawrd to get it working. The work is well worth it. Also, a static background image shouldn't take up many resources, unless it's some super-high resolution image from a digital camera.
FrustratedWithFormsDesigner
@René: I didn't know transparencies had gotten that much more difficult. In the past I'd used a GIF-format sprite sheet with transparencies and it worked fine. I would imagine that a PNG file with transparencies would also work, but *might* not perform as well. Or you could use some other format with a "transparency colour".
FrustratedWithFormsDesigner
+1  A: 

Try this:

Private Sub TimerEnemyMovement_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerEnemyMovement.Tick
    SuspendLayout()
    For Each Enemy As Control In PanelBackground.Controls
        If Enemy.Name.Substring(0, 5) = "Enemy" Then
            _enemy.MoveEnemy(Enemy, 2)
        End If
    Next
    ResumeLayout()
End Sub
Joel Coehoorn
Also I think you can use UpdateBegin() just for the panel to avoid blocking the all layout (I think!)
dr. evil
What exactly does that?
René
@Rene - it let form wait to do the repainting until all of the enemy movements for that cycle are determined, so it can do one refresh instead of several.
Joel Coehoorn
A: 

Just a couple of suggestions, that may improve speed

a) Rather than going through through all the controls, how about storing them in an array / list b) old game trick is to draw the scene in memory and then copy it to screen .. so why not have a memory bitmap (or graphic) draw the "enemies" to this and then copy to entire bmp to screen once all is done

spacemonkeys
hmm... your suggestion with the array makes sense. I tried that. But I didn't found a way to make a reference in vb.net such as in c#.
René
A: 

Do all the enemies need to move at the same time? Do some move faster?

if there are 6 enemies, you could move 1,3,5 on the first tick, then 2.4.6 on the next tick etc.

?

JohnnyJP
A: 

I've been using XNA for creating a simple game, it was really cool. Though VB.NET is not officially supported, you can make it work. It's optimized for scenery and large number of animated objects.

Sphynx