2D 游戏开发基础知识

发布于 2024-09-08 06:10:14 字数 391 浏览 8 评论 0原文

我想使用 C 语言从头开始编写一些简单的类似马里奥的游戏。但老实说,我不知道如何做到这一点,而且我找不到任何好的免费教程。

但对于实际问题,到目前为止我只编写了WinAPI程序,因此所有事件处理和用户输入都是由操作系统处理的,工作量最少,但要开发游戏,例如带有非矩形按钮的菜单、动画等,我想,WinAPI 中没有这样的东西可以帮助我解决这个问题,而不仅仅是一些基本的例程鼠标指针位置和按键。

那么,通过操纵玩家、敌人甚至背景的对象来编写游戏引擎的整个绘制部分,而不是仅仅使用 directdraw 输出到屏幕,这是编写游戏的正确方法吗?

编辑: 我实际上想学习如何从头开始编写游戏,因为它一定是很棒的编程体验,如果你考虑像 DOS 上的 Commander Keen 这样的游戏,在没有框架或库的情况下创建,但仍然很棒。

I would like to write some simple Mario-like game from scratch using language C. But honestly I have no idea how to do so, and I can´t find any good tutorial for this, which is for free.

But to the actuall question, I have only written WinAPI programs so far, so all event handling and user input was handled by OS, with minimum work, But to develope game, with for example menus with non-rectangular buttons, animations, and so, I guess, there is no such thing in WinAPI taht could help me with this more than just some basic routines mouse pointer location and keypresses.

So, is the right way to write your game to write entire draw part of game engine by manipulating objects for player, enemies, and even background yourself, and than just use directdraw for output to screen?

EDIT:
I actually want to learn how to write games from scratch, becouse it must be great programming experience, and if you consider games like Commander Keen on DOS, created with no framework or libraries, but still so great.

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

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

发布评论

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

