论坛交流
首页办公自动化| 网页制作| 平面设计| 动画制作| 数据库开发| 程序设计| 全部视频教程
应用视频: Windows | Word2007 | Excel2007 | PowerPoint2007 | Dreamweaver 8 | Fireworks 8 | Flash 8 | Photoshop cs | CorelDraw 12
编程视频: C语言视频教程 | HTML | Div+Css布局 | Javascript | Access数据库 | Asp | Sql Server数据库Asp.net  | Flash AS
当前位置 > 文字教程 > C语言程序设计教程
Tag:新手,函数,指针,数据类型,对象,Turbo,入门,运算符,数组,结构,二级,,tc,游戏,试题,问答,编译,视频教程

几个游戏代码(机器人大战)

文章类别:C语言程序设计 | 发表日期:2008-9-24 14:46:56

*/
#include "graphics.h"
#include "bios.h"
#include "conio.h"
#include "stdlib.h"
#include "dos.h"

#define UP 0x4800
#define DOWN 0x5000
#define LEFT 0x4600
#define RIGHT 0x4d00
#define ESC 0x0016

#define VGA256 0x13
#define TEXT 0x03

int x,y,l,sxzy=0,zx=0,xx=5,yy=167;
int x1=5,y1=3,x3=0,y3=0,x4=0,y4=0;
int youlife=50,mylife=30;
int board[100][170];

unsigned char far *video_buff=(char far *)0xA0000000L;

void setvideomode(int mode)
{ union REGS inregs,outregs;
  inregs.h.ah=0;
  inregs.h.al=(unsigned char)mode;
  int86(0x10,&inregs,&outregs);
}

void v_line(int y0,int y1,int x,unsigned int color)
{ unsigned int address,n,temp;
  address=320*y0+x;
  for(n=0;n<=y1-y0;n++)
    {
      if(y0>y1)
        {
          temp=y1;
          y1=y0;
          y0=temp;
        }
      video_buff[address]=color;
      address+=320;
    }
}

void h_line(int x0,int x1,int y,unsigned int color)
{ unsigned int address,n,temp;
  address=320*y+x0;
  for(n=0;n<=x1-x0;n++)
    {
      if(x0>x1)
        {
          temp=x1;
          x1=x0;
          x0=temp;
        }
      video_buff[address]=color;
      address+=1;
    }
}

void p_pixel(int x,int y,unsigned int color)
{ video_buff[y*320+x]=color;
}

void s_square(int x,int y,int side,int color)
{
  h_line(x,x+side,y,color);
  h_line(x,x+side,y+side,color);
  v_line(y,y+side,x,color);
  v_line(y,y+side,x+side,color);
}

void r_rectangle(int x1,int y1,int x2,int y2,int color)
{
  h_line(x1,x2,y1,color);
  h_line(x1,x2,y2,color);
  v_line(y1,y2,x1,color);
  v_line(y1,y2,x2,color);

}

void fillRectangle(int x1,int y1,int x2,int y2,int color)
{
 int i;
 for(i=y1;i<=y2;i++)
 {
   h_line(x1,x2,i,color);
 }
}


void ground()    /*画外框*/
{ r_rectangle(104,20,207,190,7);
}

void delzx()                           /*每次出拳后,删除拳头*/
{ delay(1000);
  s_square(x3,y3,5,0);
  s_square(x3,y3-6,5,0);
  x3=0;
  y3=0;
}

