| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1375 人关注过本帖
标题:easyX 库下的俄罗斯方块 消除方快时,函数,setwritemode()那里是不是用错了 ...
取消只看楼主 加入收藏
少林小和尚
Rank: 2
等 级:论坛游民
帖 子:57
专家分:26
注 册:2013-3-31
结帖率:73.68%
收藏
已结贴  问题点数:20 回复次数:1 
easyX 库下的俄罗斯方块 消除方快时,函数,setwritemode()那里是不是用错了?后面出现的会把前面的覆盖,求改正
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<time.h>

#define box_size 9 //方块大小   方块之间有间隔  且为1
#define m_x0 100  //左上角坐标
#define m_y0 100

#define m_mx 300    //右下角坐标
#define m_my 400

#define m_row 30            //从上到下为X轴
#define m_col 20            //从左到右为Y轴

int m_max_x;    //最大X
int m_max_y;    //最大Y
int p[30][20]={0};       //整形数组标记方块     //初始化  0代表没有    1代表有
int b[4];         
int temp,temp1,temp2;      //temp,temp1,temp2用于记住和转换方块变量的值

//声明俄罗斯方块的结构体
struct T
{
    int x;     //中心方块的x轴下标
    int y;     //中心方块的y轴下标
    int flag;    //标记方块类型的序号
    int next;    //下一个俄罗斯方块类型的序号
    int speed;    //俄罗斯方块移动的速度
    int count;    //产生俄罗斯方块的个数
    int score;    //游戏的分数
    int level;    //游戏的等级
};

void Init_system();
void count_x_y_by_i_j(int i,int j,int *x,int *y);  //利用数组下标 i j  计算    坐标  x y

void make_one(struct T *t);
void make(struct T * t);
void get_flag(struct T * t);
void print(struct T *t);
int if_moveable(struct T *t);
void clear(struct T *t);
void del_full(struct T *t);
void start_game();




int main()
{
     Init_system();
     start_game();
     getch();
     closegraph();
    return 0;
}
//制作一个方块
void make_one(int i,int j) //传入小标
{
    int x,y;  //坐标
    count_x_y_by_i_j(i,j,&x,&y);
   
    setcolor(WHITE);
    rectangle(x,y,x+box_size,y+box_size);
    //setfillcolor(LIGHTGRAY);
    //floodfill((2*x+box_size)/2,(2*y+box_size)/2,WHITE);
}
void make(struct T * t)
{
    p[t->x][t->y]=b[0];      //x   y表示坐标
    switch(t->flag)
    {
    case 1:                //田   //中心方块为右上角
        {
            p[t->x][t->y-1]=b[1];
            p[t->x+1][t->y-1]=b[2];
            p[t->x+1][t->y]=b[3];
            break;
        }
    case 2:                  //直线方块:----           //中心方块为第三块
        {
            p[t->x][t->y-1]=b[1];
            p[t->x][t->y-2]=b[2];
            p[t->x][t->y+1]=b[3];
            break;
        }
        case 3:         //直线方块: |            //第三块   
        {  
            p[t->x-2][t->y]=b[1];
            p[t->x-1][t->y]=b[2];
            p[t->x+1][t->y]=b[3];
            break;
        }
    case 4:         //T字方块            //  横线第二个
        {  
            p[t->x][t->y-1]=b[1];
            p[t->x][t->y+1]=b[2];
            p[t->x+1][t->y]=b[3];
            break;
        }
    case 5:         //T字顺时针转90度方块
        {  
            p[t->x-1][t->y]=b[1];
            p[t->x+1][t->y]=b[2];
            p[t->x][t->y-1]=b[3];
            break;
        }
    case 6:         //T字顺时针转180度方块
        {  
            p[t->x][t->y-1]=b[1];
            p[t->x][t->y+1]=b[2];
            p[t->x-1][t->y]=b[3];
            break;
        }
    case 7:         //T字顺时针转270度方块
        {  
            p[t->x-1][t->y]=b[1];
            p[t->x+1][t->y]=b[2];
            p[t->x][t->y+1]=b[3];
            break;
        }


    case 8:         //Z字方块         上排第二个
        {  
            p[t-> x][t->y-1]=b[1];
            p[t->x+1][t->y]=b[2];
            p[t->x+1][t->y+1]=b[3];
            break;
        }
    case 9:         //Z字顺时针转90度方块
        {  
            p[t->x-1][t->y]=b[1];
            p[t->x][t->y-1]=b[2];
            p[t->x+1][t->y-1]=b[3];
            break;
        }
    case 10:        //Z字顺时针转180度方块
        {  
            p[t->x][t->y+1]=b[1];
            p[t->x-1][t->y]=b[2];
            p[t->x-1][t->y-1]=b[3];
            break;
        }
    case 11:        //Z字顺时针转270度方块
        {  
            p[t->x-1][t->y+1]=b[1];
            p[t->x][t->y+1]=b[2];
            p[t->x+1][t->y]=b[3];
            break;
        }



    case 12:        //7字方块       //以竖线第2个为中心
        {  
            p[t->x-1][t->y-1]=b[1];
            p[t->x-1][t->y]=b[2];
            p[t->x+1][t->y]=b[3];
            break;
        }
    case 13:        //7字顺时针转90度方块
        {  
            p[t->x][t->y-1]=b[1];
            p[t->x][t->y+1]=b[2];
            p[t->x-1][t->y-1]=b[3];
            break;
        }

    case 14:        //7字顺时针转180度方块
        {  
            p[t->x-1][t->y]=b[1];
            p[t->x+1][t->y]=b[2];
            p[t->x][t->y+1]=b[3];
            break;
        }


    case 15:        //7字顺时针转270度方块
        {
            p[t->x][t->y-1]=b[1];
            p[t->x][t->y+1]=b[2];
            p[t->x+1][t->y-1]=b[3];
            break;
        }




    case 16:        //倒7字方块     //以竖线第3个为中心
        {
            p[t->x-1][t->y]=b[1];
            p[t->x-1][t->y+1]=b[2];
            p[t->x+1][t->y]=b[3];
            break;
        }


    case 17:        //倒7字顺指针转90度方块
        {
            p[t->x][t->y-1]=b[1];
            p[t->x][t->y+1]=b[2];
            p[t->x+1][t->y+1]=b[3];
            break;
        }




    case 18:        //倒7字顺时针转180度方块
        {  
            p[t->x-1][t->y]=b[1];
            p[t->x+1][t->y]=b[2];
            p[t->x+1][t->y-1]=b[3];
            break;
        }

    case 19:        //倒7字顺时针转270度方块
        {  
            p[t->x][t->y-1]=b[1];
            p[t->x][t->y+1]=b[2];
            p[t->x-1][t->y-1]=b[3];
            break;
        }        //表示下标
    }
}