评论(10

一口甜 2024-09-15 06:10:14

一个好的方法是查看 SDL 库。我并不是说它一定是 2D 游戏的最佳库,但它很容易上手,并且网络上充斥着使用 SDL 编写的简单自制 2D 游戏的教程和开源代码示例。

A good approach to this would be to have a look at the SDL library. I'm not saying it's necessarily the best library for 2D games, but it's easy to get started with and the web is flooded with tutorials and open source code samples for simple homebrew 2D games written using SDL.

没︽人懂的悲伤 2024-09-15 06:10:14

我也推荐 SDL,但你绝对应该看看 lazyfoo 教程,这只是伟大的。

I do recommand the SDL too, but you should definitely have a look on lazyfoo tutorial, which is just great.

绾颜 2024-09-15 06:10:14

当我开始编程时,我开始使用 Allegro 进行编程,那是在 DOS 时代。它是第一个与 SVGA 库一起使用的可用库,并且具有良好的精灵支持。然后版本3来了,他们添加了对Windows的支持(使用GDI和Directy X,你可以在运行时选择使用哪个引擎)。 Linux 移植版上线了,一切都很好。

这是一个非常基本的 2D 库,它将教您图形和动画的基础知识。现在它甚至包含音频支持,这是一个非常需要的补充(嗯,我仍然记得 V 2.9X...)。它们处于版本 5 的测试版中,我认为这是一个值得您研究的有趣项目。

http://www.talula.demon.co.uk/allegro/

When I started programming I started doing it with Allegro, back in the good old DOS days. It was the first usable library which worked with SVGA libraries, and had a good sprite support. Then version3 came and they added support for windows (using GDI and Directy X, you could choose at runtime which engine to use). The linux port came to life, and all is good.

It's a very basic 2D library, and it will teach you the very basics of graphics and animations. Now it even contains audio support which is a very needed addition (well, I still remember V 2.9X...). They are in betas for version 5, and I think this is an interesting project for you to look into.

http://www.talula.demon.co.uk/allegro/

要走干脆点 2024-09-15 06:10:14

你们在说什么,WinAPI 有低级绘图例程。

尽管使用像 SDL 这样的成熟库可能是一个更好的主意,但您可以为 WinAPI 绘图例程创建自己的抽象,而不会有太大困难。

然后只需创建包含所有绘图指令和解释输入的 while 循环即可。对于 2D 游戏来说这并不是太困难。

What are you guys talking about, the WinAPI has low level drawing routines.

Although using an established library like SDL is probably a better idea you could create your own abstractions to the WinAPI drawing routines without too much difficulty.

Then it's just a matter of creating the while loop that has all the drawing instructions and interpreting input. For 2D games this isn't too difficult.

帝王念 2024-09-15 06:10:14

我也使用过 SDL,但尝试查看 HGE。它至少需要 DirectX 8.0,因此您的应用程序只能在 Windows 上运行,但在他们的论坛上您会找到许多有关如何将其移植到 OpenGL 的主题。在我看来,HGE 会比 SDL 更容易学习,因为 SDL 是一个低级库,你必须学习如何自己处理很多事情。 HGE 已经做好了开箱即用的准备。

I also used SDL, but try to look at HGE. It requires at least DirectX 8.0 so your applications will work only on Windows but on their forum you will find many topics on how to port it to OpenGL. In my opinion HGE will be easier to learn than SDL, because SDL is a low level library and you will have to learn how to handle many things by yourself. HGE is more ready to start just out of the box.

铜锣湾横着走 2024-09-15 06:10:14

简而言之,是的 - WinAPI 中没有任何内容可以为您提供太大帮助。然而,您可以在数十种游戏引擎上构建游戏,这将需要在创建游戏本身时进行大量的繁琐工作。谷歌搜索一下会对你有所帮助。

(个人推荐:虽然从技术上来说它是一个 3D 引擎,但像 Unity 这样的引擎是一个优秀的引擎,其中包括创建 2D 游戏的教程Unity 不是 C,但它确实让您的生活变得更加轻松......)

In short, yes - there's nothing in the WinAPI that will help you much. However, there are dozens of game engines that you could build your game on that would take a huge amount of gruntwork out of creating the game itself. A bit of Googling will help you.

(Personal recommendation: although it's technically a 3D engine, something like Unity is an excellent engine that includes tutorials for creating 2D games. Unity isn't C, but it does make your life a lot easier...)

未蓝澄海的烟 2024-09-15 06:10:14

编辑:我实际上想学习如何从头开始编写游戏,因为它必须是很棒的编程体验,如果你考虑像 DOS 上的 Commander Keen 这样的游戏,在没有框架或库的情况下创建,但仍然很棒。

这其实不太正确。 Commander Keen(以及任何 DOS 游戏)确实使用库:由 DOS、BIOS 等提供的库。如果没有某种形式的库,您将无法使用 C 做任何有用的事情。对于游戏编程,您确实需要这样做想要将所有低级细节留给其他人。

我推荐 Allegro 作为入门游戏编程库。

EDIT: I actually want to learn how to write games from scratch, becouse it must be great programming experience, and if you consider games like Commander Keen on DOS, created with no framework or libraries, but still so great.

This is actually not quite right. Commander Keen (and any DOS games) do use libraries: the ones provided by DOS, BIOS, etc. Without libraries of one form or another, you wouldn't be able to do anything useful with C. For game programming, you really do want to leave all the low level details to someone else.

I'd recommend Allegro as a beginning game programming library.

半边脸i 2024-09-15 06:10:14

看看这个游戏开发启动

他们确实拥有与游戏编程相关的大量资源和很多初学者的东西。 SDL 很好,但您应该在开始编码之前甚至在开始考虑您使用的 api/lib 之前考虑学习基本的游戏技术。

明确你需要多少“智能”(Ki),考虑尽早加载/保存,......如果你想完成你的项目,你应该记住很多事情。
您需要游戏编辑器吗? (还有..工作)声音/图形怎么样?自己编写所有这些内容将花费大量时间(如果您没有经验)。创建内容是另一个大问题,如果你真的做到了,它可能会消耗大量时间。

也许它会对您了解一些开发人员有所帮助,因为这会让您了解他们的引擎是如何工作的。就像这个(已过时)开发小子

Check out this one Game dev starting

They have realy a big resource related to game programming and a lot of beginner stuff. SDL is good, but you should consider about learning basic game techniques before start coding and even before start thinking about the api/libs you use.

Clear out how much "intelligence" you need (Ki), consider about loading/Saving a an early time, ... so much things that you should keep in mind if you want to finish your project.
Do you need a game editor? (Also..work) What about sound/graphics? Writing all this stuff on your own will take a lot of time (if you do not have experience). Creating the content is another big issue which can consume a lot of time, if you make it at all.

Maybe it will help you to have a look on some dev kids, because that will give you the idea how their engine works. Like this one (outdated) Dev kid

奢欲 2024-09-15 06:10:14

几天前我刚刚启动了一个类似的项目,您可以在 GitHub 上查看。
它应该可以让您了解游戏的结构。以及带有碰撞的滚动 2D 地图的一些细节(如果你想让它 100% 无错误,这会变得相当复杂)。哦,它正在使用 SDL,正如这里许多人已经建议的那样。

对于我来说,这是我的第一个 C 项目。但我不得不承认,我以前用 Java 和 Python 做过类似的东西,所以这对我来说是快速学习 C 的好方法。由于它是学习而不是任何生产性的东西,所以我使用普通的 C99,这使得任务变得更加“有趣”。

但回到游戏,在开始编码之前,您确实需要考虑一下您的设计,将其写在一张纸上,或者如果您像我一样并且没有树的东西在达到的范围内用伪代码编写它。

尽可能多地考虑可能的游戏状态,没有什么比仅仅因为您之前没有考虑过功能 XYZ 而必须从头开始重新实现整个玩家/地图/任何东西更糟糕的了。

设计非常重要,如果你一开始就没有目标,你的项目就会达到失败的地步,就像我的 Tuff 做到了,好吧,由于缺少音乐和有人设计了敌人等,它也失败了。

说到图形等,请记住,游戏将不仅仅包含简单的内容代码。如果您不擅长图形,那么在设计时要考虑到这一点。因为当屏幕上只有彩色矩形时,您很快就会失去动力。

I've just started a similar project a few days ago, you can check it out over at GitHub.
It should give you some ideas about how the game is structured. As well as some details on a scrolling 2D map with collision (which turns out to be quite complicated if you want to get it 100% bug free). Oh, and it's using SDL as many here have already suggested.

As for me, this is my first C project. But I'll have to admit that I've done similar stuff in Java and Python before, so this was a good way for me to quickly learn C. And since it's learning and not any productive stuff, I'm using plain C99, which makes the task even "funnier".

But back to the game, you really need to think about your design before you start coding, write it down on a sheet of paper, or if you're like me and you don't have tree stuff in reach write it in pseudocode.

Think about as many possible game states as you can, nothing's worse than having to re-implement the whole player/map/whatever stuff from scratch just because you did not think about feature XYZ before.

Design is very important, if you don't have a goal to begin with, your project will reach a point where it fails, just like my Tuff did, well it also failed due to missing music and somebody who would have designed enemies, etc.

Speaking of graphics and such, bear in mind that the game will consist of much more than just the plain code. If you aren't good in graphics then take that into account while designing. Because you will quickly lose your motivation when the only things on the screen are colored rectangles.

眸中客 2024-09-15 06:10:14

动作街机冒险套装(最初作为书)可能是关于如何编写 2D 横向卷轴游戏的最完整的教程之一。尽管是较旧的参考资料,但开发 2D 横向卷轴游戏的许多基础知识并未改变。

完整的源代码示例和一些用于开发横向卷轴的工具以下载。只有一个外部库用于处理图形基元。由于这是一个较旧的 DOS 程序,您可能必须使用 DOS 模拟器,例如 DOSBox 或修改示例更现代的环境。

我建议您浏览第 1 至 9 章,重点关注第 10 至 17 章。

Action Arcade Adventure Set (originally published as a book) is probably one of the most complete tutorials on how to write a 2D side-scrolling game. Although an older reference, many fundamentals for developing a 2D side-scroller have not changed.

Full source code examples and some tools to develop a side-scroller are provided as downloads. There is only one external library used to handle graphics primitives. As this is an older DOS program, you may have to use a DOS emulator like DOSBox or modify the examples for more modern environments.

I suggest you skim chapters 1 to 9 and focus on chapters 10 to 17.

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