贪吃蛇 WPF

贪吃蛇 WPF
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Threading;
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;namespace WPFSnake
{/// <summary>/// Interaction logic for MainWindow.xaml/// </summary>public partial class MainWindow : Window{private DispatcherTimer timer;public int bodyCount = 5;public Direction direct = Direction.Right;//public BodyCell[] cells;public List<BodyCell> cells;public Image food;public MainWindow(){InitializeComponent();}private void panel_Loaded(object sender, RoutedEventArgs e){}private void Window_Loaded_1(object sender, RoutedEventArgs e){cells = new List<BodyCell>();for (int i = 0; i < bodyCount; i++){BodyCell cell = new BodyCell();double left = i * 10;double top = panel.Height / 2;Canvas.SetLeft(cell, left);Canvas.SetTop(cell, top);//cell.Margin.Top= panel.Height / 2;
cells.Add(cell);panel.Children.Add(cell);}food = new Image();food.Height = 10;food.Width = 10;BitmapImage pic = new BitmapImage(new Uri("Image/bg.jpg", UriKind.Relative));food.Source = pic;Random rY = new Random();double ftop = rY.Next(0, 30) * 10;Random rX = new Random();double fleft = rX.Next(0, 30) * 10;//food.Margin = new Thickness(fleft, ftop, 0, 0);
            Canvas.SetTop(food, ftop);Canvas.SetLeft(food, fleft);panel.Children.Add(food);timer = new DispatcherTimer();timer.Tick += timer_Tick;timer.Interval = TimeSpan.FromMilliseconds(100);timer.Start();}private void timer_Tick(object sender, EventArgs args){SnakeProcess();if (GameOver()){MessageBox.Show("Game Over.");timer.Stop();}}private void SnakeProcess(){#region  移动#region 版本1for (int i = bodyCount - 1; i >= 0; i--){if (cells[i].direct == Direction.Right){//cells[i].Left += 10;Canvas.SetLeft(cells[i], Canvas.GetLeft(cells[i]) + 10);}else if (cells[i].direct == Direction.Left){//cells[i].Left -= 10;Canvas.SetLeft(cells[i], Canvas.GetLeft(cells[i]) - 10);}else if (cells[i].direct == Direction.Up){//cells[i].Top -= 10;Canvas.SetTop(cells[i], Canvas.GetTop(cells[i]) - 10);}else if (cells[i].direct == Direction.Down){//cells[i].Top += 10;Canvas.SetTop(cells[i], Canvas.GetTop(cells[i]) + 10);}}for (int i = 0; i < bodyCount - 1; i++){cells[i].direct = cells[i + 1].direct;}#endregion#region 版本2//for (int i = bodyCount - 1; i > 0; i--)//{//    cells[i - 1] = cells[i];//}//if (cells[bodyCount - 1].direct == Direction.Right)//{//    cells[bodyCount - 1].Margin.Left += 10;//}//else if (cells[bodyCount - 1].direct == Direction.Left)//{//    cells[bodyCount - 1].Left -= 10;//}//else if (cells[bodyCount - 1].direct == Direction.Up)//{//    cells[bodyCount - 1].Top -= 10;//}//else if (cells[bodyCount - 1].direct == Direction.Down)//{//    cells[bodyCount - 1].Top += 10;//}#endregion#endregion#region 吃食物EatFood();#endregion}private bool GameOver(){bool isOver = false;//撞墙double top = Canvas.GetTop(cells[bodyCount - 1]);double left = Canvas.GetLeft(cells[bodyCount - 1]);if (top == 0 && cells[bodyCount - 1].direct == Direction.Up){isOver = true;}if (top == 300 && cells[bodyCount - 1].direct == Direction.Down){isOver = true;}if (left == 0 && cells[bodyCount - 1].direct == Direction.Left){isOver = true;}if (left == 300 && cells[bodyCount - 1].direct == Direction.Right){isOver = true;}//撞自己for (int i = bodyCount - 2; i >= 0; i--){if (Canvas.GetTop(cells[i]) == Canvas.GetTop(cells[bodyCount - 1]) && Canvas.GetLeft(cells[i]) == Canvas.GetLeft(cells[bodyCount - 1])){isOver = true;break;}}return isOver;}private void EatFood(){BodyCell head = cells[bodyCount - 1];if (Canvas.GetTop(cells[bodyCount - 1]) == Canvas.GetTop(food) && Canvas.GetLeft(cells[bodyCount - 1]) == Canvas.GetLeft(food)){BodyCell cell = new BodyCell();panel.Children.Add(cell);cell.direct = head.direct;if (head.direct == Direction.Up){//cell.Top = head.Top - 10;//cell.Left = head.Left;Canvas.SetTop(cell, Canvas.GetTop(head) - 10);Canvas.SetLeft(cell, Canvas.GetLeft(head));}else if (head.direct == Direction.Down){//cell.Top = head.Top + 10;//cell.Left = head.Left;Canvas.SetTop(cell, Canvas.GetTop(head) + 10);Canvas.SetLeft(cell, Canvas.GetLeft(head));}else if (head.direct == Direction.Left){//cell.Top = head.Top;//cell.Left = head.Left - 10;
                    Canvas.SetTop(cell, Canvas.GetTop(head));Canvas.SetLeft(cell, Canvas.GetLeft(head) - 10);}else if (head.direct == Direction.Right){//cell.Top = head.Top;//cell.Left = head.Left + 10;
                    Canvas.SetTop(cell, Canvas.GetTop(head));Canvas.SetLeft(cell, Canvas.GetLeft(head) + 10);}cells.Add(cell);bodyCount++;//lbl_Score.Text = (bodyCount - 5).ToString();
