[C#中文命名系列]开火车小游戏源代码分享

zmidl 2017-07-30 09:26:12
加精

前台

<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();
}


未完余码贴2楼

扫码关注菜鸟园回复"开火车"有超详细图文教程和源代码
...全文
4520 46 打赏 收藏 转发到动态 举报
写回复
用AI写文章
46 条回复
切换为时间正序
请发表友善的回复…
发表回复
Douillard_ 2017-08-26
  • 打赏
  • 举报
回复
参考参考
s60v20 2017-08-20
  • 打赏
  • 举报
回复
不错,不错,学写了
zmidl 2017-08-14
  • 打赏
  • 举报
回复
引用 42 楼 m_focus_wpf 的回复:
[quote=引用 40 楼 lcmsx 的回复:] 楼主,你中文编程啊??我醉了,跪拜了!! 还有,你用WPF写,结果用的是Winform的写法! 可以优化优化的哟!!!!!
楼主,你没体现WPF的优势。。。[/quote] 你一定没有关注我的教程吧 我开头就说明了只是用WPF来做,不使用WPF的特性,也不用C#的高级语法,而且用中文变量名。
zmidl 2017-08-11
  • 打赏
  • 举报
回复
引用 40 楼 lcmsx 的回复:
楼主,你中文编程啊??我醉了,跪拜了!! 还有,你用WPF写,结果用的是Winform的写法! 可以优化优化的哟!!!!!
嗯嗯好的呀,你关注一下把源代码优化下发我,我们互相交流下吧。
乱臣贼子 2017-08-11
  • 打赏
  • 举报
回复
楼主,你中文编程啊??我醉了,跪拜了!! 还有,你用WPF写,结果用的是Winform的写法! 可以优化优化的哟!!!!!
半拉调 2017-08-11
  • 打赏
  • 举报
回复
引用 40 楼 lcmsx 的回复:
楼主,你中文编程啊??我醉了,跪拜了!! 还有,你用WPF写,结果用的是Winform的写法! 可以优化优化的哟!!!!!
楼主,你没体现WPF的优势。。。
zmidl 2017-08-11
  • 打赏
  • 举报
回复
引用 37 楼 liuyuehui110 的回复:
[quote=引用 33 楼 zmidl 的回复:] [quote=引用 32 楼 liuyuehui110 的回复:] [quote=引用 27 楼 zmidl 的回复:] [quote=引用 26 楼 liuyuehui110 的回复:] 感觉还是不错的 能不能把源码分享下
扫码关注后回复"开火车"就可以看超详细图文教程,还有源代码下载[/quote] 程序还不错 你们是培训学校么[/quote] 不是,我是个人搞了一个微信公众号,想每月做一个小游戏教程积累人气的。话说朋友你有没有关注啊??[/quote] 我已经关注了, 既然是你个人的。 以后多交流啊 你的程序很不错[/quote] 嗯嗯 好的,多多交流,谢谢关注。
  • 打赏
  • 举报
回复
引用 33 楼 zmidl 的回复:
[quote=引用 32 楼 liuyuehui110 的回复:] [quote=引用 27 楼 zmidl 的回复:] [quote=引用 26 楼 liuyuehui110 的回复:] 感觉还是不错的 能不能把源码分享下
扫码关注后回复"开火车"就可以看超详细图文教程,还有源代码下载[/quote] 程序还不错 你们是培训学校么[/quote] 不是,我是个人搞了一个微信公众号,想每月做一个小游戏教程积累人气的。话说朋友你有没有关注啊??[/quote] 我已经关注了, 既然是你个人的。 以后多交流啊 你的程序很不错
MYsce 2017-08-10
  • 打赏
  • 举报
回复
萌新前来学习
hello_mcu 2017-08-07
  • 打赏
  • 举报
回复
引用 6 楼 diaodiaop 的回复:
老司机等等我。感谢楼主分享。
她微笑的脸_ 2017-08-07
  • 打赏
  • 举报
回复
zmidl 2017-08-04
  • 打赏
  • 举报
回复
引用 31 楼 xXx_JQK 的回复:
楼主够强大,仔细看码中...
朋友关注公众号吧,超详细图文教程呢。
zmidl 2017-08-04
  • 打赏
  • 举报
回复
引用 32 楼 liuyuehui110 的回复:
[quote=引用 27 楼 zmidl 的回复:] [quote=引用 26 楼 liuyuehui110 的回复:] 感觉还是不错的 能不能把源码分享下
扫码关注后回复"开火车"就可以看超详细图文教程,还有源代码下载[/quote] 程序还不错 你们是培训学校么[/quote] 不是,我是个人搞了一个微信公众号,想每月做一个小游戏教程积累人气的。话说朋友你有没有关注啊??
zmidl 2017-08-03
  • 打赏
  • 举报
回复
引用 12 楼 qq_36348017 的回复:
看不懂呀······
扫码关注后回复"开火车"就可以看超详细图文教程,还有源代码下载
zmidl 2017-08-03
  • 打赏
  • 举报
回复
引用 23 楼 cattpon 的回复:
learning~
扫码关注后回复"开火车"就可以看超详细图文教程,还有源代码下载
zmidl 2017-08-03
  • 打赏
  • 举报
回复
引用 26 楼 liuyuehui110 的回复:
感觉还是不错的 能不能把源码分享下
扫码关注后回复"开火车"就可以看超详细图文教程,还有源代码下载
  • 打赏
  • 举报
回复
感觉还是不错的 能不能把源码分享下
  • 打赏
  • 举报
回复
引用 27 楼 zmidl 的回复:
[quote=引用 26 楼 liuyuehui110 的回复:] 感觉还是不错的 能不能把源码分享下
扫码关注后回复"开火车"就可以看超详细图文教程,还有源代码下载[/quote] 程序还不错 你们是培训学校么
xXx_JQK 2017-08-03
  • 打赏
  • 举报
回复
楼主够强大,仔细看码中...
dy00544 2017-08-03
  • 打赏
  • 举报
回复
吓尿 c#啥时候能用中文作为方法名和变量名
加载更多回复(14)

110,532

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