// 游戏的初始化, // 创建游戏的内部数据结构和系统对象。 void CreateGame(HWND hwnd, // 主窗口句柄 DWORD dwInitTimerElapse, // unsigned int one_level_scores, DOUBLE level_speedup_ratio, int boundary_x, int boundary_y, int init_x, int init_y, int init_len, dirction init_dir) { // 设置随机数种子 // 需要使用随机数生成食物的位置等。 FILETIME ft; GetSystemTimeAsFileTime(&ft); srand(ft.dwLowDateTime); dbLevelSpeedupRatio = level_speedup_ratio; dwTimerElapse = dwInitTimerElapse; dwOneLevelScores = one_level_scores; // 设置游戏的边界 SetBoundary(boundary_x, boundary_y); // 创建表示贪吃蛇的数据结构 CreateSnake(init_dir, init_x, init_y, init_len); // 创建表示食物的数据结构 CreateFood(); // 创建一个计时器 // 每经过 dwTimerElapse 毫秒,hwnd窗口(主窗口)就会收到一个WM_TIMER消息。 // 计时器是驱动本游戏进行的主要时间线。 // dwTimerElapse变量影响游戏进行的快慢变化。 SetTimer(hwnd, TIMER_ID, dwTimerElapse, NULL); }
//---------------------------------------------------------------------------- void WrigglingSnake::CreateScene () { mScene = new0 Node(); mTrnNode = new0 Node(); mScene->AttachChild(mTrnNode); mWireState = new0 WireState(); mRenderer->SetOverrideWireState(mWireState); CreateSnake(); }
//---------------------------------------------------------------------------- bool WrigglingSnake::OnInitialize () { if ( !Application::OnInitialize() ) return false; m_spkScene = new Node(1); m_spkTrnNode = new Node(1); m_spkScene->AttachChild(m_spkTrnNode); m_spkWireframe = new WireframeState; m_spkScene->SetRenderState(m_spkWireframe); ZBufferState* pkZBuffer = new ZBufferState; pkZBuffer->Enabled() = true; pkZBuffer->Writeable() = true; pkZBuffer->Compare() = ZBufferState::CF_LEQUAL; m_spkScene->SetRenderState(pkZBuffer); CreateSnake(); // center-and-fit for camera viewing m_spkScene->UpdateGS(0.0f); Bound kWBound = m_spkScene->WorldBound(); m_spkTrnNode->Translate() = -kWBound.Center(); ms_spkCamera->SetFrustum(1.0f,100.0f,-0.55f,0.55f,0.4125f,-0.4125f); Vector3f kCLeft(1.0f,0.0f,0.0f); Vector3f kCUp(0.0f,0.0f,1.0f); Vector3f kCDir(0.0f,-1.0f,0.0f); Vector3f kCLoc = -3.0f*kWBound.Radius()*kCDir; ms_spkCamera->SetFrame(kCLoc,kCLeft,kCUp,kCDir); // initial update of objects ms_spkCamera->Update(); m_spkScene->UpdateGS(0.0f); m_spkScene->UpdateRS(); // camera turret and tumble mode m_spkMotionObject = m_spkScene; m_fTrnSpeed = 0.01f; m_fRotSpeed = 0.001f; m_bTurretActive = true; SetTurretAxes(); return true; }