从 HTML5 游戏开发开始 - 很困惑
我想开始使用 HTML5 开发一个“简单”的游戏,但我对网上找到的许多资源感到非常困惑。我在开发方面有扎实的背景,但是在完全不同的环境中(讽刺的是,我开始编程是因为我想成为一名游戏开发人员,这是我 13 年来唯一从未做过的事情......)。
这种困惑源于这样一个事实:尽管我非常了解 JavaScript 并且对 HTML5 有一些了解,但我不知道如何将我所知道的与所有这些新东西混合起来。例如,这就是我的想法:
游戏将是国际象棋的实现。我有一些简单的“现成的”人工智能算法,可以在单人游戏中重复使用;这里的目的是学习HTML5游戏开发,所以这部分目前不是很重要。
我想围绕游戏建立一个网站。为此,我会使用“常规”CMS,因为我已经知道其中的许多内容,并且将其安装起来会更快。
然后我就会拥有游戏本身,它的“离线”版本与网站无关,因为据我所知,它会单独存在于一个页面中。这是第一个问题:如何让游戏感知用户的会话?登录将由 CMS 处理(这种方式应该更容易,因为用户管理已经实现)。
下一步,我想将 AI 移至服务器。这是第二个问题:如何让游戏将玩家的操作发送到服务器,以及如何获取答案?
后来,我想在游戏中加入 PVP 元素,即一对一的多人游戏(就像古老的国际象棋一样)。这是第三个问题:如何将信息从一个客户发送到另一个客户,并保持对话继续进行。为此,人们建议我看看 Node.js,但它又是一个我不知道如何“粘合”到其余元素的元素。
下面是 PVP 会话中单个操作的示例,这已经让我头疼了:玩家 1 将他的动作发送到服务器(游戏如何与 Node.js 通信?)。我需要识别游戏 ID(我应该将其存储在哪里以及如何存储?),并确保玩家没有手动修改它,这样它就不会干扰其他人的游戏( 如何?)。
我知道整件事,正如我所写的,非常混乱,但这正是我此刻的感受。我不知道从哪里开始,因此非常欢迎任何建议。
I'd like to start developing a "simple" game with HTML5 and I'm quite confused by the many resources I found online. I have a solid background in development, but in completely different environments (ironically, I started programming because I wanted to become a game developer, and it's the only thing I've never done in 13 years...).
The confusion derives from the fact that, although I know JavaScript very well and I have some knowledge of HTML5, I can't figure out how to mix what I know with all this new stuff. For example, here's what I was thinking of:
The game would be an implementation of chess. I have some simple "ready made" AI algorithm that I can reuse for single player; the purpose here is to learn HTML5 game development, so this part is not very important at the moment.
I'd like build a website around the game. For this I'd use a "regular" CMS, as I know many of them already and it would be faster to put it up.
Then I'd have the game itself, which, in its "offline" version, has nothing to do with the website, as, as far as I understand, it would live in a page by itself. This is the first question: how to make the Game aware of User's session? The login would be handled by the CMS (it should be much easier this way, as User Managememt is already implemented).
As a further step, I'd like to move the AI to the server. This is the second question: how do I make the game send player's actions to the Server, and how do I get the answer back?
Later on, I'd like to bring a PVP element to the game, i.e. one-against-one multiplayer (like the good old chess). This is the third question: how to send information from a client to another, and keep the conversation going on. For this, people recommended me to have a look at Node.js, but it's one more element that I can't figure out how to "glue" to the rest.
Here's an example of a single action in a PVP session, which already gives me a headache: Player 1 sends his move to the Server (how does the game talk to Node.js?). I'd need to identify the Game Id (where and how should I store it?), and make sure the player hasn't manually modified it, so it won't interfere with someone else's game (how?).
I'm aware that the whole thing, as I wrote it, is very messy, but that's precisely how I feel at the moment. I can't figure out where to start, therefore any suggestion is extremely welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
事情太多,而且可能顺序错误。
在我看来,很多问题首先与 HTML5 并没有特别相关。
从显而易见的事情开始 - 您想要一个下棋的单个页面(基本上是一个 javascript 应用程序),所以构建它。如果你不能构建它,那么其余的基本上是无关紧要的,如果你可以构建(并且我不怀疑你可以),那么剩下的就是在该功能的基础上构建。
因此,我们回答了您的第一个问题 - 在您加载页面时,您将获得会话,它是一个网页,就像任何其他网页一样,这就是您获得会话的方式。如果您处于离线状态,那么您会通过任何方式(可能是本地存储)保留在线时的状态。
你想把人工智能移到服务器上吗?好的,所以请确保前端用户交互与“界面”进行对话,以记录玩家的动作并检索 AI 动作。鉴于这种分离,您可以用 ajax(尽管我希望 x 是 json!)使用相同的参数调用服务器来替换客户端上的 AI。
这会变得更好,如果你想做玩家到玩家,你只需谈论通过服务器从一个用户/玩家到另一个用户/玩家的路由 - 前端代码不必改变,只需改变服务器所做的事情ajax 调用的远端。
但尽管如此,退后一步,一次解决一个问题——如果你这样做了,你应该到达你想去的地方,而不是让自己发疯,试图担心一桶看起来可怕的问题,你可能可以一次轻松解决一个问题,然后我首先让您的游戏在浏览器中独立运行。
Too many things and probably in the wrong order.
A lot of the issues don't seem to me to be particularly related to HTML5 in the first instance.
Start with the obvious thing - you want a single page (basically a javascript application) that plays chess, so build that. If you can't build that then the rest is substantially irrelevant, if you can build (and I don't doubt that you can) then the rest is about building on that capability.
So we get to your first question - well at the point at which you load the page you will have the session, its a web page, like any other web page, so that's how you get the session. If you're offline then you've persisted that from when you were online by whatever means - presumably local storage.
You want to move the AI to the server? Ok, so make sure that the front end user interaction talks to an "interface" to record the player moves and retrieve the AI moves. Given this separation you can replaces the AI on the client with an ajax (although I'd expect the x to be json!) call to the server with the same parameters.
This gets better, if you want to do player to player you're just talking about routing through the server from one user/player to another user/player - the front end code doesn't have to change, just what the server does at the far end of the ajax call.
But for all this, take a step back and solve the problems one at a time - if you do that you should arrive where you want to go without driving yourself nuts trying to worry about a bucket full of problems that seem scary that you can probably easily solve one at a time and I'd start by getting your game to run, all on its own, in the browser.
关于问题一:您也许可以给用户一个签名的 cookie。例如,创建一个 cookie,其中包含他的用户 ID 或用户 ID 的 SHA2 哈希值以及秘密的长盐(例如 32 字节盐左右)。
关于问题二:为了交换内容和调用远程函数,我会使用 RPC 库 dnode。
关于问题三:在客户端之间调用方法使用相同的东西。
客户端代码(仅作为示例):
如果使用 dnode,则不必使用游戏 ID - 只需将与游戏绑定的功能交给浏览器即可。如果您出于某种原因需要 ID,请使用 UUID 模块来创建长的随机 ID - 它们是无法猜测的。
About question one: You could maybe give the user a signed cookie. E.g. create a cookie that contains his userid or so and the SHA2 hash of his userid plus a secret, long salt (e.g. 32 bytes salt or so).
About question two: For exchanging stuff and calling remote functions, I'd use the RPC library dnode.
About question three: Use the same thing for calling methods between clients.
Client code (just an example):
You don't have to use game IDs if you use dnode - just hand functions to the browser that are bound to the game. If you need IDs for some reason, use a UUID module to create long, random ones - they're unguessable.