跳跃的小球

作者在 2018-02-10 23:31:43 发布以下内容

#include <stdio.h>
#include <windows.h>
int main()
{
char title[200];//能存放200个字符的字符数组,用于保存窗口的标题
HWND hwnd;      //窗口的句柄
RECT rect;      //矩形结构体
int width,height;//窗口的宽度和高度
POINT ptCenter;  //窗口的中心点
HDC hdc;        //窗口的设备上下文
HBRUSH hBrush,hClearBrush,hOldBursh;//画刷
int disX=5,disY=5;//小球每次左右、上下移动5像素
GetConsoleTitle(title,200);//1.获取控制台窗口的标题
hwnd=FindWindow(NULL,title);//2.通过标题得到该窗口的句柄
GetClientRect(hwnd,&rect);//3.获取窗口的宽度与高度
width=rect.right-rect.left;//右边的点减左边的点就是窗口的宽度
height=rect.bottom-rect.top;//下边的点减上边的点就是窗口的高度
ptCenter.x=width/2;//4.获取窗口的中心点,为画圆作准备
ptCenter.y=height/2;
hdc=GetDC(hwnd);//5.获取窗口DC
hBrush=CreateSolidBrush(RGB(255,0,0));//6.创建红色画刷和黑色画刷
hClearBrush=CreateSolidBrush(RGB(0,0,0));
while(1)
{
hOldBursh=SelectObject(hdc,hBrush);//7.选择红色的画刷开始画圆 
Ellipse(hdc,ptCenter.x-20,ptCenter.y-20,ptCenter.x+20,ptCenter.y+20);
//8.间隔10毫秒移动一次小球,移动的距离均为5像素
Sleep(20);
//9.选择黑色的画刷进行清除移动的影子。
SelectObject(hdc,hClearBrush);
Ellipse(hdc,ptCenter.x-20,ptCenter.y-20,ptCenter.x+20,ptCenter.y+20);
if(ptCenter.x+20>=width||ptCenter.x-20<=0)
disX=-disX;
ptCenter.x+=disX;
if(ptCenter.y+20>=height||ptCenter.y-20<=0)
disY=-disY;
ptCenter.y+=disY;
//10.放回原先的画刷
SelectObject(hdc,hOldBursh);
}
return 0;
}

C | 阅读 1168 次
文章评论,共0条
游客请输入验证码
最新评论