EasyX图形随机移动实践

有一小球在一矩形框内的任意位置,初始时以随机角度,随机速度往下掉落,每次碰到边框时以随机角度,随机速度继续运行,按ESC键结束。编写程序模拟小球的运动过程。

#include <conio.h>//要用到_kbhit()函数
#include <time.h>//要用到随机数
#include <graphics.h>
int get_randx();//实现随机出现
int get_randy();
int get_kx();//1~6随机取一个
int get_ky();
int main()
{
	initgraph(800, 800);
	setbkcolor(RGB(150, 150, 90));
	cleardevice();
	int x = get_randx();
	int y = get_randy();
	int kx = get_kx();
	int ky = get_ky();
	while (!(_kbhit() && _getch() == 27))
	{
		if ((x + kx + 20) >= 800)//四个边界检测,若达到边界,进行回弹并赋予随机速度
		{
			kx = -(get_kx());
			x += kx;
		}
		else if ((x + kx - 20) <= 0)
		{
			kx = get_kx();
			x += kx;
		}
		if (y + ky + 20 >= 800)
		{
			ky = -(get_ky());
			y += ky;
		}
		else if ((y + ky - 20) <= 0)
		{
			ky = get_ky();
			y += ky;
		}
		setcolor(RGB(50, 80, 80));//图像刷新
		setfillcolor(RGB(50, 80, 80));
		fillcircle(x, y, 20);
		Sleep(20);
		setcolor(RGB(150, 150, 90));
		setfillcolor(RGB(150, 150, 90));
		fillcircle(x, y, 20);
		x += kx;
		y += ky;
	}
	closegraph();
	return 0;
}
int get_randx()
{
	srand((unsigned)time(NULL) * (unsigned)rand());
	return rand() % 800;
}
int get_randy()
{
	srand((unsigned)time(NULL) + (unsigned)rand());
	return rand() % 800;
}
int get_kx()
{
	srand((unsigned)time(NULL) + (unsigned)rand());
	int k = rand() % 6;
	while (k == 0)
		k = rand() % 6;
	return k;
}
int get_ky()
{
	srand((unsigned)time(NULL) + (unsigned)rand());
	int k = rand() % 6;
	while (k == 0)
		k = rand() % 6;
	return k;
}

 46ba91cbefc544eda97401e2e70c0c7a.png

 

 看懂的掌声?

 

 

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浮央乜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值