首先采用键盘上下左右键来触发模拟踩踏,部分程序代码及效果如下:
1. 触发踩中计分
Storyboard actionShowPos = new Storyboard();
storys.Add(actionShowPos);
actionShowPos.Completed += new EventHandler(delegate (object sender, EventArgs e)
{
storys.Remove(actionShowPos);
if (isover) { return; }
DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
TimeSpan timeSpan = DateTime.UtcNow - epoch;
long ll = (long)timeSpan.TotalMilliseconds;
if (button.Tag.ToString() == "0")
{
if (Math.Abs(ll - left) <= 300)
{
scores += 1;
txtshow.Text = scores.ToString();
return;
}
canvasLeft.Children.Remove(button);
}
else if (button.Tag.ToString() == "1")
{
if (Math.Abs(ll - right) <= 300)
{
scores += 1;
txtshow.Text = scores.ToString();
return;
}
canvasRight.Children.Remove(button);
}
else if (button.Tag.ToString() == "2")
{
if (Math.Abs(ll - up) <= 300)
{
scores += 1;
txtshow.Text = scores.ToString();
return;
}
canvasUp.Children.Remove(button);
}
else if (button.Tag.ToString() == "3")
{
if (Math.Abs(ll - down) <= 300)
{
scores += 1;
txtshow.Text = scores.ToString();
return;
}
canvasDown.Children.Remove(button);
}
});
2. 随机加载箭头
// ints 随机生成1-2个方向的随机数
foreach (int ii in ints)
{
Image button = new Image();
button.HorizontalAlignment = HorizontalAlignment.Center;
button.VerticalAlignment = VerticalAlignment.Top;
button.Width = 120;//箭头宽度
button.Height = 120; //箭头高度
TranslateTransform sctr = new TranslateTransform();
sctr.X = 0;
sctr.Y = 0;
TransformGroup trfg = new TransformGroup();
trfg.Children.Add(sctr);
button.RenderTransform = trfg;
button.Source = imagesGame[ii];
if (ii == 0)
{
button.Tag = "0";
canvasLeft.Children.Add(button);
}
else if (ii == 1)
{
button.Tag = "1";
canvasRight.Children.Add(button);
}
else if (ii == 2)
{
button.Tag = "2";
canvasUp.Children.Add(button);
}
else if (ii == 3)
{
button.Tag = "3";
canvasDown.Children.Add(button);
}
}
3. 效果图
此程序已实际应用,需要的可私信。