这篇文章主要讲解了C语言实现乒乓球比赛的实例分析,内容清晰明了,对此有兴趣的小伙伴可以学习一下,相信大家阅读完之后会有帮助。
1)、基本要求
用8个LED发光管(红黄绿)的来回滚动显示来模拟打乒乓球时乒乓球在两边球台上的来回运动。比赛双方用按钮/开关(双方各用1个按钮/开关)的方法来模拟发球与接球,即发球方按动其控制的按钮/开关/健,球从发球方一侧向对方运动(LED发光管从发球方到对方逐个点亮,滚动显示),当球运动至接球方时,接球方立即按动其控制的按钮/开关/键,“击球”使球“弹回”发球方一侧,如此周而复始,直至在规定的击球时间内有一方未能完成击球动作,从而对方得一分,然后继续比赛。比赛规则可参照一般的乒乓球比赛规则。
要求:
(1). 球以每0.5秒滚过1个LED的速度移动;
(2). 回球需在球到达后的0.5秒内进行(按下按钮),否则按失球计;
(3). 球未到达提前按下按钮按失球处理,不起回球作用;
(4). 用扩展的4个七段数码(小键盘上)为双方记分(每方2个八段码, ,按10进制计数,初始值00:00,可自行设计显示及规则)
2)、设计提示
球到达接球方后,立即读接球方的按钮状态,若未按则有效(若已按下则按失球计),然后不断读此按钮状态,直到按下代表“击球”动作。(采用中断,可以用同一个按键轮流按动识别)。
3)、进一步设计要求
(1). 在8×8点阵(虚拟大屏)同步显示球移动情况,设计显示输赢球图形状态、效果等;移动、击球等精确(中断)计时;
(2). 通过拨动开关或键盘设定改变球移动速度,即每个LED管点亮时间分别为0.5秒、0.3秒、0.2秒;通过拨动开关或键盘设定改变允许击球时间0.3秒、0.2秒、0.1秒;球移动速度取决于击球时间,即球移动速度(每个LED点亮时间)在0.2~0.5秒内可变,具体取决于球到达接球方后击球所花的时间0~0.5秒如花费的时间短则回球后球移动速度快反之回球后移动
#include "interface.h" void init8255(void); const unsigned short Port8255Base=0x288; const unsigned char ControlWord8255=0x80; Input/Output.PortA:Output unsigned char gData=0x80; int flag=0; int num=0; const unsigned char ControlWord8255B = 0x82; const unsigned char ControlWord8255E = 0x88; void init8255B(void); int scoreleft,scoreright; int speedlamp; int speed2; int speed3; void turnleft() { gData=1; while(1) { PortWriteByte(Port8255Base,gData); if(gData==128) { flag=0; break; } gData*=2; sleep(speedlamp); } } void turnright() { gData=128; while(1) { PortWriteByte(Port8255Base,gData); if(gData==1) { flag=1; break; } gData/=2; sleep(speedlamp); } } unsigned char led[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; BYTE pdata; BYTE reflect; void main( ) { init8255B(); //int speedlamp=500; BYTE controldudu; PortReadByte(Port8255Base + 2, &controldudu); printf("%d \n",controldudu); controldudu/=32; int k5=controldudu%2; controldudu/=2; int k6=controldudu%2; if(k6==0) speedlamp=500; else speedlamp=50; if(k5==0) speed2=500; else speed2=1; printf("%d %d\n",k6,k5); pdata/=16; scoreleft=0; scoreright=0; BYTE pdata=0; int k0=0; int k7=0; int score0=0; int score1=0; BYTE old=0; BYTE now; int k0old,k0new,k7old,k7new; k0old=old%2; k7old=old/8; while(1) { printf("\n当前分数:%d : %d\n",scoreleft,scoreright); int yyy=5; while(yyy--){ PortWriteByte(Port8255Base+2,0x01); PortWriteByte(Port8255Base+1,led[scoreright%10]); sleep(1); //delay 100ms PortWriteByte(Port8255Base+2,0x02); PortWriteByte(Port8255Base+1,led[scoreright/10]); sleep(1); PortWriteByte(Port8255Base+2,0x04); PortWriteByte(Port8255Base+1,led[scoreleft%10]); sleep(1); //delay 100ms PortWriteByte(Port8255Base+2,0x08); PortWriteByte(Port8255Base+1,led[scoreleft/10]); sleep(1); PortWriteByte(Port8255Base+2,0); } PortReadByte(Port8255Base + 2, &pdata); pdata/=16; sleep(500); printf("--%d",pdata); printf("old:%d new:%d\n",old,pdata); if(old==pdata) { continue; } else { k7old=old/8; k0old=old%2; k0new=pdata%2; k7new=pdata/8; if(k7old!=k7new) { //printf("---here---\n"); again: turnright(); old=pdata; k0old=old%2; PortReadByte(Port8255Base + 2, &now); now/=16; k0old=now%2; if(speed2>50) { sleep(200); PortReadByte(Port8255Base + 2, &reflect); reflect/=16; if(!(reflect==pdata)) { pdata=reflect; speedlamp=200; goto r1; } else { sleep(100); PortReadByte(Port8255Base + 2, &reflect); reflect/=16; if(!(reflect==pdata)) { pdata=reflect; speedlamp=1000; goto r1; } else { sleep(100); PortReadByte(Port8255Base + 2, &reflect); reflect/=16; if(!(reflect==pdata)) { pdata=reflect; speedlamp=4000; goto r1; } else { sleep(100); goto r2; } } } } else sleep(speed2); r2: PortReadByte(Port8255Base + 2, &pdata); pdata/=16; r1: k0new=pdata%2; printf("speedlamp:%d\n",speedlamp); if(!(old!=pdata&&k0old!=k0new)) { printf("\n--right lose--\n"); scoreleft++; sleep(500); PortReadByte(Port8255Base + 2, &pdata); pdata/=16; old=pdata; k7old=old/8; k0old=old%2; k0new=pdata%2; k7new=pdata/8; } else { printf("\nback\n"); turnleft(); old=pdata; k7old=old/8; PortReadByte(Port8255Base + 2, &now); k7old=now/128; if(speed2>50) { sleep(200); PortReadByte(Port8255Base + 2, &reflect); reflect/=16; if(!(reflect==pdata)) { pdata=reflect; speedlamp=200; goto r3; } else { sleep(100); PortReadByte(Port8255Base + 2, &reflect); reflect/=16; if(!(reflect==pdata)) { pdata=reflect; speedlamp=1000; goto r3; } else { sleep(100); PortReadByte(Port8255Base + 2, &reflect); reflect/=16; if(!(reflect==pdata)) { pdata=reflect; speedlamp=4000; goto r3; } else { sleep(100); goto r4; } } } } else sleep(speed2); r4: PortReadByte(Port8255Base + 2, &pdata); pdata/=16; r3: k7new=pdata/8; if(!(old!=pdata&&k7old!=k7new)) { printf("\n--left lose--\n"); scoreright++; } else { goto again; } } } else if(k0old!=k0new) { again2: turnleft(); old=pdata; k7old=old/8; PortReadByte(Port8255Base + 2, &now); k7old=now/128; if(speed2>50) { sleep(200); PortReadByte(Port8255Base + 2, &reflect); reflect/=16; if(!(reflect==pdata)) { pdata=reflect; speedlamp=200; goto r7; } else { sleep(100); PortReadByte(Port8255Base + 2, &reflect); reflect/=16; if(!(reflect==pdata)) { pdata=reflect; speedlamp=1000; goto r7; } else { sleep(100); PortReadByte(Port8255Base + 2, &reflect); reflect/=16; if(!(reflect==pdata)) { pdata=reflect; speedlamp=2000; goto r7; } else { sleep(100); goto r8; } } } } sleep(speed2); r8: PortReadByte(Port8255Base + 2, &pdata); pdata/=16; r7: k7new=pdata/8; if(!(old!=pdata&&k7old!=k7new)) { printf("\n--left lose--\n"); scoreright++; sleep(500); PortReadByte(Port8255Base + 2, &pdata); pdata/=16; old=pdata; k7old=old/8; k0old=old%2; k0new=pdata%2; k7new=pdata/8; } else { printf("\nback\n"); turnright(); old=pdata; k7old=old/8; PortReadByte(Port8255Base + 2, &now); now/=16; k0old=now%2; if(speed2>50) { sleep(200); PortReadByte(Port8255Base + 2, &reflect); reflect/=16; if(!(reflect==pdata)) { pdata=reflect; speedlamp=200; goto r5; } else { sleep(100); PortReadByte(Port8255Base + 2, &reflect); reflect/=16; if(!(reflect==pdata)) { pdata=reflect; speedlamp=100; goto r5; } else { sleep(100); PortReadByte(Port8255Base + 2, &reflect); reflect/=16; if(!(reflect==pdata)) { pdata=reflect; speedlamp=2000; goto r5; } else { sleep(100); goto r6; } } } } sleep(speed2); r6: PortReadByte(Port8255Base + 2, &pdata); pdata/=16; r5: k7new=pdata/8; if(!(old!=pdata&&k0old!=k0new)) { printf("\n--right lose--\n"); scoreleft++; } else { goto again2; } } } } } Cleanup(); } void init8255(void) { if (!Startup()) { printf("\n\n ERROR: Open Device Error!请打开实验箱电源\n"); _getch(); exit(0); } PortWriteByte(Port8255Base + 3, ControlWord8255); } void init8255B(void) { if (!Startup()) { printf("\n\n ERROR: Open Device Error!请打开实验箱电源\n"); _getch(); exit(0); // return to Windows } PortWriteByte(Port8255Base + 3, ControlWord8255E); }
看完上述内容,是不是对C语言实现乒乓球比赛的实例分析有进一步的了解,如果还想学习更多内容,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。