游戏邦在:
杂志专栏:
gamerboom.com订阅到鲜果订阅到抓虾google reader订阅到有道订阅到QQ邮箱订阅到帮看

分享零基础制作游戏教程之编写代码(1)

发布时间:2012-11-22 15:56:10 Tags:,,,

作者:Alex Rose

你想要制作游戏,但你并不了解游戏制作必备的各种技术,怎么办?

不用担心!我在一年前也遭遇过同样困境,而你所应该做的是打破自己的心理防线,不要认为自己缺乏相关经验。

唯一的前提是能够逻辑性地思考问题。

第一部分:编写代码

在青年时期,我会偶然决定学习编写代码,然后参考一些入门指南,写下一堆文本,只是为了在主机上显示“hello,world”。直到上了大学(学习物理专业),我的必修课C语言模块迫使我坐在室内编写代码,此时,我才意识到自己喜爱这方面的学习。

该课程十分精彩,因为所有“长篇大论”课程都属于选修范畴。相关的实际操作也十分简洁,假如你已学习了这门课程,基本上你只会说:“这是双精度浮点数,这是函数,这是条件;这样就可以组成一个相对论动能计算器”。

如果你想快速掌握编码,你应必备这些知识!当然,你不必采用某个单一模块进行。你只需知道从哪下手。

最近,我的室友选择的论文题目涉及到C#语言知识。他根本就不了解C#,因此他正投入大量时间阅读相关教程。我建议他采取我的学习方法:先思考下如何着手操作,而后分为几个步骤,如果有些步骤涉及到自己并不了解的关键技术,上谷歌查找相关信息。肯定有人在之前碰到相关问题,而且某些小助手已经分享了相应的解决方案,你很容易便能找到。

现在,我已充分告知这些有用信息,但你仍不知道从何下手。此时,你应该了解在命令提示符中制作一款简单游戏所需的关键结构:

*条件(if,else,switch)

*循环(for,while)

*变量(ints,floats,bools,strings)

*功能

*如何利用IDE。

如果你想要快速便捷地掌握前四种结构,你可以从http://codecademy.org这个网站获取相关信息。你可以在10秒内掌握JavaScript语言。你要持续坚持代码编写,直到创作出首款二十一点游戏。该网站可以为你提供相关帮助,如果你遇到某些困难,你可以到讨论区寻求帮助。

14bits-codeacademy(from gamasutra)

14bits-codeacademy(from gamasutra)

完成上述事项后,我提议,转向使用C#,并下载Visual Studio。在此,由于C#与JavaScript均属于NET语言,因此我们无需区分两者的差异,而且其基本内容大致相似,但并非完全相同。接着应进入最重要步骤:独立操作。

不仅因为Visual Studio极为困难,转化语言也会令你陷入操作困境。对此,你应利用谷歌搜索大量相关的基本信息,这也许是掌握语言转化的最佳渠道。而这些技术确实是之后操作所必备的。

VS中包含的一项功能被称为“智能感应”。当你输入一个变量名时,它会有选择性地自动补充完整。之后当你频繁输入GetComponent这类名称时,你便可以节省大量时间。而且,你还可以让鼠标箭头停留在某个变量上,这时,智能感应功能会告知该变量的类型。

也许,Visual Studio 2010最棒的地方在于:当你还未编完代码时,你无需等到点击编辑按钮,才发现弹出10条错误信息。当你开始编写代码时,它便会划出其中的某些错误!现在,我正在试验分析来自Tevatron粒子加速器的数据,我被迫使用包含Root编辑器(游戏邦注:CERN自主研发的编辑器)的notepad2程序。而且其中有一半时间我是在修复那些愚蠢的错误。

因此,首先,你应想出一个力所能及的游戏理念,即未涉及物理知识的核心游戏机制。如果你没有点子,你可以选择复制现存理念。你会发现这一过程很有趣并有助于巩固你的知识,为你传送新想法。

为此,最简单的方式是选用RPG或益智游戏的基本机制练习编写代码。

比如,我首次编码的是针对MS DOS主机的一个相当简单的RPG系统。它会从二维整数数组中生成一张ASCII地图(比如,“-1”指代墙壁,“1”指代玩家),这样,你可以四处移动玩家,避免他们撞到墙壁。这是学习有关编码基本知识的不错训练方式,你还应谷歌一些相关信息(游戏邦注:比如如何设置键盘输入,如何清除主机系统,并在每次执行行动时再次生成地图)。

cgame(from gamaustra)

cgame(from gamaustra)

比如,你应为此学习Console.Write()这类重要指令,借此将信息传送到主机上。

