aoshiwuji

 

两个月C++学习----------俄罗斯方块(能储存哦)

#include <iostream>
#include <fstream>
using namespace std;
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include "graphics.h"
#include <conio.h>

void suiji(int array[][4]);
void beixuan();
void fuzhi(int w[][4],int array[][4]);
void jiafen();
void begain();
void end();
void rongqi(int,int);
void huatu1(int,int,int array[][4]);
void huatu2(int,int,int array[][4]);
void bian(int,int,int array[][4]);
int xiace(int,int,int array[][4]);
int leftce(int,int,int array[][4]);
int rightce(int,int,int array[][4]);
void cunchu();
void du();

int h[4][4],w[4][4],l[4][4],shu=0,
    a[4][4]={0,0,0,0,
             0,1,1,0,
          0,1,1,0,
          0,0,0,0},
 b[4][4]={0,0,0,0, 
             0,1,0,0,
       1,1,1,0,
          0,0,0,0}, 
    c[4][4]={0,1,0,0,
       0,1,0,0,
          0,1,1,0,
    0,0,0,0},
 d[4][4]={0,0,1,0,
          0,0,1,0,
    0,1,1,0,
       0,0,0,0},
 e[4][4]={0,1,0,0,
          0,1,0,0,
    0,1,0,0,
    0,1,0,0},
 f[4][4]={0,0,0,0,
          0,1,1,0,
    1,1,0,0,
    0,0,0,0},
    g[4][4]={0,0,0,0,
          1,1,0,0,
    0,1,1,0,
    0,0,0,0},
    z[21][17]={1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
               1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
      1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
      1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
               1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
      1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
         1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
      1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
      1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
      1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
         1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
         1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
         1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
         1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
         1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
         1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
            1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
         1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
         1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
         1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
         1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
bool over=false;
char fen[2];

int main()

 //初始化图形界面
 begain();
 //是否从上次继续
 du();
 //随机生成
 suiji(w);
 //绘制移动块
 do
 {
 beixuan();
 huatu2(170,-10,w);
 end();
 if(over) break;
 fuzhi(w,l);
 }while(1);
 //结束前任意键暂停
 getchar();
 return 0;
}

 void begain()
{
 initgraph(640,480);
 //清屏
 cleardevice();
 //画备选区
 setcolor(LIGHTGREEN);                    //设为亮绿色
 setlinestyle(SOLID_LINE,0,THICK_WIDTH);  //设置边线线型为粗线
 rectangle(430,30,550,150);
 //输出文字
 SetFont(0,0,0,0,0,FALSE,FALSE,FALSE,"宋体"); //文字设为宋体
 outtextxy(400,180,"成绩: 0");                                     
 outtextxy(455,225,"游戏说明");
 outtextxy(430,245,"w: 旋转  s:加速");
 outtextxy(430,265,"a: 左移  d:右移");
 outtextxy(430,285,"空格:暂停 c: 存储");
 outtextxy(455,315,"软件声明");
 outtextxy(430,340,"本游戏 朱明友 编写");
 outtextxy(430,360,"仅供学习");
 outtextxy(430,380,"如涉版权");
 outtextxy(430,400,"敬请谅解");
 setcolor(RED);                    //设为红色
 setlinestyle(SOLID_LINE,0,THICK_WIDTH);  //设置边线线型为粗线
 rectangle(400,215,580,430);
  //画容器
 setfillstyle(SOLID_FILL,LIGHTBLUE); //将容器边框颜色设为亮蓝色
 rongqi(30,30);
}

 int m,n,r,s,ce=0;

 void rongqi(int x,int y)
 { 
     for(m=0;m<21;m++)
   for(n=0;n<17;n++)
    {if(z[m][n]==1)
     bar(x+20*n,y+20*m,x+18+20*n,y+18+20*m);
     }
}

 void huatu1(int x,int y,int array[][4])
{
       for(m=0;m<4;m++)
  for(n=0;n<4;n++)
       if(array[m][n]==1)     
          bar(x+20*n,y+20*m,x+18+20*n,y+18+20*m);
      
}

 void huatu2(int x,int y,int array[][4])
 {
   int i,k,t,oldk,j,oldj,u=0,v=0,time=500;
      setfillstyle(SOLID_FILL,LIGHTRED);
      huatu1(170,-10,w);
      k=oldk=x;j=oldj=y;  
      for(i=1;ce==0;i++) 
   {     //画旧的
      for(m=0;m<4;m++)
    for(n=0;n<4;n++)
        if(w[m][n]==1)
       {setfillstyle(SOLID_FILL,BLACK);    //旧位置擦除  设置黑色底色
        bar(oldk+20*n,oldj+20*m+(i-1)*20,oldk+18+20*n,oldj+18+20*m+(i-

1)*20);
           }                      
           if(kbhit())                  //键盘检测
      {
          t=getch();           //读入键盘按键
                   switch(t)            //键盘控制
                        {case 'A':
                         case 'a': leftce(i,u,w);
              if(ce==1) break;
           k-=20;u--; break;        

     //向左移动
                         case 'D':
                         case 'd': rightce(i,u,w);                //向右移动
              if(ce==1) break;
              k+=20;u++; break;            

 
                         case 'S':
                         case 's': time=time/3; break;            //向下移动
       case 'W':
       case 'w': bian(i,u,w);break;   

  //翻转变形
                         case ' ': getch();break;     //暂停
       case 'C':
       case 'c': cunchu();
       }        

                                        
            }
      ce=0;
      xiace(i,u,w);
            //画新的
            for(m=0;m<4;m++)
    for(n=0;n<4;n++)  
     if(w[m][n]==1)
       {setfillstyle(SOLID_FILL,LIGHTRED);
              bar(k+20*n,j+20*m+i*20,k+18+20*n,j+18+20*m+i*20);
              }                     
   Sleep(time);  //延迟时间
   oldk=k;oldj=j;//把新的赋给旧的
     }
     //赋给容器
     for(m=0;m<=3;m++)
    for(n=0;n<=3;n++)
     if(w[3-m][n]==1) z[i-m][7+u+n]=1;

        //消行
        for(m=0;m<=19;m++)            //检测是否满行
  { for(n=1;n<=15;n++)
       if(z[m][n]==1) v++;
    else break; 
    if(v==15)    
    {jiafen();
     for(n=1;n<=15;n++)         //特效
       {setfillstyle(LINE_FILL,LIGHTGREEN);
        bar(50+20*(n-1),30+20*m,50+18+20*(n-1),30+18+20*m);
        }
     Sleep(200);  //延迟时间
     for(r=0;r<20;r++) 
      for(s=1;s<=15;s++)
       if(z[r][s]==1)
      {setfillstyle(SOLID_FILL,BLACK);
          bar(50+20*(s-1),30+20*r,50+18+20*(s-1),30+18+20*r);
          }
     for(r=m;r>=1;r--)
   for(s=1;s<=15;s++)
    z[r][s]=z[r-1][s];   
           for(r=0;r<20;r++)
      for(s=1;s<=15;s++)
       if(z[r][s]==1)
    {setfillstyle(SOLID_FILL,LIGHTRED); //将容器内部颜色设为亮红色
           bar(50+20*(s-1),30+20*r,50+18+20*(s-1),30+18+20*r);
       }               
     }
    v=0;  
  }
  ce=0;   
 }

