Hello everyone,
I created a simple snake game. My project includes a panel and 2 buttons (go left, go right). After clicking right button program checks whether snake's head reached to the feed or not. If yes gets the fish. Every click steps the snakes just 1 picturebox.
There is no problem until here. But I want to develop my project. namely, I want to convert my project into 4 direction snake game. For instance, the table will be 10*10 picture box are and there will be 4 buttons; left, right, up and down.
I have spend too much time but I couldn't solve. Is there any solution or suggestion?
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;
namespace snakeEW
{
public partial class Form1 : Form
{
private PictureBox[] pc;
private int f, r;
int sp, x = 0, y = 0;
Random random = new Random();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
f=0;r=0;
pc = new PictureBox[20];
sp = random.Next() % 18 + 1;
sp = 5;
int with = 40, height = 40;
for (int i = 0; i < 20; i++)
{
pc[i] = new PictureBox();
pc[i].Image = imageList1.Images[0];
pc[i].Size = new Size(with, height);
pc[i].Location = new Point(x, y);
if (i == 0) pc[0].Visible = true;
else pc[i].Visible = false;
if (i == sp)
{
pc[i].Image = imageList1.Images[1];
pc[i].Visible = true;
}
x += 40;
this.panel1.Controls.Add(pc[i]);
}
}
private void btnE_Click(object sender, EventArgs e)
{
if (f == 19)
{ MessageBox.Show("No more RIGHT"); return; }
else
{
pc[++f].Visible = true;
if (f != sp)
{
pc[r++].Visible = false;
}
else
{
pc[f].Image = imageList1.Images[0];
getFish();
}
}
}
private void getFish()
{
do
sp = random.Next() % 19 + 1;
while (sp >=r && sp<= f);
pc[sp].Image = imageList1.Images[1];
pc[sp].Visible = true;
}
private void btnW_Click(object sender, EventArgs e)
{
if (r == 0) { MessageBox.Show("No more LEFT"); return; }
else
{
pc[--r].Visible = true;
if (r != sp)
{
pc[f--].Visible = false;
}
else
{
pc[r].Image = imageList1.Images[0];
getFish();
}
}
}
}
}
I should use node.
Thanks to everyone. Have nice day.