C语言项目:绘制函数(实现动画效果)

绘制一个函数动画。
要求:1.碰到边界反弹;
2.会像贪吃蛇一样消失。

老师已给出提示代码:三个辅助函数

#include<stdio.h>
#include<windows.h>
#include<math.h>
// two functions to clear screen and set cursor position for printing.
// one function to take a rest in 10 microseconds.

// use this function to clear screen
void clear()//清屏
{
#if defined _WIN32
    system("cls");
#elif defined (__LINUX__) || defined(__gnu_linux__) || defined(__linux__)
    system("clear");
#elif defined (__APPLE__)
    system("clear");
#endif
}

#if defined _WIN32
    #include <conio.h>
    #include <windows.h>
    void setcursor(int x, int y)
    {
        HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
        COORD setps;
        setps.X = x; setps.Y = y;
        SetConsoleCursorPosition(hCon, setps);
    }
#endif


// use this function to positioning before calling printf  
void gotoxy(int x, int y)//光标移动到x,y处
{
#if defined _WIN32
    setcursor(y - 1, x - 1);
#elif defined (__LINUX__) || defined(__gnu_linux__) || defined(__linux__)
    printf("\033[%d;%dH", x, y);
#elif defined (__APPLE__)
    printf("\033[%d;%dH", x, y);
#endif
}

int factor = 10;

// rest msec * 10 microseconds
void rest(int msec)//当前画面静止一定时间
{
#if defined _WIN32
    Sleep(msec * factor);
#elif defined (__LINUX__) || defined(__gnu_linux__) || defined(__linux__)
    usleep(msec * 1000 * factor);
#elif defined (__APPLE__)
    usleep(msec * 1000* factor);
#endif
}

其中clear()函数清屏;gotoxy()函数使光标移动到指定位置;rest()当前画面静止。

思路
1.动画绘制
动画的原理是一帧帧的图片有短暂的时间间隔,连起来放映之后就产生了会动的感觉。我们可以每打印一个点之后rest()一小段时间来形成动画的效果。

2.边界反弹
即为碰到边界反向输出。我想的方法是纵坐标y1不变,只需要设计分段函数让x反向即可。

3.延时消失
要求的消失是像贪吃蛇一样,前面一边画着点,后面尾巴一边消失。但自己怎么想都只能做到全部绘制完成之后再让它开始消失。
请教助教与Mc大佬之后得知他的思路是保留点数。屏幕中最多出现50个点,第51个点之后,打印第x个点时,x-50号点打印空格覆盖,让他消失。

注意事项
1.审题。给定的gotoxy()函数的横纵坐标是反着的,而且从左上角开始为(1,1)。
2.rest()的用法非常灵活,比如高人指点:用户可以输入打印速度speed,rest(100-speed)。(妙啊)
3.程序中出现几段几乎完全相似的代码(除了字母不同),“代码复制”是程序不良的表现。因为将来做修改、维护的时候要维护很多处。所以比起重复一段好多遍,使用函数会更好。

感谢Mc、fOrever_jAckY、olinr1222、–_–等人指点!
我の代码

int zong(int i)//纵坐标y1
{
	double x=(i-51);
	double y=12.0*sin(x/7);
	int y1=(int)(15-y);
	return y1;
}

int heng(int i)//横坐标j
{
	int j=i;
	if(i>101&&i<=202)j=203-i;
	else if(i>202&&i<=303)j=i-202;
	else if(i>303&&i<=404)j=405-i;
	return j;
}

void dayin(char ch,int j,int y1)//在(y1,j)处输出给定字符
{
		gotoxy(y1,j);
		if(ch==' ')
		{
			if(y1==15)printf("-");
			else if(j==51)printf("|");
			else printf(" ");
		}
		else printf("#");
		gotoxy(29-y1,102-j);
		if(ch==' ')//防止坐标点和坐标轴被空格覆盖
		{
			if(29-y1==15)printf("-");
			else if(102-j==51)printf("|");
			else if(29-y1==26&&102-j==49)printf("-");
			else if((29-y1==26&&102-j==50)||(29-y1==3&&102-j==50))printf("1");
			else if(29-y1==16&&102-j==100)printf("X");
			else printf(" ");
		}
		else printf("#");
		gotoxy(1,1);
}




