2

I am programming "Snake" game (in which the snake moves in the game board and eats apples in order to grow) and the player can navigate the snake by swiping.
I want to program the game using canvas.
How do I use swipe in canvas?

class SnakeBoard : View
    {
        Paint p;
        int x, y;
        int deltax, deltay;
        bool b;
        public SnakeBoard(Context context) : base(context)
        {
            p = new Paint();
            p.Color = Color.Blue;
            x = 100;
            y = 500;
            deltax = 5;
            deltay = 5;
        }
        protected override void OnDraw(Canvas canvas)
        {
            base.OnDraw(canvas);
            canvas.DrawCircle(x, y, 50, p);
            x += deltax;
            if (x < 0 || x > canvas.Width)
                deltax = -deltax;
            if (y < 0 || y > canvas.Height)
                deltay = -deltay;
            Invalidate();
        }

For example here I painted a circle which moves constantly from right to left.
What I want to do is when the user swipes left the circle will move left, when the user swipes right the circle will move right, when the user swipes up the circle will move up and when the user swipes down the circle will move down.

  • Can you provide code of what you have already tried or any material that the community could support you on? – Constantin Oct 27 '20 at 13:09
  • @Constantin Here I added some code – Michal Ozeri Oct 27 '20 at 14:43
  • You should detect the swipe directions and here are some thread may help: [how-to-detect-swipe-direction-between-left-right-and-up-down](https://stackoverflow.com/questions/13095494/how-to-detect-swipe-direction-between-left-right-and-up-down) and [how-to-detect-the-swipe-left-or-right-in-android](https://stackoverflow.com/questions/6645537/how-to-detect-the-swipe-left-or-right-in-android). – nevermore Oct 28 '20 at 08:11

0 Answers0