void get_flag(struct T * t)
{
    t->count++;
    srand((unsigned)time(NULL));
    if(t->count==1)
    {
        t->flag=rand()%19+1;
    }
    t->next=rand()%19+1;
}

void print(struct T *t)
{
    int i,j; //数组下标
    int x,y; //对应坐标
    for(int k=0;k<4;k++)
    {
        b[k]=1;
    }
    make(t);
    for(i=t->x-2;i<t->x+1;i++)
    {
        for(j=t->y-2;j<t->y+1;j++)
        {
            count_x_y_by_i_j(i,j,&x,&y);
            if(p[i][j]==1 && x>m_x0 &&y>m_y0)
            {
                make_one(i,j);
            }
        }
    }
    //输出其他信息  测试
        settextcolor(BLUE);
    outtextxy(m_x0+m_mx+50, m_y0+20,"10000");
    settextcolor(RED);
    outtextxy(m_x0+m_mx+50,m_y0+50, "3");
}


int if_moveable(struct T *t)
{
    if(p[t->x][t->y]!=0)  //中心方块有   不能移动
        return 0;
    else
    {
        if(( t->flag==1  && ( p[t->x][t->y-1]==0 && p[t->x+1][t->y-1]==0 && p[t->x+1][t->y]==0) ) ||
            //直线方块且除中心方块位置外,其他"口"字方块位置上无图案时,返回值为1,即可移动
            
            ( t->flag==2  && ( p[t->x][t->y-1]==0 && p[t->x][t->y-2]==0 && p[t->x][t->y+1]==0))    ||
        
            ( t->flag==3  && (p[t->x-2][t->y]==0 && p[t->x-1][t->y]==0 && p[t->x+1][t->y]==0) )    ||
            
            ( t->flag==4  && ( p[t->x][t->y-1]==0 && p[t->x][t->y+1]==0 && p[t->x+1][t->y]==0)     ||
            
            ( t->flag==5  && (p[t->x-1][t->y]==0 && p[t->x+1][t->y]==0 && p[t->x][t->y-1]==0) )    ||
            
            ( t->flag==6  && (p[t->x][t->y-1]==0 &&  p[t->x][t->y+1]==0 && p[t->x-1][t->y]==0) )   ||
            
            ( t->flag==7  && ( p[t->x-1][t->y]==0 && p[t->x+1][t->y]==0 && p[t->x][t->y+1]==0) )   ||
            
            ( t->flag==8  && p[t-> x][t->y-1]==0 &&p[t->x+1][t->y]==0  &&  p[t->x+1][t->y+1]==0)) ||
            
            ( t->flag==9  && ( p[t->x-1][t->y]==0 &&    p[t->x][t->y-1]==0 && p[t->x+1][t->y-1]==0) ) ||
            
            ( t->flag==10 && (p[t->x][t->y+1]==0 && p[t->x-1][t->y]==0 && p[t->x-1][t->y-1]==0))   ||
            
            ( t->flag==11 && ( p[t->x-1][t->y+1]==0 && p[t->x][t->y+1]==0 && p[t->x+1][t->y]==0)) ||
            
            ( t->flag==12 && ( p[t->x-1][t->y-1]==0 && p[t->x-1][t->y]==0 && p[t->x+1][t->y]==0 ) ) ||
            
            ( t->flag==13 && (p[t->x][t->y-1]==0 && p[t->x][t->y+1]==0 && p[t->x-1][t->y-1]==0) )  ||
            
            ( t->flag==14 && (p[t->x-1][t->y]==0 && p[t->x+1][t->y]==0 && p[t->x][t->y+1]==0) )    ||
            
            ( t->flag==15 && (p[t->x][t->y-1]==0 && p[t->x][t->y+1]==0 && p[t->x+1][t->y-1]==0) )   ||
            
            ( t->flag==16 && ( p[t->x-1][t->y]==0 && p[t->x-1][t->y+1]==0 && p[t->x+1][t->y]==0) ) ||
            
            ( t->flag==17 && ( p[t->x][t->y-1]==0 &&p[t->x][t->y+1]==0 && p[t->x+1][t->y+1]==0) ) ||
            
            ( t->flag==18 && (p[t->x-1][t->y]==0 && p[t->x+1][t->y]==0 && p[t->x+1][t->y-1]==0) )   ||
            
            ( t->flag==19 && ( p[t->x][t->y-1] ==0 && p[t->x][t->y+1]==0 && p[t->x-1][t->y-1]==0) )
          )
          return 1;
    }
     return 0;
}

