您好,登錄后才能下訂單哦!
使用C語言怎么實現一個彈跳的小球?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
#include "graphics.h" #include "math.h" #define LEFT 0x4b00 #define RIGHT 0x4d00 #define ESC 0x011b int x,y,x1,y1,key,t1,t2,keyx,keylong,keymove,over=0,i,tt=0,fast; int ballx[9]={53,103,263,120,236,390,410,450,600}; int bally[9]={158,239,120,113,30,313,120,263,231},ballz[9]={0}; float ls; win() {float lx,ly,ll; for(i=0;i<9;i++) { if(ballz[i]==1)/*假如球體已經消失就不判定*/ continue; lx=(float)(ballx[i]-x); ly=(float)bally[i]-y; ll=lx*lx+ly*ly; ls=sqrt(ll); if((int)ls<17)/*判定兩球是否碰到*/ {tt++;/*記錄消失幾個球*/ fast-=50;/*速度加快*/ if(i%2==0)/*移動加快*/ keymove+=5; else { setcolor(0); rectangle(keyx,460,keyx+keylong,470); keylong-=10;/*長條變短*/ setcolor(YELLOW); rectangle(keyx,460,keyx+keylong,470); } ballz[i]=1; setcolor(0); circle(ballx[i],bally[i],10); if(y<bally[i]) if(x<ballx[i]) {t1=-1;t2=-1;} else {t1=1;t2=-1;} else if(x<ballx[i]) {t1=-1;t2=1;} else {t1=1;t2=1;} } } } fungo() { x=100,y=50,keyx=200,keylong=100,keymove=15,fast=5000; for(i=0;i<9;i++) { setcolor(i+1); circle(ballx[i],bally[i],10); } sleep(1); t1=t2=1; while(1) { while(!kbhit()) { setcolor(YELLOW); rectangle(keyx,460,keyx+keylong,470); x1=x; y1=y; x+=t1; y+=t2; setcolor(4); circle(x,y,6); delay(fast); setcolor(0); circle(x,y,6); win(); if(tt==9)/*勝利*/ { cleardevice(); over=1; setcolor(RED); settextstyle(0,0,4); setusercharsize(3,1,3,1); outtextxy(200,200," YOU WIN! "); break; } if(y>480)/*失敗*/ { cleardevice(); over=1; setcolor(RED); settextstyle(0,0,4); setusercharsize(3,1,3,1); outtextxy(200,200,"GAME OVER"); break; } else if(x==11&&y==11)/*判定碰到東西*/ {t1=1;t2=1;} else if(x==629&&y==11) {t1=-1;t2=1;} else if(x==11) { if(y<y1) {t1=1;t2=-1;} else {t1=1;t2=1;} } else if(y==11) { if(x<x1) {t1=-1;t2=1;} else {t1=1;t2=1;} } else if(x==629) { if(y1>y) {t1=-1;t2=-1;} else {t1=-1;t2=1;} } else if(y==449) { if(x>=keyx&&x<=keyx+keylong) { if(x<(keyx+keylong/4)) {t1=-1;t2=-1;} else if(x>=(keyx+keylong/4)&&x<(keyx+keylong/2)) {t1=-1;t2=-2;} else if(x>=(keyx+keylong/2)&&x<(keyx+keylong/4*3)) {t1=1;t2=-2;} else {t1=1;t2=-1;} } } } if(over) break; key=bioskey(0); setcolor(0); rectangle(keyx,460,keyx+keylong,470); if(key==ESC) {cleardevice();break;} else if(key==LEFT) keyx-=keymove; else if(key==RIGHT) keyx+=keymove; } } main() {int gd=DETECT,gr; initgraph(&gd,&gr,"c:\\tc"); fungo(); getch(); sleep(1); closegraph(); }
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。