| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 553 人关注过本帖
标题:那位了解C语言时钟中断函数帮忙看一下,这个俄罗斯方块游戏无法运行
只看楼主 加入收藏
Explorerlxz
Rank: 9Rank: 9Rank: 9
来 自:zzu
等 级:蜘蛛侠
威 望:4
帖 子:302
专家分:1032
注 册:2013-4-24
结帖率:100%
收藏
 问题点数:0 回复次数:0 
那位了解C语言时钟中断函数帮忙看一下,这个俄罗斯方块游戏无法运行
#include<stdio.h>
#include<stdlib.h>
#include<dos.h>
#include<graphics.h> /*图形函数库*/
/*定义按键码*/
#define VK_LEFT 0x4b00
#define VK_RIGHT 0x4d00
#define VK_DOWN 0x5000
#define VK_UP 0x4800
#define VK_ESC 0x011b
#define TIMER 0x1c /*设置中断号*/

/*定义常量*/
#define MAX_BOX 19
/*总共有19种形态的游戏方块*/
#define BSIZE 20
/*游戏方块的边长是20个像素*/
#define Sys_x 160
/*显示游戏方块界面的左上角x坐标*/
#define Sys_y 25
/*显示游戏方块界面的左上角y坐标*/
#define Horizontal_boxs 10
/*水平的方向以小方块为单位的长度*/
#define Vertical_boxs 15
/*垂直的方向以小方块为单位的长度,也就说长是15个小方块*/
#define Begin_boxs_x Horizontal_boxs/2
#define FgColor 3
#define BgColor 0
#define LeftWin_x Sys_x+Horizontal_boxs*BSIZE+46
#define false 0
#define true 1
#define MoveLeft 1
#define MoveRight 2
#define MoveDown 3
#define MoveRoll 4
int current_box_numb;
int Curbox_x=Sys_x+Begin_boxs_x*BSIZE,Curbox_y=Sys_y;
int flag_newbox=false;
int speed=1;
int score=0;
int speed_step=30;
void interrupt (*oldtimer)(void);
struct BOARD
{
    int var;
    int color;
} Table_board[Vertical_boxs][Horizontal_boxs];
struct SHAPE
{
    char box[2];
    int color;
    int next;
};
struct SHAPE shapes[MAX_BOX]=
{
    {0x88, 0xc0, CYAN, 1},
    {0xe8, 0x0, CYAN, 2},
    {0xc4, 0x40, CYAN, 3},
    {0x2e, 0x0, CYAN, 0},
    {0x44, 0xc0, MAGENTA, 5},
    {0x8e, 0x0, MAGENTA, 6},
    {0xc8, 0x80, MAGENTA, 7},
    {0xe2, 0x0, MAGENTA, 4},
    {0x8c, 0x40, YELLOW, 9},
    {0x6c, 0x0, YELLOW, 8},
    {0x4c, 0x80, BROWN, 11},
    {0xc6, 0x0, BROWN, 10},
    {0x4e, 0x0, WHITE, 13},
    {0x8c, 0x80, WHITE, 14},
    {0xe4, 0x0, WHITE, 15},
    {0x4c, 0x40, WHITE, 12},
    {0x88, 0x88, RED, 17},
    {0xf0, 0x0, RED, 16},
    {0xcc, 0x0, BLUE, 18}
};
unsigned int TimerCounter=0;
void main()
{
    int GameOver=0;
    int key,nextbox;
    int Currentaction=0;
    int gd=VGA,gm=VGAHI,errorcode;
    initgraph(&gd,&gm,"");
    errorcode=graphresult();
    if (errorcode!=grOk)
    {
        printf("\nNotice:Graphics error: %s\n",grapherrormsg(errorcode));
        printf("Press any key to quit!");
        getch();
        exit(1);
    }
    setbkcolor(BgColor);
    setcolor(FgColor);
    randomize();
    SetTimer(newtimer);
    initialize(Sys_x,Sys_y,Horizontal_boxs,Vertical_boxs);
    nextbox=MkNextBox(-1);

show_box(Curbox_x,Curbox_y,current_box_numb,shapes[current_box_numb].color);
    show_box(LeftWin_x,Curbox_y+200,nextbox,shapes[nextbox].color);
    show_help(Sys_x,Curbox_y+320);
    getch();
    while(1)
    {
        Currentaction=0;
        flag_newbox=false;
        if(bioskey(1)){key=bioskey(0);    }
        else{key=0;    }
        switch(key)
        {
            case VK_LEFT:
                if(MoveAble(Curbox_x,Curbox_y,current_box_numb,MoveLeft))
                {
                    EraseBox(Curbox_x,Curbox_y,current_box_numb);
                    Curbox_x-=BSIZE;
                    Currentaction=MoveLeft;
                }
                    break;
            case VK_RIGHT:
                if(MoveAble(Curbox_x,Curbox_y,current_box_numb,MoveRight))
                {EraseBox(Curbox_x,Curbox_y,current_box_numb);
                Curbox_x+=BSIZE;
                Currentaction=MoveRight;
                }
                    break;
            case VK_DOWN:
                if(MoveAble(Curbox_x,Curbox_y,current_box_numb,MoveDown))
                {
                    EraseBox(Curbox_x,Curbox_y,current_box_numb);
                    Curbox_y+=BSIZE;
                    Currentaction=MoveDown;
                }
                else flag_newbox=true;
                break;
            case VK_UP:
                if(MoveAble(Curbox_x,Curbox_y,shapes[current_box_numb].next,MoveRoll))
                {
                    EraseBox(Curbox_x,Curbox_y,current_box_numb);
                    current_box_numb=shapes[current_box_numb].next;
                    Currentaction=MoveRoll;
                }
                break;
            case VK_ESC:
                GameOver=1;
                break;
            default:
                break;
        }
        
        if(Currentaction)
        {
            show_box(Curbox_x,Curbox_y,current_box_numb,shapes[current_box_numb].color);
            Currentaction=0;
        }
        if(flag_newbox)
        {
            ErasePreBox(LeftWin_x,Sys_y+200,nextbox);
            nextbox=MkNextBox(nextbox);
            show_box(LeftWin_x,Sys_y+200,nextbox);
            nextbox=MkNextBox(nextbox);
            show_box(LeftWin_x,Curbox_y+200,nextbox,shapes[nextbox].color);
            if(!MoveAble(Curbox_x,Curbox_y,current_box_numb,MoveDown))
            {
                show_box(Curbox_x,Curbox_y,current_box_numb,shapes[current_box_numb].color);
                GameOver=1;
            }
            else
            {
                flag_newbox=false;
            }
            Currentaction=0;
        }
        else
        {
            if(Currentaction==MoveDown||TimerCounter>(20-speed*2))
            {
                if(MoveAble(Curbox_x,Curbox_y,current_box_numb,MoveDown))
                {
                    EraseBox(Curbox_x,Curbox_y,current_box_numb);
                    Curbox_y+=BSIZE;
                    show_box(Curbox_x,Curbox_y,current_box_numb,shapes[current_box_numb].color);
                }
                TimerCounter=0;
            }
        }
        
        if(GameOver)
        {
            printf("game over,thank you! your score if %d",score);
            getch();
            break;
        }
    }
    getch();
    KillTimer();
    closegraph();
}
/*初始化界面*/
/**************************************
*参数说明:
*    x,y对应于Vertical_boxs,Horizontal_boxs
*    分别表示纵横方向上小方块的个数
*    BSIZE Sys_x Sys_y
***************************************/
void initialize(int x,int y,int m,int n)
{
    int i,j,oldx;
    oldx=x;
    for(j=0;j<n;j++)
    {
        for(i=0;i<m;i++)
        {
        
        Table_board[j][i].var=0;
        Table_board[j][i].color=BgColor;
        line(x,y,x+BSIZE,y);
        line(x,y,x,y+BSIZE);
        line(x,y+BSIZE,x+BSIZE,y+BSIZE);
        line(x+BSIZE,y,x+BSIZE,y+BSIZE);
        x+=BSIZE;
        }
        y+=BSIZE;
        x=oldx;
    }
    Curbox_x=x;
    Curbox_y=y;
    flag_newbox=false;
    speed=1;
    score=0;
    ShowScore(score);
    ShowSpeed(speed);
}