//下降时的清除

void clear(struct T *t)
{
    int i,j;   //下标
    int x,y;    //坐标
    for(int k=0;k<4;k++)
    {
        b[k]=0;
    }
    make(t);    //将方块重新质数
    for(i=t->x-2;i<t->x+1;i++)
    {
        for(j=t->y-2;j<t->y+1;j++)
        {
            count_x_y_by_i_j(i,j,&x,&y);
            if(p[i][j]==1 && x>m_x0 &&y>m_y0)
            {
                 //setcolor(LIGHTGRAY);
                 //setlinestyle(PS_SOLID,1,0,1);
                setwritemode(7);
                rectangle(x,y,x+box_size,y+box_size);
                //setwritemode(0);
                //    rectangle(x,y,x+box_size,y+box_size);
            }
        }
    }
}

//判断是否满行
void del_full(struct T *t)
{
    int k,del_count=0;  //k 记录某行方块的个数,del_count删除行的个数
    int i,j;   //数组下标
    for(i=m_row-1;i>=0;i--)
    {
        k=0;
        for(j=0;j<m_col;j++)
        {
            if(p[i][j]==1)
            {
                k++;            //该行已满
                if(k==m_col-1)
                {
                    for(k=0;k<m_col;k++)
                    {
                        p[i][k]=0;   //置零清空
                        outtextxy(400,400,"1");
                        Sleep(1);
                    }
                    //删除行上面有方块,清除,下移
                    for(k=i-1;k>0;k--)
                    {
                        for(j=0;j<m_col;j++)
                        {
                            if(p[k][j]==1)
                            {
                                //p[][]
                                outtextxy(300,300,"0");
                            p[k+1][j]=1;
                            make_one(k+1,j);
                            }
                        }
                    }
                    i++;   //重新判断是否满行
                    del_count++;
                }
            }
        }
    }
    t->score+=del_count*10;
    if(del_count>0 && (t->score%100)==0)
    {
        t->speed-=20;
        t->level++;
    }
}

