silverlight超小游戏开发包SmallGameLib(二)—— 一个最简单的DEMO

 这次我们来基于 SmallGameLib构建一个最简单的游戏DEMO。

我们把它叫做walkMan,基本功能就是 玩家可以控制一个角色在屏幕上 上下左右 四个方向行走。

 

截图如下:

 

 

下面一步步来构建我们的DEMO:

 

我们先建立工程,导入SmallGameLib源码。(为何不以dll的形式发布?因为代码实在没多少,所以我们以源码形式发布更加方便)

 

 

修改Config/GameStatus.cs 我们此游戏只有一个状态,修改如下:

 


namespace GDE.SmallGameLib
{
    public enum GameStatus
    {
        游戏中,
    }
}

 

将图片 map.jpg man.jpg添加到 resource/image/下

然后构建我们的角色类,在Logic下新建 Man.cs,引用 GDE.SmallGameLib,构造我们的主角,派生自BaseObj,并实现其四个方向行走的逻辑:

 

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using GDE.SmallGameLib;

namespace example1.Logic
{
    public enum ManDirection
    {
        上,下,左,右,停
    }

    public class Man : BaseObj
    {
        static public ManDirection direction = ManDirection.停;

        public Man()
        {
            setImage("man.png");
            H = 80;
            W = 60;
        }

        public override void logic()
        {
            switch (direction)
            { 
                case ManDirection.上:
                    Y-=5;
                    break;
                case ManDirection.下:
                    Y+=5;
                    break;
                case ManDirection.左:
                    X-=5;
                    break;
                case ManDirection.右:
                    X+=5;
                    break;
            }
            if (X < 0) X = 0;
            if (X + W > Config.ScreenW) X = Config.ScreenW - W;
            if (Y < 0) Y = 0;
            if (Y + H > Config.ScreenH) Y = Config.ScreenH - H;
        }
    }
}

 

 

然后构建我们的层类,在Logic目录下新建 Layer.cs,引用GDE.SmallGameLib,构建我们的主层。该层中只有一个角色,就是我们的主角:

 

using GDE.SmallGameLib;

namespace example1.Logic
{
    public class Layer : BaseLayer
    {
        public Layer()
        {
            AddObj(new Man());
        }
    }
}

 

 

构建我们的引擎核心类,在Logic下新建 walkMan.cs

 

using GDE.SmallGameLib;

namespace example1.Logic
{
    public class walkMan : GameEngine
    {
        public walkMan(UserControl mainPage, Canvas rootCanvas, GameStatus startStatus) :
            base(mainPage, rootCanvas, startStatus)
        { 
            
        }

        protected override void TickGameFrameLoop(object sender, EventArgs e)
        {
            layersLogic();
        }
    }
}

 

 

然后在我们的MainPage下启动该引擎,并编写按键处理相关程序:

 

    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();

            walkMan gameCore = new walkMan(this, RootCanvas, GameStatus.游戏中);
            gameCore.AddLayer(new Layer(), 0);
            GameEngine.LoopStart();
        }

        private void MainPage_KeyUp(object sender, KeyEventArgs e)
        {
            //Man.direction = ManDirection.停;
        }

        private void MainPage_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Key.Left:
                    Man.direction = ManDirection.左;
                    break;
                case Key.Right:
                    Man.direction = ManDirection.右;
                    break;
                case Key.Up:
                    Man.direction = ManDirection.上;
                    break;
                case Key.Down:
                    Man.direction = ManDirection.下;
                    break;
            }
        }
    }

 

 

在MainPage.xaml中指定按键处理函数及游戏背景图片,并设置根画布

 

 

<UserControl x:Class="example1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400" KeyDown="MainPage_KeyDown" KeyUp="MainPage_KeyUp">

    <Canvas x:Name="RootCanvas">
    	<Image Height="300" Source="Resource/Image/map.jpg" Stretch="Fill" Width="400" Canvas.ZIndex="-1"/>
    </Canvas>
</UserControl>

 

 

整个DEMO就完成了~

 

用户可以通过上下左右来操作角色移动,角色在碰到四周的时候将不能继续移动。是不是很简单?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值