/*时钟中断处理*/
void interrupt newtimer(void)
{
    (*oldtimer)();
    TimerCounter++;
}
void SetTimer(void interrupt(*IntProc)(void))
{
    oldtimer=getvect(TIMER);
    disable();
    setvect(TIMER,IntProc);
    enable();
}
void KillTimer()
{
    disable();
    setvect(TIMER,oldtimer);
    enable();
}
/*成绩、速度及帮助的显示*/
void ShowScore(int score)
{
    int x,y;
    char score_str[5];
    setfillstyle(SOLID_FILL,BgColor);
    x=LeftWin_x;
    y=100;
    bar(x-BSIZE,y,x+BSIZE*3,y+BSIZE*3);
    sprintf(score_str,"%3d",score);
    outtextxy(x,y,"SCORE");
    outtextxy(x,y+10,score_str);
}
void ShowSpeed(int speed)
{
    int x,y;
    char speed_str[5];
    setfillstyle(SOLID_FILL,BgColor);
    x=LeftWin_x;
    y=150;
    bar(x-BSIZE,y,x+BSIZE*3,y+BSIZE*3);
    sprintf(speed_str,"%3d",speed);
    outtextxy(x,y,"Level");
    outtextxy(x,y+10,speed_str);
    outtextxy(x,y+50,"Nextbox");
}
void show_help(int xs, int ys)
{
    char stemp[50];
    setcolor(15);
    rectangle(xs,ys,xs+239,ys+100);
    sprintf(stemp,"-Roll -Downwards");
    stemp[0]=24;
    stemp[8]=25;
    setcolor(14);
    outtextxy(xs+40,ys+30,stemp);
   
    sprintf(stemp,"-Turn Left -Turn Right");
    stemp[0]=27;
    stemp[13]=26;
   
    outtextxy(xs+40,ys+45,stemp);
    outtextxy(xs+40,ys+60,"Esc-Exit ");
    setcolor(FgColor);
}
/*满行处理*/
void setFullRow(int t_boardy)
{
    int n,full_numb=0,top=0;
    register m;
    for(n=t_boardy+3;n>=t_boardy;n--)
    {
        if(n<0||n>=Vertical_boxs){continue;}
        for(m=0;m<Horizontal_boxs;m++)
        {
            if(!Table_board[n+full_numb][m].var) break;
        }
        if(m==Horizontal_boxs)
        {
            if(n==t_boardy+3)
                top=DelFullRow(n+full_numb);
            else
                DelFullRow(n+full_numb);
                full_numb++;
        }
    }
    if(full_numb)
    {
        int oldx, x=Sys_x,y=BSIZE*top+Sys_y;
        oldx=x;
        score=score+full_numb*10;
        for(n=top;n<t_boardy+4;n++)
        {
            if(n>=Vertical_boxs)continue;
            for(m=0;m<Horizontal_boxs;m++)
        {
            if(Table_board[n][m].var)
            setfillstyle(SOLID_FILL,Table_board[n][m].color);
            else
                setfillstyle(SOLID_FILL,BgColor);
                bar(x,y,x+BSIZE,y+BSIZE);
                line(x,y,x+BSIZE,y);
                line(x,y,x,y+BSIZE);
                line(x,y+BSIZE,x+BSIZE,y+BSIZE);
                line(x+BSIZE,y,x+BSIZE,y+BSIZE);
                x+=BSIZE;
        }
        y+=BSIZE;
        x=oldx;
        }
        ShowScore(score);
        if(speed!=score/speed_step)
        {
            speed=score/speed_step;ShowSpeed(speed);
        }
        else
        {
            ShowSpeed(speed);
        }
    }
}

