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.