自己编写的silverlight小游戏
using System; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Ink; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using Microsoft.Expression.Shapes; using System.Collections.Generic; using Microsoft.Expression.Controls; using System.Windows.Media.Imaging;
namespace SilverlightApplication1 { public partial class MainPage : UserControl { List<Grid> positions = new List<Grid>(); List<Grid> savepositions = new List<Grid>(); public MainPage() { // 为初始化变量所必需 InitializeComponent(); InitGrid(); ScaleTransform st = new ScaleTransform(); st.ScaleX = 2; st.ScaleY = 2; LayoutRoot.RenderTransform = st; } private void InitGrid() { Path gridPath = new Path(); PathGeometry pg = new PathGeometry(); PathFigure pf = new PathFigure(); pf.StartPoint = new Point(0, 0); LineSegment line1 = new LineSegment(); line1.Point = new Point(300,0); pf.Segments.Add(line1); LineSegment line2 = new LineSegment(); line2.Point = new Point(300, 300); pf.Segments.Add(line2); LineSegment line3 = new LineSegment(); line3.Point = new Point(0, 300); pf.Segments.Add(line3); pf.IsClosed = true; pg.Figures.Add(pf); PathFigure pf2 = new PathFigure(); pf2.StartPoint = new Point(100, 0); LineSegment ls2 = new LineSegment(); ls2.Point = new Point(100, 300); pf2.Segments.Add(ls2); pg.Figures.Add(pf2); PathFigure pf3 = new PathFigure(); pf3.StartPoint = new Point(200, 0); LineSegment ls3 = new LineSegment(); ls3.Point = new Point(200, 300); pf3.Segments.Add(ls3); pg.Figures.Add(pf3); PathFigure pf4 = new PathFigure(); pf4.StartPoint = new Point(0, 100); LineSegment ls4 = new LineSegment(); ls4.Point = new Point(300, 100); pf4.Segments.Add(ls4); pg.Figures.Add(pf4); PathFigure pf5 = new PathFigure(); pf5.StartPoint = new Point(0, 200); LineSegment ls5 = new LineSegment(); ls5.Point = new Point(300, 200); pf5.Segments.Add(ls5); pg.Figures.Add(pf5); gridPath.Data = pg; gridPath.Stroke = new SolidColorBrush(Colors.Black); gridPath.StrokeThickness = 1; this.LayoutRoot.Children.Add(gridPath); List<Grid> plist = new List<Grid>(); for (int i = 0; i <= 300;i = i + 100 ) { for (int j = 0; j <= 300;j=j+100 ) { Grid grid = new Grid(); grid.Width = 30; grid.Height = 30; grid.VerticalAlignment = VerticalAlignment.Top; grid.HorizontalAlignment = HorizontalAlignment.Left; grid.Margin = new Thickness(i-15, j-15, 0, 0); grid.Background = new SolidColorBrush(Colors.LightGray); MyCard card = new MyCard(new Point(i, j)); card.IsSave = false; grid.Tag = card; plist.Add(grid); } } Random random = new Random(); int indext=1; while (plist.Count > 0) { Grid grid = plist[random.Next(plist.Count)]; plist.Remove(grid); MyCard card = grid.Tag as MyCard; card.Index = indext++; grid.Tag = card; positions.Add(grid); this.LayoutRoot.Children.Add(grid); grid.Background = new SolidColorBrush(Colors.LightGray); grid.MouseLeftButtonDown += new MouseButtonEventHandler(grid_MouseLeftButtonDown); } for (int i = 50; i <= 250;i+=100 ) { for (int j = 50; j <= 250;j+=100 ) { Grid grid = new Grid(); grid.Width = 30; grid.Height = 30; grid.VerticalAlignment = VerticalAlignment.Top; grid.HorizontalAlignment = HorizontalAlignment.Left; grid.Margin = new Thickness(i - 15, j - 15, 0, 0); MyCard card = new MyCard(new Point(i, j)); card.IsSave = true; grid.Tag = card; savepositions.Add(grid);
grid.MouseLeftButtonDown += new MouseButtonEventHandler(grid_MouseLeftButtonDown); } } } Grid selectGrid=null; void grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { if (selectGrid==null) { selectGrid = sender as Grid; MyCard card = selectGrid.Tag as MyCard; if (card==null) { selectGrid.Background = new SolidColorBrush(Colors.White); selectGrid = null; return; } if (!card.IsActive) { Image image = new Image(); image.Source = new BitmapImage(new Uri("/SilverlightApplication1;component/Images/" + card.Index.ToString() + ".jpg", UriKind.Relative)); //TextBlock textblock = new TextBlock(); //textblock.Text = card.Index.ToString(); selectGrid.Children.Add(image); card.ImageCard = image; selectGrid.Background = new SolidColorBrush(Colors.White); selectGrid = null; card.IsActive = true; } else { selectGrid.Background = new SolidColorBrush(Colors.Purple); } } else { Grid directGrid = sender as Grid; MyCard card1 = selectGrid.Tag as MyCard; MyCard card2 = directGrid.Tag as MyCard; double distance = Math.Sqrt(Math.Pow(card1.Position.X - card2.Position.X, 2) + Math.Pow(card1.Position.Y - card2.Position.Y, 2)); if (distance>100) { selectGrid.Background = new SolidColorBrush(Colors.White); selectGrid = null; return; } if (card1==card2) { selectGrid.Background = new SolidColorBrush(Colors.White); selectGrid = null; return; } if (card2!=null) { if (!card2.IsActive) { return; } if (card1 != null && card1.ImageCard != null) { if ((card1.Index > 8 && card2.Index > 8) || (card1.Index <= 8 && card2.Index <= 8)) { MessageBox.Show("自己人"); return; } if (card1.Index%8>card2.Index%8) { if (card1.Index % 8 != 1 && card2.Index % 8==0) { if (selectGrid.Children.Contains(card1.ImageCard)) selectGrid.Children.Remove(card1.ImageCard); selectGrid.Tag = null; selectGrid.Background = null; directGrid.Tag = card1; directGrid.Children.Add(card1.ImageCard); selectGrid = directGrid; selectGrid.Background = new SolidColorBrush(Colors.Purple); } } else if (card1.Index % 8 < card2.Index % 8) { if (card1.Index % 8 == 0 && card2.Index % 8!=1) { MessageBox.Show("不可以"); return; } else { if (selectGrid.Children.Contains(card1.ImageCard)) selectGrid.Children.Remove(card1.ImageCard); selectGrid.Tag = null; selectGrid.Background = null; directGrid.Tag = card1; directGrid.Children.Add(card1.ImageCard); selectGrid = directGrid; selectGrid.Background = new SolidColorBrush(Colors.Purple); ; } } } } } } } public class MyCard { private bool m_isactive; public bool IsActive { get { return m_isactive; } set { m_isactive = value; } } private bool m_issave; public bool IsSave { get { return m_issave; } set { m_issave = value; } } private Point m_position; public Point Position { get { return m_position; } set { m_position = value; } } private int index; public int Index { get { return index; } set { index = value; } } //public TextBlock TextCard; public MyCard(Point point) { m_isactive = false; m_issave = false; m_position = point; ImageCard = new Image(); } public Image ImageCard; public void SetImage(int dex) { //ImageSource imageSource = new BitmapImage(new Uri("image1.jpg", UriKind.RelativeOrAbsolute)); //ImageSource imageSource=new ImageSource() ImageCard.Source = new BitmapImage(new Uri("/SilverlightApplication1;component/Images/"+dex.ToString()+".jpg", UriKind.Relative)); } } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。