panel.Children.Remove(food);Random rY = new Random();//food.Top = rY.Next(0, 30) * 10;Canvas.SetTop(food, rY.Next(0, 30) * 10);Random rX = new Random();//food.Left = rX.Next(0, 30) * 10;Canvas.SetLeft(food, rX.Next(0, 30) * 10);panel.Children.Add(food);}}private void Window_KeyDown_1(object sender, KeyEventArgs e){if (e.Key == Key.Up && cells[bodyCount - 1].direct != Direction.Down){//direct = Direction.Up;cells[bodyCount - 1].direct = Direction.Up;}else if (e.Key == Key.Left && cells[bodyCount - 1].direct != Direction.Right){//direct = Direction.Left;cells[bodyCount - 1].direct = Direction.Left;}else if (e.Key == Key.Down && cells[bodyCount - 1].direct != Direction.Up){//direct = Direction.Down;cells[bodyCount - 1].direct = Direction.Down;}else if (e.Key == Key.Right && cells[bodyCount - 1].direct != Direction.Left){//direct = Direction.Right;cells[bodyCount - 1].direct = Direction.Right;}if (e.Key == Key.Space){if (timer.IsEnabled){timer.Stop();}else{timer.Start();}}}}public class BodyCell : Image{public Direction direct{ get; set; }public BodyCell(): base(){this.Width = 10;this.Height = 10;direct = Direction.Right;BitmapImage pic = new BitmapImage(new Uri("Image/bg.jpg", UriKind.Relative));this.Source = pic;}}public enum Direction{Up,Down,Left,Right}
}
View Code

 

posted on 2014-10-29 18:03 飔颸颸飔 阅读(...) 评论(...) 编辑 收藏

转载于:https://www.cnblogs.com/september-bk/p/4060184.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/432219.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Linux修改开机启动logo

默认开机LOGO会在液晶屏的左上脚显示一只小企鹅&#xff0c;分辨率为80*80&#xff0c;具体在kernel/drivers/video/logo下会有logo_linux_clut224.ppm这幅图像&#xff0c;程序会根据这幅图像生成logo_linux_clut224.c和 logo_linux_clut224.o两个文件&#xff0c;最终将小企…

sqlite3使用简介(内含解决sqlite内存的方法)

一&#xff0e;使用流程 要使用sqlite&#xff0c;需要从sqlite官网下载到三个文件&#xff0c;分别为sqlite3.lib&#xff0c;sqlite3.dll&#xff0c;sqlite3.h&#xff0c;然后再在自己的工程中配置好头文件和库文件&#xff0c;同时将dll文件放到当前目录下&#xff0c;就完…

动态更新纹理

1. 创建C类继承自AStaticMeshActor 2. 定义用于渲染队列的数据结构 struct FUpdateTextureData {FTexture2DResource* Texture2DResource;FUpdateTextureRegion2D* UpdateTextureRegion;uint8* DynamicColors;int32 w; }; 3. 增加成员变量 UMaterialInstanceDynamic* mDynam…

阿旺wifi智能系统源码

系统简介本系统适合DD-WRT固件路由器和OoenWrt固件路由器或者任何带有WIFIDOG插件的路由器。系统基于ThinkPHP框架PHPMySQL的技术开发。系统主要功能: 1.无密码认证&#xff1a;只点击按钮或强制看广告1.验证码认证&#xff1a;招待券认证、一次性账号、指定时间限制3.用户名密…

动态更新纹理闪烁问题

1. 模型旋转角度尽量取整数&#xff0c;保证线条不会出现锯齿 2. 修改反锯齿模型为FXAA方式&#xff0c;默认的TemporalAA方式会闪烁 3. 动态更新的纹理尺寸一般都不会是2的N次方&#xff0c;比如401X518。 解决方案是 动态更新一张1024x1024的贴图的其中401X518&#xff0c;…

warning C4828问题的处理

warning C4828: 文件包含在偏移 0x215 处开始的字符&#xff0c;该字符在当前源字符集中无效(代码页 65001)。 (编译源文件 XXXXXXcpp) 这提示是由于字符集的问题导致&#xff0c;解决方案如下 点击VS2017 文件->另存为->编码保存->65001 然后重新编译,警告问题解决…