接着,我会增添一个魔力扩散系统。以此呈现具有魔力数值的隐性2D浮点数组,随后,魔力会从高浓度地带扩散到低浓度地带,因此,当你吸收一些魔力值后,该区域并不会剩余多少数量。同时,这也是热力学零定律发挥的完美作用。

之后,我编码制作了《Rotation Station》的首个版本。回想起来,它只是一款简单的四子棋游戏,你可以自由选择以连接多少方格为胜,每经过n次转动,由于重力作用,整个网格会90度旋转。接下去,我还会增添一些简单道具,比如rowblitz,重力转变,以及生成阻块等等。

cmd(from gamasutra)

cmd(from gamasutra)

如果你已熟悉Javascript语言,并且下载了适用C#的Visual Studio,那么你应采取以下基本步骤:

以C#制作针对主机平台的新项目。在此,如果你从未使用IDE,那么你将无法真正明了自己从事的事项。观看“hello,world”的制作教程可以提供一些辅助,但记住:如果你已经使用Codecademy学习了编程,那么你必定明了循环、条件、变量以及功能!

首先,打开行号:

http://msdn.microsoft.com/en-us/library/ms165340.aspx

现在插入“主要部分”。你应研究如何使用Console.Write()与Console.Read(),以此获得一些输入与输出,而谷歌也能提供一些辅助。如果你已编辑好代码,那它将会在命令提示符中运行。为了防止主机一闪而灭,你应按下Ctrl+F5键,而不是一味编辑。借此,它不会在你完成后直接关闭。

如果你搞砸了某些事项,你可以尝试使用2D组数,让系统在ASCII中以cout形式呈现游戏地图或平台。你还应构造某些基本游戏功能。

如果你对此步骤感到满意,你可以进入下一步骤:采用Unity3D。

Unity3D是一个功能强大的直观游戏引擎。借此,开发者能够相当简单地呈现游戏画面,方便我们构造类别。

graphic(from gamasutra)

graphic(from gamasutra)

你应利用立方体再次制作游戏!你可以采用UnityScript或Unity中的C#语言。我提议采用C#语言,它确实能够在运行中完美展示动态数组分配。也就说,你无需具体指出代码中的组数,便可以自由选择组数的规模大小。在下篇文章中我们将介绍美术内容,探讨如何在没有美术基础的前提下,在一个月内制作出如以下效果的2D图像。

2d art(from gamasutra)

2d art(from gamasutra)

本文为游戏邦/gamerboom.com编译,拒绝任何不保留版权的转载,如需转载请联系:游戏邦

Making Games with No Previous Experience – Part 1: Code

by Alex Rose

So, you love the idea of making games but you have no knowledge of some or all of the skills necessary to do so?

Fear not! I was in the same position a year ago, but all you need to break is the psychological barrier that you’re missing years of experience necessary to do anything.

The only prerequisite is the ability to think logically.

Part 1: I don’t know how to code!

This is fine. Throughout my teenage years, I would occasionally decide I’d learn to code, follow some guide, and get struck by a wall of text just to get a console to display “Hello World”. It wasn’t until on my degree (Physics), when I had a compulsory module of C and was forced to sit in a room and code that I could actually be sufficiently motivated to learn.

The course was perfect though, because all the “walls of text” were covered in non-compulsory lectures. The actual exercises were extremely brief, assuming you’d attended the lectures and essentially only saying “This is a double, this is a function, this is a conditional; go make a relativistic kinetic energy calculator”.

And, if you want to learn quickly, that’s all you really need! You certainly don’t need to take a uni module to do it. You just need to know where to start.

Recently my housemate chose a dissertation topic which required knowledge in C#. He didn’t know C# at all, and he was wasting hours reading tutorials. I told him the same advice I followed myself when learning it: Think about how you would do the operations yourself, break it into steps, and if there’s any step where you don’t know some key technique, google it. Someone has had your problem before, and some helpful person has uploaded a solution to it, and it’s easy for you to find it.

Now, it’s all well and good me saying this, but you still don’t have any idea where to start. You need to know some key things to make a simple game in a command prompt (which is a great place to start):

Conditionals (if, else, switch)

Loops (for, while)

Variables (ints, floats, bools, strings)

Functions

How to use an IDE.

If you want to learn the first four of these things very quickly and very easily, go to http://codecademy.org. You will be on the road to learning JavaScript within 10 seconds. Keep coding until you’ve made your first blackjack game. It’s really helpful, and if you get stuck they have discussion boards to help you.

http://graphics8.nytimes.com/images/2011/09/14/technology/14bits-codeacademy/14bits-codeacademy-blog480.jpg

Now, what I would advise once you’ve done this is to switch to C#, and download Visual Studio. At this point, the difference between C# and JavaScript won’t be a great leap, as both are .NET languages, and most of the very basics are very similar if not identical. This will be the biggest step for you though: working independently.