int main()
{
	float x,y;
	int tim=20;
	system("color F0");
	system("mode con cols=101 lines=30");
	clear();
	
	for(int i=0;i<=100;i++)//打印x轴 
	{
		gotoxy(15,i+1);
		if(i!=50)printf("-");
		else printf("+");
		gotoxy(1,1);
		rest(1);
	}
	
	for(int i=29;i>=0;i--)//打印y轴 
	{
		gotoxy(i+1,51);
		if(i!=14)printf("|");
		gotoxy(1,1); 
		rest(1);
	}
	//打印轴上的点
	gotoxy(16,52); printf("O");
	
	gotoxy(16,100);printf("X");
	
	gotoxy(1,49);printf("Y");
	
	gotoxy(3,50);printf("1");
	
	gotoxy(26,49);printf("-1");
	
	
	
	
	int i=0,j,y1;

	for(i=52;i<=404;i++)//打印函数 
	{
		j=heng(i),y1=zong(i);
		dayin('#',j,y1);
		rest(4);
		
		if(i-51-tim>0)
		{
			y1=zong(i-tim);
			j=heng(i-tim);
			dayin(' ',j,y1);
		}
	}	
	for(int r=i-tim;r<=i;r++)
	{
		j=heng(r);
		y1=zong(r);
		dayin(' ',j,y1);
		rest(4);
	} 
	gotoxy(1,1);
	rest(1000);
	return 0;
}
  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
