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\$
7
  • 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 '19 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 '19 at 0:36
  • \$\begingroup\$ DM Gregory can you give me some input. \$\endgroup\$ – gamer67 Feb 4 '19 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 '19 at 22:09
  • 1
    \$\begingroup\$ Hello gamer67! I have deleted the answer you posted because it was not attempting to answer the question. You could edit the question and add your progress. Also, adding the framework you use in the tags could help you get the answer you're looking for. \$\endgroup\$ – Vaillancourt Oct 9 '19 at 12:33

Your Answer

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

Browse other questions tagged or ask your own question.