int DelFullRow(int y)
{
    int n,top=0;
    register m,totoal;
    for(n=y;n>=0;n--)
    {
        totoal=0;
        for(m=0;m<Horizontal_boxs;m++)
        {
            if(!Table_board[n][m].var) totoal++;
             if(Table_board[n][m].var!=Table_board[n-1][m].var)
            {
                Table_board[n][m].var=Table_board[n-1][m].var;
                Table_board[n][m].color=Table_board[n-1][m].color;
            }
        }
        if(totoal==Horizontal_boxs)
        {
            top=n;
            break;
        }
    }
    return(top);
}
/*游戏方块的显示和清除*/
void show_box(int x,int y,int box_numb,int color)
{
    int i,ii,ls_x=x;
    if(box_numb<0||box_numb>=MAX_BOX)
    box_numb=MAX_BOX/2;
    setfillstyle(SOLID_FILL,color);
    for(ii=0;ii<2;ii++)
    {
        int mask=128;
        for(i=0;i<8;i++)
        {
            if(i%4==0&&i!=0)
            {
                y+=BSIZE;
                x=ls_x;
            }
            if((shapes[box_numb].box[ii])&mask)
            {
                bar(x,y,x+BSIZE,y+BSIZE);
                line(x,y,x+BSIZE,y);
                line(x,y,x,y+BSIZE);
                line(x,y+BSIZE,x+BSIZE,y+BSIZE);
                line(x+BSIZE,y,x+BSIZE,y+BSIZE);
            }
                x+=BSIZE;
                mask/=2;
        }
        y+=BSIZE;
        x=ls_x;
    }
}
void EraseBox(int x,int y,int box_numb)
{
    int mask=128,t_boardx,t_boardy,n,m;
    setfillstyle(SOLID_FILL,BgColor);
    for(n=0;n<4;n++)
    {
        for(m=0;m<4;m++)
        {
            if(((shapes[box_numb].box[n/2])&mask))
            {
                bar(x+m*BSIZE,y+n*BSIZE,x+m*BSIZE+BSIZE,y+n*BSIZE+BSIZE);
                line(x+m*BSIZE,y+n*BSIZE,x+m*BSIZE+BSIZE,y+n*BSIZE);
                line(x+m*BSIZE,y+n*BSIZE,x+m*BSIZE,y+n*BSIZE+BSIZE);
                line(x+m*BSIZE,y+n*BSIZE+BSIZE,x+m*BSIZE+BSIZE,y+n*BSIZE+BSIZE);
                line(x+m*BSIZE+BSIZE,y+n*BSIZE,x+m*BSIZE+BSIZE,y+n*BSIZE+BSIZE);
            }
            mask=mask/(2);
            if(mask==0)mask=128;
        }
    }
}

