void InitializeBalls() { // 初始化数组 balls1 = new Ball[width, height]; balls2 = new Ball[width, height]; drawingBalls = new Ellipse[width, height]; for (int j = 0; j < height; j++) { for (int i = 0; i < width; i++) { // 新建小球 balls1[i, j] = new Ball(); Ball b = balls1[i, j]; b.position = new Point(i * 30, j * 30); if (i == 0 && j == 0) b.isAnchor = true; // 复制一份 balls2[i, j] = balls1[i, j]; // 当前的和上一帧的 currBalls = balls1; lastBalls = balls2; // 画出小球 Ellipse ball = new Ellipse(); ball.Height = ball.Width = b.diameter ; if (b.isAnchor) ball.Fill = new SolidColorBrush(Colors.Gray); else ball.Fill = new SolidColorBrush(Colors.White); ball.Stroke = new SolidColorBrush(Colors.Black); ball.StrokeThickness = 2; ball.RenderTransform = new TranslateTransform(b.position.X, b.position.Y); Box.Children.Add(ball); // 为小球添加响应函数 ball.MouseLeftButtonDown += onPress; //ball.MouseMove += onMove; // 加入数组 drawingBalls[i, j] = ball; } } }
void onRelease(Object sender, MouseButtonEventArgs e) { if (dragingBall != null) dragingBall.isDraging = false; dragingBall = null; dragingElli = null; }
public Ball GetNeighbour(Ball b) { return b == b1 ? b2 : b1; }
void onPress(Object sender, MouseButtonEventArgs e) { lastMousePos = e.GetPosition(this); Ellipse elli = sender as Ellipse; // 寻找被选中的球 for (int j=0; j<height; j++) for (int i = 0; i < width; i++) { if (drawingBalls[i, j] == elli && !lastBalls[i, j].isAnchor) { dragingElli = elli; dragingBall = lastBalls[i, j]; dragingBall.isDraging = true; return; } } }
public Stick(Ball b1, Ball b2) { this.b1 = b1; this.b2 = b2; }