C# App 中显示游戏图形的最佳方向

发布于 2024-09-03 13:01:36 字数 815 浏览 3 评论 0 原文

我正在制作一个小游戏作为测试项目,没什么大不了的。我刚刚开始处理图形部分,但我不确定将图形绘制到屏幕上的最佳方法。

它将有点像旧的《塞尔达》,所以使用位图等非常简单。我开始认为我可以使用 Drawing.Graphics 和控件中的 Handle 来绘制 Picture Box 控件,但这似乎很麻烦。我也不确定是否可以使用此方法使用双缓冲。

我查看了 XNA,但现在我想使用一种简单的方法来显示所有内容。

那么,我的问题。使用当前的 C# Windows 控件和框架,显示游戏图形的最佳方法是什么(即图片框、构建自定义控件等)

编辑: 我会将我当前的绘制方式添加到图片框中。我有一个 Tile 对象,它只包含图块的像素( List>texture; ),为了简单起见,仅此而已。然后,我通过迭代像素并使用带有当前像素颜色和由比例变量指定的大小的画笔的 FillRectangle 方法将其绘制到图片框:

int scale = 5;
for (int i = 0; i < texture.Width;)
{
    for (int j = 0; j < texture.Height; ++j)
    {
        int x = i * scale;
        int y = j * scale;
        picBox.FillRectangle(new SolidBrush(currentPixelColor), new Rectangle(x, y, scale, scale));
    }
}

是的,相当麻烦。如有任何建议或意见,我们将不胜感激。

I am making a small game as sort of a test project, nothing major. I just started and am working on the graphics piece, but I'm not sure the best way to draw the graphics to the screen.

It is going to be sort of like the old Zelda, so pretty simple using bitmaps and such. I started thinking that I could just paint to a Picture Box control using Drawing.Graphics with the Handle from the control, but this seems cumbersome. I'm also not sure if I can use double buffering with this method either.

I looked at XNA, but for now I wanted to use a simple method to display everything.

So, my question. Using the current C# windows controls and framework, what is the best approach to displaying game graphics (i.e. Picture Box, build a custom control, etc.)

EDIT:
I will add how I am currently drawing to the picture box. I have a Tile object that just contains the pixels for the tile ( List< List< Color>> texture; ), nothing more for simplicity. I then draw that to the pic box by iterating through the pixels and using the FillRectangle method using a brush with the current pixel color and the size specified by a scale variable:

int scale = 5;
for (int i = 0; i < texture.Width;)
{
    for (int j = 0; j < texture.Height; ++j)
    {
        int x = i * scale;
        int y = j * scale;
        picBox.FillRectangle(new SolidBrush(currentPixelColor), new Rectangle(x, y, scale, scale));
    }
}

Yah, pretty cumbersome. Any suggestions or comments are appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

陌路黄昏 2024-09-10 13:01:37

我建议您再看一下 XNA 并尝试一些示例。只要你坚持使用 2D,用 XNA 制作简单的游戏真的很简单。该框架非常出色地将所有细节包装在一个易于理解的流程中,可以说您基本上可以填补空白。

我在短短 8 小时内为我儿子制作了一款完整(但非常简单)的 Xbox 游戏,之前几乎没有任何经验。

如果您转向 3D,事情会变得更加复杂,因为您必须了解各种视图模型、着色器等,但对于 2D,入门非常简单。

2D 的另一个优点是所需的工具更容易获得。我使用 Photoshop 制作了所有图形,声音是 MP3,声音是我使用 Windows 录音机录制的。对于 3D,您需要复杂的工具来构建和导出模型等。

I would recommend, that you take another look at XNA and try a few samples. It is really simple to make simple games with XNA as long as you stick with 2D. The framework does an excellent job at wrapping all the details in a easy to understand flow, where you basically fill in the blanks so to speak.

I did a complete (but very simple) Xbox game for my son in just 8 hours without little previous experience.

If you move to 3D things become more complex, as you have to understand various view models, shaders and so forth, but for 2D it is really simple to get started.

Another advantage of 2D is that the required tools are easier to get. I did all the graphics using Photoshop, the sounds were MP3s and voices I recorded using the Windows recorder. For 3D you need complex tools for building and exporting models and so forth.

许仙没带伞 2024-09-10 13:01:37

老实说,我相信 XNA 是迄今为止我使用过的最简单的游戏设计框架之一。除此之外,您还可以利用 WPF 在屏幕上绘制对象。

I honestly believe XNA is by far one of the simplest game design frameworks I've used. Aside from that you could utilize WPF to draw objects on screen.

阿楠 2024-09-10 13:01:37

首先,我强烈建议使用XNADirectX进行游戏开发...

但是,如果您不需要XNADirectX强>帮助....那么你将被迫使用.Net GDI+绘画工具.....

通过这种方式,你可以通过GDI+绘制点、线、圆、矩形、圆弧、位图、文本等等(所有这些当然,在 C# 和 .Net 中可用...)

您可以创建一个新的简单控件(或使用现有的控件,例如图像框或表单本身!),然后您必须重写其 OnPaint (PaintEventArgs e) 函数...然后您将使用 e.Graphics 绘制您想要的任何内容...

示例:
C# 中的简单俄罗斯方块游戏教程

编辑:
使用 GDI+ 逐像素绘制图片会导致性能下降!!

更好的是自己绘制形状(圆形等)

First I strongly recommend using XNA or DirectX for game development...

However, if you do not want XNA nor DirectX help.... then you will be forced to use .Net GDI+ painting tools.....

By this way, you can draw points, lines, circles, rectangles, arcs, bitmaps, texts and many more by GDI+ (that all is -of course- available in C# and .Net...)

You may make a new simple control (or use an existing one such as image box or the form itself !), then you will have to override its OnPaint(PaintEventArgs e) function... Then you will use e.Graphics to draw whatever you want...

Example:
Simple Tetris Game Tutorial in C#

EDIT:
Using a GDI+ to draw pixel by pixel picture is quite a performance drop!!

Better is to draw shapes yourself (circles,.. etc)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文