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]); } }}
下载C# wpf 贪吃蛇 小游戏源码用户还喜欢
- 18480 文章数
- 500万+ 热度
作者专栏
编辑推荐
- 淡抹u2引擎,修复内容较多,物有所值
- 界域传说·经典巨作=传世单机(一键安装)
- 丸子版本(175个传世版本大集合)
- GS版本:神话公益服务端+客户端
- 图片放大工具(放大图片不模糊)
- 剪映无限制VIP版
- 传奇世界客户端下载器,史上最全传世客户端
- 传世GS20220920商业引擎注册+登录配置器 解压密码是1
- U2官方排行榜游戏网关 支持元神,支持传家宝
- GS开战传世客户端+服务端
- (淡漠夕阳)u2引擎合区工具
- 传世GS引擎消除“你的游戏客户端版本号过旧,请及时更新”提示
- 传世一机多区双线路配置器--免密码版本
- 传世凤凰登陆器劫持修复软件
- SQLite3 for Navicat
- 传奇世界npc对话框编辑工具
- 传世GS落霞铭文服务器端
- gs_20210409引擎包+注册机(无限制)
- 传奇世界NPC对话封包查看器[支持时长版和极速版]
- 彩虹引擎传世脚本编辑工具1.7版来了,支持函数脚本翻译
评论