void start_game()
{
    struct T t,*te;
    te=&t;
    char ch;
    int i;
    //初始化
    te->count=0;
    te->speed=300;
    te->score=0;
    te->level=1;
    while(1)
    {
        get_flag(te);         //产生方块序号
        temp=te->flag;         //得到当前方块序号
        //输出下一个方块
        te->x=25;
        te->y=30;
        te->flag=te->next;   //得到下一个方块序号
        print(te);          //打印下一个方块序号
        //初始化中心方块的坐标
        te->x=1;
        te->y=10;
        te->flag=temp;    //得到第一个方块的序号
        while(1)
        {
lab:
        print(te);           //打印当前的方块呢
        Sleep(te->speed);    //利用当前速度延时
        clear(te);
        temp1=te->y;         //记住当前中心的横坐标
        temp2=te->flag;      //记住当前俄罗斯方块的序号
        if(kbhit())
        {
            ch=getch();
            if(ch==75 && te->y-2>0 &&te->y+1<20)       //左移
            {
                te->y-=1;
            }
            if(ch==77 && te->y-2>0 &&te->y+1<20)      //右移
            {
                te->y+=1;
            }
        //    if(ch== )
            if(ch==72)   //变形
            {
                if( te->flag>=2 && te->flag<=3 )
              {
                  te->flag++;
                  te->flag%=2;
                  te->flag+=2;
              }
              if( te->flag>=4 && te->flag<=7 )
              {
                  te->flag++;
                  te->flag%=4;
                  te->flag+=4;
              }   
              if( te->flag>=8 && te->flag<=11 )
              {
                  te->flag++;
                  te->flag%=4;
                  te->flag+=8;
              }   
              if( te->flag>=12 && te->flag<=15 )
              {
                  te->flag++;
                  te->flag%=4;
                  te->flag+=12;
              }   
              if( te->flag>=16 && te->flag<=19 )
              {
                  te->flag++;
                  te->flag%=4;
                  te->flag+=16;
              }
            }   //if(ch==72)

            if(ch==32)     //暂停
            {
                print(te);
                while(1)
                {
                    if(kbhit())
                    {
                        ch=getch();
                            if(ch==32)
                            {
                                goto lab;
                            }
                    }
                }
            }
            if(if_moveable(te)==0)   //不能移动
            {
                te->y=temp1;               //以上操作无效   恢复中心方块的坐标及标记
                te->flag=temp2;
            }
            else         //可动
            {
                goto lab;
            }
        }   //if(kbhit())
        //没有操作
        if(te->x+2<30)
        te->x++;        //方块中心坐标下移
        else
            print(te);

        if(if_moveable(te)==0)   //不能移动
        {
            te->x--;   //恢复
            print(te);
            del_full(te);   //是否满行
            break;
        }
        }

        for(i=te->x-2;i<te->x+2;i++)
        {
            if(i==0)
            {
                outtextxy(10,10,"777");
                break;
            }
        }

        //清除右边窗口的俄罗斯方块
        te->flag=te->next;
        te->x=25;
        te->y=30;
        clear(te);
    }   
}








//利用数组下标 i j  计算    坐标  x y
void count_x_y_by_i_j(int i,int j,int *x,int *y)
{
    *x=m_x0+j*(box_size+1);
    *y=m_y0+i*(box_size+1);
}


void Init_system()
{
    int gdriver,gmode;
    initgraph(&gdriver,&gmode," ");
    setcolor(RED);
    rectangle(m_x0,m_y0,m_mx,m_my);
    //setfillcolor(LIGHTGRAY);
    //floodfill((m_x0+m_mx)/2,(m_y0+m_my)/2,RED);
    settextcolor(BLUE);
    outtextxy(m_x0+m_mx, m_y0+20,"分数");
    settextcolor(RED);
    outtextxy(m_x0+m_mx,m_y0+50, "等级");
    settextcolor(LIGHTBLUE);
    outtextxy(m_x0+m_mx,m_y0+150, "下一个");
}




能不能不Sleep函数换成其他函数来模仿等级,Sleep函数下降时太难看了  跪求大神。。。
搜索更多相关主题的帖子: 俄罗斯方块 include 
2014-07-26 16:07
少林小和尚
Rank: 2
等 级:论坛游民
帖 子:57
专家分:26
注 册:2013-3-31
收藏
得分:0 
回复 2 楼 pauljames
我想知道  我这个还能不能改。能的话,求大神  指导。。。。
2014-07-27 09:28
快速回复:easyX 库下的俄罗斯方块 消除方快时,函数,setwritemode()那里是不是用 ...
数据加载中...
 
   



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

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