基本信息
源码名称:C# wpf 贪吃蛇 小游戏源码
源码大小:0.06M
文件格式:.zip
开发语言:C#
更新时间:2018-05-15
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
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]);
}
}
}