| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1036 人关注过本帖, 1 人收藏
标题:C#一道问题,难以解决
只看楼主 加入收藏
雄哥现世
Rank: 1
等 级:新手上路
帖 子:26
专家分:7
注 册:2012-9-25
结帖率:87.5%
收藏(1)
已结贴  问题点数:10 回复次数:13 
C#一道问题,难以解决
小球一开始在与水平线45度夹角的直线上移动,碰到窗体边缘后会反弹。小球碰撞后沿来路反回。中间的过程我不会编写。。感觉思绪理解不清楚while (f.pictureBox3.Location.Y > -1 && f.pictureBox3.Location.Y < 450 && f.pictureBox3.Location.X > -1 && f.pictureBox3.Location.X < 670)
            {
                if (f.pictureBox3.Location.Y == 432)
                {

                    for (int i = 0; i < 432; i++)
                    {
                        f.pictureBox3.Location = new Point(f.pictureBox3.Location.X + 1, f.pictureBox3.Location.Y - 1);
                        Thread.Sleep(10);

                    }
                }
                if (f.pictureBox3.Location.Y == 0)
                {
                    for (int i = 0; i < 432; i++)
                    {
                        f.pictureBox3.Location = new Point(f.pictureBox3.Location.X + 1, f.pictureBox3.Location.Y + 1);
                        Thread.Sleep(10);

                    }

                }
                if (f.pictureBox2.Location.X == 650)
                {

                    for (int i = 0; i < 650; i++)
                    {
                        f.pictureBox3.Location = new Point(f.pictureBox3.Location.X - 1, f.pictureBox3.Location.Y + 1);
                        Thread.Sleep(10);

                    }
                }
                if (f.pictureBox3.Location.X == 0)
                {
                    for (int i = 0; i < 650; i++)
                    {
                        f.pictureBox3.Location = new Point(f.pictureBox3.Location.X + 1, f.pictureBox3.Location.Y + 1);
                        Thread.Sleep(10);
                    }
                }
                if (f.pictureBox3.Location.Y > 0 && f.pictureBox3.Location.Y < 432 && f.pictureBox3.Location.X ==0 && f.pictureBox3.Location.X < 650)
                {
                                                           
                   for (int i = f.pictureBox3.Location.Y; i <432;++i)

                    {
                        f.pictureBox3.Location = new Point(f.pictureBox3.Location.X -1, f.pictureBox3.Location.Y + 1);
                        Thread.Sleep(10);
            
                    }

                }

            }
这是我编写的一部分,感觉不是很对求大神解答啊
搜索更多相关主题的帖子: 水平线 反弹 
2012-10-28 21:51
hellovfp
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:禁止访问
威 望:30
帖 子:2976
专家分:7697
注 册:2009-7-21
收藏
得分:10 
大至流程:
    static int dirx = 1, diry = 1; // 定义x ,y 方向为1,如果是碰边,则dirx = -dirx; 反向
    static int speedx =  5, speedy; // 定义x, y 方向的移动速度,值越大越快,值越小运行越慢。
    static double pi = 3.1415926f; //这个值不用偶说了吧?很明显你清楚的。


            RECT r = {0, 0, 535, 350}; // 边框大小 535 * 350
            speedy = tan(pi / 4) * speedx; // Y的速度为 4分之园周然后取正切,并和X方向速度相乘。

            if((rt.left + 50) > 530 || rt.left < 0 ) dirx = -dirx; //如果X碰边,则反向X。
            if((rt.top  + 50) > 350 || rt.top  < 0 ) diry = -diry; //如果Y碰边,则反向Y。

            rt.left += dirx * speedx; //移动物的x坐标 += X方向 * X速度
            rt.top  += diry * speedy; //移动物的y坐标 += Y方向 * Y速度

            // 将移动物显示出来,这里是用的画方框。
            Rectangle(hdc, rt.left, rt.top, rt.left + rt.right, rt.top + rt.bottom);

效果如exe里的演示:
DropBlock.rar (4.52 KB)