void suiji(int array[][4])
{
        srand((int) time(NULL));  
        switch (char('a'+rand()%('g'-'a'+1)))
  {case 'a': fuzhi(array,a);break;
   case 'b': fuzhi(array,b);break;
   case 'c': fuzhi(array,c);break;
   case 'd': fuzhi(array,d);break;
   case 'e': fuzhi(array,e);break;
    case 'f': fuzhi(array,f);break;
   case 'g': fuzhi(array,g);break;}
}

void fuzhi(int w[][4],int array[][4])
{
 for(m=0;m<=3;m++)
       for(n=0;n<=3;n++)
   w[m][n]=array[m][n];
}

void beixuan()

    setfillstyle(SOLID_FILL,BLACK);
 huatu1(450,50,w);
    suiji(l);
    setfillstyle(SOLID_FILL,LIGHTRED);
 huatu1(450,50,l);
}

void bian(int i,int u,int w[4][4])
{
      for(m=0;m<=3;m++)
   for(n=0;n<=3;n++)
    h[m][n]=w[3-n][m];
   leftce(i,u,h);
      rightce(i,u,h);
   xiace(i,u,h);
   if(ce==0)
   { for(m=0;m<=3;m++)
   for(n=0;n<=3;n++)
    w[m][n]=h[m][n];
   }
}

