2009-05-04 20 views
3

我正在構建一個簡單的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. 

} 

無論如何,這是所有非常基本的。只是好奇,如果有更好的方式來做到這一點。

感謝, 馬修

回答

6

有因爲有天上的星星建設主迴路的儘可能多的方式!你的完全有效,可能會正常工作。這裏有一些你可能會嘗試的東西:

  • 你在哪裏閱讀控件?如果你在你的「ui.update」中做了這個,然後在你的「stage.update」中做出響應,那麼你就添加了一個滯後幀。確保你在循環中按照「讀取控件 - >應用控件 - >更新遊戲世界 - >渲染」的順序。

  • 您是使用3D API進行渲染嗎?大量的書籍告訴你,當你渲染做這樣的事情(使用OpenGL上下的僞代碼):

    glClear()   // clear the buffer 
    glBlahBlahDraw() // issue commands 
    glSwapBuffers() // wait for commands to finish and display 
    

    這是更好地做

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

    如果你這樣做的,該GPU可在CPU正在更新下一幀的階段的同時繪製屏幕。

  • 即使遊戲「暫停」,某些遊戲引擎也會繼續渲染幀並接受某些UI(例如,相機控制)。這使您可以在暫停的遊戲世界中飛行,並在嘗試調試時查看屏幕外顯示的內容。 (有時這稱爲「調試暫停」而不是「真正的暫停」)。

  • 此外,大量遊戲引擎可以「單步執行」(運行一幀,然後立即暫停)。如果您試圖捕捉髮生在特定事件上的錯誤。

希望其中的一些是有幫助的 - 這只是我看着你的代碼的隨機想法。