Interview Question in LINQ


 

Interview Question :: How to call paint method in cSharp using Visual Studios

trying to call paint method
here is the code I have so far

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

public partial class Form1 : Form
{
public class Snake
{
static void Main() { }
}
//somewhat of a coordinate system for the game
bool[,] point = new bool[51, 51];
//sets the direction that the snake is supposed to move: 1:up, 2:down, 3:left, 4:right,
int direction = 4;
public Form1()
{
InitializeComponent();
//This sets the starting snake
point[0, 0] = true;
}

private void InitializeComponent()
{
throw new NotImplementedException();
}
//This method is necessary because InitializeComponent() calls it
//this.Load += new System.EventHandler(this.Form1_Load);
private void Form1_Load(object sender, EventArgs e)
{

}


private void Form1_Paint(object sender, PaintEventArgs e)
{
//according to the array point this displays the blocks of the snake and fruit
Graphics g = e.Graphics;
int x = 0; int y = 0;
while (y < 51)
{
bool skip = false;
if (point[x, y] == true)
{ g.FillRectangle(Brushes.GreenYellow, x * 10, y * 10, 10, 10); }
if (x >= 50) { x = 0; y++; skip = true; }
if (x < 50 && skip == false) { x++; }
}
}
private void timer1(object sender, EventArgs e)
{
try
{
int x = 0; int y = 0;
while (y < 51)
{
bool skip = false;
if (direction == 4)
{ if (point[x, y] == true) { point[x, y] = false; point[x + 1, y] = true; break; } }
if (direction == 3)
{ if (point[x, y] == true) { point[x, y] = false; point[x - 1, y] = true; break; } }
if (direction == 2)
{ if (point[x, y] == true) { point[x, y] = false; point[x, y + 1] = true; break; } }
if (direction == 1)
{ if (point[x, y] == true) { point[x, y] = false; point[x, y - 1] = true; break; } }
if (x >= 50) { x = 0; y++; skip = true; }
if (x < 50 && skip == false) { x++; }
}
Invalidate();
}
catch
{
MessageBox.Show("Game Over");
}
}

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
// Invalidate the Eater before moving it
string result = e.KeyData.ToString();
switch (result)
{
case "Left":
direction = 3; break;
case "Right":
direction = 4; break;
case "Up":
direction = 1; break;
case "Down":
direction = 2; break;
}
}
}

by ksk