110,534
社区成员
发帖
与我相关
我的任务
分享
<Window x:Class="TrackGame.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TrackGame"
xmlns:text="clr-namespace:TrackGame.Properties"
mc:Ignorable="d"
Title="MainWindow" Height="610" Width="750">
<Window.Resources>
<SolidColorBrush x:Key="BlueColor" Color="#FF35AAF7"/>
<SolidColorBrush x:Key="GrayColor" Color="#FFD8D8D8"/>
<SolidColorBrush x:Key="GreenColor" Color="#FF97C300"/>
<SolidColorBrush x:Key="WathetColor" Color="#FFA1D0FF"/>
<Style TargetType="{x:Type Button}">
<Setter Property="Foreground" Value="White"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Padding" Value="1"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="border" SnapsToDevicePixels="true" Background="{StaticResource BlueColor}" >
<ContentPresenter x:Name="contentPresenter" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsDefaulted" Value="true">
<Setter Property="BorderBrush" TargetName="border" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter TargetName="border" Property="Background" Value="{StaticResource GreenColor }"/>
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter Property="Foreground" Value="{StaticResource BlueColor}"/>
<Setter TargetName="border" Property="Background" Value="{StaticResource WathetColor}"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter TargetName="border" Property="Background" Value="{StaticResource WathetColor}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="FontSize" Value="20"/>
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Foreground" Value="{StaticResource GreenColor}"/>
</Style>
</Window.Resources>
<Viewbox Stretch="Fill">
<Grid Margin="15">
<StackPanel Orientation="Vertical" >
<Canvas x:Name="大地" Height="500" Width="700"/>
<Canvas Margin="0,10,0,0" Height="35">
<TextBlock x:Name="游戏信息" Text="点击按钮开始游戏" Canvas.Top="5" Canvas.Left="150"/>
<Button x:Name="开始游戏按钮" Content="开始游戏" Width="150" Height="35" Canvas.Left="400" />
</Canvas>
</StackPanel>
</Grid>
</Viewbox>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace TrackGame
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
private readonly int 地块数量 = 35;
private readonly int 地块尺寸 = 100;
private int 倒计时秒数 = 10;
private readonly Point 关键坐标 = new Point(5, 2);
private readonly Point 起点坐标 = new Point(1, 1);
private readonly Point 终点坐标 = new Point(1, 3);
private 地块 下一地块缓存;
private Rectangle 关键地块;
private Image 火车;
private List<地块> 地块列队;
private List<地块> 已铺地块列队;
private List<Path> 路径集合;
private bool 是否铺完铁轨;
private System.Timers.Timer 游戏开始倒计时;
public MainWindow()
{
InitializeComponent();
this.开始游戏按钮.Click += (s, e) => { this.初始化游戏(); this.开始游戏按钮.IsEnabled = false; };
}
private void 初始化游戏()
{
this.游戏信息.Text = Properties.Resources.游戏信息_开始游戏;
this.初始化地图();
this.初始化起点();
this.初始化终点();
this.初始化计时器();
}
private Path 计算路径(Point 起点, Point 终点, SweepDirection? 方向 = null)
{
var 结果 = new Path();
结果.Stroke = Brushes.Red;
结果.StrokeThickness = 0;
switch (方向)
{
case SweepDirection.Clockwise:
case SweepDirection.Counterclockwise:
{
PathGeometry 几何图形 = new PathGeometry();
PathFigure 图形 = new PathFigure();
ArcSegment 圆弧 = new ArcSegment
(
终点,
new Size(this.地块尺寸 / 2, this.地块尺寸 / 2),
0,
false,
(SweepDirection)方向,
true
);
图形.StartPoint = 起点;
图形.Segments.Add(圆弧);
几何图形.Figures.Add(图形);
结果.Data = 几何图形;
break;
}
default:
{
LineGeometry 直线 = new LineGeometry();
直线.StartPoint = new Point(起点.X, 起点.Y);
直线.EndPoint = new Point(终点.X, 终点.Y);
结果.Data = 直线;
break;
}
}
return 结果;
}
private void 初始化路径()
{
this.路径集合 = new List<Path>(this.地块数量);
var 路径起点 = new Point
(
(this.起点坐标.X * this.地块尺寸) + (this.地块尺寸 * 0.5),
(this.起点坐标.Y * this.地块尺寸) + (this.地块尺寸 * 0.5)
);
var 路径终点 = new Point
(
(this.起点坐标.X * this.地块尺寸) + this.地块尺寸,
(this.起点坐标.Y * this.地块尺寸) + (this.地块尺寸 * 0.5)
);
var 路径 = this.计算路径(路径起点, 路径终点);
this.路径集合.Add(路径);
this.大地.Children.Add(路径);
}
private void 初始化地图()
{
this.是否铺完铁轨 = false;
this.大地.Children.Clear();
this.地块列队 = new List<地块>(this.地块数量);
this.已铺地块列队 = new List<地块>(this.地块数量);
for (int i = 0; i < this.地块数量; i++)
{
var 地块对象 = new 地块(new Point(i % 7, i / 7), 类型.无)
{
Width = this.地块尺寸,
Height = this.地块尺寸,
Margin = new Thickness((i % 7) * this.地块尺寸, (i / 7) * this.地块尺寸, 0, 0),
IsEnabled = false
};
地块对象.Click += 铺设铁轨;
this.地块列队.Add(地块对象);
this.大地.Children.Add(地块对象);
}
this.关键地块 = new Rectangle();
this.关键地块.Margin = new Thickness(this.关键坐标.X * this.地块尺寸, this.关键坐标.Y * this.地块尺寸, 0, 0);
this.关键地块.Stroke = Brushes.CornflowerBlue;
this.关键地块.RadiusX = this.关键地块.RadiusY = 5;
this.关键地块.StrokeThickness = 4;
this.关键地块.Width = this.关键地块.Height = this.地块尺寸;
this.大地.Children.Add(this.关键地块);
}
private void 初始化起点()
{
this.火车 = new Image();
火车.Width = 70;
火车.Height = 30;
this.火车.Stretch = Stretch.Uniform;
this.火车.Source = new BitmapImage(new Uri(System.IO.Directory.GetCurrentDirectory() + Properties.Resources.火车图片路径, UriKind.RelativeOrAbsolute));
this.大地.Children.Add(火车);
Canvas.SetLeft(this.火车, (起点坐标.X + 0.5) * this.地块尺寸 - this.火车.Width / 2);
Canvas.SetTop(this.火车, (起点坐标.Y + 0.5) * this.地块尺寸 - this.火车.Height / 2);
var 起点地块 = this.地块列队.FirstOrDefault(o => o.坐标.Equals(起点坐标));
this.已铺地块列队.Add(起点地块);
起点地块.设为起点();
this.地块列队.FirstOrDefault(o => o.坐标.X == 起点坐标.X + 1 && o.坐标.Y == 起点坐标.Y).IsEnabled = true;
this.初始化路径();
}
private void 初始化终点()
{
var 终点地块 = this.地块列队.FirstOrDefault(o => o.坐标.Equals(终点坐标));
终点地块.设为终点();
}
private void 初始化计时器()
{
this.倒计时秒数 = 10;
this.游戏开始倒计时 = new System.Timers.Timer(1000);
this.游戏开始倒计时.AutoReset = true;
this.游戏开始倒计时.Enabled = true;
this.游戏开始倒计时.Elapsed += 游戏开始倒计时_Elapsed;
this.游戏开始倒计时.Start();
}