void ErasePreBox(int x,int y,int box_numb)
{
    int mask=128,t_boardx,t_boardy,n,m;
    setfillstyle(SOLID_FILL,BgColor);
    for(n=0;n<4;n++);
    {
        for(m=0;m<4;m++)
        {
            if(((shapes[box_numb].box[n/2])&mask))
            {
                bar(x+m*BSIZE,y+n*BSIZE,x+m*BSIZE+BSIZE,y+n*BSIZE+BSIZE);
            }
            mask=mask/(2);
            if(mask==0)mask=128;
        }
        
    }
}
int MkNextBox(int box_numb)
{
    int mask=128,t_boardx,t_boardy,n,m;
    t_boardx=(Curbox_x-Sys_x)/BSIZE;
    t_boardy=(Curbox_y-Sys_y)/BSIZE;
    for(n=0;n<4;n++)
    {
        for(m=0;m<4;m++)
        {
            if(((shapes[current_box_numb].box[n/2])&mask))
            {
                Table_board[t_boardy+n][t_boardx+m].var=1;
                Table_board[t_boardy+n][t_boardx+m].color=shapes[current_box_numb].color;
            }
            mask=mask/(2);
            if(mask==0)mask=128;
        }
    }
    setFullRow(t_boardy);
    Curbox_x=Sys_x+Begin_boxs_x*BSIZE,Curbox_y=Sys_y;
    if(box_numb==-1) box_numb=rand()%MAX_BOX;
    current_box_numb=box_numb;
    flag_newbox=false;
    return(rand()%MAX_BOX);
}
/*游戏方块操作判断处理*/
int MoveAble(int x,int y,int box_numb,int direction)
{
    /*判断是否可以移动,X,Y为当前游戏方块位置
    box_numb为游戏方块号
    direction方向标志
    返回true和false
    #define MoveLeft 1
    #define MoveRight 2
    #define MoveDown 3
    #define MoveRoll4
    */
    int n,m,t_boardx,t_boardy;
        int mask;
    if(direction==MoveLeft)
    {
        mask=128;
        x-=BSIZE;
        t_boardx=(x-Sys_x)/BSIZE;
        t_boardy=(y-Sys_y)/BSIZE;
        for(n=0;n<4;n++)
        {
            for(m=0;m<4;m++)
            {
                if((shapes[box_numb].box[n/2])&mask)
                {
                    if((x+BSIZE*m)<Sys_x)    return(false);
                    else if(Table_board[t_boardy+n][t_boardx+m].var)
                    {
                        return(false);
                    }
                }
                mask=mask/(2);
                if(mask==0)mask=128;
            }
        }
        return(true);
    }
    else if(direction==MoveRight)
    {
        x+=BSIZE;
        t_boardx=(x-Sys_x)/BSIZE;
        t_boardy=(y-Sys_y)/BSIZE;
        mask=128;
        for(n=0;n<4;n++)
        {
            for(m=0;m<4;m++)
            {
                if((shapes[box_numb].box[n/2])&mask)
                {
                    if((x+BSIZE*m)>=(Sys_x+BSIZE*Horizontal_boxs))return(false);
                    else if(Table_board[t_boardy+n][t_boardx+m].var)
                    {
                        return(false);
                    }
                }
                mask=mask/(2);
                if(mask==0)mask=128;
            }
        }
        return(true);
    }
    else if(direction==MoveDown)
    {
        y+=BSIZE;
        t_boardx=(x-Sys_x)/BSIZE;
        t_boardy=(y-Sys_y)/BSIZE;
        mask=128;
        for(n=0;n<4;n++)
        {
            for(m=0;m<4;m++)
            {
                if((shapes[box_numb].box[n/2])&mask)
                {
                    if((y+BSIZE*n)>=(Sys_y+BSIZE*Vertical_boxs)||Table_board[t_boardy+n][t_boardx+m].var)
                    {
                        flag_newbox=true;
                        break;
                    }
                }
                mask=mask/(2);
                if(mask==0)mask=128;
            }
        }
        if(flag_newbox)
        {
            return(false);
        }
        else
            return(true);
    }
    else if(direction==MoveRoll)
    {
        t_boardx=(x-Sys_x)/BSIZE;
        t_boardy=(y-Sys_y)/BSIZE;
        mask=128;
        for(n=0;n<4;n++)
        {
            for(m=0;m<4;m++)
            {
                if((shapes[box_numb].box[n/2])*mask)
                {
                    if((y+BSIZE*n)>=(Sys_y+BSIZE*Vertical_boxs))return(false);
                    if((x+BSIZE*n)>=(Sys_x+BSIZE*Horizontal_boxs))return(false);
                    if((x+BSIZE*m)>=(Sys_x+BSIZE*Horizontal_boxs))return(false);
                    else if(Table_board[t_boardy+n][t_boardx+m].var)
                    {
                        return (false);
                    }
                }
                mask=mask/(2);
                if(mask==0)mask=128;
            }
        }
        return(true);
    }
    else
    {
        return(false);
    }
}
搜索更多相关主题的帖子: 游戏 include C语言 
2013-04-24 12:37
快速回复:那位了解C语言时钟中断函数帮忙看一下,这个俄罗斯方块游戏无法运行
数据加载中...
 
   



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

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