您的位置:首页 > 编程语言 > C语言/C++

c语言编写的小游戏

2014-03-17 16:57 288 查看
#include <bios.h>
#include <conio.h>
#include <dos.h>
#include <graphics.h>
#include <stdlib.h>
#include <time.h>
#include "conf.h" 

typedef struct node
{
     int x,y;
     struct node *next;
}Node; 

typedef struct
{
     Node *head,*tail;
     int length;
}Snake;
typedef struct                    
{
     int left,top,right,bottom;
}Frame; 

typedef enum                     //四个方向
{
     up,down,left,right
}Direction; 

typedef enum
{
     false,true
}bool;//*/ 

void InitGraphMode();                     //初始化图形驱动
void CloseGraphMode();                
void Foot(int,int);                            
void Head(int,int);
void CreateFrame();                     //完成整个游戏框架的绘制    
void CreateSnake();     //创建一条两个节点的蛇,蛇的每一节是队列中的一个节点
bool PlayGame();                         //游戏的主体函数,
int Hit(int,int);      //判断是否越界,或者撞到自身,两个参数分别是新的头接点的x,y坐标
bool GameOver();                         //绘制游戏结束时弹出的对话框                       
void Enqueue(Node);                     //入队函数
Node Dequeue();                         //出队函数
void ClearKeyBuf();     //清除键盘缓冲,此函数可以消除不停的按无效键的影响 

Snake snake;
Frame frame;
void main()
{
     InitGraphMode();
     do
     {
       CreateFrame();
     }while(PlayGame());
     CloseGraphMode();
}

  http://v.17173.com/playlist_13938489.html

  http://v.17173.com/playlist_13939680.html

  http://v.17173.com/playlist_13940838.html

  http://v.17173.com/playlist_13942305.html

  http://v.17173.com/playlist_13943443.html

  http://v.17173.com/playlist_13944295.html

  http://v.17173.com/playlist_13945435.html

  http://v.17173.com/playlist_13946131.html

  http://v.17173.com/playlist_13947669.html

  http://v.17173.com/playlist_13948598.html

  http://v.17173.com/playlist_13949386.html

  http://v.17173.com/playlist_13950253.html

  http://v.17173.com/playlist_13951065.html

  http://v.17173.com/playlist_13951841.html

  http://v.17173.com/playlist_13952703.html

  http://v.17173.com/playlist_13953491.html

  http://v.17173.com/playlist_13954243.html

  http://v.17173.com/playlist_13954975.html

  http://v.17173.com/playlist_13955837.html

  http://v.17173.com/playlist_13956703.html

  http://v.17173.com/playlist_13957462.html

  http://v.17173.com/playlist_13958117.html

  http://v.17173.com/playlist_13959267.html

  http://v.17173.com/playlist_13959901.html

  http://v.17173.com/playlist_13960660.html

  http://v.17173.com/playlist_13961401.html

  http://v.17173.com/playlist_13962080.html

  http://v.17173.com/playlist_13962726.html

  http://v.17173.com/playlist_13963334.html

  http://v.17173.com/playlist_13964063.html

  http://v.17173.com/u/112491257/playlist/

  http://v.17173.com/playlist_13965404.html

  http://v.17173.com/playlist_13965998.html

  http://v.17173.com/playlist_13966839.html

  http://v.17173.com/playlist_13967762.html

  http://v.17173.com/playlist_13968527.html

  http://v.17173.com/playlist_14014375.html

  http://v.17173.com/playlist_14014999.html

  http://v.17173.com/playlist_14015817.html

  http://v.17173.com/playlist_14016522.html

  http://v.17173.com/playlist_14017479.html

  http://v.17173.com/playlist_14018216.html

  http://v.17173.com/playlist_14020977.html

  http://v.17173.com/playlist_14013332.html

  http://bbs.taobao.com/catalog/thread/154521-266598260.htm

  http://bbs.taobao.com/catalog/thread/154521-266599664.htm

  http://bbs.taobao.com/catalog/thread/154521-266592400.htm

  http://bbs.taobao.com/catalog/thread/154521-266598673.htm

  http://bbs.taobao.com/catalog/thread/154521-266597284.htm

  http://blog.1688.com/article/i36511560.html

  http://blog.1688.com/article/i36511614.html

  http://blog.1688.com/article/i36511688.html

  http://blog.1688.com/article/i36511754.html

  http://blog.1688.com/article/i36511914.html

  http://blog.1688.com/article/i36512490.html

  http://blog.1688.com/article/i36512550.html

  http://blog.1688.com/article/i36512630.html

  http://blog.1688.com/article/i36512061.html