int leftce(int i,int u,int array[][4] )
{
        for(m=0;m<=3;m++)
   for(n=0;n<=3;n++)
   {
    if(array[3-m][n]==1)
    if(z[i+1-m][6+u+n]==1)
    {
                 ce=1;
     break;
    }
    if(ce==1) break;
   }
    return ce;
 }

int rightce(int i,int u,int array[][4] )
{
        for(m=0;m<=3;m++)
   for(n=0;n<=3;n++)
   {
    if(array[3-m][3-n]==1)
    if(z[i+1-m][11+u-n]==1)
    {
                 ce=1;
     break;
    }
    if(ce==1) break;
   }
    return ce;
}

int xiace(int i,int u,int array[][4] )
 {
  for(m=0;m<=3;m++)
   for(n=0;n<=3;n++)
   {
    if(array[3-m][n]==1)
    if(z[i+2-m][7+u+n]==1)
    {
                 ce=1;
     break;
    }
    if(ce==1) break;
   }
    return ce;
 }
void cunchu()
{
 char Q;
 SetFont(0,0,0,0,0,FALSE,FALSE,FALSE,"宋体"); //文字设为宋体
    outtextxy(480,170,"是否确定存储");
    outtextxy(490,190,"Y?  N?");
 outtextxy(490,190,"Y?  N?");
 Q=getchar();
 setfillstyle(SOLID_FILL,BLACK);
 bar(480,160,610,210);
 if(Q=='y')
 {SetFont(0,0,0,0,0,FALSE,FALSE,FALSE,"宋体"); //文字设为宋体
  outtextxy(480,170,"游戏已储存");
  ofstream outfile("f1.dat",ios::out);
     for(m=0;m<20;m++)
  {for(n=1;n<16;n++)
      outfile<<z[m][n]<<' ';
   outfile<<endl;}  
  outfile<<shu;
 outfile.close();} 
}
void du()
{
 int data[21][17],Q;
 SetFont(0,0,0,0,0,FALSE,FALSE,FALSE,"宋体"); //文字设为宋体
    outtextxy(480,170,"是否从上一次继续");
 outtextxy(490,190,"Y?  N?");
    outtextxy(490,190,"Y?  N?");
 Q=getchar();
 setfillstyle(SOLID_FILL,BLACK);
 bar(480,160,610,210);
 if(Q=='y')
 {ifstream infile("f1.dat",ios::in);
     for(m=0;m<20;m++)
  for(n=1;n<16;n++)
  {infile>>data[m][n];
   z[m][n]=data[m][n];
   if(z[m][n]==1)
   {setfillstyle(SOLID_FILL,LIGHTRED); //将容器内部颜色设为亮红色
       bar(50+20*(n-1),30+20*m,50+18+20*(n-1),30+18+20*m);
    }
  }
 setfillstyle(SOLID_FILL,BLACK);
 bar(450,180,465,200);
    infile>>data[20][0];
 shu=data[20][0];
    sprintf(fen,"%d",shu);
 SetFont(0,0,0,0,0,FALSE,FALSE,FALSE,"宋体"); //文字设为宋体
    outtextxy(457,180,fen);
 infile.close();
 }
}
   
void jiafen()
{
 shu++;
    setfillstyle(SOLID_FILL,BLACK);
 bar(450,180,470,200);
 sprintf(fen,"%d",shu);
 SetFont(0,0,0,0,0,FALSE,FALSE,FALSE,"宋体"); //文字设为宋体
    outtextxy(457,180,fen);
}

void end()
{
 for(n=1;n<=15;n++)
  if(z[0][n]==1)
  {
   over=true;
   setcolor(MAGENTA); 
         SetFont(40,40,0,0,0,FALSE,FALSE,FALSE,"宋体"); //文字设为宋体
         outtextxy(40,200,"结束游戏");
   getchar();
   }
  
}
 

 

 

posted on 2011-12-18 16:26 朱明 阅读(146) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航:   博客园   博客园最新博文   博问   管理


导航

统计

常用链接

留言簿

随笔档案

文章档案

搜索

最新评论