1.求C语言小游戏源程序
2.ç®å好ç©çç¼ç¨ä»£ç ï¼
求C语言小游戏源程序
我的系统楼主可以自己玩一下
试试吧
#define N
#include <graphics.h>
#include <stdlib.h>
#include <dos.h>
#define LEFT 0x4b
#define RIGHT 0x4d
#define DOWN 0x
#define UP 0x
#define ESC 0xb
int i,key;
int score=0;/*得分*/
int gamespeed=;/*游戏速度自己调整*/
struct Food
{
int x;/*食物的横坐标*/
int y;/*食物的纵坐标*/
int yes;/*判断是否要出现食物的变量*/
}food;/*食物的结构体*/
struct Snake
{
int x[N];
int y[N];
int node;/*蛇的节数*/
int direction;/*蛇移动方向*/
int life;/* 蛇的生命,0活着,1死亡*/
}snake;
void Init(void);/*图形驱动*/
void Close(void);/*图形结束*/
void DrawK(void);/*开始画面*/
void GameOver(void);/*结束游戏*/
void GamePlay(void);/*玩游戏具体过程*/
void PrScore(void);/*输出成绩*/
/*主函数*/
void main(void)
{
Init();/*图形驱动*/
DrawK();/*开始画面*/
GamePlay();/*玩游戏具体过程*/
Close();/*图形结束*/
}
/*图形驱动*/
void Init(void)
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc");
cleardevice();
}
/*开始画面,左上角坐标为(,源码最好的黄金分割指标源码),系统更换域名源码右下角坐标为(,源码开场源码)的系统源码接入tfs围墙*/
void DrawK(void)
{
/*setbkcolor(LIGHTGREEN);*/
setcolor();
setlinestyle(SOLID_LINE,0,THICK_WIDTH);/*设置线型*/
for(i=;i<=;i+=)/*画围墙*/
{
rectangle(i,,i+,); /*上边*/
rectangle(i,,i+,);/*下边*/
}
for(i=;i<=;i+=)
{
rectangle(,i,,i+); /*左边*/
rectangle(,i,,i+);/*右边*/
}
}
/*玩游戏具体过程*/
void GamePlay(void)
{
randomize();/*随机数发生器*/
food.yes=1;/*1表示需要出现新食物,0表示已经存在食物*/
snake.life=0;/*活着*/
snake.direction=1;/*方向往右*/
snake.x[0]=;snake.y[0]=;/*蛇头*/
snake.x[1]=;snake.y[1]=;
snake.node=2;/*节数*/
PrScore();/*输出得分*/
while(1)/*可以重复玩游戏,压ESC键结束*/
{
while(!kbhit())/*在没有按键的情况下,蛇自己移动身体*/
{
if(food.yes==1)/*需要出现新食物*/
{
food.x=rand()%+;
food.y=rand()%+;
while(food.x%!=0)/*食物随机出现后必须让食物能够在整格内,这样才可以让蛇吃到*/
food.x++;
while(food.y%!=0)
food.y++;
food.yes=0;/*画面上有食物了*/
}
if(food.yes==0)/*画面上有食物了就要显示*/
{
setcolor(GREEN);
rectangle(food.x,food.y,food.x+,food.y-);
}
for(i=snake.node-1;i>0;i--)/*蛇的每个环节往前移动,也就是贪吃蛇的关键算法*/
{
snake.x[i]=snake.x[i-1];
snake.y[i]=snake.y[i-1];
}
/*1,2,3,4表示右,左,上,下四个方向,通过这个判断来移动蛇头*/
switch(snake.direction)
{
case 1:snake.x[0]+=;break;
case 2: snake.x[0]-=;break;
case 3: snake.y[0]-=;break;
case 4: snake.y[0]+=;break;
}
for(i=3;i<snake.node;i++)/*从蛇的第四节开始判断是否撞到自己了,因为蛇头为两节,源码spring 4.0.1源码第三节不可能拐过来*/
{
if(snake.x[i]==snake.x[0]&&snake.y[i]==snake.y[0])
{
GameOver();/*显示失败*/
snake.life=1;
break;
}
}
if(snake.x[0]<||snake.x[0]>||snake.y[0]<||
snake.y[0]>)/*蛇是系统否撞到墙壁*/
{
GameOver();/*本次游戏结束*/
snake.life=1; /*蛇死*/
}
if(snake.life==1)/*以上两种判断以后,如果蛇死就跳出内循环,重新开始*/
break;
if(snake.x[0]==food.x&&snake.y[0]==food.y)/*吃到食物以后*/
{
setcolor(0);/*把画面上的源码食物东西去掉*/
rectangle(food.x,food.y,food.x+,food.y-);
snake.x[snake.node]=-;snake.y[snake.node]=-;
/*新的一节先放在看不见的位置,下次循环就取前一节的位置*/
snake.node++;/*蛇的身体长一节*/
food.yes=1;/*画面上需要出现新的食物*/
score+=;
PrScore();/*输出新得分*/
}
setcolor(4);/*画出蛇*/
for(i=0;i<snake.node;i++)
rectangle(snake.x[i],snake.y[i],snake.x[i]+,
snake.y[i]-);
delay(gamespeed);
setcolor(0);/*用黑色去除蛇的的最后一节*/
rectangle(snake.x[snake.node-1],snake.y[snake.node-1],
snake.x[snake.node-1]+,snake.y[snake.node-1]-);
} /*endwhile(!kbhit)*/
if(snake.life==1)/*如果蛇死就跳出循环*/
break;
key=bioskey(0);/*接收按键*/
if(key==ESC)/*按ESC键退出*/
break;
else
if(key==UP&&snake.direction!=4)
/*判断是系统否往相反的方向移动*/
snake.direction=3;
else
if(key==RIGHT&&snake.direction!=2)
snake.direction=1;
else
if(key==LEFT&&snake.direction!=1)
snake.direction=2;
else
if(key==DOWN&&snake.direction!=3)
snake.direction=4;
}/*endwhile(1)*/
}
/*游戏结束*/
void GameOver(void)
{
cleardevice();
PrScore();
setcolor(RED);
settextstyle(0,0,4);
outtextxy(,,"GAME OVER");
getch();
}
/*输出成绩*/
void PrScore(void)
{
char str[];
setfillstyle(SOLID_FILL,YELLOW);
bar(,,,);
setcolor(6);
settextstyle(0,0,2);
sprintf(str,"score:%d",score);
outtextxy(,,str);
}
/*图形结束*/
void Close(void)
{
getch();
closegraph();
}
ç®å好ç©çç¼ç¨ä»£ç ï¼
ç«é ·çç¼ç¨ä»£ç
ç«é ·çç¼ç¨ä»£ç æ¯@echooffmodeconcols=lines=colorsetlocalenabledelayedexpansionsetcode=abcdefsetm=1setv=1ã
ç¼å®ç¨åºï¼ç®ç§°ç¼ç¨ï¼æ¯æ让计ç®æºä»£ä¸ºè§£å³æ个é®é¢ã对æ个计ç®ä½ç³»è§å®ä¸å®çè¿ç®æ¹å¼ï¼ä½¿è®¡ç®ä½ç³»æç §è¯¥è®¡ç®æ¹å¼è¿è¡ï¼å¹¶æç»å¾å°ç¸åºç»æçè¿ç¨ã
ç¼ç¨å°±æ¯ä¸ºäºåå©äºè®¡ç®æºæ¥è¾¾å°æä¸ç®çæ解å³æ个é®é¢ï¼è使ç¨æç§ç¨åºè®¾è®¡è¯è¨ç¼åç¨åºä»£ç ï¼å¹¶æç»å¾å°ç»æçè¿ç¨ã计ç®æºè½ç¶åè½åå强大ï¼å¯ä»¥ä¸ç½ãç©æ¸¸æã管çå ¬å¸äººäºå ³ç³»ççãä½æ¯æ²¡æç¨åºï¼å®å°±çäºæ¯ä¸å åºéï¼ä¸ä¼çä¼æ们对å®ä¸è¾¾çâå½ä»¤âã
ç¼ç¨è¯è¨ä»ç»
使ç¨æ±ç¼è¯è¨ç¼å计ç®æºç¨åºï¼ç¨åºåä»ç¶éè¦ååçæ计ç®æºç³»ç»ç硬件ç»æï¼æ以ä»ç¨åºè®¾è®¡æ¬èº«ä¸æ¥çä»ç¶æ¯ä½æççãç¦ççãä½æ£æ¯ç±äºæ±ç¼è¯è¨ä¸è®¡ç®æºç¡¬ä»¶ç³»ç»å ³ç³»å¯åï¼å¨æäºç¹å®çåºåï¼å¦å¯¹æ¶ç©ºæçè¦æ±å¾é«çç³»ç»æ ¸å¿ç¨åºä»¥åå®æ¶æ§å¶ç¨åºçï¼è¿ä»ä¸ºæ¢æ±ç¼è¯è¨ä»ç¶æ¯ååææçç¨åºè®¾è®¡å·¥å ·ã
使ç¨é«çº§è¯è¨ç¼åç¨åºçä¼ç¹æ¯ï¼ç¼ç¨ç¸å¯¹ç®åãç´è§ãæç解ãä¸å®¹æåºéãé«çº§è¯è¨æ¯ç¬ç«äºè®¡ç®æºçï¼å èç¨é«çº§è¯è¨ç¼åç计ç®æºç¨åºéç¨æ§å¥½ï¼å ·æè¾å¥½ç移æ¤æ§ã
å¨è®¡ç®æºåºç¨çåæï¼ç¨åºå使ç¨æºå¨çæ令系ç»æ¥ç¼å计ç®æºåºç¨ç¨åºï¼è¿ç§ç¨åºç§°ä¸ºæºå¨è¯è¨ç¨åºã
æ±è¡ç®åCè¯è¨ç¨åºä»£ç ï¼åºç¡ç就好
#includestdio.h
#includestdlib.h
#defineNUM
/*runthisprogramusingtheconsolepauseroraddyourowngetch,system("pause")orinputloop*/
//å泡æåºç®æ³
//åºæ¬ææ³ï¼æ¯è¾ç¸é»ç两个æ°ï¼å¦æåè æ¯åè 大ï¼åè¿è¡äº¤æ¢ãæ¯ä¸è½®æåºç»æï¼éåºä¸ä¸ªæªæåºä¸æ大çæ°æ¾å°æ°ç»åé¢ã
voidbubbleSort(int*arr,intn){
inti,j;
for(i=0;in-1;i++)
for(j=0;jn-i-1;j++){
//å¦æåé¢çæ°æ¯åé¢å¤§ï¼è¿è¡äº¤æ¢
if(arr[j]arr[j+1]){
inttemp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
//æå·®æ¶é´å¤æ度为O(n^2),å¹³åæ¶é´å¤æ度为O(n^2)ã稳å®æ§ï¼ç¨³å®ãè¾ å©ç©ºé´O(1)ã
//å级çå泡æåºæ³ï¼éè¿ä»ä½å°é«éåºæ大çæ°æ¾å°åé¢ï¼åä»é«å°ä½éåºæå°çæ°æ¾å°åé¢ï¼
//å¦æ¤åå¤ï¼ç´å°å·¦è¾¹çåå³è¾¹çéåãå½æ°ç»ä¸æå·²æåºå¥½çæ°æ¶ï¼è¿ç§æåºæ¯ä¼ ç»å泡æåºæ§è½ç¨å¥½ã
//å级çå泡æåºç®æ³
voidbubbleSort_1(int*arr,intn){
//设置æ°ç»å·¦å³è¾¹ç
intleft=0,right=n-1;
//å½å·¦å³è¾¹çæªéåæ¶ï¼è¿è¡æåº
while(left=right){
inti,j;
//ä»å·¦å°å³éåéåºæ大çæ°æ¾å°æ°ç»å³è¾¹
for(i=left;iright;i++){
if(arr[i]arr[i+1]){
inttemp=arr[i];
arr[i]=arr[i+1];
arr[i+1]=temp;
}
}
right--;
//ä»å³å°å·¦éåéåºæå°çæ°æ¾å°æ°ç»å·¦è¾¹
for(j=right;jleft;j--){
if(arr[j+1]arr[j]){
inttemp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
left++;
}
}
intmain(intargc,char*argv[]){
intarr[NUM],i,j,temp;
printf("请è¾å ¥ä¸ªæ°ï¼\n");
for(i=0;iNUM;i++){
printf("请è¾å ¥ç¬¬(%d)个æ°ï¼",i+1);
scanf("%d",arr[i]);
}
printf("\nè¾å ¥å¦ä¸æåï¼\n");
for(i=0;iNUM;i++){
printf("%4d",arr[i]);
}/
*for(i=0;iNUM;i++){
for(j=i+1;jNUM;j++){
if(arr[i]arr[j]){
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}*/
bubbleSort_1(arr,NUM);
/*printf("\nä»å°å°å¤§å¦ä¸æåï¼\n");
for(i=0;iNUM;i++){
printf("%4d",arr[i]);
}*/
printf("\nä»å¤§å°å°å¦ä¸æåï¼\n");
for(i=NUM-1;i=0;i--){
printf("%4d",arr[i]);
}
return0;
}
é便ç»æä¸ä¸ªç¼ç¨ä»£ç 带ææ谢谢main()?
{ ?
inti,j,k;?
printf("\n");?
for(i=1;i5;i++)ï¼*以ä¸ä¸ºä¸é循ç¯*/
for(j=1;j5;j++)
for(k=1;k5;k++)
{
if(i!=ki!=jj!=k)/*ç¡®ä¿iãjãkä¸ä½äºä¸ç¸å*/
printf("%d,%d,%d\n",i,j,k);
}?
}
1ã代ç 解éï¼ä»¥ä¸è¿æ®µä»£ç æ¯ç¨Cè¯è¨ååºæ¥ç代ç ï¼ä»æ解å³çé®é¢æ¯ï¼æ1ã2ã3ã4个æ°åï¼è½ç»æå¤å°ä¸ªäºä¸ç¸åä¸æ éå¤æ°åçä¸ä½æ°ï¼é½æ¯å¤å°ï¼?å¯å¡«å¨ç¾ä½ãåä½ã个ä½çæ°åé½æ¯1ã2ã3ã4ãç»æææçæåååå»?æä¸æ»¡è¶³æ¡ä»¶çæåã
2ãCè¯è¨ä»ç»ï¼Cè¯è¨æ¯ä¸é¨éç¨è®¡ç®æºç¼ç¨è¯è¨ï¼åºç¨å¹¿æ³ãCè¯è¨ç设计ç®æ æ¯æä¾ä¸ç§è½ä»¥ç®æçæ¹å¼ç¼è¯ãå¤çä½çº§åå¨å¨ã产çå°éçæºå¨ç 以åä¸éè¦ä»»ä½è¿è¡ç¯å¢æ¯æ便è½è¿è¡çç¼ç¨è¯è¨ã
3ãCè¯è¨çä¼ç¹ï¼ç®æ´ç´§åãçµæ´»æ¹ä¾¿ãè¿ç®ç¬¦ä¸°å¯ãæ°æ®ç»æ丰å¯ãCæ¯ç»æå¼è¯è¨ãCè¯æ³éå¶ä¸å¤ªä¸¥æ ¼ãç¨åºè®¾è®¡èªç±åº¦å¤§ï¼ä¸è¬çé«çº§è¯è¨è¯æ³æ£æ¥æ¯è¾ä¸¥ï¼è½å¤æ£æ¥åºå ä¹ææçè¯æ³é误ãèCè¯è¨å 许ç¨åºç¼åè æè¾å¤§çèªç±åº¦ãCè¯è¨ç¨åºçæ代ç è´¨éé«ï¼ç¨åºæ§è¡æçé«ï¼ä¸è¬åªæ¯æ±ç¼ç¨åºçæçç®æ 代ç æçä½ã¸%ã
4ãå ¶ä»ç¼ç¨è¯è¨ä»ç»ï¼Python(è±å½åé³:/?pa?θ?n/ç¾å½åé³:/?pa?θÉ?n/),æ¯ä¸ç§é¢å对象ç解éå计ç®æºç¨åºè®¾è®¡è¯è¨ï¼ç±è·å °äººGuidovanRossumäºå¹´åæï¼ç¬¬ä¸ä¸ªå ¬å¼åè¡çåè¡äºå¹´ãPythonæ¯çº¯ç²¹çèªç±è½¯ä»¶ï¼?æºä»£ç å解éå¨CPythonéµå¾ªGPL(GNU?GeneralPublicLicense)åè®®ãPythonè¯æ³ç®æ´æ¸ æ°ï¼ç¹è²ä¹ä¸æ¯å¼ºå¶ç¨ç©ºç½ç¬¦(whitespace)ä½ä¸ºè¯å¥ç¼©è¿ã
pythonæ趣çç¼ç¨ä»£ç class?Point:
row=0col=0def?__init__(self,?row,?col):self.row=row
self.col=col
def?copy(self):return?Point(row=self.row,?col=self.col)
#åå§æ¡æ¶
import?pygame
import?random
#åå§å
pygame.init()
W=
H=
ROW=
COL=
size=(W,H)
window=pygame.display.set_mode(size)
pygame.display.set_caption('è´ªåè')
bg_color=(,,)
snake_color=(,,)
head=Point(row=int(ROW/2),?col=int(COL/2))
head_color=(0,,)
snakes=[
Point(row=head.row,?col=head.col+1),Point(row=head.row,?col=head.col+2),Point(row=head.row,?col=head.col+3)]
#çæé£ç©
def?gen_food():
while?1:pos=Point(row=random.randint(0,ROW-1),?col=random.randint(0,COL-1))
#
is_coll=False
#æ¯å¦è·è碰ä¸äº
if?head.row==pos.row?and?head.col==pos.col:
is_coll=True#è身å
for?snake?in?snakes:
if?snake.row==pos.row?and?snake.col==pos.col:is_coll=True
break
if?not?is_coll:
breakreturn?pos#å®ä¹åæ
food=gen_food()
food_color=(,,0)
direct='left'#left,right,up,down
#
def?rect(point,?color):
cell_width=W/COLcell_height=H/ROWleft=point.col*cell_widthtop=point.row*cell_heightpygame.draw.rect(window,?color,
(left,?top,?cell_width,?cell_height)
)pass#游æ循ç¯
quit=True
clock=pygame.time.Clock()
while?quit:
#å¤çäºä»¶for?event?in?pygame.event.get():if?event.type==pygame.QUIT:
quit=Falseelif?event.type==pygame.KEYDOWN:
if?event.key==?or?event.key==:if?direct=='left'?or?direct=='right':
direct='up'elif?event.key==?or?event.key==:if?direct?==?'left'?or?direct?==?'right':
direct='down'elif?event.key==?or?event.key==:if?direct?==?'up'?or?direct?==?'down':
direct='left'elif?event.key==?or?event.key==:if?direct?==?'up'?or?direct?==?'down':
direct='right'#åä¸è¥¿eat=(head.row==food.row?and?head.col==food.col)#éæ°äº§çé£ç©if?eat:food?=?gen_food()
#å¤ç身å#1.æåæ¥ç头ï¼æå ¥å°snakesç头ä¸snakes.insert(0,?head.copy())#2.æsnakesçæåä¸ä¸ªå æif?not?eat:snakes.pop()
#移å¨if?direct=='left':head.col-=1
elif?direct=='right':head.col+=1
elif?direct=='up':head.row-=1
elif?direct=='down':head.row+=1
#æ£æµdead=False#1.æå¢if?head.col0?or?head.row0?or?head.col=COL?or?head.row=ROW:dead=True
#2.æèªå·±for?snake?in?snakes:if?head.col==snake.col?and?head.row==snake.row:
dead=Truebreakif?dead:print('æ»äº')
quit=False
#渲æââç»åºæ¥#èæ¯pygame.draw.rect(window,?bg_color,?(0,0,W,H))#è头for?snake?in?snakes:rect(snake,?snake_color)
rect(head,?head_color)rect(food,?food_color)#pygame.display.flip()#设置帧é¢ï¼é度ï¼clock.tick(8)#æ¶å°¾å·¥ä½
è¿æ¯ä¸ä¸ªç®æçè´ªåèç代ç ï¼è½ç¶ç»æç®åï¼ä½æ¯è¯¥æçåè½é½æ¯å®æ´çï¼å¯ç©æ§ä¹ä¸é
cè¯è¨ç¼ç¨çè±ä»£ç ç®åcè¯è¨ç¼ç¨çè±ä»£ç ç®åå¦ä¸ï¼
#include"stdlib.h"
#include"graphics.h"
#include"stdio.h"
#include"math.h"
#include"conio.h"
#definePI?3.
main()
{
intgdriver=DETECT,gmode,errorcode;
inta[],b[],x,y,c,r,i,j,t;
doublerad=0.0;
/*initializegraphicsandlocalvariables*/
initgraph(gdriver,gmode,"");
/*readresultofinitialization*/
errorcode=graphresult();
if(errorcode!=grOk)?/*anerroroccurred*/
{
printf("Graphicserror:%s/n",grapherrormsg(errorcode));
printf("Pleaseanykeytohalt:");
getch();
exit(1);?/*terminatewithanerrorcode*/
}
randomize();
for(;!kbhit();)
{
x=rand()%+;/*éæºä¸å¿åæ */
y=rand()%+;
for(r=0;r=8;r++?)?/*çè±ç大å°è®¾å®*/
{
for(i=0,rad=0.0;rad2*PI;rad+=0.)?/*设å®åæ */
{
a[i++]=x+(int)r**cos(rad);
b[i]=y+(int)r**sin(rad);
}
t=i;
for(i=1;it;i++)
{
c=rand()%+1;/*åç¹çé¢è²éæº*/
setcolor(c);/*åè½:å°å½åå¾å½¢å±å¹çå½åç¬ç»é¢è²ç½®ä¸ºcolor.*/
circle(a[i],b[i],1);/*a[i],b[i]为åå¿1为åå¾ç»å*/
}
delay();
delay();
cleardevice();
å½æ°å:cleardevice?
å?è½:æ¸ é¤å¾å½¢å±å¹?
ç¨?æ³:voidfarcleardevice(void);
}
}
getch();
closegraph();
å½æ°å:closegraph
å?è½:å ³éå¾å½¢ç³»ç»
ç¨?æ³:voidfarclosegraph(void);
}
åå§åçè±åæ°
voidInit(inti)
{
//åå«ä¸ºï¼çè±ä¸å¿å°å¾çè¾¹ç¼çæè¿è·ç¦»ãçè±ä¸å¿å°å¾çå·¦ä¸è§çè·ç¦»(xãy)两个åé
intr[]={ ,,,,,,,,,,,,};
intx[]={ ,,,,,,,,,,,,};
inty[]={ ,,,,,,,,,,,,};
/****åå§åçè±*****/
Fire[i].x=0;//çè±ä¸å¿åæ
Fire[i].y=0;
Fire[i].width=;//å¾ç宽
Fire[i].height=;//å¾çé«
Fire[i].max_r=r[i];//æ大åå¾
Fire[i].cen_x=x[i];//ä¸å¿è·å·¦ä¸è§è·ç¦»
Fire[i].cen_y=y[i];
Fire[i].show=false;//æ¯å¦ç»½æ¾
Fire[i].dt=5;//绽æ¾æ¶é´é´é
Fire[i].t1=timeGetTime();
Fire[i].r=0;//ä»0å¼å§ç»½æ¾
/****åå§åçè±å¼¹*****/
Jet[i].x=-;//çè±å¼¹å·¦ä¸è§åæ
Jet[i].y=-;
Jet[i].hx=-;//çè±å¼¹åå°æé«ç¹åæ
Jet[i].hy=-;
Jet[i].height=0;//åå°é«åº¦
Jet[i].t1=timeGetTime();
Jet[i].dt=rand()%;//åå°é度æ¶é´é´é
Jet[i].n=0;//çè±å¼¹éªçå¾çä¸æ
Jet[i].shoot=false;//æ¯å¦åå°
}