public void ChangeDirection(Snake snake) { Snakes.TryGetValue(snake.id, out Snake exists); if(exists == null) return; exists.direction = snake.direction; SendSnakes(); }
private void onMove(object state) { foreach (Snake snake in Snakes.Values.ToList()) { Snakes.TryGetValue(snake.id, out Snake exists); if(exists == null) continue; if(exists.direction == "xx") { exists.x += exists.speed; }else if (exists.direction == "-xx") { exists.x -= exists.speed; }else if (exists.direction == "yy") { exists.y -= exists.speed; }else if (exists.direction == "-yy") { exists.y += exists.speed; } if (exists.x >= Canvas.width) { exists.x = 0; } else if (exists.y >= Canvas.height) { exists.y = 0; } else if (exists.x <= 0) { exists.x = Canvas.width; } else if (exists.y <= 0) { exists.y = Canvas.height; } var head = new Pixel {x = exists.x, y = exists.y}; exists.trail.Add(head); if (exists.trail.Count >= exists.tail) { exists.trail.RemoveAt(0); } var hasEaten = Food.Find(i => (i.x - 5) <= head.x && (i.x + 5 >= head.x) && (i.y + 5 >= head.y) && (i.y - 5 <= head.y)) != null; if (hasEaten) { exists.tail += 10; AddFood(); } //check colisions foreach (Snake s in Snakes.Values.ToList()) { if (s.id != snake.id && s.trail.Find(i => (i.x == head.x && i.y == head.y)) != null) { Remove(s.id); }; } } SendSnakes(); }