Click here to Skip to main content
15,886,773 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?

C#
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;// 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;
                this.panel1.Controls.Add(pc[i]);
            }

        }
        private void btnE_Click(object sender, EventArgs e)//Go East
        {
            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)//Go West
        {
            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.
Posted
Updated 8-Dec-12 9:53am
v2

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
 
Comments
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