百科知识

用c语言编一个小游戏

2006-06-20 22:26:26w***
用c语言编一个小游戏:在不同机子上编译时需修改init()函数中TC路径 #include "graphics.h" #include "stdlib.h" ?

最佳回答

  •   在不同机子上编译时需修改init()函数中TC路径 #include "graphics。h" #include "stdlib。h" #include "math。h" #include "bios。h" #define pi 3。
      14 #define r 10 #define right 8292 #define left 7777 #define esc 283 #define pause 14624 struct ball { int x,y; int oldx,oldy; int derection;/*球的方向采用角度制,范围是0-360度*/ }ball; struct platform { int x,y; unsigned key; int oldx,oldy; }platform; int score=0;/*得分*/ int speed=1;/*速度*/ unsigned key; void init(); void base(); void scoreview(); void play(); double change(int x); void gameover(); void close(); void main() { init(); base(); scoreview(); play(); getch(); close(); } void init()/*初始化图形模式*/ { int gm=DETECT,gd; initgraph(&gm,&gd,"G:\\tc\\bgi");/*根据不同的TC进行路径修改*/ } void close()/*关闭图形模式*/ { closegraph(); } void base()/*画边框,球以及板并且对球和板的各参数初始化*/ { int i; setbkcolor(GREEN); setcolor(8); setfillstyle(4,LIGHTGRAY); rectangle(149,149,501,351); rectangle(140,140,510,360); floodfill(141,141,8); setfillstyle(9,BLUE); platform。
      x=300; platform。y=343; bar(platform。x,platform。y,platform。x+40,platform。y+5); randomize(); ball。x=random(330)+160; ball。
      y=160; rection=random(30)+280; setfillstyle(1,YELLOW); setcolor(YELLOW); fillellipse(ball。x,ball。y,r,r); } void scoreview()/*显示当前的得分数*/ { char sco[20]; setcolor(WHITE); setfillstyle(1,YELLOW); bar(300,50,440,80); sprintf(sco,"score:%d",score); setcolor(RED); settextstyle(0,0,2); outtextxy(310,60,sco); } void play() { int i=5; settextstyle(0,0,2); setcolor(11); outtextxy(50,400,"'A'for left 'D'for right");/*提示各个控制键*/ outtextxy(50,450,"'space'for pause 'ESC' for quit"); setcolor(RED); setfillstyle(1,GREEN); while(!kbhit())/*开始之前提示按任意键进入游戏,且提示不停闪烁*/ { outtextxy(100,100,"press any key to begin!"); delay(400); bar(100,100,500,120); delay(400); } while(1) { while(!kbhit()) { ball。
      oldx=ball。x; ball。oldy=ball。y;/*记录下当前球的位置*/ /*根据球的方向来利用角度的正切值来改变球的坐标*/ if( rection>0&& rection90&& rection180&& rection270&& rection=platform。
      y)/*如果球到底有两种情况*/ {if(ball。x+r-platform。x40) /*球不在板上*/ { ball。y=platform。y-r+7; setfillstyle(1,GREEN); setcolor(GREEN); fillellipse(ball。
      oldx,ball。oldy,r,r); /*球在游戏结束处闪烁五次*/ while(i) { setfillstyle(1,YELLOW); setcolor(YELLOW); fillellipse(ball。
      x,ball。y,r,r); delay(300); setfillstyle(1,GREEN); setcolor(GREEN); fillellipse(ball。x,ball。
      y,r,r); delay(300); i--; } gameover();/*游戏结束*/ } else { ball。y=platform。
      y-r; rection= rection;/*球在板上的话得一分,且速度加一但不能超过5*/ score++; speed++; if(speed>5) speed=5; scoreview();/*刷新得分*/ } } /*如果球撞墙或板后修正坐标并改变方向*/ if(ball。
      y-r180&& rection90&& rection500) { ball。x=500-r; if( rection>270&& rection0&& rection460) platform。
      x=460;/*对底板坐标修正*/ setfillstyle(1,GREEN); bar(platform。oldx,platform。y,platform。oldx+40,platform。y+5);/*在先前位置抹掉底板*/ setfillstyle(9,BLUE); bar(platform。
      x,platform。y,platform。x+40,platform。
      y+5);/*在新位置显示底板*/ } } void gameover()/*游戏结束,显示最终得分*/ { char buffer[50]; cleardevice(); sprintf(buffer,"your final score is:%d",score); setcolor(BLUE); settextstyle(0,0,3); outtextxy(100,300,buffer); setcolor(RED); settextstyle(0,0,5); outtextxy(0,150,"thanks for play!"); getch(); exit(0); } double change(int x)/*将角度转化为弧度*/ { double radians; return(x*pi/180); } 。
    2006-06-20 22:32:11
  • 很赞哦! (207)