EleCho.ScratchGame 0.2.2-alpha

Prefix Reserved
This is a prerelease version of EleCho.ScratchGame.
dotnet add package EleCho.ScratchGame --version 0.2.2-alpha                
NuGet\Install-Package EleCho.ScratchGame -Version 0.2.2-alpha                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="EleCho.ScratchGame" Version="0.2.2-alpha" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EleCho.ScratchGame --version 0.2.2-alpha                
#r "nuget: EleCho.ScratchGame, 0.2.2-alpha"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install EleCho.ScratchGame as a Cake Addin
#addin nuget:?package=EleCho.ScratchGame&version=0.2.2-alpha&prerelease

// Install EleCho.ScratchGame as a Cake Tool
#tool nuget:?package=EleCho.ScratchGame&version=0.2.2-alpha&prerelease                

EleCho.ScratchGame

一个小型的的, 基于 GDI+ 的 2D 游戏引擎. 点击查看预览视频

灵感来自于: MIT 的 Scratch

功能 / Features

  • 游戏对象
  • 鼠标与键盘
  • 游戏角色缩放与旋转

入门 / Get started

要进行游戏对象的逻辑更新, 你需要创建一个 Game 对象, 它用来承载所有的游戏对象(GameObject)

Game game = new Game();

接下来, 你可以向游戏中添加一个最简单的贴图:

Bitmap bmp;  // 假设这是我们要显示的贴图
game.AddSprite(new GameSprite()
{
    Sprite = bmp    // 为 GameSprite 赋贴图
});

为了在 WinForm 窗口中进行游戏渲染, 你需要一个 GamePanel, 将其拖动到窗口, 然后在指定其要渲染的游戏

void Load(object sender, EventArgs args)
{
    gamePanel.Game = game;    // 为 GamePanel 指定要渲染的游戏
}

要启动游戏逻辑更新循环以及渲染循环, 调用其方法即可:

game.StartGame();
gamePanel.StartRender();

使用 / Usage

自定义游戏对象

实现复杂的功能, 你必须使用定义自己的类型, 继承 GameSprite 或者 GameText, 并重写相关方法.

下面是一个不断向右移动的游戏对象定义:

class MoveRightForever : GameSprite
{
    public float Speed { get; set; } = 1f;
    public override void Update()
    {
        // SizeF 表示位移, 乘以 Game.DeltaTime 以使其速度不受帧率变化所影响
        Position += new SizeF(Speed, 0) * Game.DeltaTime;
    }
}

碰撞检测

GameSprite 包含默认的简单的碰撞检测, 它基于多边形的碰撞检测.

通过 Game 类的静态 IsCollided 方法来判断两个游戏对象是否碰撞

Game.IsCollided(a, b)

你可以自定义游戏对象的碰撞器多边形, 只需要为其 Collider 属性赋值即可

gameSprite.Collider = new GameObjectCollider(
    new PointF(.25f, .25f),          // 多边形顶点坐标 (0,0 为左上角, 1,1 为右下角)
    new PointF(.75f, .25f),
    new PointF(.75f, .75f),
    new PointF(.75f, .25f));

精灵动画

通过变换 Sprite 的值, 来实现简单的动画

class Warplane : GameSprite
{
    Bitmap[] bodies;
    public Warplane(Bitmap[] bodies)
    {
        this.bodies = bodies;
        SpriteChangeLoop();
    }
    
    private async void SpriteChangeLoop()
    {
        while (true)
        {
            foreach (var me in bodies)
            {
                await Task.Delay(100);
                Sprite = me;
            }
        }
    }
}
Product Compatible and additional computed target framework versions.
.NET net6.0-windows7.0 is compatible.  net7.0-windows was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.0-windows7.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.2.2-alpha 102 5/11/2023
0.2.1-alpha 111 8/31/2022
0.1.1-alpha 121 8/29/2022
0.0.1-alpha 110 8/23/2022