void InitGraphMode()
{
     int gdriver=DETECT,gmode;
     initgraph(&gdriver,&gmode,"../bgi/");
     cleardevice();
}
void CloseGraphMode()
{
     cleardevice();
     closegraph();
}
void CreateFrame()
{
     setbkcolor(CYAN);                    
//下面的四行代码用于计算主框架的左上角和右下角的坐标
     frame.left=(getmaxx()+1-BlockWidth*RowOfFrame)/2; 
     frame.top=(getmaxy()+1-BlockHeight*ColumnOfFrame)/2;
     frame.right=frame.left+BlockWidth*RowOfFrame;
     frame.bottom=frame.top+BlockHeight*ColumnOfFrame;
     Head(frame.left+100,frame.top-20);
     setfillstyle(SOLID_FILL,LIGHTGRAY);
     bar(frame.left,frame.top,frame.right,frame.bottom);
     setlinestyle(SOLID_LINE,1,1);
     setcolor(DARKGRAY);
     line(frame.left,frame.top,frame.right,frame.top);
     line(frame.left,frame.top,frame.left,frame.bottom);
     setlinestyle(SOLID_LINE,1,1);
     setcolor(WHITE);
     line(frame.left,frame.bottom,frame.right,frame.bottom);
     line(frame.right,frame.top,frame.right,frame.bottom);
     setlinestyle(DOTTED_LINE,1,1);
     setcolor(BLUE);
 
c356
    for(int row=1;row<RowOfFrame;row++)
           line(frame.left+row*BlockWidth,frame.top,frame.left+row*BlockWidth,frame.bottom);
     for(int column=1;column<ColumnOfFrame;column++)
           line(frame.left,frame.top+column*BlockHeight,frame.right,frame.top+column*BlockHeight);
     Foot(frame.left,frame.bottom+20);
}
void CreateSnake()
{
     Node *node1=new Node;
     Node *node2=new Node;
       node1->x=frame.left+BlockWidth;
     node1->y=frame.top;
     node1->next=NULL;
     snake.tail=node1;
     node2->x=frame.left;
     node2->y=frame.top;
     node2->next=snake.tail;
     snake.head=node2;
     snake.length=2;
     setfillstyle(SOLID_FILL,BLUE);
     bar(snake.head->x+1,snake.head->y+1,snake.head->x+BlockWidth-1,snake.head->y+BlockHeight-1);
     bar(snake.tail->x+1,snake.tail->y+1,snake.tail->x+BlockWidth-1,snake.tail->y+BlockHeight-1);


bool PlayGame()
{
     int speed=300,key;
     Direction CurrentDirection=right;
     Node randomNode;
     Node newNode,outNode;
     bool neednode=true;
     bool overlap=false;
     int randx,randy;
     CreateSnake(); 

while(true)
{
     if(neednode==true)
       {
           randomize();
           do
           {
               randx=frame.left+rand()%RowOfFrame*BlockWidth;
               randy=frame.top+rand()%ColumnOfFrame*BlockHeight;  
               for(Node *p=snake.head;p!=NULL;p=p->next)//hit itself
                     if(randx==p->x&&randy==p->y)
                         {overlap=true;break;}
       }
       while(overlap==true);
       randomNode.x=randx;
       randomNode.y=randy;
       randomNode.next=NULL;
       setfillstyle(SOLID_FILL,RED);
       bar(randomNode.x+1,randomNode.y+1,randomNode.x+BlockWidth-1,randomNode.y+BlockHeight-1);
       neednode=false;
   } 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c语言与vb