I'm writting a program to create a "Snake Game" in C#.

What I want to do, is to constantly call the FOrm Paint Event Method to draw the snake and fruits in the form.

I added a Timer that will call the Paint Event Method every 100ms.

The thing is that I don't know how to do this.

I don't know if it is the best approach but is my best idea. The paint event method, has code to add nodes to the linked list that represents the snake and draw them in the screen.

Thanks in advance.

6 Years
Discussion Span
Last Post by MrCapuchino

Just call this.Invalidate(); in your timer event handler. It will cause the form to redraw. You'll probably get flickering though, so you should look into bitmaps and how to draw on one while showing another.



Well, I saw one of my friends and it did not showed flickering, I understand what you say, but she did not say she had trouble with the flickering, she just calls the Paint event, and moves the square in the form, if the square collides with the "fruit" it adds another node to the linked list and draws the linked list in the screen. Of course the nodes have information about the position. It look very nice and in few lines of code.

I did not see the invalidate method though.


Just a thought: Maybe you shoul'd try to paint only whatever needs to be updated from time to time and not the whole form! Maybe the whole content in a panel or such.. Or maybe just the snake, no?


Ideally you'd want to use the Invalidate(Rectangle) method to just invalidate the region where changes happen. But without knowing the skill level of the poster, the basic Invalidate() will do :)


Well, but the snake needs to be painted in the form, wouldn't that need to update the whole form?

The game will end when the snake touches one of the forms border.

For instance,

The paint event will work like this:

if (snake collides with fruit (add another node - draw fruit)

if (user key left) move left
if (user key right) move right
if (user key down) move down
if (user key up) move up

if (snake collides with border) end game

Draw Snake() <--- shows the snake in the form.

I think this is a simple logic and might work, but how can I constantly call the paint event method?

Edited by MrCapuchino: n/a


Ok, I can move the square now with the invalidate method, but like you said it would happen it flickers.

What can I do to stop this? I know using XNA would make this easier, but this is a homework and they want us to use a windows form.

Thanks in advance.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.