• c语言游戏实战(10):坤坤的篮球回避秀


      前言:

    这款小游戏是博主耗时两天半完成的,玩家需要控制坤坤在游戏界面上移动,来躲避游戏界面上方不断掉下来的篮球。本游戏使用C语言和easyx图形库编写,旨在帮助初学者了解游戏开发的基本概念和技巧。

    在开始编写代码之前,我们需要先了解一下游戏的基本规则和功能:

    游戏界面:游戏界面是一个矩形区域,玩家可以在这个区域内控制球的移动。

    坤坤:玩家控制的坤坤可以在游戏界面内自由移动,按下特定的按键后可以跳跃。

    篮球:篮球在游戏界面的正上方源源不断地生成,并下降。

    坤坤触碰篮球:当坤坤触碰到上方掉下来的篮球时,坤坤的血量就会降低一格,一共五格血量降完为止。

    接下来,我们将通过以下几个步骤来实现这个游戏:

    1. 初始化游戏界面和模型的信息。

    2. 处理键盘输入,实现玩家控制坤坤的移动和跳跃。

    3. 生成足够数量的篮球。

    4. 生成篮球,并控制其移动。

    5. 检测篮球与坤坤之间的触碰关系,并减少相应的血量。

    通过学习这个游戏的开发过程,初学者将能够掌握C语言编程和easyx图形库的基本技巧。

    1. 前期准备

    第一步:我们需要在easyx官网下载好easyx图形库。(具体操作可以去b站搜索相关视频)

    第二步:按照下图的步骤将字符集改为多节字符集,因为如果使用的字符集只包含有限数量的字符,可能无法支持所有需要的字符,导致无法正确加载图像。因此,将字符集改为多字符集可以确保包含所有可能需要的字符,从而避免加载图像时出现错误或乱码问题。

    2. 游戏的背景设置

    游戏界面的长和宽根据背景图片的长宽(右键点击图片的属性可以查看)来设置,这里的背景图片有两张一张是游戏界面,另一张是游戏结束的图片。这里首先需要声明一个IMAGE类型的变量来存储加载的图片数据。这是后续对图片进行处理的基础,利用函数loadimage可以从本地文件中加载图片(尽量将游戏所需要的素材放在和代码的同一个目录里),最后利用putimage可以将图片绘制到窗口上来。

    1. int main()
    2. {
    3. Init();
    4. Itset();
    5. //设置窗口的长宽
    6. initgraph(Wide, Hight);
    7. //缓冲
    8. BeginBatchDraw();
    9. while (1)
    10. {
    11. show();
    12. //刷新
    13. FlushBatchDraw();
    14. }
    15. closegraph;
    16. return 0;
    17. }
    1. //声明IMAGE类型的变量
    2. IMAGE img[2];
    3. //加载图片
    4. loadimage(&img[0], "resource/微信图片_20240222202456.jpg");
    5. loadimage(&img[1], "resource/微信图片_20240303132408.jpg");
    6. //绘制图片
    7. putimage(0, 0, &img[Img]);

    3. 初始模型的信息

    在这里篮球和坤坤的模型都是球,只是后面用图片覆盖而已。首先在游戏游戏界面的正上方生成多个球(具体数量自己定),然后在游戏的下方生成一个玩家控制的球,最后就是加载图片了。

    初始化小球 

    1. void Itset()
    2. {
    3. //
    4. for (int i = 0; i < Ball_num; i++)
    5. {
    6. Enemy[i].x = Wide / 2;
    7. Enemy[i].y = 10;
    8. Enemy[i].r = 10;
    9. }
    10. //玩家
    11. Player.x = Wide / 2;
    12. Player.r = 10;
    13. Player.y = Hight - Player.r * 4;
    14. }

    加载图片

    因为loadimage函数只能加载图片,加载不了视频或者动图,所以我们需要将视频一帧一帧的加载上去,然后用循环绘制图片,这样就产生了一个动图的效果。

    1. IMAGE kunkun[58];
    2. IMAGE ball[Ball_num];
    3. loadimage(&kunkun[0], "resource/2月22日.png", 34, 34);
    4. loadimage(&kunkun[1], "resource/2月22日(1).png", 34, 34);
    5. loadimage(&kunkun[2], "resource/2月22日(2).png", 34, 34);
    6. loadimage(&kunkun[3], "resource/2月22日(3).png", 34, 34);
    7. loadimage(&kunkun[4], "resource/2月22日(4).png", 34, 34);
    8. loadimage(&kunkun[5], "resource/2月22日(5).png", 34, 34);
    9. loadimage(&kunkun[6], "resource/2月22日(6).png", 34, 34);
    10. loadimage(&kunkun[7], "resource/2月22日(7).png", 34, 34);
    11. loadimage(&kunkun[8], "resource/2月22日(8).png", 34, 34);
    12. loadimage(&kunkun[9], "resource/2月22日(9).png", 34, 34);
    13. loadimage(&kunkun[10], "resource/2月22日(10).png", 34, 34);
    14. loadimage(&kunkun[11], "resource/2月22日(11).png", 34, 34);
    15. loadimage(&kunkun[12], "resource/2月22日(12).png", 34, 34);
    16. loadimage(&kunkun[13], "resource/2月22日(13).png", 34, 34);
    17. loadimage(&kunkun[14], "resource/2月22日(14).png", 34, 34);
    18. loadimage(&kunkun[15], "resource/2月22日(15).png", 34, 34);
    19. loadimage(&kunkun[16], "resource/2月22日(16).png", 34, 34);
    20. loadimage(&kunkun[17], "resource/2月22日(17).png", 34, 34);
    21. loadimage(&kunkun[18], "resource/2月22日(18).png", 34, 34);
    22. loadimage(&kunkun[19], "resource/2月22日(19).png", 34, 34);
    23. loadimage(&kunkun[20], "resource/2月22日(20).png", 34, 34);
    24. loadimage(&kunkun[21], "resource/2月22日(21).png", 34, 34);
    25. loadimage(&kunkun[22], "resource/2月22日(22).png", 34, 34);
    26. loadimage(&kunkun[23], "resource/2月22日(23).png", 34, 34);
    27. loadimage(&kunkun[24], "resource/2月22日(24).png", 34, 34);
    28. loadimage(&kunkun[25], "resource/2月22日(25).png", 34, 34);
    29. loadimage(&kunkun[26], "resource/2月22日(26).png", 34, 34);
    30. loadimage(&kunkun[27], "resource/2月22日(27).png", 34, 34);
    31. loadimage(&kunkun[28], "resource/2月22日(28).png", 34, 34);
    32. loadimage(&kunkun[29], "resource/2月22日(29).png", 34, 34);
    33. loadimage(&kunkun[30], "resource/2月22日(30).png", 34, 34);
    34. loadimage(&kunkun[31], "resource/2月22日(31).png", 34, 34);
    35. loadimage(&kunkun[32], "resource/2月22日(32).png", 34, 34);
    36. loadimage(&kunkun[33], "resource/2月22日(33).png", 34, 34);
    37. loadimage(&kunkun[34], "resource/2月22日(34).png", 34, 34);
    38. loadimage(&kunkun[35], "resource/2月22日(35).png", 34, 34);
    39. loadimage(&kunkun[36], "resource/2月22日(36).png", 34, 34);
    40. loadimage(&kunkun[37], "resource/2月22日(37).png", 34, 34);
    41. loadimage(&kunkun[38], "resource/2月22日(38).png", 34, 34);
    42. loadimage(&kunkun[39], "resource/2月22日(39).png", 34, 34);
    43. loadimage(&kunkun[40], "resource/2月22日(40).png", 34, 34);
    44. loadimage(&kunkun[41], "resource/2月22日(41).png", 34, 34);
    45. loadimage(&kunkun[42], "resource/2月22日(42).png", 34, 34);
    46. loadimage(&kunkun[43], "resource/2月22日(43).png", 34, 34);
    47. loadimage(&kunkun[44], "resource/2月22日(44).png", 34, 34);
    48. loadimage(&kunkun[45], "resource/2月22日(45).png", 34, 34);
    49. loadimage(&kunkun[46], "resource/2月22日(46).png", 34, 34);
    50. loadimage(&kunkun[47], "resource/2月22日(47).png", 34, 34);
    51. loadimage(&kunkun[48], "resource/2月22日(48).png", 34, 34);
    52. loadimage(&kunkun[49], "resource/2月22日(49).png", 34, 34);
    53. loadimage(&kunkun[50], "resource/2月22日(50).png", 34, 34);
    54. loadimage(&kunkun[51], "resource/2月22日(51).png", 34, 34);
    55. loadimage(&kunkun[52], "resource/2月22日(52).png", 34, 34);
    56. loadimage(&kunkun[53], "resource/2月22日(53).png", 34, 34);
    57. loadimage(&kunkun[54], "resource/2月22日(54).png", 34, 34);
    58. loadimage(&kunkun[55], "resource/2月22日(55).png", 34, 34);
    59. loadimage(&kunkun[56], "resource/2月22日(56).png", 34, 34);
    60. for (int i = 0; i < Ball_num; i++)
    61. {
    62. loadimage(&ball[i], "resource/5459.png_860.png", 36, 36);
    63. }

    4. 绘制图片

    图片是需要根据球来移动的,所以putimage函数的格式应该为:

    putimage(x, y, Wide, Hight, &kunkun[a], 0, 0,SRCAND);
    1. x, y:图像左上角在窗口中的坐标。
    2. Wide, Hight:要绘制的图像的宽度和高度。
    3. &kunkun[a]:指向图像数据数组的指针,a是数组中图像数据的索引。
    4. 0, 0:源图像中要复制的区域的左上角坐标。

    这样图片的位置就可以根据球的为止移动了。 

    1. void show()
    2. {
    3. srand((unsigned)time(NULL));
    4. //清屏函数
    5. cleardevice();
    6. //setbkcolor(WHITE);
    7. putimage(0, 0, &img[Img]);
    8. putimage(0, 0, &Heal[Health], SRCAND);
    9. for (int i = 0; i < Ball_num; i++)
    10. {
    11. setfillcolor(RGB(229, 124, 77));
    12. solidcircle(Enemy[i].x, Enemy[i].y, Enemy[i].r);
    13. }
    14. putimage(Player.x - 14, Player.y - 14, Wide, Hight, &kunkun[a], 0, 0,SRCAND);
    15. for (int i = 0; i < Ball_num; i++)
    16. {
    17. putimage(Enemy[i].x - 18, Enemy[i].y - 18, Wide, Hight, &ball[i], 0, 0, SRCAND);
    18. }
    19. }

     5. 篮球的移动

    随机生成8个随机数,然后根据这八个随机数在执行向下移动的同时执行向左或向右的指令。为了防止篮球的移动速度太快,我们需要加一个Sleep(10)函数给它降速。当篮球移动出游戏界面的时候我们让它重新生成。

    1. void Enemy_move()
    2. {
    3. srand((unsigned)time(NULL));
    4. for (int i = 0; i < Ball_num; i++)
    5. {
    6. int direction = rand() % 8;
    7. if (direction == 0)
    8. {
    9. Enemy[i].y++;
    10. Enemy[i].x--;
    11. }
    12. else if (direction == 1)
    13. {
    14. Enemy[i].y++;
    15. Enemy[i].x++;
    16. }
    17. else if (direction == 2)
    18. {
    19. Enemy[i].y += 2;
    20. Enemy[i].x += 2;
    21. }
    22. else if (direction == 3)
    23. {
    24. Enemy[i].y += 2;
    25. Enemy[i].x -= 2;
    26. }
    27. else if (direction == 4)
    28. {
    29. Enemy[i].y += 3;
    30. Enemy[i].x += 3;
    31. }
    32. else if (direction == 5)
    33. {
    34. Enemy[i].y += 3;
    35. Enemy[i].x -= 3;
    36. }
    37. else if (direction == 6)
    38. {
    39. Enemy[i].y += 4;
    40. Enemy[i].x -= 4;
    41. }
    42. else if (direction == 7)
    43. {
    44. Enemy[i].y += 4;
    45. Enemy[i].x += 4;
    46. }
    47. if (Enemy[i].x <0 || Enemy[i].x>Wide || Enemy[i].y > Hight - Player.r * 3)
    48. {
    49. Enemy[i].x = Wide / 2;
    50. Enemy[i].y = 10;
    51. Enemy[i].r = 10;
    52. }
    53. }
    54. }

    6. 玩家与球碰撞

    生成游戏血条,每碰撞一次血条减少,并且重新生成篮球。

    1. IMAGE Heal[6];
    2. loadimage(&Heal[5], "resource/微信图片_20240303142935.jpg");
    3. loadimage(&Heal[4], "resource/微信图片_20240303142958.jpg");
    4. loadimage(&Heal[3], "resource/微信图片_20240303142931.jpg");
    5. loadimage(&Heal[2], "resource/微信图片_20240303142926.jpg");
    6. loadimage(&Heal[1], "resource/微信图片_20240303142922.jpg");
    1. //玩家与球碰撞
    2. void collide()
    3. {
    4. for (int i = 0; i < Ball_num; i++)
    5. {
    6. if (Distance(Player.x, Player.y, Enemy[i].x, Enemy[i].y) < Player.r + Enemy[i].r && Health > 0)
    7. {
    8. Health--;
    9. Enemy[i].x = Wide / 2;
    10. Enemy[i].y = 10;
    11. Enemy[i].r = 10;
    12. }
    13. }
    14. }

    7. 人物的移动 

    在这里需要用到GetAsyncKeyState(vk virtual key)函数获取异步按键状态,其中vk virtual key是虚拟键值,如果接受到这个虚拟键值,它会返回真。VK_UP、VK_LEFT、VK_RIGHT、0x20、0x41、0x44、0x57分别是上箭头键、左箭头键、右箭头键、空格键、a、d、w的虚拟键值。最后这里比较难处理的就是跳跃的这个动作了,我在这里设置人物跳跃后最高上升60个像素格,然后通过while循环循环上升每次上升5个像素个,如果是直接上升60个像素格的话,就是闪现了达不到跳跃的效果,在人物上升的同时其他动作是任然要进行的,所以我们还需要将这些动作函数打包放到这个人物跳跃的while循环当中。值得注意的是我们还需要在这个while循环中加一个Sleep(20)调节循环速度,使这里运动速度与主函数的while循环的运动速度一致。

    1. void player_move()
    2. {
    3. if (GetAsyncKeyState(VK_LEFT)|| GetAsyncKeyState(0x41))
    4. {
    5. if (Player.x > 0)
    6. Player.x -= Player_sleep;
    7. }
    8. if (GetAsyncKeyState(VK_RIGHT)|| GetAsyncKeyState(0x44))
    9. {
    10. if (Player.x < Wide)
    11. Player.x += Player_sleep;
    12. }
    13. if (Player.y == Hight - Player.r * 4)
    14. {
    15. if (GetAsyncKeyState(0x20) || GetAsyncKeyState(0x57)|| GetAsyncKeyState(VK_UP))
    16. {
    17. BeginBatchDraw();
    18. while (Player.y > Hight - Player.r * 4 - 60)
    19. {
    20. Sleep(20);
    21. Player.y -= 5;
    22. player_move();
    23. Enemy_move();
    24. show();
    25. collide();
    26. FlushBatchDraw();
    27. }
    28. }
    29. if (Health == 0)
    30. {
    31. printf("\a");
    32. system("pause");
    33. exit(0);
    34. }
    35. }
    36. }

    效果展示:

    坤坤的篮球回避秀

    源码:

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #pragma comment(lib,"winmm.lib")
    8. #define Wide 1280
    9. #define Hight 720
    10. #define Wide1 780
    11. #define Hight1 286
    12. #define Ball_num 20
    13. #define Player_sleep 5
    14. int Health = 5;
    15. int sleep = 1;
    16. int Img = 0;
    17. struct Ball
    18. {
    19. float x = 0;
    20. int y = 0;
    21. float r = 0;
    22. };
    23. struct Ball Enemy[Ball_num];
    24. struct Ball Player;
    25. IMAGE img[2];
    26. IMAGE kunkun[58];
    27. IMAGE ball[Ball_num];
    28. IMAGE Heal[6];
    29. int a = 0;
    30. //加载图片
    31. void Init()
    32. {
    33. loadimage(&img[0], "resource/微信图片_20240222202456.jpg");
    34. loadimage(&img[1], "resource/微信图片_20240303132408.jpg");
    35. loadimage(&Heal[5], "resource/微信图片_20240303142935.jpg");
    36. loadimage(&Heal[4], "resource/微信图片_20240303142958.jpg");
    37. loadimage(&Heal[3], "resource/微信图片_20240303142931.jpg");
    38. loadimage(&Heal[2], "resource/微信图片_20240303142926.jpg");
    39. loadimage(&Heal[1], "resource/微信图片_20240303142922.jpg");
    40. loadimage(&kunkun[0], "resource/2月22日.png", 34, 34);
    41. loadimage(&kunkun[1], "resource/2月22日(1).png", 34, 34);
    42. loadimage(&kunkun[2], "resource/2月22日(2).png", 34, 34);
    43. loadimage(&kunkun[3], "resource/2月22日(3).png", 34, 34);
    44. loadimage(&kunkun[4], "resource/2月22日(4).png", 34, 34);
    45. loadimage(&kunkun[5], "resource/2月22日(5).png", 34, 34);
    46. loadimage(&kunkun[6], "resource/2月22日(6).png", 34, 34);
    47. loadimage(&kunkun[7], "resource/2月22日(7).png", 34, 34);
    48. loadimage(&kunkun[8], "resource/2月22日(8).png", 34, 34);
    49. loadimage(&kunkun[9], "resource/2月22日(9).png", 34, 34);
    50. loadimage(&kunkun[10], "resource/2月22日(10).png", 34, 34);
    51. loadimage(&kunkun[11], "resource/2月22日(11).png", 34, 34);
    52. loadimage(&kunkun[12], "resource/2月22日(12).png", 34, 34);
    53. loadimage(&kunkun[13], "resource/2月22日(13).png", 34, 34);
    54. loadimage(&kunkun[14], "resource/2月22日(14).png", 34, 34);
    55. loadimage(&kunkun[15], "resource/2月22日(15).png", 34, 34);
    56. loadimage(&kunkun[16], "resource/2月22日(16).png", 34, 34);
    57. loadimage(&kunkun[17], "resource/2月22日(17).png", 34, 34);
    58. loadimage(&kunkun[18], "resource/2月22日(18).png", 34, 34);
    59. loadimage(&kunkun[19], "resource/2月22日(19).png", 34, 34);
    60. loadimage(&kunkun[20], "resource/2月22日(20).png", 34, 34);
    61. loadimage(&kunkun[21], "resource/2月22日(21).png", 34, 34);
    62. loadimage(&kunkun[22], "resource/2月22日(22).png", 34, 34);
    63. loadimage(&kunkun[23], "resource/2月22日(23).png", 34, 34);
    64. loadimage(&kunkun[24], "resource/2月22日(24).png", 34, 34);
    65. loadimage(&kunkun[25], "resource/2月22日(25).png", 34, 34);
    66. loadimage(&kunkun[26], "resource/2月22日(26).png", 34, 34);
    67. loadimage(&kunkun[27], "resource/2月22日(27).png", 34, 34);
    68. loadimage(&kunkun[28], "resource/2月22日(28).png", 34, 34);
    69. loadimage(&kunkun[29], "resource/2月22日(29).png", 34, 34);
    70. loadimage(&kunkun[30], "resource/2月22日(30).png", 34, 34);
    71. loadimage(&kunkun[31], "resource/2月22日(31).png", 34, 34);
    72. loadimage(&kunkun[32], "resource/2月22日(32).png", 34, 34);
    73. loadimage(&kunkun[33], "resource/2月22日(33).png", 34, 34);
    74. loadimage(&kunkun[34], "resource/2月22日(34).png", 34, 34);
    75. loadimage(&kunkun[35], "resource/2月22日(35).png", 34, 34);
    76. loadimage(&kunkun[36], "resource/2月22日(36).png", 34, 34);
    77. loadimage(&kunkun[37], "resource/2月22日(37).png", 34, 34);
    78. loadimage(&kunkun[38], "resource/2月22日(38).png", 34, 34);
    79. loadimage(&kunkun[39], "resource/2月22日(39).png", 34, 34);
    80. loadimage(&kunkun[40], "resource/2月22日(40).png", 34, 34);
    81. loadimage(&kunkun[41], "resource/2月22日(41).png", 34, 34);
    82. loadimage(&kunkun[42], "resource/2月22日(42).png", 34, 34);
    83. loadimage(&kunkun[43], "resource/2月22日(43).png", 34, 34);
    84. loadimage(&kunkun[44], "resource/2月22日(44).png", 34, 34);
    85. loadimage(&kunkun[45], "resource/2月22日(45).png", 34, 34);
    86. loadimage(&kunkun[46], "resource/2月22日(46).png", 34, 34);
    87. loadimage(&kunkun[47], "resource/2月22日(47).png", 34, 34);
    88. loadimage(&kunkun[48], "resource/2月22日(48).png", 34, 34);
    89. loadimage(&kunkun[49], "resource/2月22日(49).png", 34, 34);
    90. loadimage(&kunkun[50], "resource/2月22日(50).png", 34, 34);
    91. loadimage(&kunkun[51], "resource/2月22日(51).png", 34, 34);
    92. loadimage(&kunkun[52], "resource/2月22日(52).png", 34, 34);
    93. loadimage(&kunkun[53], "resource/2月22日(53).png", 34, 34);
    94. loadimage(&kunkun[54], "resource/2月22日(54).png", 34, 34);
    95. loadimage(&kunkun[55], "resource/2月22日(55).png", 34, 34);
    96. loadimage(&kunkun[56], "resource/2月22日(56).png", 34, 34);
    97. for (int i = 0; i < Ball_num; i++)
    98. {
    99. loadimage(&ball[i], "resource/5459.png_860.png", 36, 36);
    100. }
    101. }
    102. //初始化小球的信息
    103. void Itset()
    104. {
    105. //
    106. for (int i = 0; i < Ball_num; i++)
    107. {
    108. Enemy[i].x = Wide / 2;
    109. Enemy[i].y = 10;
    110. Enemy[i].r = 10;
    111. }
    112. //玩家
    113. Player.x = Wide / 2;
    114. Player.r = 10;
    115. Player.y = Hight - Player.r * 4;
    116. }
    117. void Enemy_move()
    118. {
    119. srand((unsigned)time(NULL));
    120. for (int i = 0; i < Ball_num; i++)
    121. {
    122. int direction = rand() % 8;
    123. if (direction == 0)
    124. {
    125. Enemy[i].y++;
    126. Enemy[i].x--;
    127. }
    128. else if (direction == 1)
    129. {
    130. Enemy[i].y++;
    131. Enemy[i].x++;
    132. }
    133. else if (direction == 2)
    134. {
    135. Enemy[i].y += 2;
    136. Enemy[i].x += 2;
    137. }
    138. else if (direction == 3)
    139. {
    140. Enemy[i].y += 2;
    141. Enemy[i].x -= 2;
    142. }
    143. else if (direction == 4)
    144. {
    145. Enemy[i].y += 3;
    146. Enemy[i].x += 3;
    147. }
    148. else if (direction == 5)
    149. {
    150. Enemy[i].y += 3;
    151. Enemy[i].x -= 3;
    152. }
    153. else if (direction == 6)
    154. {
    155. Enemy[i].y += 4;
    156. Enemy[i].x -= 4;
    157. }
    158. else if (direction == 7)
    159. {
    160. Enemy[i].y += 4;
    161. Enemy[i].x += 4;
    162. }
    163. if (Enemy[i].x <0 || Enemy[i].x>Wide || Enemy[i].y > Hight - Player.r * 3)
    164. {
    165. Enemy[i].x = Wide / 2;
    166. Enemy[i].y = 10;
    167. Enemy[i].r = 10;
    168. }
    169. }
    170. }
    171. void show()
    172. {
    173. srand((unsigned)time(NULL));
    174. cleardevice();
    175. putimage(0, 0, &img[Img]);
    176. putimage(0, 0, &Heal[Health], SRCAND);
    177. for (int i = 0; i < Ball_num; i++)
    178. {
    179. setfillcolor(RGB(229, 124, 77));
    180. solidcircle(Enemy[i].x, Enemy[i].y, Enemy[i].r);
    181. }
    182. putimage(Player.x - 14, Player.y - 14, Wide, Hight, &kunkun[a], 0, 0,SRCAND);
    183. for (int i = 0; i < Ball_num; i++)
    184. {
    185. putimage(Enemy[i].x - 18, Enemy[i].y - 18, Wide, Hight, &ball[i], 0, 0, SRCAND);
    186. }
    187. }
    188. //距离
    189. int Distance(int x, int y, int x1, int y1)
    190. {
    191. return sqrt((x - x1) * (x - x1) + (y - y1) * (y - y1));
    192. }
    193. //玩家与球碰撞
    194. void collide()
    195. {
    196. for (int i = 0; i < Ball_num; i++)
    197. {
    198. if (Distance(Player.x, Player.y, Enemy[i].x, Enemy[i].y) < Player.r + Enemy[i].r && Health > 0)
    199. {
    200. Health--;
    201. Enemy[i].x = Wide / 2;
    202. Enemy[i].y = 10;
    203. Enemy[i].r = 10;
    204. }
    205. }
    206. }
    207. void player_move()
    208. {
    209. if (GetAsyncKeyState(VK_LEFT)|| GetAsyncKeyState(0x41))
    210. {
    211. if (Player.x > 0)
    212. Player.x -= Player_sleep;
    213. }
    214. if (GetAsyncKeyState(VK_RIGHT)|| GetAsyncKeyState(0x44))
    215. {
    216. if (Player.x < Wide)
    217. Player.x += Player_sleep;
    218. }
    219. if (Player.y == Hight - Player.r * 4)
    220. {
    221. if (GetAsyncKeyState(0x20) || GetAsyncKeyState(0x57)|| GetAsyncKeyState(VK_UP))
    222. {
    223. BeginBatchDraw();
    224. while (Player.y > Hight - Player.r * 4 - 60)
    225. {
    226. Sleep(20);
    227. Player.y -= 5;
    228. player_move();
    229. Enemy_move();
    230. show();
    231. collide();
    232. FlushBatchDraw();
    233. }
    234. }
    235. if (Health == 0)
    236. {
    237. printf("\a");
    238. system("pause");
    239. exit(0);
    240. }
    241. }
    242. }
    243. int main()
    244. {
    245. Init();
    246. Itset();
    247. initgraph(Wide, Hight);
    248. BeginBatchDraw();
    249. while (1)
    250. {
    251. Sleep(20);
    252. if (a <= 56)
    253. {
    254. a++;
    255. }
    256. if (a > 56)
    257. {
    258. a = 0;
    259. }
    260. if (Player.y < Hight - Player.r * 4)
    261. {
    262. Player.y += 2;
    263. }
    264. if (sleep > 0)
    265. {
    266. sleep--;
    267. }
    268. if (Health == 0)
    269. Img = 1;
    270. show();
    271. FlushBatchDraw();
    272. Enemy_move();
    273. player_move();
    274. collide();
    275. }
    276. closegraph;
    277. return 0;
    278. }

  • 相关阅读:
    java并发编程看这一篇就够了
    【DW组队学习—动手学数据分析】第二章:第三节数据重构2
    【总结】ui自动化selenium知识点总结
    目标检测YOLO实战应用案例100讲-船舶目标检测及编队识别(续)
    基于群智能的路径规划算法(三)------遗传算法
    使用TortoiseGit建立本地仓库,上传初版工程
    帆软FineBI随时记
    暂停数据迁移任务
    分页存储逻辑地址转物理地址
    如何将Python程序打包并保护源代码
  • 原文地址:https://blog.csdn.net/weixin_58252863/article/details/136459373