Example #1
0
        private void move(object sender, EventArgs e)
        {
            int x = snake[front].Location.X, y = snake[front].Location.Y;

            if (dx == 0 && dy == 0)
            {
                return;
            }
            if (game_over(x + dx, y + dy))
            {
                t.Stop();
                MessageBox.Show("Game Over");
                return;
            }
            if (collision(x + dx, y + dy))
            {
                score += 1;
                if (hits((y + dy) / 20, (x + dx) / 20))
                {
                    return;
                }
                this.Text = score.ToString();
                btn_bx hd = new btn_bx(0, 0);
                front = (front - 1 + 1500) % 1500; snake[front] = hd;
                snake[front].Location = new Point(x + dx, y + dy);
                visit[hd.Location.Y / 20, hd.Location.X / 20] = 1;
                Controls.Add(hd); food_random();
            }
            else
            {
                if (hits((y + dy) / 20, (x + dx) / 20))
                {
                    return;
                }
                visit[snake[back].Location.Y / 20, snake[back].Location.X / 20] = 0;
                front                 = (front - 1 + 1500) % 1500;
                snake[front]          = snake[back];
                snake[front].Location = new Point(x + dx, y + dy);
                back = (back - 1 + 1500) % 1500;
                visit[(y + dy) / 20, (x + dx) / 20] = 1;
            }
        }
Example #2
0
        public Play()
        {
            InitializeComponent();
            visit = new int[this.Height / 20, this.Width / 20];
            btn_bx hd = new btn_bx((rand.Next() % 50) * 20, (rand.Next() % 25) * 20);

            goal.Location = new Point((rand.Next() % 50) * 20, (rand.Next() % 25) * 20);
            snake[front]  = hd; Controls.Add(hd);
            for (int i = 0; i < 25; i++)
            {
                for (int j = 0; j < 50; j++)
                {
                    visit[i, j] = 0;
                    available.Add(i * 50 + j);
                }
            }
            visit[hd.Location.Y / 20, hd.Location.X / 20] = 1;
            available.Remove((hd.Location.Y / 20) * 25 + hd.Location.X / 20);
            userPlay();
        }