构建乒乓球
我正在使用 Visual Express 2010 使用 C++ 和 OpenGL 制作 Pong。这是我制作的第一批游戏之一,我想知道如何最好地构建它。困扰我的主要部分是游戏菜单。我应该将不同的游戏模式放在不同的功能中,并在我的主要功能中进行切换吗?例如,在 Main.cpp 中,我将包含以下行
glutDisplayFunction(maincode)
In another file, I would Define maincode as like such (再次,伪代码):
maincode():
switch m:
case 1:
singleplayer = true
multiplayer = false
menu = false
case 2:
singleplayer = false
multiplayer = true
menu = false
case 3:
singleplayer = false
multiplayer = false
menu = true
然后,在每个文件中,检查单人游戏、多人游戏和菜单来确定我处于哪种模式,然后相应地显示代码。
然而,我感觉随着游戏变得更加复杂,这种方法会变得更加复杂,所以我认为这不是正确的方法。
我应该如何构建游戏和菜单?
PS 我是一名 C++ 程序员,但我没有编程游戏的经验。
I am making Pong using C++ and OpenGL using Visual Express 2010. It is one of the first games I have made, and I am wondering how to best structure it. The main part that is stumping me is the game menu. Should I put the different game modes in different functions, and have a switch in my main function? For example, in Main.cpp, I would include the line
glutDisplayFunction(maincode)
In another file, I would define maincode as something like such (again, psuedocode):
maincode():
switch m:
case 1:
singleplayer = true
multiplayer = false
menu = false
case 2:
singleplayer = false
multiplayer = true
menu = false
case 3:
singleplayer = false
multiplayer = false
menu = true
I would then, in each file, check to see the values of singleplayer, multiplayer, and menu to determine which mode I am in, then display code accordingly.
However, I get the feeling that this method would get much more complicated as the game gets more complicated, so I don't think this is the proper way to do it.
How should I structure the game and menu?
P.S. I am a C++ programmer for a living, but I just don't have experience programming games.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您需要的唯一信息是您当前所处的模式时,拥有 3 个不同的
bool
(总共 8 种不同的状态,其中 3 种有效)只是不必要的复杂性。最好使用枚举,即
和一个
GameMode
类型的变量:具有不同的、互斥(这就是您在这里错过的一点!)在状态之间进行切换是编程中的一个重要概念,所以你绝对是在正确的轨道上。继续工作。
It's just unnecessary complexity to have 3 distinct
bool
s (totaling to 8 different states, 3 of which would be valid) when the only information you need is which mode you are currently in.Better use an enumeration, i.e.
and a single variable of type
GameMode
:Having different, mutually exclusive (that's the point that you missed here!) states to switch in between is an important concept in programming, so you are definitely on the right track. Keep working.
组织 GUI 应用程序的最佳方法是使用 MVC 设计模式。要么是这样,要么是一个小修改演示者优先。
知道这一点,像你这样的问题的答案很容易回答(亚历山大·盖斯勒的回答相当不错)
The best way to organize a GUI application is using the MVC design pattern. Either that, or it's small modification presenter first.
Knowing that, answers to such questions as your are simple to answer (Alexander Gessler's answer is quite ok)
glutDisplayFunc() 是一种显示内容的快速方法,但在编写简单项目时,它很快就会变得比它值得的麻烦。
除非你是多线程的并且保持游戏状态的恒定副本以供 glutDisplayFunc() 读取,否则我会说避免使用它 - 即使那样,也可能是这样。
对于游戏循环,我相信驱动 OpenGL 比让它驱动你更好。
简单的菜单循环。 (请原谅我的代码风格与平常不同)。
这不是一个很好的布局,但它适用于简单的游戏。
init()
初始化 openGL。menu()
、single()
和multi()
各自有自己的循环,手动渲染和交换缓冲区。menu()
返回一个值,指定选择了哪个菜单项。游戏循环以恒定速度运行,不浪费CPU。
再说一遍,虽然不是很好,但是功能足够了。 (我用这样的东西)。
CGame 是游戏类(如果你想成为 OO)。
get_time()、wait_till()、time_type等都是组成的。使用任何适合的函数/类型。
速度很快,从我的脑海中浮现出来 - 甚至可能有一些错误 - 但它可以给你一个想法。
glutDisplayFunc()
is a quick way to display stuff but it quickly becomes more trouble than it's worth when writing simple projects.Unless you're multithreading and keeping a constant copy of your game-state for glutDisplayFunc() to read, I'd say avoid using it - and even then, probably, too.
For game loops, I believe it's better to drive OpenGL rather than let it drive you.
Simple menu loop. (Forgive my code style it's different than usual).
It's not a great layout, but it would work for a simple game.
init()
initializes openGL.menu()
,single()
andmulti()
each have their own loops, which render and swap the buffers manually.menu()
returns a value specifying which menu item was chosen.Game loop that runs at constant speed and doesn't waste CPU.
Again, not wonderful, but function enough. (I use something like this).
CGame is the game class (if you want to be OO).
get_time(), wait_till(), time_type etc are all made up. Use whatever function/type suits.
Quick and off the top of my head - probably even has some bugs - but it could give you an idea.