| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1557 人关注过本帖
标题:求一个C语言小游戏源代码
只看楼主 加入收藏
夏末沫沫
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2015-3-17
结帖率:0
收藏
已结贴  问题点数:20 回复次数:4 
求一个C语言小游戏源代码
我是初学者,谁能给我一个小游戏源代码,可以运行的。
搜索更多相关主题的帖子: 小游戏 源代码 C语言 
2015-03-17 20:14
zx191316
Rank: 1
等 级:新手上路
帖 子:37
专家分:5
注 册:2015-3-9
收藏
得分:5 
我学程序也是为了做游戏。
2015-03-17 21:07
qiu13151015
Rank: 1
等 级:新手上路
帖 子:1
专家分:5
注 册:2015-3-17
收藏
得分:5 
??????
2015-03-17 22:14
夏殇
Rank: 1
等 级:新手上路
帖 子:1
专家分:5
注 册:2015-3-17
收藏
得分:5 
我没帮上什么忙,还在学习与探索中。
贪吃蛇游戏:(文字转码自己弄,程序基本上是对的,后续问题都可以百度解决的)

#define N 200
#include <graphics.h>
#include <stdlib.h>
#include <dos.h>
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define ESC 0x011b
int i,key;
int score=0;/*&micro;&Atilde;·&Ouml;*/
int gamespeed=50000;/*&Oacute;&Icirc;&Iuml;·&Euml;&Ugrave;&para;&Egrave;×&Ocirc;&frac14;&ordm;&micro;÷&Otilde;&ucirc;*/
struct Food
{
   int x;/*&Ecirc;&sup3;&Icirc;&iuml;&micro;&Auml;&ordm;á×&oslash;±ê*/
   int y;/*&Ecirc;&sup3;&Icirc;&iuml;&micro;&Auml;×&Yacute;×&oslash;±ê*/
   int yes;/*&Aring;&ETH;&para;&Iuml;&Ecirc;&Ccedil;·&ntilde;&Ograve;&ordf;&sup3;&ouml;&Iuml;&Ouml;&Ecirc;&sup3;&Icirc;&iuml;&micro;&Auml;±&auml;&Aacute;&iquest;*/
}food;/*&Ecirc;&sup3;&Icirc;&iuml;&micro;&Auml;&frac12;á&sup1;&sup1;&Igrave;&aring;*/
struct Snake
{
   int x[N];
   int y[N];
   int node;/*&Eacute;&szlig;&micro;&Auml;&frac12;&Uacute;&Ecirc;&yacute;*/
   int direction;/*&Eacute;&szlig;&Ograve;&AElig;&para;&macr;·&frac12;&Iuml;ò*/
   int life;/* &Eacute;&szlig;&micro;&Auml;&Eacute;ú&Atilde;ü,0&raquo;&icirc;×&Aring;,1&Euml;&Agrave;&Iacute;&ouml;*/
}snake;
void Init(void);/*&Iacute;&frac14;&ETH;&Icirc;&Ccedil;&yacute;&para;&macr;*/
void Close(void);/*&Iacute;&frac14;&ETH;&Icirc;&frac12;á&Ecirc;&oslash;*/
void DrawK(void);/*&iquest;&ordf;&Ecirc;&frac14;&raquo;&shy;&Atilde;&aelig;*/
void GameOver(void);/*&frac12;á&Ecirc;&oslash;&Oacute;&Icirc;&Iuml;·*/
void GamePlay(void);/*&Iacute;&aelig;&Oacute;&Icirc;&Iuml;·&frac34;&szlig;&Igrave;&aring;&sup1;&yacute;&sup3;&Igrave;*/
void PrScore(void);/*&Ecirc;&auml;&sup3;&ouml;&sup3;&Eacute;&frac14;¨*/
/*&Ouml;÷&ordm;&macr;&Ecirc;&yacute;*/
void main(void)
{
   Init();/*&Iacute;&frac14;&ETH;&Icirc;&Ccedil;&yacute;&para;&macr;*/
   DrawK();/*&iquest;&ordf;&Ecirc;&frac14;&raquo;&shy;&Atilde;&aelig;*/
   GamePlay();/*&Iacute;&aelig;&Oacute;&Icirc;&Iuml;·&frac34;&szlig;&Igrave;&aring;&sup1;&yacute;&sup3;&Igrave;*/
   Close();/*&Iacute;&frac14;&ETH;&Icirc;&frac12;á&Ecirc;&oslash;*/
}
/*&Iacute;&frac14;&ETH;&Icirc;&Ccedil;&yacute;&para;&macr;*/
void Init(void)
{
   int gd=DETECT,gm;
   initgraph(&gd,&gm,"c:\\tc");
   cleardevice();
}
/*&iquest;&ordf;&Ecirc;&frac14;&raquo;&shy;&Atilde;&aelig;&pound;&not;×ó&Eacute;&Iuml;&frac12;&Ccedil;×&oslash;±ê&Icirc;&ordf;&pound;¨50&pound;&not;40&pound;&copy;&pound;&not;&Oacute;&Ograve;&Iuml;&Acirc;&frac12;&Ccedil;×&oslash;±ê&Icirc;&ordf;&pound;¨610&pound;&not;460&pound;&copy;&micro;&Auml;&Icirc;§&Ccedil;&frac12;*/
void DrawK(void)
{
/*setbkcolor(LIGHTGREEN);*/
   setcolor(11);
   setlinestyle(SOLID_LINE,0,THICK_WIDTH);/*&Eacute;è&Ouml;&Atilde;&Iuml;&szlig;&ETH;&Iacute;*/
   for(i=50;i<=600;i+=10)/*&raquo;&shy;&Icirc;§&Ccedil;&frac12;*/
   {
      rectangle(i,40,i+10,49); /*&Eacute;&Iuml;±&szlig;*/
      rectangle(i,451,i+10,460);/*&Iuml;&Acirc;±&szlig;*/
   }
  for(i=40;i<=450;i+=10)
  {
     rectangle(50,i,59,i+10); /*×ó±&szlig;*/
     rectangle(601,i,610,i+10);/*&Oacute;&Ograve;±&szlig;*/
  }
}
/*&Iacute;&aelig;&Oacute;&Icirc;&Iuml;·&frac34;&szlig;&Igrave;&aring;&sup1;&yacute;&sup3;&Igrave;*/
void GamePlay(void)
{
   randomize();/*&Euml;&aelig;&raquo;ú&Ecirc;&yacute;·&cent;&Eacute;ú&AElig;÷*/
   food.yes=1;/*1±í&Ecirc;&frac34;&ETH;è&Ograve;&ordf;&sup3;&ouml;&Iuml;&Ouml;&ETH;&Acirc;&Ecirc;&sup3;&Icirc;&iuml;,0±í&Ecirc;&frac34;&Ograve;&Ntilde;&frac34;&shy;&acute;&aelig;&Ocirc;&Uacute;&Ecirc;&sup3;&Icirc;&iuml;*/
   snake.life=0;/*&raquo;&icirc;×&Aring;*/
   snake.direction=1;/*·&frac12;&Iuml;ò&Iacute;ù&Oacute;&Ograve;*/
   snake.x[0]=100;snake.y[0]=100;/*&Eacute;&szlig;&Iacute;·*/
   snake.x[1]=110;snake.y[1]=100;
   snake.node=2;/*&frac12;&Uacute;&Ecirc;&yacute;*/
   PrScore();/*&Ecirc;&auml;&sup3;&ouml;&micro;&Atilde;·&Ouml;*/
   while(1)/*&iquest;&Eacute;&Ograve;&Ocirc;&Ouml;&Oslash;&cedil;&acute;&Iacute;&aelig;&Oacute;&Icirc;&Iuml;·,&Ntilde;&sup1;ESC&frac14;ü&frac12;á&Ecirc;&oslash;*/
   {
      while(!kbhit())/*&Ocirc;&Uacute;&Atilde;&raquo;&Oacute;&ETH;°&acute;&frac14;ü&micro;&Auml;&Ccedil;é&iquest;&ouml;&Iuml;&Acirc;,&Eacute;&szlig;×&Ocirc;&frac14;&ordm;&Ograve;&AElig;&para;&macr;&Eacute;í&Igrave;&aring;*/
      {
     if(food.yes==1)/*&ETH;è&Ograve;&ordf;&sup3;&ouml;&Iuml;&Ouml;&ETH;&Acirc;&Ecirc;&sup3;&Icirc;&iuml;*/
     {
         food.x=rand()%400+60;
         food.y=rand()%350+60;
         while(food.x%10!=0)/*&Ecirc;&sup3;&Icirc;&iuml;&Euml;&aelig;&raquo;ú&sup3;&ouml;&Iuml;&Ouml;&ordm;ó±&Oslash;&ETH;&euml;&Egrave;&Atilde;&Ecirc;&sup3;&Icirc;&iuml;&Auml;&Uuml;&sup1;&raquo;&Ocirc;&Uacute;&Otilde;&ucirc;&cedil;&ntilde;&Auml;&Uacute;,&Otilde;&acirc;&Ntilde;ù&sup2;&Aring;&iquest;&Eacute;&Ograve;&Ocirc;&Egrave;&Atilde;&Eacute;&szlig;&sup3;&Ocirc;&micro;&frac12;*/
        food.x++;
         while(food.y%10!=0)
        food.y++;
        food.yes=0;/*&raquo;&shy;&Atilde;&aelig;&Eacute;&Iuml;&Oacute;&ETH;&Ecirc;&sup3;&Icirc;&iuml;&Aacute;&Euml;*/
     }
     if(food.yes==0)/*&raquo;&shy;&Atilde;&aelig;&Eacute;&Iuml;&Oacute;&ETH;&Ecirc;&sup3;&Icirc;&iuml;&Aacute;&Euml;&frac34;&Iacute;&Ograve;&ordf;&Iuml;&Ocirc;&Ecirc;&frac34;*/
     {
              setcolor(GREEN);
              rectangle(food.x,food.y,food.x+10,food.y-10);
     }
         for(i=snake.node-1;i>0;i--)/*&Eacute;&szlig;&micro;&Auml;&Atilde;&iquest;&cedil;&ouml;&raquo;·&frac12;&Uacute;&Iacute;ù&Ccedil;°&Ograve;&AElig;&para;&macr;,&Ograve;&sup2;&frac34;&Iacute;&Ecirc;&Ccedil;&Igrave;°&sup3;&Ocirc;&Eacute;&szlig;&micro;&Auml;&sup1;&Oslash;&frac14;ü&Euml;&atilde;·¨*/
         {
            snake.x[i]=snake.x[i-1];
        snake.y[i]=snake.y[i-1];
     }
       /*1,2,3,4±í&Ecirc;&frac34;&Oacute;&Ograve;,×ó,&Eacute;&Iuml;,&Iuml;&Acirc;&Euml;&Auml;&cedil;&ouml;·&frac12;&Iuml;ò,&Iacute;¨&sup1;&yacute;&Otilde;&acirc;&cedil;&ouml;&Aring;&ETH;&para;&Iuml;&Agrave;&acute;&Ograve;&AElig;&para;&macr;&Eacute;&szlig;&Iacute;·*/
     switch(snake.direction)
     {
        case 1:snake.x[0]+=10;break;
        case 2: snake.x[0]-=10;break;
        case 3: snake.y[0]-=10;break;
        case 4: snake.y[0]+=10;break;
     }
     for(i=3;i<snake.node;i++)/*&acute;&Oacute;&Eacute;&szlig;&micro;&Auml;&micro;&Uacute;&Euml;&Auml;&frac12;&Uacute;&iquest;&ordf;&Ecirc;&frac14;&Aring;&ETH;&para;&Iuml;&Ecirc;&Ccedil;·&ntilde;×&sup2;&micro;&frac12;×&Ocirc;&frac14;&ordm;&Aacute;&Euml;&pound;&not;&Ograve;ò&Icirc;&ordf;&Eacute;&szlig;&Iacute;·&Icirc;&ordf;&Aacute;&frac12;&frac12;&Uacute;&pound;&not;&micro;&Uacute;&Egrave;&yacute;&frac12;&Uacute;&sup2;&raquo;&iquest;&Eacute;&Auml;&Uuml;&sup1;&Otilde;&sup1;&yacute;&Agrave;&acute;*/
     {
        if(snake.x[i]==snake.x[0]&&snake.y[i]==snake.y[0])
        {
               GameOver();/*&Iuml;&Ocirc;&Ecirc;&frac34;&Ecirc;§°&Uuml;*/
               snake.life=1;
               break;
        }
        }
    if(snake.x[0]<55||snake.x[0]>595||snake.y[0]<55||
       snake.y[0]>455)/*&Eacute;&szlig;&Ecirc;&Ccedil;·&ntilde;×&sup2;&micro;&frac12;&Ccedil;&frac12;±&Uacute;*/
    {
        GameOver();/*±&frac34;&acute;&Icirc;&Oacute;&Icirc;&Iuml;·&frac12;á&Ecirc;&oslash;*/
        snake.life=1; /*&Eacute;&szlig;&Euml;&Agrave;*/
    }
    if(snake.life==1)/*&Ograve;&Ocirc;&Eacute;&Iuml;&Aacute;&frac12;&Ouml;&Ouml;&Aring;&ETH;&para;&Iuml;&Ograve;&Ocirc;&ordm;ó,&Egrave;&ccedil;&sup1;&ucirc;&Eacute;&szlig;&Euml;&Agrave;&frac34;&Iacute;&Igrave;&oslash;&sup3;&ouml;&Auml;&Uacute;&Ntilde;&shy;&raquo;·&pound;&not;&Ouml;&Oslash;&ETH;&Acirc;&iquest;&ordf;&Ecirc;&frac14;*/
           break;
    if(snake.x[0]==food.x&&snake.y[0]==food.y)/*&sup3;&Ocirc;&micro;&frac12;&Ecirc;&sup3;&Icirc;&iuml;&Ograve;&Ocirc;&ordm;ó*/
    {
           setcolor(0);/*°&Ntilde;&raquo;&shy;&Atilde;&aelig;&Eacute;&Iuml;&micro;&Auml;&Ecirc;&sup3;&Icirc;&iuml;&para;&laquo;&Icirc;÷&Egrave;&yen;&micro;&ocirc;*/
           rectangle(food.x,food.y,food.x+10,food.y-10);
       snake.x[snake.node]=-20;snake.y[snake.node]=-20;
          /*&ETH;&Acirc;&micro;&Auml;&Ograve;&raquo;&frac12;&Uacute;&Iuml;&Egrave;·&Aring;&Ocirc;&Uacute;&iquest;&acute;&sup2;&raquo;&frac14;&ucirc;&micro;&Auml;&Icirc;&raquo;&Ouml;&Atilde;,&Iuml;&Acirc;&acute;&Icirc;&Ntilde;&shy;&raquo;·&frac34;&Iacute;&Egrave;&iexcl;&Ccedil;°&Ograve;&raquo;&frac12;&Uacute;&micro;&Auml;&Icirc;&raquo;&Ouml;&Atilde;*/
       snake.node++;/*&Eacute;&szlig;&micro;&Auml;&Eacute;í&Igrave;&aring;&sup3;¤&Ograve;&raquo;&frac12;&Uacute;*/
       food.yes=1;/*&raquo;&shy;&Atilde;&aelig;&Eacute;&Iuml;&ETH;è&Ograve;&ordf;&sup3;&ouml;&Iuml;&Ouml;&ETH;&Acirc;&micro;&Auml;&Ecirc;&sup3;&Icirc;&iuml;*/
       score+=10;
       PrScore();/*&Ecirc;&auml;&sup3;&ouml;&ETH;&Acirc;&micro;&Atilde;·&Ouml;*/
    }
    setcolor(4);/*&raquo;&shy;&sup3;&ouml;&Eacute;&szlig;*/
    for(i=0;i<snake.node;i++)
       rectangle(snake.x[i],snake.y[i],snake.x[i]+10,
               snake.y[i]-10);
    delay(gamespeed);
    setcolor(0);/*&Oacute;&Atilde;&ordm;&Uacute;&Eacute;&laquo;&Egrave;&yen;&sup3;&yacute;&Eacute;&szlig;&micro;&Auml;&micro;&Auml;×&icirc;&ordm;ó&Ograve;&raquo;&frac12;&Uacute;*/
    rectangle(snake.x[snake.node-1],snake.y[snake.node-1],
    snake.x[snake.node-1]+10,snake.y[snake.node-1]-10);
     }  /*endwhile&pound;¨&pound;&iexcl;kbhit&pound;&copy;*/
    if(snake.life==1)/*&Egrave;&ccedil;&sup1;&ucirc;&Eacute;&szlig;&Euml;&Agrave;&frac34;&Iacute;&Igrave;&oslash;&sup3;&ouml;&Ntilde;&shy;&raquo;·*/
        break;
    key=bioskey(0);/*&frac12;&Oacute;&Ecirc;&Otilde;°&acute;&frac14;ü*/
    if(key==ESC)/*°&acute;ESC&frac14;ü&Iacute;&Euml;&sup3;&ouml;*/
       break;
    else
       if(key==UP&&snake.direction!=4)
/*&Aring;&ETH;&para;&Iuml;&Ecirc;&Ccedil;·&ntilde;&Iacute;ù&Iuml;à·&acute;&micro;&Auml;·&frac12;&Iuml;ò&Ograve;&AElig;&para;&macr;*/
      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)*/
}
/*&Oacute;&Icirc;&Iuml;·&frac12;á&Ecirc;&oslash;*/
void GameOver(void)
{
    cleardevice();
    PrScore();
   setcolor(RED);
   settextstyle(0,0,4);
   outtextxy(200,200,"GAME OVER");
   getch();
}
/*&Ecirc;&auml;&sup3;&ouml;&sup3;&Eacute;&frac14;¨*/
void PrScore(void)
{   
   char str[10];
   setfillstyle(SOLID_FILL,YELLOW);
   bar(50,15,220,35);
   setcolor(6);
   settextstyle(0,0,2);
   sprintf(str,"score:%d",score);
   outtextxy(55,20,str);
}
/*&Iacute;&frac14;&ETH;&Icirc;&frac12;á&Ecirc;&oslash;*/
void Close(void)
{   
    getch();
    closegraph();
}
2015-03-17 23:59
BuilderZ
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:82
专家分:123
注 册:2014-9-22
收藏
得分:5 
2015-03-18 05:17
快速回复:求一个C语言小游戏源代码
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019002 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved