C/C++自制程序:双人击球游戏

描述

每天一个编程小项目,提升你的编程能力! 

游戏说明

Player1玩家通过 W、S 按键控制左板上下移动,Player2 玩家通过 O、L 按键控制右板上下移动。在游戏过程中,小球的速度会随着接触木板的次数增加而加快,增加游戏难度。当小球没有碰到木板时游戏结束。

游戏效果

程序

简单了解游戏后我们就来试试吧!(直接上源码,大家可以看注释)

 

///////////////////////////////////////////////////
// 程序名称:双人击球游戏
// 编译环境:VS2010,EasyX_20200902
//


#include 
#include 


int main()
{
  initgraph(640, 480);
  srand(GetTickCount());
  setbkcolor(WHITE);
  cleardevice();


  setfillcolor(BLACK);
  setlinecolor(BLACK);
  settextstyle(30, 0, L"verdana");
  settextcolor(BLACK);


  int p1_x, p1_y, p2_x, p2_y, ball_x, ball_y;
  int dx, dy;
  int win = 1;              // 赢玩家
  p1_x = 45;                // 左板
  p1_y= 240;
  p2_x = 595;                // 右板
  p2_y = 240;  
  ball_x = rand() % 240 + 200;      // 小球 x 坐标
  ball_y = rand() % 180 + 150;      // 小球 y 坐标
  dx = (rand() % 2 * 2 - 1)*2;      // 小球移动增量
  dy = (rand() % 2 * 2 - 1)*2;      // 小球移动增量


  outtextxy(190, 200, L"Put Enter To Begin");
  while(_getwch() != 13);


  cleardevice();


  // player
  settextstyle(20, 0, L"Verdana");
  outtextxy(20, 50, L"Payer1");
  outtextxy(565, 50, L"Player2");


  // 提示
  settextstyle(15, 0, L"Verdana");
  outtextxy(20, 400, L"W 向上");
  outtextxy(20, 425, L"S 向下");
  outtextxy(590, 400, L"O 向上");
  outtextxy(590, 425, L"L 向下");


  line(50, 100, 590, 100);                      // 上边界
  line(50, 380, 590, 380);                      // 下边界
  solidcircle(ball_x, ball_y, 10);


  wchar_t key=0;
  while(true)
  {
    clearcircle(ball_x, ball_y, 10);                // 清除小球位置
    clearrectangle(p1_x - 5, p1_y - 20, p1_x + 5, p1_y + 20);    // 清除左板
    clearrectangle(p2_x - 5, p2_y - 20, p2_x + 5, p2_y + 20);    // 清除右板


    if(_kbhit())
    {
      // 获取按键
      key = _getwch();    
  
      // 判断板移动的位置
      if(key == L's')
        p1_y += 30;
      else if(key == L'w')
        p1_y -= 30;
      else if(key == L'l')
        p2_y += 30;
      else if(key == L'o')
        p2_y -= 30;
    }


    // 判断左右俩板是否超出移动界限
    if((p1_y - 20) < 100)
      p1_y = 120;
    else if((p1_y + 20) > 380)
      p1_y = 360;
    else if((p2_y - 20) < 100)
      p2_y = 120;
    else if((p2_y + 20)> 380)
      p2_y = 360;


    // 画左右两板位置
    solidrectangle(p1_x - 5, p1_y - 20, p1_x + 5, p1_y + 20);
    solidrectangle(p2_x - 5, p2_y - 20, p2_x + 5, p2_y + 20);


    ball_x = ball_x + dx;
    ball_y = ball_y + dy;


    // 判断小球遇到左右板反弹
    if(ball_x < 62 )
    {
      if(ball_y < p1_y + 20 && ball_y > p1_y - 20)
      {
        dx = -dx;
        dx += dx / 10;
      }
      else 
      {
        win = 2;
        solidcircle(ball_x, ball_y, 10);
        break;
      }
    }
    else if(ball_x > 578)
    {
      if(ball_y < p2_y + 20 && ball_y > p2_y - 20)
      {
        dx = -dx;
        dx += dx / 10;
      }
      else
      {
        solidcircle(ball_x, ball_y, 10);
        break;
      }
    }


    // 判断小球遇到上下边界反弹
    if(ball_y < 115)
      dy = -dy;
    else if(ball_y > 364)
      dy = -dy;


    solidcircle(ball_x, ball_y, 10);


    Sleep(20);
  }


  settextstyle(30, 0, L"Verdana");
  if(win == 1)
    outtextxy(250, 200, L"Player1 赢");
  else
    outtextxy(250, 200, L"Player2 赢");
  
  _getch();
  closegraph();
  return 0;
}

 

大家赶紧去动手试试吧!

  审核编辑:汤梓红
 
打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分