鱼C论坛

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

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

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

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


//数据初始化
void init()
{
        ball_x = W / 2;
        ball_y = H / 2;
        ball_vx =2;
        ball_vy = 2;
        bar_h = H / 20;
        bar_w = W / 5;
        ball_r = 10;
        bar_v = 15;
        bar_x = W / 2;
        bar_y = H - bar_h / 2;
        bar_left = bar_x - bar_w / 2;
        bar_right = bar_x + bar_w / 2;
        bar_up = bar_y - bar_h / 2;
        bar_down = bar_y + bar_h / 2;
        brick_h = 30;
        brick_w = W / MUN;
        for(int i=0;i<MUN;i++)
                for(int j=0;j<NUM;j++)
        isbrick[i][j]=1;//判断方块是否存在
        a = NUM * MUN;
}

//绘制
void show()
{
        //绘制球
        setfillcolor(WHITE);
        fillcircle(ball_x,ball_y,ball_r);
        //绘制矩形挡板
        solidrectangle(bar_left, bar_up, bar_right, bar_down);
        
        setcolor(BLACK);
        setfillcolor(YELLOW);
        for (int i = 0; i < MUN; i++)
        {
                for (int j = 0; j < NUM; j++)
                {
                        if(isbrick[i][j]==1)
                fillrectangle(brick_w*i, brick_h*j, brick_w*i + brick_w, brick_h+brick_h*j);
        }}
        Sleep(1);

}


//清除之前绘制的图形
void endshow()
{
        setcolor(BLACK);
        setfillcolor(BLACK);
        fillcircle(ball_x, ball_y, ball_r);
        setcolor(BLACK);
        setfillcolor(BLACK);
        solidrectangle(bar_left,bar_up,bar_right,bar_down );
        for (int i = 0; i < MUN; i++)
        {
                for (int j = 0; j < NUM; j++)
                {
                        if (isbrick[i][j]== 0)
                                setcolor(BLACK);
                        setfillcolor(BLACK);
                        fillrectangle(brick_w*i, brick_h*j, brick_w*i + brick_w, brick_h+brick_h*j);
                }
        }
}

//小球运动状态
void AI()
{

        //小球自动移动
        ball_x += ball_vx;
        ball_y += ball_vy;
        //小球撞到墙壁之后反弹
        if (ball_x <= ball_r || ball_x >= (W - ball_r))
                ball_vx = -ball_vx;
        if (ball_y <= ball_r || ball_y >= (H - ball_r))
                ball_vy = -ball_vy;
        //小球撞到挡板之后反弹
        
        //小球撞挡板上边
        if (ball_y + ball_r == bar_up)
                if ((ball_x + ball_r >= bar_left) && (ball_x - ball_r <= bar_right))
                        ball_vy = -ball_vy;
       //小球撞到挡板下面
        if(ball_y-ball_r==bar_down)
                if ((ball_x + ball_r >= bar_left) && (ball_x - ball_r <= bar_right))
                        ball_vy = -ball_vy;
      //小球撞到挡板左边
        if (ball_x + ball_r == bar_left)
                if ((ball_y + ball_r >= bar_up) && (ball_y - ball_r <= bar_down))
                        ball_vx = -ball_vx;
     //小球撞到挡板右边
        if (ball_x - ball_r == bar_right)
                if ((ball_y + ball_r >= bar_up) && (ball_y - ball_r <= bar_down))
                        ball_vx = -ball_vx;
        
        
        //小球撞砖块
        for (int i = 0; i < MUN; i++)
        {
                for (int j = 0; j < NUM; j++)
                {
                        if (isbrick[i][j] == 1)
                        {
                                if (((ball_y - ball_r) == brick_h*j+brick_h) && (ball_x >= brick_w * i) && (ball_x <= (brick_w*i + brick_w)))
                                {
                                        isbrick[i][j] = 0;
                                        ball_vy = -ball_vy;
                                }
                                if (((ball_y + ball_r) == brick_h * j) && (ball_x >= brick_w * i) && (ball_x <= (brick_w*i + brick_w)))
                                {
                                        isbrick[i][j] = 0;
                                        ball_vy = -ball_vy;
                                }
                                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))
                                {
                                        isbrick[i][j] = 0;
                                        ball_vx = -ball_vx;
                                }

                                if ((ball_x + ball_r == brick_w * i) && (ball_y - ball_r <= brick_h*j+brick_h) && (ball_y + ball_r >= brick_h*j))
                                {
                                        isbrick[i][j] = 0;
                                        ball_vx = -ball_vx;
                                }
                                //a--;

                        }
                }

                }
        }

//键盘操作
void key()
{
        char ch;
        if (_kbhit())
        {
                ch = _getch();
                if ((ch=='A'||ch == 'a')&&bar_left > bar_v)
                {
                        bar_x = bar_x - bar_v;
                        bar_left = bar_x - bar_w / 2;
                        bar_right = bar_x + bar_w / 2;
                }

                if ((ch=='D'||ch == 'd')&&bar_right < W-bar_v)
                {
                        bar_x = bar_x + bar_v;
                        bar_left = bar_x - bar_w / 2;
                        bar_right = bar_x + bar_w / 2;
                }
                if ((ch=='W'||ch == 'w')&&bar_up >0)
                {
                        bar_y = bar_y - bar_v;
                        bar_up = bar_y - bar_h / 2;
                        bar_down = bar_y + bar_h / 2;
                }
                if ((ch=='S'||ch == 's')&&bar_down <H)
                {
                        bar_y = bar_y + bar_v;
                        bar_up = bar_y - bar_h / 2;
                        bar_down = bar_y + bar_h / 2;
                }
}

}

int main()
{
        init();
        initgraph(W, H);
        BeginBatchDraw();//缓冲函数
        while (1)
        {
                endshow();
                AI();
                key();
                show();
                
                FlushBatchDraw();
                //Sleep(1);
        }
        EndBatchDraw();
        return 0;
}
[/code]

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-9-21 13:28

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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