代码: #include<graphics.h> #include<stdio.h> #include<math.h> #include<dos.h> #define PI 3.1415926 /*定义常量*/ #define UP 0x4800 /*上移↑键:修改时间*/ #define DOWN 0x5000 /*下移↓键:修改时间*/ #define ESC 0x11b /*ESC键 : 退出系统*/ #define TAB 0xf09 /*TAB键 : 移动光标*/ /*函数声明*/ int keyhandle(int,int); /*键盘按键判断,并调用相关函数处理*/ int timeupchange(int); /*处理上移按键*/ int timedownchange(int); /*处理下移按键*/ int digithour(double); /*将double型的小时数转换成int型*/ int digitmin(double); /*将double型的分钟数转换成int型*/ int digitsec(double); /*将double型的秒钟数转换成int型*/ void digitclock(int,int,int ); /*在指定位置显示时钟或分钟或秒钟数*/ void drawcursor(int); /*绘制一个光标*/ void clearcursor(int);/*消除前一个光标*/ void clockhandle(); /*时钟处理*/ double h,m,s; /*全局变量:小时,分,秒*/ double x,x1,x2,y,y1,y2; /*全局变量:坐标值*/ struct time t[1];/*定义一个time结构类型的数组*/ main() { int driver, mode=0,i,j; driver=DETECT; /*自动检测显示设备*/ initgraph(&driver, &mode, "");/*初始化图形系统*/ setlinestyle(0,0,3); /*设置当前画线宽度和类型:设置三点宽实线*/ setbkcolor(0);/*用调色板设置当前背景颜色*/ setcolor(9); /*设置当前画线颜色*/ line(82,430,558,430); line(70,62,70,418); line(82,50,558,50); line(570,62,570,418); line(70,62,570,62); line(76,56,297,56); line(340,56,564,56); /*画主体框架的边直线*/ /*arc(int x, int y, int stangle, int endangle, int radius)*/ arc(82,62,90,180,12); arc(558,62,0,90,12); setlinestyle(0,0,3); arc(82,418,180,279,12); setlinestyle(0,0,3); arc(558,418,270,360,12); /*画主体框架的边角弧线*/ setcolor(15); outtextxy(300,53,"CLOCK"); /*显示标题*/ setcolor(7); rectangle(342,72,560,360); /*画一个矩形,作为时钟的框架*/ setwritemode(0); /*规定画线的方式。mode=0, 则表示画线时将所画位置的原来信息覆盖*/ setcolor(15); outtextxy(433,75,"CLOCK");/*时钟的标题*/ setcolor(7); line(392,310,510,310); line(392,330,510,330); arc(392,320,90,270,10); arc(510,320,270,90,10); /*绘制电子动画时钟下的数字时钟的边框架*/ /*绘制数字时钟的时分秒的分隔符*/ setcolor(5); for(i=431;i<=470;i+=39) for(j=317;j<=324;j+=7){ setlinestyle(0,0,3); circle(i,j,1); /*以(i, y)为圆心,1为半径画圆*/ } setcolor(15); line(424,315,424,325); /*在运行电子时钟前先画一个光标*/ /*绘制表示小时的圆点*/ for(i=0,m=0,h=0;i<=11;i++,h++){ x=100*sin((h*60+m)/360*PI)+451; y=200-100*cos((h*60+m)/360*PI); setlinestyle(0,0,3); circle(x,y,1); } /*绘制表示分钟或秒钟的圆点*/ for(i=0,m=0;i<=59;m++,i++){ x=100*sin(m/30*PI)+451; y=200-100*cos(m/30*PI); setlinestyle(0,0,1); circle(x,y,1); } /*在电子表的左边打印帮助提示信息*/ setcolor(4); outtextxy(184,125,"HELP"); setcolor(15); outtextxy(182,125,"HELP"); setcolor(5); outtextxy(140,185,"TAB : Cursor move"); outtextxy(140,225,"UP : Time ++"); outtextxy(140,265,"DOWN: Time --"); outtextxy(140,305,"ESC : Quit system!"); outtextxy(140,345,"Version : 2.0"); setcolor(12); outtextxy(150,400,"Nothing is more important than time!"); clockhandle();/*开始调用时钟处理程序*/ closegraph(); /*关闭图形系统*/ return 0; /*表示程序正常结束,向操作系统返回一个0值*/ } void clockhandle() { int k=0,count; setcolor(15); gettime(t);/*取得系统时间,保存在time结构类型的数组变量中*/ h=t[0].ti_hour; m=t[0].ti_min; x=50*sin((h*60+m)/360*PI)+451; /*时针的x坐标值*/ y=200-50*cos((h*60+m)/360*PI); /*时针的y坐标值*/ line(451,200,x,y);/*在电子表中绘制时针*/ x1=80*sin(m/30*PI)+451; /*分针的x坐标值*/ y1=200-80*cos(m/30*PI); /*分针的y坐标值*/ line(451,200,x1,y1); /*在电子表中绘制分针*/ digitclock(408,318,digithour(h)); /*在数字时钟中,显示当前的小时值*/ digitclock(446,318,digitmin(m)); /*在数字时钟中,显示当前的分钟值*/ setwritemode(1); /*规定画线的方式,如果mode=1,则表示画线时用现在特性的线 与所画之处原有的线进行异或(XOR)操作,实际上画出的线是原有线与现在规定 的线进行异或后的结果。因此, 当线的特性不变, 进行两次画线操作相当于没有 画线,即在当前位置处清除了原来的画线*/ for(count=2;k!=ESC;){ /*开始循环,直至用户按下ESC键结束循环*/ setcolor(12);/*淡红色*/ sound(500);/*以指定频率打开PC扬声器,这里频率为500Hz*/ delay(700);/*发一个频率为500Hz的音调,维持700毫秒*/ sound(200);/*以指定频率打开PC扬声器,这里频率为200Hz*/ delay(300); /*以上两种不同频率的音调,可仿真钟表转动时的嘀哒声*/ nosound(); /*关闭PC扬声器*/ s=t[0].ti_sec; m=t[0].ti_min; h=t[0].ti_hour; x2=98*sin(s/30*PI)+451; /*秒针的x坐标值*/ y2=200-98*cos(s/30*PI); /*秒针的y坐标值*/ line(451,200,x2,y2); /*绘制秒针*/ /*利用此循环,延时一秒*/ while(t[0].ti_sec==s&&t[0].ti_min==m&&t[0].ti_hour==h) { gettime(t);/*取得系统时间*/ if(bioskey(1)!=0){ k=bioskey(0); count=keyhandle(k,count); if(count==5) count=1; } } setcolor(15); digitclock(485,318,digitsec(s)+1);/*数字时钟增加1秒*/ setcolor(12); /*淡红色*/ x2=98*sin(s/30*PI)+451; y2=200-98*cos(s/30*PI); line(451,200,x2,y2); /*用原来的颜色在原来位置处再绘制秒针,以达到清除当前秒针的目的*/ /*分钟处理*/ if(t[0].ti_min!=m){ /*若分钟有变化*/ /*消除当前分针*/ setcolor(15); /*白色*/ x1=80*sin(m/30*PI)+451; y1=200-80*cos(m/30*PI); line(451,200,x1,y1); /*绘制新的分针*/ m=t[0].ti_min; digitclock(446,318,digitmin(m)); /*在数字时钟中显示新的分钟值*/ x1=80*sin(m/30*PI)+451; y1=200-80*cos(m/30*PI); line(451,200,x1,y1); } /*小时处理*/ if((t[0].ti_hour*60+t[0].ti_min)!=(h*60+m)){ /*若小时数有变化*/ /*消除当前时针*/ setcolor(15); /*白色*/ x=50*sin((h*60+m)/360*PI)+451;/*50:时钟的长度(单位:像素),451:圆心的x坐标值*/ y=200-50*cos((h*60+m)/360*PI); line(451,200,x,y); /*绘制新的时针*/ h=t[0].ti_hour; digitclock(408,318,digithour(h)); x=50*sin((h*60+m)/360*PI)+451; y=200-50*cos((h*60+m)/360*PI); line(451,200,x,y); } } } int keyhandle(int key,int count) /*键盘控制 */ { switch(key) {case UP: timeupchange(count-1); /*因为count的初始值为2,所以此处减1*/ break; case DOWN:timedownchange(count-1); /*因为count的初始值为2,所以此处减1*/ break; case TAB:setcolor(15); clearcursor(count); /*清除原来的光标*/ drawcursor(count); /*显示一个新的光标*/ count++; break; } return count; } int timeupchange(int count) /*处理光标上移的按键*/ { if(count==1){ t[0].ti_hour++; if(t[0].ti_hour==24) t[0].ti_hour=0; settime(t); /*设置新的系统时间*/ } if(count==2){ t[0].ti_min++; if(t[0].ti_min==60) t[0].ti_min=0; settime(t); /*设置新的系统时间*/ } if(count==3){ t[0].ti_sec++; if(t[0].ti_sec==60) t[0].ti_sec=0; settime(t); /*设置新的系统时间*/ } } int timedownchange(int count) /*处理光标下移的按键*/ { if(count==1) { t[0].ti_hour--; if(t[0].ti_hour==0) t[0].ti_hour=23; settime(t);/*设置新的系统时间*/ } if(count==2) { t[0].ti_min--; if(t[0].ti_min==0) t[0].ti_min=59; settime(t);/*设置新的系统时间*/ } if(count==3) { t[0].ti_sec--; if(t[0].ti_sec==0) t[0].ti_sec=59; settime(t);/*设置新的系统时间*/ } } int digithour(double h)/*将double型的小时数转换成int型*/ {int i; for(i=0;i<=23;i++) {if(h==i) return i;} } int digitmin(double m)/*将double型的分钟数转换成int型*/ {int i; for(i=0;i<=59;i++) {if(m==i) return i;} } int digitsec(double s) /*将double型的秒钟数转换成int型*/ {int i; for(i=0;i<=59;i++) {if(s==i) return i;} } void digitclock(int x,int y,int clock)/*在指定位置显示数字时钟:时\分\秒*/ {char buffer1[10]; setfillstyle(0,2); bar(x,y,x+15,328); if(clock==60) clock=0; sprintf(buffer1,"%d",clock); outtextxy(x,y,buffer1); } void drawcursor(int count) /*根据count的值,画一个光标*/ {switch(count) { case 1:line(424,315,424,325);break; case 2:line(465,315,465,325);break; case 3:line(505,315,505,325);break; } } void clearcursor(int count) /*根据count的值,清除前一个光标*/ {switch(count) { case 2:line(424,315,424,325);break; case 3:line(465,315,465,325);break; case 1:line(505,315,505,325);break; } }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

灰海宽松

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

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

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

打赏作者

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

抵扣说明:

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

余额充值