贪吃蛇 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 = ;
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 = ; i < bodyCount; i++)
{ BodyCell cell = new BodyCell();
double left = i * ;
double top = panel.Height / ;
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 = ;
food.Width = ;
BitmapImage pic = new BitmapImage(new Uri("Image/bg.jpg", UriKind.Relative));
food.Source = pic;
Random rY = new Random();
double ftop = rY.Next(, ) * ;
Random rX = new Random();
double fleft = rX.Next(, ) * ;
//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();
timer.Start();
} private void timer_Tick(object sender, EventArgs args)
{
SnakeProcess(); if (GameOver())
{
MessageBox.Show("Game Over.");
timer.Stop();
}
} private void SnakeProcess()
{
#region 移动
#region 版本1
for (int i = bodyCount - ; i >= ; i--)
{
if (cells[i].direct == Direction.Right)
{
//cells[i].Left += 10;
Canvas.SetLeft(cells[i], Canvas.GetLeft(cells[i]) + );
}
else if (cells[i].direct == Direction.Left)
{
//cells[i].Left -= 10;
Canvas.SetLeft(cells[i], Canvas.GetLeft(cells[i]) - );
}
else if (cells[i].direct == Direction.Up)
{
//cells[i].Top -= 10;
Canvas.SetTop(cells[i], Canvas.GetTop(cells[i]) - );
}
else if (cells[i].direct == Direction.Down)
{
//cells[i].Top += 10;
Canvas.SetTop(cells[i], Canvas.GetTop(cells[i]) + );
}
}
for (int i = ; i < bodyCount - ; i++)
{
cells[i].direct = cells[i + ].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 - ]);
double left = Canvas.GetLeft(cells[bodyCount - ]);
if (top == && cells[bodyCount - ].direct == Direction.Up)
{
isOver = true;
}
if (top == && cells[bodyCount - ].direct == Direction.Down)
{
isOver = true;
}
if (left == && cells[bodyCount - ].direct == Direction.Left)
{
isOver = true;
}
if (left == && cells[bodyCount - ].direct == Direction.Right)
{
isOver = true;
} //撞自己
for (int i = bodyCount - ; i >= ; i--)
{
if (Canvas.GetTop(cells[i]) == Canvas.GetTop(cells[bodyCount - ]) && Canvas.GetLeft(cells[i]) == Canvas.GetLeft(cells[bodyCount - ]))
{
isOver = true;
break;
}
}
return isOver;
} private void EatFood()
{
BodyCell head = cells[bodyCount - ];
if (Canvas.GetTop(cells[bodyCount - ]) == Canvas.GetTop(food) && Canvas.GetLeft(cells[bodyCount - ]) == 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) - );
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) + );
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) - );
}
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) + );
}
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(, ) * );
Random rX = new Random();
//food.Left = rX.Next(0, 30) * 10;
Canvas.SetLeft(food, rX.Next(, ) * );
panel.Children.Add(food);
}
} private void Window_KeyDown_1(object sender, KeyEventArgs e)
{
if (e.Key == Key.Up && cells[bodyCount - ].direct != Direction.Down)
{
//direct = Direction.Up;
cells[bodyCount - ].direct = Direction.Up;
}
else if (e.Key == Key.Left && cells[bodyCount - ].direct != Direction.Right)
{
//direct = Direction.Left;
cells[bodyCount - ].direct = Direction.Left;
}
else if (e.Key == Key.Down && cells[bodyCount - ].direct != Direction.Up)
{
//direct = Direction.Down;
cells[bodyCount - ].direct = Direction.Down;
}
else if (e.Key == Key.Right && cells[bodyCount - ].direct != Direction.Left)
{
//direct = Direction.Right;
cells[bodyCount - ].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 = ;
this.Height = ;
direct = Direction.Right;
BitmapImage pic = new BitmapImage(new Uri("Image/bg.jpg", UriKind.Relative));
this.Source = pic; }
}
public enum Direction
{
Up,
Down,
Left,
Right
}
}
贪吃蛇 WPF的更多相关文章
- 基于C# WPF框架的贪吃蛇
游戏开始界面 游戏开始 共有两条蛇,吃到红色食物加1分,吃到绿色毒食物减1分,知道0不减: 碰到墙壁游戏结束,碰到对方游戏结束,碰到自己游戏结束 此游戏通过Canvas画布布局,通过C#代码实现 游戏 ...
- Android快乐贪吃蛇游戏实战项目开发教程-01项目概述与目录
一.项目简介 贪吃蛇是一个很经典的游戏,也很适合用来学习.本教程将和大家一起做一个Android版的贪吃蛇游戏. 我已经将做好的案例上传到了应用宝,无病毒.无广告,大家可以放心下载下来把玩一下.应用宝 ...
- 用C++实现的贪吃蛇游戏
我是一个C++初学者,控制台实现了一个贪吃蛇游戏. 代码如下: //"贪吃蛇游戏"V1.0 //李国良于2016年12月29日编写完成 #include <iostream& ...
- [LeetCode] Design Snake Game 设计贪吃蛇游戏
Design a Snake game that is played on a device with screen size = width x height. Play the game onli ...
- JavaScript-简单的贪吃蛇小游戏
实现逻辑: //获取Html中的格子(行,列) //建立数组存储所有格子(x,y) //建立数组用于存储蛇身(x,y) //生成随机坐标(x,y)的函数 //随机创建蛇身并存储到蛇身数组 //创建食物 ...
- juery实现贪吃蛇的游戏
今天用juery做了一个贪吃蛇的游戏,代码比较简陋,不过作为这些天学习juery的成果,非常有成就感.另外关于代码内容如有雷同不胜荣幸. 更改了下 让头和身子的颜色不一样 这样好区分些,虽然还是不怎么 ...
- HTML 5 背离贪吃蛇 写成了类似于屏幕校准
中间写了改 改了写 还是没做出自己满意的效果 ,看来自己的确不是一个走前端的料子.当然h5还是学一点好一点 具体说来 就是 在canvas 的画布中 鼠标点击后画上一个圆形 然后就有随机的在画布上面出 ...
- 控制台游戏引擎CGE——贪吃蛇
今天我也来发一个控制台游戏.先看图: 缘起 LZ是一个有严重拖延症的人,表现的形式就是隔一段时间就要刷一刷博客园. 这不前几天,看到了魏大师<使用Lua脚本语言开发出高扩展性的系统...> ...
- 原生JS制作贪吃蛇小游戏
感情都在代码里,来,干了!... <!doctype html> <html> <head> <meta http-equiv="Content-T ...
随机推荐
- x86汇编指令具体解释
80x86指令系统 80x86指令系统,指令按功能可分为下面七个部分. (1) 数据传送指令. (2) 算术运算指令. (3) 逻辑运算指令. (4) 串操作指令. (5) 控制转移指令. (6) 处 ...
- CodePen's CSS
p{text-indent:2em;}前端开发whqet,csdn,王海庆,whqet,前端开发专家 翻译自:CodePen's CSS 翻译人员:前端开发whqet,意译为主.不当之处欢迎大家指正. ...
- 淘宝开源任务调度框架tbschedule
背景 分布式任务调度是非常常见的一种应用场景,一般对可用性和性能要求不高的任务,采用单点即可,例如linux的crontab,spring的quarz,但是如果要求部署多个节点,达到高可用的效果,上面 ...
- Ribbon2: 创建动态的Ribbon库
Sam Radakovitz曾在Excel团队博客中发表过一篇文章,介绍了如何创建动态的Ribbon库,即如何通过RibbonX和VBA放置动态的图形图像到功能区库中,在该文中,作者创建了两个库:一个 ...
- Android图片编译报错
一. AAPT err(1118615418): ERROR: 9-patch image icon_item_bottom_line.9.png malformed No marked region ...
- HDU 2510 - 符号三角形
DFS后打表 #include <iostream> using namespace std; ,,,,,,,,,,,,,,,,,,,,,,,,}; int main() { int n; ...
- UVa 11488 - Hyper Prefix Sets
找 前缀长度*符合该前缀的字符串数 的最大值 顺便练了一下字典树的模板 #include <iostream> #include <cstdio> #include <c ...
- [转]activiti5用户任务分配
用户任务分配办理人:1.用户任务可以直接分配给一个用户,这可以通过humanPerformer元素定义. humanPerformer定义需要一个 resourceAssignmentExpressi ...
- Linux下,命令 wget 的使用
wget是一个从网络上自动下载文件的自由工具.它支持HTTP,HTTPS和FTP协议,可以使用HTTP代理. 所谓的自动下载是指,wget可以在用户退出系统的之后在后台执行.这意味这你可以登录系统, ...
- Python2.7 转义和正则匹配中文
今天爬虫(新浪微博 个人信息页面)的时候遇到了转义和正则匹配中文出乱码的问题. 先给出要匹配的部分网页源代码如下: <span class=\"pt_title S_txt2\&quo ...