用c語言編的射箭游戲代碼18_第1頁
用c語言編的射箭游戲代碼18_第2頁
用c語言編的射箭游戲代碼18_第3頁
用c語言編的射箭游戲代碼18_第4頁
用c語言編的射箭游戲代碼18_第5頁
已閱讀5頁,還剩27頁未讀 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領

文檔簡介

1、#include #include #include #include #include #include #define G9.8 /* 重力加速度 */#define PI3.141593 /*圓周率 */#define L160 /* 小屋運動的范圍 */#define T1100#define R1200#define B1450#define AMD17.8 /* 修訂數(shù) 7.8*/#define AMD21.78 /* 修訂數(shù) */* 鼠標信息宏定義 */#define WAITING 0xff00#define LEFTPRESS 0xff01#define LEFTCLICK

2、0xff10#define LEFTDRAG 0xff19#define RIGHTPRESS 0xff02#define RIGHTCLICK 0xff20#define RIGHTDRAG 0xff2a#define MIDDLEPRESS 0xff04#define MIDDLECLICK 0xff40#define MIDDLEDRAG 0xff4c#define MOUSEMOVE 0xff08int Keystate;int MouseExist;int MouseButton;int MouseX;int MouseY;int up1616,down1616,mouse_draw

3、1616,pixel_save1616;void MouseMath()/* 計算鼠標的樣子 */int i,j,jj,k;long UpNum16=0x3fff,0x1fff,0x0fff,0x07ff,0x03ff,0x01ff,0x00ff,0x007f,0x003f,0x00ff,0x01ff,0x10ff,0x30ff,0xf87f,0xf87f,0xfc3f;long DownNum16=0x00,0x7c00,0x6000,0x7000,0x7800,0x7c00,0x7e00,0x7f00,0x7f80,0x7e00,0x7c00,0x4600,0x0600,0x0300,0x

4、0300,0x0180;for(i=0;i=0;k-) upik=0;for(k=jj;k=0;k-) downik=0;for(k=0;k16;k+)/* 四種組合方式 */if(upik=0&downik=0) mouse_drawik=1;else if(upik=0&downik=1) mouse_drawik=2;else if(upik=1&downik=0) mouse_drawik=3;elsemouse_drawik=4; mouse_draw12=4;/* 特殊點 */* 鼠標光標顯示 */ void MouseOn()int x=MouseX,y=MouseY;int i

