ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

游戏开发程序设计基础(自主学习2)第二章:打字母小游戏

2021-10-20 13:34:33  阅读:245  来源: 互联网

标签:score hdc swprintf per 小游戏 health str 程序设计 第二章


游戏开发程序设计基础【中国传媒大学】 

第二章:打字母小游戏

 

第一节:流程图如下

第二节:实现上图打字母游戏初阶

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);

  }

标签:score,hdc,swprintf,per,小游戏,health,str,程序设计,第二章
来源: https://www.cnblogs.com/Anjoras-bk/p/15423034.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有