反弹球

运动中的球,碰撞墙壁后会45角反弹。消除旧图再画新圆的缺点是圆重叠时会闪烁,而且覆盖的黑圆被看见。

// 反弹球.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "graphics.h"
#include "conio.h"
#include "time.h"

#define MAXCIRCLE 10 //圆的数目

void main( )
{
	int point;
	int circlenum=0;
	int x[MAXCIRCLE],y[MAXCIRCLE],i[MAXCIRCLE],j[MAXCIRCLE];
	for(point=MAXCIRCLE;point>=0;point--)//x方向上的移动距离,正负表示方向
	{
		 x[point]=rand()%30+1;					//y方向上的移动距离,正负表示方向
		 y[point]=rand()%30+1;
	}
	for(point=MAXCIRCLE-1;point>=0;point--)
	{
		i[point]=590;
		j[point]=rand()%380+50;
	}
	//初始化图形窗口
	initgraph(640,480);

	//设置当前线型以及填充类型,并画圆
	setlinecolor(YELLOW);
	setfillcolor(BLUE);
	fillcircle(i[circlenum],j[circlenum],50);
	BeginBatchDraw();
	//圆在45度移动
	while(true)
	{		
		//消除旧圆
		setlinecolor(BLACK);
		setfillcolor(BLACK);
		fillcircle(i[circlenum],j[circlenum],50);

		//改变坐标
		i[circlenum]=i[circlenum]+x[circlenum];
		j[circlenum]=j[circlenum]+y[circlenum];
		
		//重绘新圆
		setlinecolor(YELLOW);
		setfillcolor(BLUE);
		fillcircle(i[circlenum],j[circlenum],50);

		//边界检测
		if(i[circlenum]-50<0 || i[circlenum]+50>640 )
			//反弹
			x[circlenum]=-x[circlenum];
		if(j[circlenum]-50<0 || j[circlenum]+50>480)
			y[circlenum]=-y[circlenum];
		circlenum++;
		if(circlenum>MAXCIRCLE-1)
			circlenum=0;
		FlushBatchDraw();
		Sleep(2);
	}
	EndBatchDraw();
	getch();
	//关闭图形窗口
	closegraph();
}



 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值