Not only is Visual Studio incredible, but switching language will be a great way to get you out of your comfort zone. You’ll be at a point where you’ll need to be googling lots of basic things to work them out for yourself, and this is probably the best way to learn. These skills will really be required later on.

VS has something called, “Intellisense”, which is an absolute lifesaver. If you’re typing a variable name, it will optionally autocomplete your word. Later on when you’re typing things like GetComponent frequently, this saves huge amounts of time. You can also hover over a variable and it’ll tell you what kind of variable it is.

And, perhaps, the best thing about Visual Studio 2010: When your code isn’t going to compile, you don’t have to wait until you click the compile button to be struck by 10 error messages. It underlines your errors for you as soon as you make them! I’m currently analysing data from the Tevatron for lab, and I’m forced to use notepad2 with CERN’s own compiler, Root. I spend half my time fixing compiler errors from stupid mistakes.

So first of all: come up with a game idea that’s simple enough for you to code – something whose core game mechanic doesn’t have any physics. It can even be a simple clone of something that already exists if you’re out of ideas. Anything that you will find compelling and will reinforce what you know, and teach you some new ideas will do.

To start, it would probably be easiest to code one basic mechanic from an RPG or a puzzle game.

For example, the first thing I coded was a very simple RPG overworld system in an MS DOS console. It’d generate an ASCII map from a 2 dimensional integer array (where, e.g. “-1” would be a wall, “1” would be the player etc.), in which you could move your player around, and bump into walls. This is a good exercise for learning basics, and there will be some things you need to google (like how to take keyboard input, how to wipe the console and publish the map again whenever an action is taken, etc.).

One important command you’ll need to learn for this, for instance, is  Console.Write(), which will print to the console.

I then added a system for mana diffusion. An invisible 2D float array of numerical mana values was present, and mana would diffuse from areas of high concentration to areas of low concentration, so when you absorbed mana there was less left around you to absorb. It was also a nice visualisation of the Zeroth law of thermodynamics in action too.

After that I coded my first version of my game, Rotation Station. Back then, it was just a simple connect-4 game, but you could choose how many pieces you’d need to connect to win and every n turns the whole grid rotated 90°, letting gravity contort the playing field. Next I added some simple powerups like rowblitz, gravity shift, the ability to spawn blocks etc.

The basics steps you’ll need to take once you’re comfortable with Javascript and you’ve downloaded Visual Studio for C# are as follows:

Make a new project. Make it a C# Console. At this point, if you’ve never used an IDE, you won’t really know what you’re doing. This is where looking at a hello world tutorial might help, but remember: if you’ve done Codecademy, you already know loops, conditionals, variables and functions!

First, turn on displaying line numbers:

http://msdn.microsoft.com/en-us/library/ms165340.aspx

Now work in “main”. You’ll need to work out how to use Console.Write() and Console.Read(), in order to get some input and output, which Google will assist with. Once your code is ready, compile it and it’ll run in the command prompt. To stop the console from flashing and immediately disappearing, press Ctrl+F5 to compile, rather than just compiling. This way it won’t immediately shut down after it’s finished.

Once you’ve fiddled about, try using 2D arrays (i.e. int[,] arrays) to make your own system of displaying a game map or board in ASCII with cout. You should be able to build some kind of basic game functionality.

Once you’re happy with this, it’s time for the next step up: Get Unity3D.

Unity3D is an incredibly intuitive game engine. It takes all of the difficulty out of representing things graphically, and has lots of incredibly convenient built in classes.

Make your game again, but with cubes! You can use UnityScript (which borrows heavily from JavaScript) or C# in Unity, (or Boo, if you’re so inclined). I’d really recommend C#. It has the beautiful ability to perform dynamic array allocation at runtime. In other words, you can choose how big the dimensions of your arrays are without having to specifying them in your code.

I’ll go into more detail on starting out in Unity for the first time in a later blog.

If you encounter any issues along the way, feel free to contact me; I’ve been there. But for the most part, you should be able to troubleshoot any issues or gaps in your knowledge with Google. You’ll learn to love Stack Overflow and Unity Answers.

ETA on learning all this? A couple of months, tops, if you’re motivated. It’ll take a bit longer once you start working in Unity, and I’ll discuss timeframes you can expect for that too.

Also, stay tuned for the next installment to this series, where I’ll discuss in depth where to start if you have real difficulty creating your own 2D artwork, how to use motion to make static gameplay more aesthetically pleasing, where to begin if you want to write your video game music and getting started from coding in a console to coding in Unity3D. The next article will be about art, where I’ll show how to go from the previous screenshot to have something that looks more like:
with no prior knowledge of 2D art design within a month.(source:gamasutra)


上一篇:

下一篇: