基本游戏结构问题

mja*_*mes 2 architecture

我正在构建一个简单的2D游戏,我想知道是否有一些比我更好的编码器可以建议一个设计其基本架构的好方法.

游戏非常简单.屏幕上有许多类型的单元可以进行拍摄,移动和执行命中检测.屏幕放大和缩小,屏幕侧面有一个菜单UI栏.

我现在的架构看起来像这样:

Load a "stage".
Load a UI.
Have the stage and UI pass references to each other.
Load a camera, feed it the "stage" as a parameter. Display on screen what the camera is told to "see"

Main Loop {
if (gameIsActive){
   stage.update()
   ui.update()
   camera.updateAndRedraw()
   }
}else{
   if (!ui.pauseGame()){
       gameIsActive=true
   }

if(ui.pauseGame()){
   gameIsActive=false
}


stage update(){
Go through a list of objects on stage, perform collision detection and "action" checks.
objects on stage are all subclasses of an object that has a reference to the stage, and use that reference to request objects be added to the stage (ie. bullets from guns).
}

ui update(){
updates bars, etc. 

}
Run Code Online (Sandbox Code Playgroud)

无论如何,这都是非常基本的.只是好奇是否有更好的方法来做到这一点.

谢谢,马修

Cha*_*lie 6

建造主循环的方法与天空中的星星一样多!你的完全有效,可能会很好.以下是您可能尝试的一些事项:

  • 你在哪里阅读控件?如果您在"ui.update"中执行此操作,然后在"stage.update"中进行响应,那么您将添加一个滞后帧.确保你在循环中按顺序执行"读取控件 - >应用控件 - >更新gameworld - > render".

  • 您是使用3D API渲染的吗?渲染时很多书都会告诉你做这样的事情(使用OpenGL-ish伪代码):

    glClear()          // clear the buffer
    glBlahBlahDraw()   // issue commands
    glSwapBuffers()    // wait for commands to finish and display
    
    Run Code Online (Sandbox Code Playgroud)

    这样做更好

    glSwapBuffers()    // wait for commands from last time to finish
    glClear()          // clear the buffer
    glBlahBlahDraw()   // issue commands
    glFlush()          // start the GPU working
    
    Run Code Online (Sandbox Code Playgroud)

    如果你这样做,GPU可以在CPU更新下一帧的阶段的同时绘制屏幕.

  • 即使游戏"暂停",一些游戏引擎仍继续渲染帧并接受一些UI(例如,相机控件).这允许您在暂停的游戏世界中飞行,并在您尝试调试时查看屏幕外的情况.(有时这称为"调试暂停"而不是"真正的暂停".)

  • 此外,许多游戏引擎可以"单步"(运行一帧,然后立即暂停.)如果你试图捕捉特定事件发生的错误,这非常方便.

希望其中一些有用 - 这只是我看过你代码的随机想法.