实例介绍
【实例截图】
【核心代码】
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Windows.Threading; namespace WpfApplication5 { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { double x = 800; double y = 600; string key_name = "start"; Border[] bdr = new Border[30]; Random r = new Random(); DispatcherTimer dt = new DispatcherTimer(); double a = 0, b = 0; public MainWindow() { InitializeComponent(); this.Top = 100; this.Left = 200; this.Width = x; this.Height = y; this.Background = Brushes.Black; dt.Interval = TimeSpan.FromMilliseconds(500); dt.Tick = new EventHandler(dt_Tick); this.KeyDown = new KeyEventHandler(lol_KeyDown); snack_start(); snake_food(); dt.Start(); } void dt_Tick(object sender, EventArgs e) { double x_1, y_1; x_1 = Canvas.GetLeft(bdr[0]); y_1 = Canvas.GetTop(bdr[0]); if (key_name == "start") { Canvas.SetLeft(bdr[0], x_1 20); snake_move(x_1, y_1); } if (key_name == "Right") { Canvas.SetLeft(bdr[0], x_1 20); snake_move(x_1, y_1); } if (key_name == "Up") { Canvas.SetTop(bdr[0], y_1 - 20); snake_move(x_1, y_1); } if (key_name == "Down") { Canvas.SetTop(bdr[0], y_1 20); snake_move(x_1, y_1); } if (key_name == "Left") { Canvas.SetLeft(bdr[0], x_1 - 20); snake_move(x_1, y_1); } if(x_1>x) { Canvas.SetLeft(bdr[0],0); } if(x_1<0) { Canvas.SetLeft(bdr[0],x); } if(y_1>y) { Canvas.SetTop(bdr[0],0); } if(y_1<0) { Canvas.SetTop(bdr[0],y); } eat_time(); } void lol_KeyDown(object sender, KeyEventArgs e) { double x_1, y_1; x_1 = Canvas.GetLeft(bdr[0]); y_1 = Canvas.GetTop(bdr[0]); key_name = e.Key.ToString(); if (e.Key.ToString() == "Right") { Canvas.SetLeft(bdr[0], x_1 20); snake_move(x_1, y_1); } if (e.Key.ToString() == "Up") { Canvas.SetTop(bdr[0], y_1 - 20); snake_move(x_1, y_1); } if (e.Key.ToString() == "Down") { Canvas.SetTop(bdr[0], y_1 20); snake_move(x_1, y_1); } if (e.Key.ToString() == "Left") { Canvas.SetLeft(bdr[0], x_1 - 20); snake_move(x_1, y_1); } eat_time(); } private void eat_time() { double x1 = 20, y1 = 20, x2 = 20, y2 = 20, a1 = 20, b1 = 20; foreach (Border bo in lol.Children) { if (bo.Tag.ToString() == "food".ToString()) { x2 = Canvas.GetLeft(bo); y2 = Canvas.GetTop(bo); } if (bo.Tag.ToString() == "0".ToString()) { x1 = Canvas.GetLeft(bo); //左 y1 = Canvas.GetTop(bo); //上 a1 = Canvas.GetBottom(bo); //底 b1 = Canvas.GetRight(bo); //右 } } if ((x1 == x2 && y1 == y2) || (x1 == y2 && y1 == x2) || (a1 == x2 && b1 == y2) || (a1 == y2 && b1 == x2)) { snake_eat(); foreach (Border bo in lol.Children) { if (bo.Tag.ToString() == "food".ToString()) { Canvas.SetTop(bo, r.Next(1, 30) * 20); Canvas.SetLeft(bo, r.Next(1, 30) * 20); } } } } void snack_start() { for (int i = 0; i < 5; i ) { Border bor = new Border(); bor.Width = 20; bor.Height = 20; Canvas.SetLeft(bor, 400-i*20); Canvas.SetTop(bor, 400); bor.Background = Brushes.Red; bor.CornerRadius = new CornerRadius(6); bor.Tag = i; bdr[i] = bor; lol.Children.Add(bor); } } void snake_move(double x_1, double y_1) { /* double bor_hei, bor_wid; bor_wid = bdr[0].Width; bor_hei = bdr[0].Height;*/ double xx = 0; double yy = 0; for (int i = 1; bdr[i] != null; i ) { if (i >= 3) { xx = a; yy = b; } if (i == 1) { xx = Canvas.GetLeft(bdr[i]); yy = Canvas.GetTop(bdr[i]); Canvas.SetLeft(bdr[1], x_1); Canvas.SetTop(bdr[1], y_1); } else { a = Canvas.GetLeft(bdr[i]); b = Canvas.GetTop(bdr[i]); Canvas.SetLeft(bdr[i], xx); Canvas.SetTop(bdr[i], yy); } } } void snake_food() { double xx = Canvas.GetLeft(bdr[0]); double yy = Canvas.GetTop(bdr[0]); Border bd = new Border(); bd.Width = 20; bd.Height = 20; Canvas.SetTop(bd, r.Next(1, 30) * 20); Canvas.SetLeft(bd, r.Next(1, 30) * 20); bd.Tag = "food"; bd.Background = Brushes.Yellow; lol.Children.Add(bd); } void snake_eat() { int i = 0; for (; bdr[i] != null; i ) ; Border bd = new Border(); bd.CornerRadius = new CornerRadius(6); bd.Width = 20; bd.Height = 20; bd.Tag = i; bd.Background = Brushes.Red; bdr[i] = bd; Canvas.SetLeft(bdr[i], a); Canvas.SetTop(bdr[i], b); lol.Children.Add(bdr[i]); } } }
标签: 贪吃蛇
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论