Java教程

游戏开发程序设计基础(自主学习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);

  }

这篇关于游戏开发程序设计基础(自主学习2)第二章:打字母小游戏的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!