Hello,

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.

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.

Ok,

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?

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.

You could try to set the DoubleBuffered property of the form to true.

NICE!!!!!!

That was easy...

Thank you

Hello all,

When i call invalidate function in timer then bmp image move on form. But another issue arises that when i add textbox or button or any other control then form stopped working and some part of control are not visible. i.e. When we call invalidate function then textbox and button are not visible. Why?

Can anyone help me please?

Hi Kundan_4 welcome here at Daniweb.
Please read this item befor posing questions in the future.
Could you send in some code and a screenshot?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.