0
\$\begingroup\$

I am making a snake game. I have got it to animate. I just want it to follow the mouse pointer. I am able to find the mouse cursor position. I am also to get my snake to move to the right. What I want is to get my snake to move in the direction in the mouse pointer.

        private void timer1_Tick(object sender, EventArgs e)
    {
        Graphics g = this.CreateGraphics();
        SolidBrush greenBrush = new SolidBrush(Color.Green);
        SolidBrush blackBrush = new SolidBrush(Color.Black);
        Rectangle rect_green = new Rectangle(340 + x, 280 + y, 100, 10);
        Rectangle rect_black = new Rectangle(240 + x, 280 + y, 100, 10);
        g.FillRectangle(greenBrush, rect_green);
        g.FillRectangle(blackBrush, rect_black);
        if (x >= 340)
        {
            x = 340;
        }
        x += 5;
        greenBrush.Dispose();
        g.Dispose();

        if (MouseButtons == MouseButtons.Left)
        {
            Graphics g_two = this.CreateGraphics();
            SolidBrush greenBrush_two = new SolidBrush(Color.Green);
            SolidBrush blackBrush_two = new SolidBrush(Color.Black);
            Rectangle rect_green_two = new Rectangle(340 + x, 280 + y, 10, 100);
            Rectangle rect_black_two = new Rectangle(340 + x, 180 + y, 10, 100);
            g_two.FillRectangle(greenBrush_two, rect_green_two);
            g_two.FillRectangle(blackBrush_two, rect_black_two);
            y += 5;
            greenBrush_two.Dispose();
            g_two.Dispose();
        }
    }
\$\endgroup\$
  • 1
    \$\begingroup\$ That sounds like a reasonable goal. How have you tried to implement this cursor-following behaviour so far, or, what's blocking you from making progress on it? Do you know how to get the cursor's position, how to translate that position into your snake's world coordinates, how to choose a direction based on that position, etc...? The more you can tell us about where specifically you're stuck, the faster we can get you high-quality answers. \$\endgroup\$ – DMGregory Feb 4 at 0:22
  • \$\begingroup\$ I have got the snake to move to the right by using a timer function. I want to get the snake to move down by using the mouse pointer. when I use the mouse in my code it draws a snake but does not move the snake down in an animated way. \$\endgroup\$ – gamer67 Feb 4 at 0:36
  • \$\begingroup\$ DM Gregory can you give me some input. \$\endgroup\$ – gamer67 Feb 4 at 21:08
  • \$\begingroup\$ It's still not clear to me what aspect of this you need help with. Try breaking your problem down into steps, like "1. Find the position of the cursor on the screen. 2. Determine the direction the snake should steer. 3. Set the snake's movement state to travel in that direction. 4. Animate the snake 1 step in the direction specified by its movement state" and show us how you're attempting to perform each step, or which step you don't know how to attempt. \$\endgroup\$ – DMGregory Feb 4 at 22:09
  • \$\begingroup\$ I can determine the cursor position. also I can get the snake to move to the right. I am unable to determine how to get the snake to steer in the direction of the mouse. I am confused on how to do step 2. \$\endgroup\$ – gamer67 Feb 4 at 23:43
0
\$\begingroup\$

Well I have made some progress in my game. I have got my snake to move around the screen but I want the animation to be more smoother.

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        Graphics g = this.CreateGraphics();
        SolidBrush greenBrush = new SolidBrush(Color.Green);
        SolidBrush blackBrush = new SolidBrush(Color.Black);
        Rectangle rect_green = new Rectangle(340 + x, 280 + y, 10, 10);
        Rectangle rect_black = new Rectangle(330 + x, 280 + y, 10, 10);
        g.FillRectangle(greenBrush, rect_green);
        g.FillRectangle(blackBrush, rect_black);
        greenBrush.Dispose();
        g.Dispose();
    }

    int x = 0, y = 0;

    Random rnd = new Random();

    private void timer1_Tick(object sender, EventArgs e)
    {
        int move = rnd.Next(1, 5);

        switch (move)
        {
            case 1:
                for (int i = 0; i <= 20; i++)
                {
                    x++;
                }
                break;
            case 2:
                for (int i = 0; i <= 20; i++)
                {
                    x--;
                }
                break;
            case 3:
                for (int i = 0; i <= 20; i++)
                {
                    y++;
                }
                break;
            case 4:
                for (int i = 0; i <= 20; i++)
                {
                    y--;
                }
                break;
        }

        if (MouseButtons == MouseButtons.Left)
        {

        }
        Invalidate();
    }

    private void Form1_MouseDown(object sender, MouseEventArgs e)
    {

    }
}

}

\$\endgroup\$
  • \$\begingroup\$ The timer1_tick is adding 20 places to the position before the game is drawn again. Try to make it so it adds 1, draws, adds 1 again, draws again, etc. \$\endgroup\$ – Peethor Feb 17 at 7:40
  • \$\begingroup\$ can I get an example of this answer? \$\endgroup\$ – gamer67 Feb 18 at 2:20
  • \$\begingroup\$ is there anyway I can use the mouse to change the snake direction? \$\endgroup\$ – gamer67 Feb 22 at 2:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.