void me()                             /*画出自己*/
{ int cas=0;
  if(sxzy==18432)                    /*假如按上键,就往上移动*/
    { yy-=5;
      if(yy < y1+30)
        yy+=5;
      if(yy==12)
        yy=17;
    }
  if(sxzy==20480)   /*假如按下键,就往下移动*/
    { yy+=5;
      if(yy==172)
        yy=167;
    }
  if(sxzy==19200)   /*假如按左键,就往左移动*/
    { xx--;
      if(xx==-1)
        xx=0;
    }
  if(sxzy==19712)   /*假如按右键,就往右移动*/
    { xx++;
      if(xx==13)
        xx=12;
    }
  sxzy=0;
  for(x=xx;x<5+xx;x++)   /*给自己搭建骨架*/
     {
       if(x==xx)                        /*左手*/
         {
            y=167;
            board[x][yy]=1;
            if(zx==11386)
              {
         board[x][yy-12]=3;
         board[x][yy-11]=3;
  if((yy-11 < y1+30) && (xx>=x1 && xx<=x1+4))
                  youlife--;
  cas=1;
                zx=0;
              }
            x++;
         }
       if(x==xx+4)   /*右手*/
         {
            y=167;
            board[x][yy]=1;
            if(zx==11640)
              {
  board[x][yy-12]=3;
  board[x][yy-11]=3;
                if((yy-11 < y1+30) && (xx+4<=x1+4 && xx+4>=x1))
                  youlife--;
                cas=2;
                zx=0;
              }
            break;
         }
       for(y=yy;y<yy+3;y++)         /*身体*/
         {
            board[x][y]=1;

         }
     }
  for(x=0;x<100;x++)     /*开始往骨架上放‘肉’*/
    {l=1;
     for(y=0;y<170;y++)
      if(board[x][y]==1 || board[x][y]==3)
 {if(cas==1)
           { board[x][yy-10]=1;
             board[x][yy]=0;
             cas=0;
           }
         if(cas==2)
    { board[x+4][yy-10]=1;
      board[x+4][yy]=0;
             cas=0;
           }
         if(board[x][y]==3)
           {
             x3=x+105+x*5;
             y3=y+l*5;

           }
         s_square(x+105+x*5,y+l*5,5,3);
         l++;
        }
    }
  delzx();
}


void zyou()                           /*搭建设对方骨架*/
{ int ran,cas=0,p=0;
  delay(10000);
  for(x=99;x>=0;x--)
           {
               l=1;
               for(y=169;y>=0;y--)
   if(board[x][y]==2 || board[x][y]==4)
                   {
                       s_square(x+105+x*5,35+y-l*5,5,0);
                       l++;
                       board[x][y]=0;
                   }
           }

   ran=random(5);
   if(ran==0)                       /*往上走*/
     { y1-=5;
       if(y1==-2)
         y1=3;
     }
   if(ran==1)
     { y1+=5;
       if(y1+30 > yy)  /*不答应超过我的机器人的身后*/
         y1-=5;
       if(y1==158)  /*往下走,假如超过外框,就减去一步,实际上是永远也不会超过*/
  y1=153;
     }
   if(ran==2)       /*往左走*/
     { x1--; 
       if(x1==-1)
         x1=0;
     }
   if(ran==3)    /*往右走*/
     { x1++;
       if(x1==13)
         x1=12;
     }
   if(ran==4)
     { y1+=5;
       if(y1+30 > yy)            /*不答应超过我的机器人的身后*/
         y1-=5;
       if(y1==158)               /*往下走,假如超过外框,就减去一步,实际上是永远也不会超过*/
  y1=153;
     }
   for(x=x1;x<x1+5;x++)               /*对方拳头*/
    {
       if(x==x1)
         {  if((y1+45 >= yy) && x>=xx)      /*判定敌人是否靠近,假如是就出拳头*/
       { p=random(2);
                if(p==1)
    {
      board[x][y1+15]=4;
      board[x][y1+16]=4;
      board[x][y1+17]=4;
                    if((yy-11 < y1+30) && (xx+4<=x1+4 && xx+4>=x1))
                      mylife--;
                    cas=1;
                  }
                p=0;
              }
            if(cas==1)
       { board[x][y1+14]=2;
                cas=0;
              }
            else
              board[x][y1]=2;
            x++;
         }
       if(x==x1+4)
         {  if((y1+45 >= yy) && x<=xx+4)
       { p=random(2);
                if(p==1)
    {
      board[x][y1+15]=4;
      board[x][y1+16]=4;
      board[x][y1+17]=4;
                    if((yy-11 < y1+30) && (xx>=x1 && xx<=x1+4))
                      mylife--; 
                    cas=1;
                  }
                p=0;
              }
            if(cas==1)
       { board[x][y1+14]=2;
  cas=0;
              }
            else
              board[x][y1]=2;
            break;
         }
       for(y=y1;y+3>y1;y--)
         {
            board[x][y]=2;
         }
    }
}

void delt()    /*删除对方拳头*/
{ delay(1000);
  s_square(x4,y4+12,5,0);
  s_square(x4,y4+6,5,0);
  s_square(x4,y4,5,0);
  x4=0;
  y4=0;
}

