鱼C论坛

 找回密码
 立即注册
查看: 3721|回复: 1

用EasyX做弹跳球小球速度快了,为什么会穿过挡板和砖块

[复制链接]
发表于 2021-4-26 20:34:16 | 显示全部楼层 |阅读模式
20鱼币
只有小球速度是1时正常,速度调快就直接穿过去了,感觉逻辑没错啊


  1. #include<iostream>
  2. #include<graphics.h>
  3. #include<conio.h>
  4. using namespace std;
  5. #define H 480
  6. #define W 640
  7. #define MUN 20  //砖块个数
  8. #define NUM 4
  9. int ball_x, ball_y;//小球坐标
  10. int ball_vx, ball_vy;//小球速度
  11. int ball_r;//小球坐标
  12. int bar_x, bar_y;//挡板中心的坐标
  13. int bar_h, bar_w;//挡板的高度和宽度
  14. int bar_left, bar_right, bar_up, bar_down;//挡板上下左右的坐标
  15. int bar_v;//挡板速度
  16. int isbrick[MUN][NUM];//砖块是否存在 1为存在 0为消失
  17. int brick_h, brick_w;//砖的高度和宽度
  18. int a;


  19. //数据初始化
  20. void init()
  21. {
  22.         ball_x = W / 2;
  23.         ball_y = H / 2;
  24.         ball_vx =2;
  25.         ball_vy = 2;
  26.         bar_h = H / 20;
  27.         bar_w = W / 5;
  28.         ball_r = 10;
  29.         bar_v = 15;
  30.         bar_x = W / 2;
  31.         bar_y = H - bar_h / 2;
  32.         bar_left = bar_x - bar_w / 2;
  33.         bar_right = bar_x + bar_w / 2;
  34.         bar_up = bar_y - bar_h / 2;
  35.         bar_down = bar_y + bar_h / 2;
  36.         brick_h = 30;
  37.         brick_w = W / MUN;
  38.         for(int i=0;i<MUN;i++)
  39.                 for(int j=0;j<NUM;j++)
  40.         isbrick[i][j]=1;//判断方块是否存在
  41.         a = NUM * MUN;
  42. }

  43. //绘制
  44. void show()
  45. {
  46.         //绘制球
  47.         setfillcolor(WHITE);
  48.         fillcircle(ball_x,ball_y,ball_r);
  49.         //绘制矩形挡板
  50.         solidrectangle(bar_left, bar_up, bar_right, bar_down);
  51.        
  52.         setcolor(BLACK);
  53.         setfillcolor(YELLOW);
  54.         for (int i = 0; i < MUN; i++)
  55.         {
  56.                 for (int j = 0; j < NUM; j++)
  57.                 {
  58.                         if(isbrick[i][j]==1)
  59.                 fillrectangle(brick_w*i, brick_h*j, brick_w*i + brick_w, brick_h+brick_h*j);
  60.         }}
  61.         Sleep(1);

  62. }


  63. //清除之前绘制的图形
  64. void endshow()
  65. {
  66.         setcolor(BLACK);
  67.         setfillcolor(BLACK);
  68.         fillcircle(ball_x, ball_y, ball_r);
  69.         setcolor(BLACK);
  70.         setfillcolor(BLACK);
  71.         solidrectangle(bar_left,bar_up,bar_right,bar_down );
  72.         for (int i = 0; i < MUN; i++)
  73.         {
  74.                 for (int j = 0; j < NUM; j++)
  75.                 {
  76.                         if (isbrick[i][j]== 0)
  77.                                 setcolor(BLACK);
  78.                         setfillcolor(BLACK);
  79.                         fillrectangle(brick_w*i, brick_h*j, brick_w*i + brick_w, brick_h+brick_h*j);
  80.                 }
  81.         }
  82. }

  83. //小球运动状态
  84. void AI()
  85. {

  86.         //小球自动移动
  87.         ball_x += ball_vx;
  88.         ball_y += ball_vy;
  89.         //小球撞到墙壁之后反弹
  90.         if (ball_x <= ball_r || ball_x >= (W - ball_r))
  91.                 ball_vx = -ball_vx;
  92.         if (ball_y <= ball_r || ball_y >= (H - ball_r))
  93.                 ball_vy = -ball_vy;
  94.         //小球撞到挡板之后反弹
  95.        
  96.         //小球撞挡板上边
  97.         if (ball_y + ball_r == bar_up)
  98.                 if ((ball_x + ball_r >= bar_left) && (ball_x - ball_r <= bar_right))
  99.                         ball_vy = -ball_vy;
  100.        //小球撞到挡板下面
  101.         if(ball_y-ball_r==bar_down)
  102.                 if ((ball_x + ball_r >= bar_left) && (ball_x - ball_r <= bar_right))
  103.                         ball_vy = -ball_vy;
  104.       //小球撞到挡板左边
  105.         if (ball_x + ball_r == bar_left)
  106.                 if ((ball_y + ball_r >= bar_up) && (ball_y - ball_r <= bar_down))
  107.                         ball_vx = -ball_vx;
  108.      //小球撞到挡板右边
  109.         if (ball_x - ball_r == bar_right)
  110.                 if ((ball_y + ball_r >= bar_up) && (ball_y - ball_r <= bar_down))
  111.                         ball_vx = -ball_vx;
  112.        
  113.        
  114.         //小球撞砖块
  115.         for (int i = 0; i < MUN; i++)
  116.         {
  117.                 for (int j = 0; j < NUM; j++)
  118.                 {
  119.                         if (isbrick[i][j] == 1)
  120.                         {
  121.                                 if (((ball_y - ball_r) == brick_h*j+brick_h) && (ball_x >= brick_w * i) && (ball_x <= (brick_w*i + brick_w)))
  122.                                 {
  123.                                         isbrick[i][j] = 0;
  124.                                         ball_vy = -ball_vy;
  125.                                 }
  126.                                 if (((ball_y + ball_r) == brick_h * j) && (ball_x >= brick_w * i) && (ball_x <= (brick_w*i + brick_w)))
  127.                                 {
  128.                                         isbrick[i][j] = 0;
  129.                                         ball_vy = -ball_vy;
  130.                                 }
  131.                                 if ((ball_x - ball_r == brick_w * i + brick_w) && (ball_y - ball_r <= brick_h*j+brick_h) && (ball_y + ball_r >= brick_h*j))
  132.                                 {
  133.                                         isbrick[i][j] = 0;
  134.                                         ball_vx = -ball_vx;
  135.                                 }

  136.                                 if ((ball_x + ball_r == brick_w * i) && (ball_y - ball_r <= brick_h*j+brick_h) && (ball_y + ball_r >= brick_h*j))
  137.                                 {
  138.                                         isbrick[i][j] = 0;
  139.                                         ball_vx = -ball_vx;
  140.                                 }
  141.                                 //a--;

  142.                         }
  143.                 }

  144.                 }
  145.         }

  146. //键盘操作
  147. void key()
  148. {
  149.         char ch;
  150.         if (_kbhit())
  151.         {
  152.                 ch = _getch();
  153.                 if ((ch=='A'||ch == 'a')&&bar_left > bar_v)
  154.                 {
  155.                         bar_x = bar_x - bar_v;
  156.                         bar_left = bar_x - bar_w / 2;
  157.                         bar_right = bar_x + bar_w / 2;
  158.                 }

  159.                 if ((ch=='D'||ch == 'd')&&bar_right < W-bar_v)
  160.                 {
  161.                         bar_x = bar_x + bar_v;
  162.                         bar_left = bar_x - bar_w / 2;
  163.                         bar_right = bar_x + bar_w / 2;
  164.                 }
  165.                 if ((ch=='W'||ch == 'w')&&bar_up >0)
  166.                 {
  167.                         bar_y = bar_y - bar_v;
  168.                         bar_up = bar_y - bar_h / 2;
  169.                         bar_down = bar_y + bar_h / 2;
  170.                 }
  171.                 if ((ch=='S'||ch == 's')&&bar_down <H)
  172.                 {
  173.                         bar_y = bar_y + bar_v;
  174.                         bar_up = bar_y - bar_h / 2;
  175.                         bar_down = bar_y + bar_h / 2;
  176.                 }
  177. }

  178. }

  179. int main()
  180. {
  181.         init();
  182.         initgraph(W, H);
  183.         BeginBatchDraw();//缓冲函数
  184.         while (1)
  185.         {
  186.                 endshow();
  187.                 AI();
  188.                 key();
  189.                 show();
  190.                
  191.                 FlushBatchDraw();
  192.                 //Sleep(1);
  193.         }
  194.         EndBatchDraw();
  195.         return 0;
  196. }
复制代码
[/code]

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-1 06:54

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表