用c语言编一个小游戏
2006-06-20 22:26:26w***
用c语言编一个小游戏:在不同机子上编译时需修改init()函数中TC路径
#include "graphics.h"
#include "stdlib.h"
?
最佳回答
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)
相关文章
- C语言是用什么语言编的
- 用C语言编99表怎么做 ?/ 要快哦,,哥哥们,,,谢谢了哦
- 谁能提供一个用C语言编写小游戏程序源代码啊????谢谢阿
- 哪个大神能帮我现编100行c语言代码啊 谢了!!!!
- 用Java编一个小游戏,是俄罗斯方块
- 用C语言编杨辉三角问题!!!!!!!!!
- 用C语言编一个通讯录
- 定时程序如何编?
- 用Java编一个小游戏,是俄罗斯方块
- 用C语言编杨辉三角问题!!!!!!!!!
- 怎样用c语言编一个杨辉三角?
- 编程之c语言介绍有哪些呢?
- 哪个大神能帮我现编100行c语言代码啊 谢了!!!!
- 用C语言编杨辉三角问题!!!!!!!!!
- 用Java编一个小游戏,是俄罗斯方块
- C语言C语言编游戏好吗
- c语言用什么程序编
- 如何用C语言编一个程序?
- 谁会用c语言编哈夫曼编码器?
- 关于C语言的题目!不难。
- C语言的一个问题~!
- 编一个小游戏要学哪些知识?
- 能帮用C语言编一个程序
- 怎么用C语言编一个小软件,转换图片格式?
- 哪个大神能帮我现编100行c语言代码啊 谢了?
- 用C语言编一些小游戏
- 杨辉三角用c语言怎么编啊
- C语言有什么用?能编游戏吗?
- 用C语言编99表怎么做 ?/ 要快哦,,哥哥们,,,谢谢了哦
- C语言是用什么语言编的