void you()    /*画出对方*/
{

 int key=0;

 while(key!=283 && youlife>0 && mylife>0)
  {
    fillRectangle(250+youlife,50,300,53,0);
    fillRectangle(250+mylife,150,280,153,0);
    while(bioskey(1)==0)
     {   zyou();
         for(x=99;x>=0;x--)
           {
               l=1;
               for(y=169;y>=0;y--)
                 if(board[x][y]==2 || board[x][y]==4)
                   {
                       s_square(x+105+x*5,35+y-l*5,5,3);
                       if(board[x][y]==4)
     {
                            x4=x+105+x*5;
                            y4=35+y-l*5;
                         }
                       l++;

                   }
           }
         delt();
     }
   key=bioskey(0);
   
     if(key==18432 || key==20480 || key==19200 || key==19712 || key==11386 || key==11640)
       {
          if(key==11386 || key==11640)        /*按键的判定*/
            zx=key;       
          else
            sxzy=key;
          key=0;
          for(x=0;x<100;x++)
            {
             l=1;
             for(y=0;y<170;y++)
             if(board[x][y]==1 || board[x][y]==3)
               {
                  s_square(x+105+x*5,y+l*5,5,0);
                  l++;
                  board[x][y]=0;
                  /*getch();*/
               }
            }
          me();
      }
     
  }
 
}

main()
{ int x=0,y,i=1,key,c=0,p,k=0,e=1;
  int gdriver=DETECT,gmode;
  registerbgidriver(EGAVGA_driver);
  initgraph(&gdriver,&gmode,"d:\\tc");
  setbkcolor(BLACK);
  setcolor(WHITE);
  settextstyle(0,0,4);
  outtextxy(100,20,"TIAN YU SOFT");
  setcolor(GREEN);
  outtextxy(101,20,"TIAN YU SOFT");
  settextstyle(1,0,1);
  outtextxy(70,60,"This game is TianYu designed,maybe this game not good,");
  outtextxy(50,80,"but the game is my second game programe.Welcome play my");
  outtextxy(50,100,"game,and make friend.");
  outtextxy(301,250,"E-mainmicrozhujie@citiz.net");
  outtextxy(301,300,"http://angel24.51.net"; );


  getch();
  cleardevice();
  setcolor(RED);
  while(i)
    {
      settextstyle(1,0,5);
      setwritemode(XOR_PUT);
      outtextxy(x,200,"X man");
      delay(1000);
      outtextxy(x,200,"X man");
      x+=2;
      if(x>200)
        i=0;

    }
  outtextxy(x,200,"X man");
  while(!kbhit())
    {
     setcolor(c);
        outtextxy(x,200,"X man");
        c++;
        if(c>16)
          c=0;
    }
  settextstyle(0,0,1);
  setcolor(GREEN);
  outtextxy(300,300,"right->x   left->z");
  outtextxy(300,330,"please press 's' and start");
  while(e)
    {
      k=bioskey(0);
      if(k==8051)
        e=0;
    }
 
      board[99][169]=4;

      closegraph();
      setvideomode(VGA256);
      ground();
      fillRectangle(250,50,250+youlife,53,4);
      fillRectangle(250,150,250+mylife,153,4);
      me();
      you();
      initgraph(&gdriver,&gmode,"d:\\tc");
      if(youlife==0)
       {
         setcolor(WHITE);
         settextstyle(0,0,4);
         outtextxy(100,20,"You Win");
         setcolor(GREEN);
         outtextxy(101,20,"You Win");
         settextstyle(0,0,4);
         setcolor(WHITE);
         outtextxy(100,120,"TIAN YU SOFT");
         setcolor(RED);
         outtextxy(101,120,"TIAN YU SOFT");
         outtextxy(301,300,"2002/10/11");
         getch();
         getch();
         getch();
       }
      else if(mylife==0)
       {
         setcolor(WHITE);
         settextstyle(0,0,4);
         outtextxy(100,20,"You Lost");
         setcolor(GREEN);
         outtextxy(101,20,"You Lost");
         settextstyle(0,0,4);
         setcolor(WHITE);
         outtextxy(100,120,"TIAN YU SOFT");
         setcolor(RED);
         outtextxy(101,120,"TIAN YU SOFT");
         outtextxy(301,300,"2002/10/11");
         getch();
         getch();
         getch();
       }
      else
        {
          settextstyle(0,0,4);
          setcolor(WHITE);
          outtextxy(100,120,"TIAN YU SOFT");
          setcolor(RED);
          outtextxy(101,120,"TIAN YU SOFT");
          outtextxy(301,300,"2002/10/11");
        }
      getch();
      closegraph();
      setvideomode(TEXT);
    
 
}

上一篇:{应用}几个游戏代码(麻将原代码) 人气:4864
下一篇:{应用}潜艇大战 人气:4988
视频教程列表
文章教程搜索
 
C语言程序设计推荐教程
C语言程序设计热门教程
看全部视频教程
购买方式/价格
购买视频教程: 咨询客服
tel:15972130058