1. 程式人生 > 其它 >遊戲開發程式設計基礎(自主學習2)第二章:打字母小遊戲

遊戲開發程式設計基礎(自主學習2)第二章:打字母小遊戲

遊戲開發程式設計基礎【中國傳媒大學】

第二章:打字母小遊戲

第一節:流程圖如下

第二節:實現上圖打字母遊戲初階

1)全域性變數內新增

COLORREF color = RGB(255,0,0);  //文字顏色初值

int health =3;  //生命值

int score =0;  //分數

unsigned long times =0;  //遊戲執行時間

char c; //待輸入字母

WCHAR str[64]; //WCHAR寬字元型別;輸出字串str

char hit; //玩家當前按鍵字母

2)wWinMain函式內

//TODO:在此處放置程式碼

c = 'A' +rand()%26; //生成隨機字母

times = GetTickCount(); //程式啟動時間,毫秒

3)WndProc函式內新增

case WM_KEYDOWN:

if (health <= 0)  //無生命值

  break;  //不處理按鍵訊息

hit = wParm;  //得到當前玩家按鍵字母

if (hit == c)  //若輸入正確

{  c ='A' +rand()%26+32 *(rand()%2);

   score = score + 1;

   SetTimer(hWnd, 1, time_per, NULL);

}

else

{  health -= 1;

   if (health <= 0)

    times = GetTickCount() - times;  //計算總耗時

}

if(score %20 == 0)

{  level = score/20 +1;

   health = health + 1;

   if (time_per >= 4000)

    time_per = time_per * 0.8;

   else

    time_per = 3000;

InvalidateRect(hWnd, NULL, TRUE);  //重繪螢幕,觸發重繪訊息

break;

case WM_PAINT:  //繪製訊息

{  ...

  //TODO...

  SetTextColor(hdc, color);  //設文字顏色

  TextOut(hdc, 0, 0, L"請輸入正確的字母!", 9);  //提示

  swprintf(str, 64, L"生命數:%d", health);  //swprintf:把後面的資訊格式化儲存到str

  TextOut(hdc, 200, 0, str, wcslen(str));  //輸出str(生命數)

  swprintf(str, 64, L"得分:%d", score);  //得分格式化儲存到str

  TextOut(hdc, 400, 0, str, wcslen(str));  //輸出str(得分)

  if(health <= 0)

    swprintf(str, 64, L"遊戲結束,打字速度:%。2f個每秒", float(score * 1000)/ times);  //輸出遊戲結束相關資訊

  else

    swprintf(str, 64, L"%c", c);  //遊戲繼續,待輸入的字元儲存到str

  TextOut(hdc, 20, 40, str, wcslen(str));  //輸出當前待使用者輸入的字元在螢幕上

  EndPaint(hWnd, &ps);

}

break;

第三節:打字母遊戲進階

要求:

1)修改輸出文字的大小

2)要求使用者規定時間內完成每個字母的輸入,否則減少生命

3)增加對字母大小寫的支援

1)全域性變數內新增

int time_per =5000;  //每個字母

int timecount =3;  

int level = 1;  //第幾級,每20分升一級,加1生命值,規定時間縮短20%,最低三秒

2)WndProc函式內新增

case WM_TIMER:  //處理遊戲時間訊息

{  timecount -= 1;

   if (timecount <0)

  {  health -= 1;

     if (health <= 0)  //若遊戲結束,計算總耗時

       times = GetTickCount64() - times;

  }

}

case WM_CHAR:  //處理鍵盤按鍵訊息

  if (health <= 0)  //若生命值耗盡

    break;  //不處理按鍵訊息

  hit = wParm;  //得到當前使用者按鍵

  if (hit == VK_SHIFT || hit == VK_CAPITAL || hit == VK_CONTROL)

  {  score = score;

     health = health;

  }

  else if (hit == c)  //若輸入正確

  {  c ='A' +rand()%26 + 32 * (rand()%2);  //隨機生成新的待輸入字母

     score = score + 1;

    SetTimer(hWnd, 1, time_per, NULL);

  }

  else  //否則減少生命點

  {  health -= 1;

    if (health <= 0)

      times = GetTickCount() - times;  //計算總耗時

  }

  if(score %20 == 0)  //每20分升一級level,加1生命值,縮短規定時間

  {  level = score /20 +1;

     health += 1;

     if (time_per >= 4000)

      time_per = time_per *0.8;

     else

      time_per = 3000;

  }

  InvalidateRect(hWnd, NULL, TRUE);  //重繪螢幕,觸發重繪訊息

  break;

case WM_PAINT:  //繪製訊息

  {  ...

    //TODO...

    {  HFONT hf;

       LOGFONT lf;

       lf.lfHeight = 30;

       //lf.lfWidth = 20;

      lf.lfEscapement = 0;

      lf.lfTtalic = FALSE;

      lf.lfUnderline = FALSE;

      lf.lfStrikeOut = FALSE;

       hf = CreateFontIndirect(&lf);

       SetObject(hdc, hf);

    }

    SetTextColor(hdc, color);  //設文字顏色

    TextOut(hdc, 0, 0, L"請輸入正確的字母!", 9);  //提示

    swprintf(str, 64, L"生命數:%d", health);  //swprintf:把後面的資訊格式化儲存到str

    TextOut(hdc, 200, 0, str, wcslen(str));  //輸出str(生命數)

    swprintf(str, 64, L"得分:%d", score);  //得分格式化儲存到str

    TextOut(hdc, 400, 0, str, wcslen(str));  //輸出str(得分)

    swprintf(str, 64, L"level:%d", level);  //得分格式化儲存到str

    TextOut(hdc, 400, 40, str, wcslen(str));  //輸出str(得分

    if(health <= 0)

      swprintf(str, 64, L"遊戲結束,打字速度:%。2f個每秒", float(score * 1000)/ times);  //輸出遊戲結束相關資訊

    else

      swprintf(str, 64, L"%c", c);  //遊戲繼續,待輸入的字元儲存到str

    TextOut(hdc, 20, 80, str, wcslen(str));  //輸出當前待使用者輸入的字元在螢幕上

    EndPaint(hWnd, &ps);

  }