Click here to Skip to main content
15,742,477 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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()

        private void Form1_Load(object sender, EventArgs e)
            pc = new PictureBox[20];

            sp = random.Next() % 18 + 1;// Position of Bird selected
            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;

        private void btnE_Click(object sender, EventArgs e)//Go East
            if (f == 19)
            { MessageBox.Show("No more RIGHT"); return; }
                pc[++f].Visible = true;
                if (f != sp)
                    pc[r++].Visible = false;

                    pc[f].Image = imageList1.Images[0];


        private void getFish()
                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)//Go West
            if (r == 0) { MessageBox.Show("No more LEFT"); return; }
                pc[--r].Visible = true;
                if (r != sp)
                    pc[f--].Visible = false;

                    pc[r].Image = imageList1.Images[0];



I should use node.

Thanks to everyone. Have nice day.
Updated 8-Dec-12 9:53am

1 solution

The issue is just the same as your existing code. For the up arrow you decrement the vertical position and for the down arrow you increment it. You also might find it easier to use more useful variable names than f and r.
Share this answer
FoxRoot 7-Dec-12 12:36pm    
Thank you for your consideration. Buttos are okey however I have problems creating the surface.
Richard MacCutchan 7-Dec-12 13:19pm    
It is no good saying "I have problems". You need to explain what these problems are so people can make suggestions or give useful advice.
FoxRoot 7-Dec-12 14:32pm    
Actually I have problem to create a 8*8 pictureboxeed panel. For example my software just creates 1 dimensional 1*18 pictureboxed panel.
Richard MacCutchan 7-Dec-12 15:46pm    
Well just create 8 of those.
FoxRoot 8-Dec-12 15:22pm    
I have created the surface . But I want to create as 2 dimensional array. [8][8] not [64] because I can't control when I used [64].

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900