OC中description、 SEL、类本质、self和super用法

一&#xff1a;description方法 description有对象方法和类方法两种&#xff0c;&#xff08;是NSObject类的方法&#xff09;1&#xff0c;-description&#xff08;对象方法&#xff09; 使用NSLog和%输出某个对象时&#xff0c;会调用对象的description方法&#xff0c;并拿…

修改场景默认pawn的方法

playstart方法 1. 放置playstart到场景中 2. 修改world settings的GameMode 直接放置pawn方法 1. 放置pawn到场景中 2. 修改world settings的GameMode 3. 修改pawn的Auto Possess Player为Player 0

lecture3-线性神经元和算法

Hinton第三课 这节课主要是介绍NN的输出端常用的神经元&#xff0c;然后重点是说明怎么使用BP来计算偏导数&#xff0c;在Hinton这一课中&#xff0c;他提供了他1986年参与写的《并行分布处理》一书的第8章&#xff0c;49页&#xff0c;这本书的编者是当你的认知神经界的Rumelh…

ubuntu开机出现错误“Error found when loading /root/.profile”解决

在刚修改完root权限自动登录后&#xff0c;发现开机出现以下提示&#xff1a; Error found when loading /root/.profile stdin:is not a tty 解决方法&#xff1a;在终端中用命令gedit /root/.profile&#xff0c;打开文件后找到“mesg n”&#xff0c; 将其更改为“tty -s …

DropBox 超实用的免费文件网络同步、备份、分享工具

http://www.iplaysoft.com/dropbox.html DropBox 就是一款非常好用的免费网络文件同步工具(当然它也算是一个服务)。当你在电脑A使用DropBox时&#xff0c;指定文件夹里所有文件的改动均会自动地“同步”到DropBox的服务器&#xff0c;当下次你在电脑B需要使用这些文件时&#…

8个有趣的Linux提示与技巧

我们时不时给你带来关于Linux的提示与技巧。这里我们总结了8个最有趣的提示和技巧。推荐学习Linux视频教程。 以它们的大小列出文件如果你想要一个基于它们大小排序的文件列表&#xff0c;你可以使用下面的命令。它会以递减顺序排列文件。# ls -l | grep ^- | sort -nr -k 5 | …

Ubuntu 14.04 文件服务器--samba的安装和配置

samba是Linux系统上的一种文件共享协议&#xff0c;可以实现Windows系统访问Linux系统上的共享资源&#xff0c;现在介绍一下如何在Ubuntu 14.04上安装和配置samba一、 一、更新源列表 打开"终端窗口"&#xff0c;输入"sudo apt-get update"-->回车--…

export LD_LIBRARY_PATH 的使用

对linux不是很熟&#xff0c;之前只是听说过可以设置程序共享库位置也就是 使用 “export LD_LIBRARY_PATH” 今天用了用&#xff0c;感觉还挺不错&#xff0c;也很常用。 比如你编译了一个so 而这个so 同时又依赖其他第三方库。如果你想把你编译的so 提供给别人用的话&#…

MongoDB命令及SQL语法对比

mongodb与mysql命令对比 传统的关系数据库一般由数据库&#xff08;database&#xff09;、表&#xff08;table&#xff09;、记录&#xff08;record&#xff09;三个层次概念组成&#xff0c;MongoDB是由数据库&#xff08;database&#xff09;、集合&#xff08;collectio…

[oracle] 组织架构退格显示 connect by

1. 按组织架构关系退格显示 create or replace view v_vieworg asselect --v.OBJID,v.OBJNAMElevel as levelid, lpad(        ,2*level-2)||v.OBJNAME as OBJNAME,v.OBJID,v.OBJCODE,v.ISDEPT,v.PARENTAREAID,v.PARENTDEPTID,v.ParentID,v.canceledfrom v_organddept …

创建mip纹理链

(1) 我们要做的是&#xff0c;根据原始纹理T0创建一系列的纹理&#xff08;通常使用平均滤波&#xff09;&#xff1a;T1、T2…Tn&#xff0c;其中每个纹理的大小都是前一个纹理的1/4&#xff0c;即长度和宽度减半&#xff0c;如图12.40所示。 要根据前一个mip纹理计算当前纹…

Oracle RAC学习笔记:基本概念及入门

oracle 10g real application clusters introduction 1、什么是cluster一个cluster是由两个或是多个独立的、通过网络连接的servers组成的。几个硬件供应商多年以来提供了Cluster性能的各种需求。一些Clusters仅仅为了提供高可用性的&#xff0c;在当前活动的node发生故障时…

微信公众平台新增语义理解接口

微信公众平台语义理解接口正式对外开放。通过语义接口&#xff0c;接收用户发送的自然语言请求&#xff0c;让系统理解用户的说话内容。 微信语义理解接口提供从用户自然语言输入到结构化解析的技术实现&#xff0c;使用先进的自然语言处理技术给开发者提供一站式的语义解析方案…