博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
贪吃蛇 WPF
阅读量:5127 次
发布时间:2019-06-13

本文共 9476 字,大约阅读时间需要 31 分钟。

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{    ///     /// Interaction logic for MainWindow.xaml    ///     public partial class MainWindow : Window    {        private DispatcherTimer timer;        public int bodyCount = 5;        public Direction direct = Direction.Right;        //public BodyCell[] cells;        public List
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
(); 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 版本1 for (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

你可能感兴趣的文章
转负二进制(个人模版)
查看>>
LintCode-Backpack
查看>>
查询数据库锁
查看>>
[LeetCode] Palindrome Number
查看>>
我对于脚本程序的理解——百度轻应用有感
查看>>
SQL更新某列包含XX的所有值
查看>>
网易味央第二座猪场落户江西 面积超过3300亩
查看>>
面试时被问到的问题
查看>>
spring 事务管理
查看>>
VS2008 去掉msvcr90的依赖
查看>>
当前记录已被另一个用户锁定
查看>>
Bootstrap
查看>>
前端安全-常见的攻击以及防御
查看>>
python-爬网页
查看>>
jsp页面之间传中文参数显示乱码问题的解决
查看>>
LeetCode461.汉明距离
查看>>
类库日期和jsp导包
查看>>
MySQL常用命令总结2
查看>>
js日期时间函数(经典+完善+实用)
查看>>
步进指令与顺控程序设计
查看>>