5、,j;int color;for(i=0;i16;i+)/* 畫鼠標 */for(j=0;j16;j+)pixel_saveij=getpixel(x+j,y+i);/* 保存原來的顏色 */if(mouse_drawij=1) putpixel(x+j,y+i,0);else if(mouse_drawij=2)putpixel(x+j,y+i,15);/* 隱藏鼠標 */void MouseOff()int i,j,x,y,color;x=MouseX;y=MouseY;for(i=0;i16;i+)/* 原位置異或消去 */ for(j=0;jrcRKpCRTn c曰KTB眩鋼怒 *A(

6、cocoxoKn9u* ZOXOHXV xfxd xlrXOIMX 4UX一 4)xps snolAI poLco_ 9*A(cocoxoKn9u6&0X0HXVI fxq xhxoima -u 一X4)AXPS snow por晅鳥叵蓋 晅鳥叵B 鋼皚屁汕B眩鋼怒*A(cocoxoKn9u66O0X0HXVMPHXQI/* 是否按下左鍵返回值 :1=按下 0=釋放 */int LeftPress()_AX=0x03;geninterrupt(0x33);return(_BX&1);/* 是否按下中鍵返回值同上 */int MiddlePress()_AX=0x03;geninterrupt(

7、0x33);return(_BX&4);/* 是否按下右鍵返回值同上 */int RightPress()_AX=0x03;geninterrupt(0x33);return(_BX&2);/* 獲取鼠標當前位置 */void MouseGetXY()_AX=0x03;geninterrupt(0x33);MouseX=_CX;MouseY=_DX;/*鼠標按鍵情況,返回0表示只移動,返回1表示左右鍵同時 按下, 2 表示只按了左鍵, 3 表示只按了右鍵 */int MouseStatus()int x,y;int status;int press=0;int i,j,color;status

8、=0;/* 默認鼠標沒有移動 */x=MouseX;y=MouseY;while(x=MouseX&y=MouseY&status=0&press=0)if(LeftPress()&Righ tPress()press=1;else if(LeftPress()press=2;else if(RightPress()press=3;MouseGetXY();if(MouseX!=x|MouseY!=y)status=1;if(status)/* 移動情況才重新顯示鼠標 */for(i=0;i16;i+)/* 原位置異 或消去 */for(j=0;j16;j+)if(mouse_drawij=3

9、|mouse_drawij=4)continue;color=getpixel(x+j,y+i);putpixel(x+j,y+i,colorSolor);putpixel(x+j,y+i,pixel_saveij);MouseOn();/* 新位置顯示 */if(press!=0)/* 有按 鍵的情況 */return press;return 0;/* 只移動的情況 */* 定義玩家的結(jié)構(gòu)體 */struct Renint x,y;int life;int color;int lr;/*1 表示左, 2 表示右。 */;/* 繪制游戲界面 */void Desktop()setcolor(

10、14);line(320,0,320,480);rectangle(L1-20,T1-40,R1+20,B1+10);rectangle(640-(R1+20),(T1-40),640-(L1-20),B1+10);outtextxy(25,20,P1);outtextxy(345,20,P2);/*判斷點(x,y)是否在以(cx,cy)為圓心,以r為半徑的 圓內(nèi)*/*int PointCircle(int cx,int cy,int r,int *x,int *y)double g; if(cx-(*x)*(cx-(*x)+(cy-(*y)*(cy-(*y)=r*r) return 1;el

11、se g=sqrt(double)(*x-cx)*(*x-cx)+(*y-cy)*(*y- cy)/(double)(r);*x=(int)(double)(*x-cx)/g)+cx;*y=(int)(double)(*y-cy)/g)+cy;return 0; */* 把一個數(shù)字 n 轉(zhuǎn)換成字符串,并存儲在 a 中,帶符號 +-*/ void numtostr(int n,char a5)int w,e;e=n;n=abs(n);a3=(n%10)+0;w=n/10;a2=(w%10)+0;w=w/10;a1=(w%10)+0;a4=0;if(e0)a0=-;elsea0=+;/* 把速度和

12、角度裝換成字符串輸出 */void AngleSpeed(double s,double angle)int ss,aa;char zzs5,zza5;int left,top,right,bottom;left=275;top=50;right=left+90;bottom=top+10;ss=(int)(s);aa=(int)(angle)*180/PI);numtostr(ss,zzs);numtostr(aa,zza);setfillstyle(1,15);setcolor(10);bar(left,top,right,bottom);outtextxy(left+5,top+3,zz

13、s);outtextxy(left+right)/2+5,top+3,zza);circle(right-6,top+3,2);/* 實現(xiàn)人機對抗的函數(shù) */void Fire (inta4,double *v,double*angle,intn)/*a 數(shù)組存放對射的兩點, angle存放機器射擊的角度和速度,n表式機器射擊的準確度*/int t;double vx,vy;double sx,sy;int m;m=12*4/n;randomize();m=random(m)-m/2;t=20;sx=(double)(a2-a0);sy=(double)(a3-a1);vx=sx/(doubl

14、e)(t); vy=(sy-0.5*PI*(double)(t*t)/(double)(t); *angle=atan(-vy)/vx);*v=sqrt(vx*vx+vy*vy); *v=(*v)*(AMD2+0.01*(double)(m);AngleSpeed(*v,*angle);/* 繪制生命線的函數(shù) */void LifePicture(int life,int color,int location)char lm5; int l,t,r,b;l=50;t=20;r=l+200;b=t+10;numtostr(life,lm);setfillstyle(1,color);setcol

15、or(15);if(location=1|location=3)bar(l,t,r,b); setfillstyle(1,4);bar(l,t+(b-t)/4,l+life,t+3*(b-t)/4);setfillstyle(1,color);bar(r+10,t,r+50,b); outtextxy(r+10+5,t+2,lm);else l=320+50;r=l+200;bar(l,t,r,b);setfillstyle(1,4);bar(l,t+(b-t)/4,l+life,t+3*(b-t)/4);setfillstyle(1,color);bar(r+10,t,r+50,b); ou

16、ttextxy(r+10+5,t+2,lm);/* 繪制小屋的函數(shù) */void RenPicture(int x,int y,int color)setcolor(color); setwritemode(1);lin e(x,y-40,x-10,y-30);/* 畫頭 */line(x,y-40,x+10,y-30); line(x-10,y-30,x+10,y-30);lin e(x-5,y-30,x-5,y-10);/* 畫脖子 */ line(x+5,y-30,x+5,y-10);lin e(x-20,y-10,x+20,y-10);/* 畫身子 */line(x-20,y+10,x

17、+20,y+10);line(x-20,y-10,x-20,y+10);line(x+20,y-10,x+20,y+10);/*繪制箭的函數(shù) */void PictureBullets (int wx,int wy,int tx,int ty)setcolor(RED);line(wx,wy,tx,ty);line(wx-1,wy-1,tx,ty);line(wx+1,wy+1,tx,ty);/*繪制小屋上箭的函數(shù)*/void InitialArrow (int x,int y,int c4)int addx=0,addy=0;int a2,b2;int bowb=7;int bowx=2;d

18、ouble bow=80.0;bow=bow/2;a0=x;a1=y;b0=x;b1=y;if(c0=c2)&(c1=c3)addx=(c2-c0)/6; addy=(c3-c1)/6;PictureBullets(x+addx,y+addy,x-addx/4,y-addy/4);setcolor(bowb);line(a0,a1,b0,b1);setcolor(bowx);line(a0,a1,x+addx,y+addy);line(b0,b1,x+addx,y+addy);else addx=(c2-c0)/6;addy=(c3-c1)/6;PictureBullets(x+addx,y+

19、addy,x-addx/4,y-addy/4);if(addx=0&addy!=0)a0=b0=x;a1=y-(int)(bow);b1=y+(int)(bow);if(addx!=0&addy=0)a1=b1=y;a0=x-(int)(bow);b0=x+(int)(bow);if(addx!=0&addy!=0)a0=x+(int)(bow*sqrt(double)(addy) *(double)(addy)/(double)(addx)*(double)(addx)+(double)(addy)*(double)(addy);a1=y-(a0-x)*(addx)/addy;b0=x- (

20、int)(bow*sqrt(double)(addy)*(double)(addy)/(double)(addx)*(double)(addx)+(dou ble)(addy)*(double)(addy);b1=y-(b0-x)*(addx)/addy;setcolor(bowb);line(a0,a1,b0,b1);setcolor(bowx);line(a0,a1,x+addx,y+addy);line(b0,b1,x+addx,y+addy);/* 判斷點 qx,qy 在直線的什么位置 */int PointPlace(int qx,int qy,int x1,int y1,int x

21、2,int y2)/* 返回 0 表示在直線上, 當斜率存在時: 1 表示在直線的上面, 2 表示在直線的下面,當斜率不存在時: 3 表示在左面, 4 表示在右面 */int s;if(x1=x2)if(qxx1)return 4;elsereturn 0;else s=(int)(double)(y1-y2)/(double)(x1-x2)*(double)(qx- x1)+(double)(y1);if(qys)return 2;elsereturn 0;/* 根據(jù)兩點坐標計算出兩點距離和向量 (twoxy0-twoxy2,twoxy1-twoxy3) 與(vectorx,vectory)

22、的夾角。*/void DistanceAngle (int twoxy4,int vectorx,int vectory,double *distance,double *angle)double a,b;double s;if(twoxy0=twoxy2)&twoxy1=twoxy3)*distance=0;*angle=0;else b=(double)(double)(twoxy3-twoxy1)*(double)(twoxy3- twoxy1)+(double)(twoxy2-twoxy0)*(double)(twoxy2-twoxy0);b=sqrt(b);*distance=b;s

23、=(double)(double)(vectorx)*(double)(vectorx)+(double)(vectory)*(double)(vect ory);s=sqrt(s);a=(double)(double)(twoxy0-twoxy2)*(double)(vectorx)+(double)(twoxy1- twoxy3)*(double)(vectory);a=a/(b*s);a=acos(a);if(twoxy3=twoxy1)*angle=a;else *angle=a+(PI-a)*2;/* 由速度角度算 sx,sy 隨時間的變化 */void RelativePositi

24、on(int *sx,int *sy,double v,double angle,double t)*sx=(int)(v*cos(angle)*t);*sy=(int)(v*sin(angle)*t-0.5*G*t*t);/* 用鼠標畫一條直線,把直線的兩點坐 標放在 twoxy 數(shù)組內(nèi)。 */void TwoPoints(int twoxy4,int dx,int dy)int i,q=1;double speed=0.0,angle=0.0;twoxy0=0;twoxy1=0;setcolor(13);line(0,dy,640,dy);setcolor(4);MouseOn();/*

25、顯示鼠標 */setwritemode(1);i=0;while(q)if(i=1)MouseOff();DistanceAngle(twoxy,1,0,&speed,&angle);AngleSpeed(speed/AMD1,angle);InitialArrow (dx,dy,twoxy);setcolor(4); line(twoxy0,twoxy1,twoxy2,twoxy3);MouseOn(); if(twoxy2!=MouseX)|(twoxy3!=MouseY)twoxy2=MouseX; twoxy3=MouseY;/*PointCircle(twoxy0,twoxy1,50

26、,&(twoxy2),&(twoxy3);*/MouseOff(); DistanceAngle(twoxy,1,0,&speed,&angle);AngleSpeed(speed/AMD1,angle);InitialArrow (dx,dy,twoxy);setcolor (4); line(twoxy0,twoxy1,twoxy2,twoxy3); MouseOn(); if(MouseStatus()sound (1000);/*響聲函數(shù) */delay (100); nosound();delay (1000);delay (1000);delay (1000);delay (100

27、0);if(i=0)twoxy0=MouseX; twoxy1=MouseY;twoxy2=MouseX;twoxy3=MouseY;i=1;else MouseOff();DistanceAngle(twoxy,1,0,&speed,&angle);AngleSpeed(speed/AMD1,angle);InitialArrow (dx,dy,twoxy);setcolor(4);line(twoxy0,twoxy1,twoxy2,twoxy3);setcolor(13);line(0,dy,640,dy);q=0;i=0;/*發(fā)射箭,speedl和speed2控制速度,返回中彈位置*/i

28、nt Launch(int lx,int ly,int tx,int ty,int hm,int grade)double speed1=0.01;int speed2=1000;int a4;int xx2,xy2;double s=0.0,angle=0.0,t=0.0;lx=lx;ly=ly-40;if(hm=3)a0=lx;a1=ly;a2=tx;a3=ty;Fire (a,&s,&angle,grade);else TwoPoints(a,lx,ly);DistanceAngle(a,1,0,&s,&angle);s=s/AMD1;RelativePosition(&xx0,&xy0

29、,s,angle,t-1);RelativePosition(&xx1,&xy1,s,angle,t);for(t=0.0;ly-xy1480;t=t+speed1)RelativePosition(&xx0,&xy0,s,angle,t-1);RelativePosition(&xx1,&xy1,s,angle,t);if(PointPlace(lx+xx1,ly-xy1,tx,ty-40,tx+10,ty-30)=2&PointPlace(lx+xx1,ly- xy1,tx,ty-40,tx-10,ty-30)=2&PointPlace(lx+xx1,ly-xy1,tx-10,ty-30,

30、tx+10,ty- 30)=1)sound(4000);/*響聲函數(shù) */delay(100);nosound();return 1;if(PointPlace(lx+xx1,ly-xy1,tx-5,ty-30,tx+5,ty-30)=2&PointPlace(lx+xx1,ly-xy1,tx-5,ty-10,tx+5,ty-10)=1&PointPlace(lx+xx1,ly-xy1,tx-5,ty-30,tx-5,ty-10)=4&PointPlace(lx+xx1,ly-xy1,tx+5,ty-30,tx+5,ty-10)=3)sound(3000);/*響聲函數(shù) */delay(100

31、);nosound();19 / 31return 2;if(PointPlace(lx+xx1,ly-xy1,tx-20,ty-10,tx-20,ty+10)=4&PointPlace(lx+xx1,ly-xy1,tx+20,ty-10,tx+20,ty+10)=3&PointPlace(lx+xx1,ly-xy1,tx-20,ty-10,tx+20,ty-10)=2&PointPlace(lx+xx1,ly-xy1,tx-20,ty+10,tx+20,ty+10)=1)sound(2000);/*響聲函數(shù) */delay(100);nosound();return 3;if(ly-xy11

32、)delay(speed2);continue;if(lx+xx1640-1)return 0;PictureBullets (lx+xx0,ly- xy0,lx+xx1,ly-xy1);delay(speed2);PictureBullets (lx+xx0,ly-xy0,lx+xx1,ly-xy1);return 0;/* 小屋移動的函數(shù) */int MoveRen(struct Ren *p)int a,k=19200,b=0,d;int q=1;randomize();for(;q;)if(b=1)p-lr=3;RenPicture(p-x,p-y,p-color);if(p-lr=3

33、)b=1;delay(100);delaydelay (100); delay (100); delay (100); delay (100); /sleep (1); d=random (10); if(d=0) k=19200; if(d=1) k=19712; if(d=2) k=18432; if(d=3) k=20480; if(d=4)k=7181;p-lr=1; else k=bioskey(0);RenPicture(p-x,p-y,p-color);switch(k)case 19200: /* 按向左鍵 */a=(p-x)-5;if(p-lr=1)if(aL1&ax=a;b

34、reak;else if(a640-R1&ax=a;break;break;case 19712: /* 按向右鍵 */a=(p-x)+5;if(p-lr=1)if(aL1&ax=a;break;else if(a640-R1&ax=a;break;break;case 18432: /* 按向上鍵 */a=(p-y)-5;if(p-lr=1)if(aT1&ay=a;21 / 31break;else if(aT1&ay=a;break;break;case 20480: /*按向下鍵 */a=(p-y)+5;if(aT1&ay=a;break;case 7181: /*enter 鍵的掃描碼

35、 */if(b=1)p-lr=3;q=0;break;case 283:return 0;RenPicture(p-x,p-y,p-color);return 1;/* 游戲開始前畫面 */3 表示單int GameStar()/*返回1表示單人游戲初級,2表示單人游戲中級, 人游戲高級, 4 表示兩人對戰(zhàn), 5 表示退出游戲 */int q,k,h=0;for(;1;)q=1;cleardevice();/* 清屏函數(shù) */setcolorsettextstyle(0,0,5);outtextxy(100,100,Start Game!);settextstyle(0,0,1);outtex

36、txy(20,300,keys used:);Arrow keys);The left mouse button)Enter);Esc to Quit!);outtextxy(20,300,outtextxy(20,310,outtextxy(20,320, outtextxy(20,330, setcolor (5);outtextxy(250,400,One player!);outtextxy(250,420,Two players!);outtextxy(250,440,Quit!);setwritemode(1);setcolor(6); rectangle(245,395+h*20

37、,345,415+h*20); for(;q;)setcolor(6);k=bioskey(0);sound(1000);/*響聲函數(shù) */delay(100);nosound();if(k=20480)rectangle(245,395+h*20,345,415+h*20);h=(h+1)%3;rectangle(245,395+h*20,345,415+h*20);else if(k=7181)if(h=0)/*單人游戲,選擇等級 */cleardevice();/* 清屏函數(shù) */ setcolor(2);outtextxy(20,30,Esc to back!);outtextxy(2

38、50,240,Lower);outtextxy(250,260,Middle);outtextxy(250,280,Higher);setcolor(4);rectangle(245,235+h*20,300,255+h*20);for(;q;)k=bioskey(0);sound(1000);/*響聲函數(shù) */delay(100);nosound();if(k=20480)rectangle(245,235+h*20,300,255+h*20);h=(h+1)%3;rectangle(245,235+h*20,300,255+h*20);else if(k=7181)return h+1;e

39、lse if(k=283)h=0;k=1;q=0;break;elseif(h=1)/*兩人對抗*/return 4;if(h=2)/* 退出游戲 */return 5;else if(k=283)return 5;else/* 退出游戲畫面 */void GameOver()cleardevice();/*清屏函數(shù) */ setcolor(14);settextstyle(0,0,6);outtextxy(100,200,Game Over!);settextstyle(1,0,1);outtextxy(400,400,Producer:Chen);outtextxy(400,410,QQ:7);o

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論