我们都在路上。。。。。
2012-10-29 11:20
雄哥现世
Rank: 1
等 级:新手上路
帖 子:26
专家分:7
注 册:2012-9-25
收藏
得分:0 
回复 2楼 hellovfp
大神啊。。。不过我没学多久啊,有些不是很懂,就是我这个球是放在PictureBox里面,我尽量看看应该可以弄懂了,可以加你好友吗??
2012-10-29 14:43
雄哥现世
Rank: 1
等 级:新手上路
帖 子:26
专家分:7
注 册:2012-9-25
收藏
得分:0 
回复 2楼 hellovfp
为什么要加上50啊就是算靠边的时候
2012-10-29 19:50
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
没搞清楚你们说的“原路返回”是什么意思

授人以渔,不授人以鱼。
2012-10-30 11:45
hellovfp
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:禁止访问
威 望:30
帖 子:2976
专家分:7697
注 册:2009-7-21
收藏
得分:0 
这是C++的完整原代码,很好的可以转换成C#代码,用System.2Drawing中的GDI+ Graphics绘图功能画园或是画其它的方式画移动物,
如果移动物是小的pic块,只需要改变pic块的左顶点坐标就可以移动了。计算方法还是那个不变,关键代码部分偶已经解释了。
移动大小是:
            rt.left = 0;
            rt.top = 0;
            rt.right = 50;
            rt.bottom = 50;
所以碰边判断需要是左顶点坐标X方向和Y方向各加50,才是方块右下角坐标。

#include <windows.h>
#include <math.h>

/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/*  Make the class name into a global variable  */
char szClassName[ ] = "CodeBlocksWindowsApp";
RECT rt;

int WINAPI WinMain (HINSTANCE hThisInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR lpszArgument,
                     int nCmdShow)
{
    HWND hwnd;               /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default colour as the background of the window */
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           "Code::Blocks Template Windows App",       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           544,                 /* The programs width */
           375,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

    /* Make the window visible on the screen */
    ShowWindow (hwnd, nCmdShow);

    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}


/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    static int dirx = 1, diry = 1;
    static int speedx =  5, speedy;
    static double pi = 3.1415926f;

    switch (message)                  /* handle the messages */
    {
        case WM_CREATE:
            rt.left = 0;
            rt.top = 0;
            rt.right = 50;
            rt.bottom = 50;
            SetTimer(hwnd, 1, 10, NULL);
            break;
        case WM_TIMER:
            {
            HDC hdc;
            RECT r = {0, 0, 535, 350};
            speedy = tan(pi / 4) * speedx;

            if((rt.left + 50) > 530 || rt.left < 0 ) dirx = -dirx;
            if((rt.top  + 50) > 350 || rt.top  < 0 ) diry = -diry;

            rt.left += dirx * speedx;
            rt.top  += diry * speedy;

            hdc = GetDC(hwnd);

            //InvalidateRect(hwnd, NULL, FALSE);
            FillRect(hdc, &r, (HBRUSH)RGB(0, 0, 0));
            Rectangle(hdc, rt.left, rt.top, rt.left + rt.right, rt.top + rt.bottom);

            ReleaseDC(hwnd, hdc);
            }
            break;
        case WM_DESTROY:
            KillTimer(hwnd, 1);
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;
        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}

我们都在路上。。。。。
2012-10-30 13:32
hellovfp
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:禁止访问
威 望:30
帖 子:2976
专家分:7697
注 册:2009-7-21
收藏
得分:0 
对了,那个 “4分之园周然后取正切” 就是延45度角的意思。。。

我们都在路上。。。。。
2012-10-30 13:36
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
我怎么看他都是要做光滑面上的完全碰撞反弹,斜碰是需要改变方向的,却又有个“原路返回”,不理解。

授人以渔,不授人以鱼。
2012-10-30 13:39
hellovfp
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:禁止访问
威 望:30
帖 子:2976
专家分:7697
注 册:2009-7-21
收藏
得分:0 
原路返回?对称算吧,最好玩的就是这个计算了,偶的那个示例最终走的还是重复的路子,只要是正方型的边框,45度角怎么都会转回来的。
呵呵,完全碰撞反弹的例子? 那个Freakout打砖块的游戏就是很好的例子,学习DX编程的第一个实例。

我们都在路上。。。。。
2012-10-30 13:59
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
他是在做类似打砖块的游戏吧?

授人以渔,不授人以鱼。
2012-10-30 14:02
快速回复:C#一道问题,难以解决
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014215 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved