Click here to Skip to main content
11,930,048 members (44,226 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# Game GUI
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.
Posted 7-Dec-12 4:32am
Edited 8-Dec-12 10:53am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.
TuranEmre 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.
TuranEmre 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.
TuranEmre 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].
Richard MacCutchan 9-Dec-12 4:26am
This is just an inner and outer loop issue. For each row in 8 rows, create 8 columns, and for each column create the square. I don't quite understand what the problem is.
TuranEmre 9-Dec-12 7:07am
I tried your suggestion. The surface is okey. However now I can't control the buttons. I should use node.
Richard MacCutchan 9-Dec-12 11:27am
What does that mean? I really cannot guess what you are doing here. You need to take a step back and spend some time thinking about what you are trying to do and how you can do it, rather than looking at a single part of the program without reference to the other parts.
TuranEmre 9-Dec-12 12:07pm
I can't use node. So I can't solve the problem. I am trying more than 4 days
Richard MacCutchan 9-Dec-12 13:05pm
Yet another meaningless statement. What do you mean "I can't use node"; what are you trying to use it for?
Sergey Alexandrovich Kryukov 18-Jan-13 21:33pm
It looks you are right, a 5.
Problems, Turan? :-)

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.151126.1 | Last Updated 8 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100