⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 game_russia.c

📁 一款在tc下可直接运行的俄罗斯方块小游戏
💻 C
📖 第 1 页 / 共 2 页
字号:
#include <graphics.h>
#include <stdio.h>
#include <dos.h>
#include <conio.h>
#include <time.h>
#include <math.h>
#include <stdlib.h>

/*定义左上角点在屏幕上的位置*/
#define MAPXOFT     9
#define MAPYOFT     5

/*定义下一个方块显示的位置*/
#define MAPXOFT1     13
#define MAPYOFT1     -2


#define LEFT  0x4b00
#define RIGHT 0x4d00
#define DOWN  0x5000  /*此键为加速键*/
#define UP    0x4800  /*此键为变形键*/
#define ESC   0x011b  /*此键为退出键*/
#define ENTER 0x1c0d


#define TIMER 0x1c /* 时钟中断的中断号 */

/* 中断处理函数在C和C++中的表示略有不同。
如果定义了_cplusplus则表示在C++环境下,否则是在C环境下。 */

#ifdef __cplusplus
#define __CPPARGS ...
#else
#define __CPPARGS
#endif

int TimerCounter=0; /* 计时变量,每秒钟增加18。 */

/* 指向原来时钟中断处理过程入口的中断处理函数指针(句柄) */
void interrupt ( *oldhandler)(__CPPARGS);

/* 新的时钟中断处理函数 */
void interrupt newhandler(__CPPARGS)
{
    /* increase the global counter */
    TimerCounter++;
    
    /* call the old routine */
    oldhandler();
}

/* 设置新的时钟中断处理过程 */
void SetTimer(void interrupt (*IntProc)(__CPPARGS))
{
    oldhandler=getvect(TIMER);
    disable(); /* 设置新的时钟中断处理过程时,禁止所有中断 */
    setvect(TIMER,IntProc);
    enable(); /* 开启中断 */
}

/* 恢复原有的时钟中断处理过程 */
void KillTimer()
{
    disable();
    setvect(TIMER,oldhandler);
    enable();
}


struct shape
{
    int xy[8],next;
};

struct shape shapes[19]=
{
/*x1,y1,x2,y2,x3,y3,x4,y4 指四个小方块的相对坐标,next指此方块变形后应变为哪个小方块
    { x1,y1,x2,y2,x3,y3,x4,y4,next}*/
    { 0,-2, 0,-1, 0, 0, 1, 0, 1},
    {-1, 0, 0, 0, 1,-1, 1, 0, 2},
    { 0,-2, 1,-2, 1,-1, 1, 0, 3},
    {-1,-1,-1, 0, 0,-1, 1,-1, 0},
    { 0,-2, 0,-1, 0, 0, 1,-2, 5},
    {-1,-1, 0,-1, 1,-1, 1, 0, 6},
    { 0, 0, 1,-2, 1,-1, 1, 0, 7},
    {-1,-1,-1, 0, 0, 0, 1, 0, 4},
    {-1, 0, 0,-1, 0, 0, 1, 0, 9},
    { 0,-2, 0,-1, 0, 0, 1,-1,10},
    {-1,-1, 0,-1, 1,-1, 0, 0,11},
    { 0,-1, 1,-2, 1,-1, 1, 0, 8},
    {-1, 0, 0,-1, 0, 0, 1,-1,13},
    { 0,-2, 0,-1, 1,-1, 1, 0,12},
    {-1,-1, 0,-1, 0, 0, 1, 0,15},
    { 0,-1, 0, 0, 1,-2, 1,-1,14},
    { 0,-3, 0,-2, 0,-1, 0, 0,17},
    {-1, 0, 0, 0, 1, 0, 2, 0,16},
    { 0,-1, 0, 0, 1,-1, 1, 0,18}
};

int board[10][20]={0};/*定义游戏板初始化为0*/
char sp[]="0",le[]="0",sc[]="00000";
int speed,speed0,level,score;
int sign,flag;
int style,style1;  /*style为当前方块的种类,style1为即将输出的方块的种类*/

void draw_block(int x,int y,int style,int way);
void draw_little_block(int x,int y);
void init();
void initialize();
void speed_change(void);
void score_change(int);
void kill_line(int y);
void fill_board(int x,int y, int style);
int  change(int *i,int *j,int key);
void renovate(void);
void ajustment(void);
void level_change(void);


main()
{
    int i,j,key,x0=5,y0=1;
    randomize();
    while(!flag)                    /*flag为0表示重新开始游戏*/
    {
        level=score=speed=0;
        strcpy(le,"0");
        strcpy(sp,"0");
        strcpy(sc,"00000");
        for(i=0;i<10;i++)
            for(j=0;j<20;j++)
                board[i][j]=0;       /*初始化一些变量为0*/
            initialize();            /*初始化进入图形模式*/
            init();                  /*初始化游戏板记分器等*/
            SetTimer(newhandler);    /* 修改时钟中断 */
            ajustment();             /*开始游戏前调整速度和高度*/
            
            if(level>0)
                level_change();       /*根据高度随机确定方块是否存在*/
            
            style=random(19);         /*随机确定方块种类*/
            
            
            while(1)
            {
                i=x0,j=y0;
                
                style1=random(19);    /*随机确定即将出现的方块种类*/
                setcolor(WHITE);
                sign=1;
                draw_block(MAPXOFT1,MAPYOFT1,style1,1);
                /*画出即将出现的方块*/
                
                for(j=y0;j<=20;j++)   /*使方块下降*/
                {
                    
                    
                    if(!check_block(i,j,style))
                        break;
                    draw_block(i,j,style,1);
                    
                    
                    while(1)
                    {
                        if(speed0==0)                /*未按下加速键时的处理*/
                            
                        {
                            if (TimerCounter>18/(speed+1))
                            {
                                /* 恢复计时变量 */
                                TimerCounter=0;
                                break;
                            }
                        }
                        else if(TimerCounter>18/(9+1))/*按下加速键时的处理*/
                        {
                            /* 恢复计时变量 */
                            TimerCounter=0;
                            speed0=0;
                            break;
                        }
                        if(bioskey(1))
                        {
                            key=bioskey(0);
                            if(change(&i,&j,key))/*根据按键值做调整*/
                            {
                                flag=1;
                                goto end;
                            }
                            
                        }
                        
                        
                        
                    }
                    
                    draw_block(i,j,style,0);
                    renovate();                   /*刷新屏幕*/
                    
                    
                }
                
                if(j==y0)
                    break;
                j--;
                
                draw_block(i,j,style,1);
                
                fill_board(i,j,style);
                
                
                sign=1;
                draw_block(MAPXOFT1,MAPYOFT1,style1,0);
                style=style1;
                kill_line(j);     /*消去的函数,消去若干行并改变分数和速度*/
                
                
                while(bioskey(1))    /*清除内存中的按键*/
                    key=bioskey(0);
            }
            setcolor(CYAN);
            settextstyle(0,0,2);
            TimerCounter=0;
            
            while(1)
                if(TimerCounter>54)
                {
                    TimerCounter=0;
                    break;
                }
                while(bioskey(1))     /*清除内存中的按键*/
                    key=bioskey(0);
                outtextxy(400,340,"Game over!");
                outtextxy(360,360,"Enter to replay.");
                outtextxy(360,380,"Esc to quit.");
                while(bioskey(1)==0);
                key=bioskey(0);
                
                 end:;
                closegraph();
                KillTimer();
                if(key==ESC||flag)
                    break;
    }
    
}



void initialize()/*初始化进入图形模式*/
{
    int gdriver = VGA, gmode=VGAHI, errorcode;
    
    /* initialize graphics mode */
    
    initgraph(&gdriver, &gmode, "f:\\tc\\BGI");
    
    /* read result of initialization */
    errorcode = graphresult();
    
    if (errorcode != grOk) /* an error occurred */
        
    {
        printf("Graphics error: %s\n", grapherrormsg(errorcode));
        printf("Press any key to halt:");
        getch();
        exit(1); /* return with error code */
    }
}

void init(void)/*初始化游戏板记分器等*/
{
    int x1;
    x1=5+MAPXOFT;
    setcolor(GREEN);
    circle((x1+0.5)*16,(MAPYOFT-2)*16,3*16);
    setcolor(WHITE);
    line((x1-0.6)*16,(MAPYOFT-3.2)*16,x1*16,(MAPYOFT-3.2)*16);
    line((x1+1.4)*16,(MAPYOFT-3.2)*16,(x1+2.0)*16,(MAPYOFT-3.2)*16);
    line((x1+0.5)*16,(MAPYOFT-2)*16,(x1+0.5)*16,(MAPYOFT-1.7)*16);
    circle((x1+0.5)*16,(MAPYOFT-0.9)*16,0.3*16);
    setcolor(CYAN);
    line((MAPXOFT+3)*16,(MAPYOFT+21)*16,MAPXOFT*16,(MAPYOFT+23.5)*16);
    line((MAPXOFT+5.5)*16,(MAPYOFT+21)*16,MAPXOFT*16,(MAPYOFT+26)*16);
    line((MAPXOFT+9)*16,(MAPYOFT+21)*16,(MAPXOFT+12)*16,(MAPYOFT+23.5)*16);
    line((MAPXOFT+6.5)*16,(MAPYOFT+21)*16,(MAPXOFT+12)*16,(MAPYOFT+26)*16);
    setcolor(MAGENTA);
    ellipse((MAPXOFT+1)*16,(MAPYOFT+11)*16,90,270,7*16,7*16);
    ellipse((MAPXOFT+11)*16,(MAPYOFT+11)*16,-90,90,7*16,7*16);
    circle((MAPXOFT+24)*16,(MAPYOFT+6)*16,6*16);
    setcolor(WHITE);
    rectangle((x1-0.5)*16,(MAPYOFT-2.9)*16,(x1-0.1)*16,(MAPYOFT-2.5)*16);
    rectangle((x1+1.5)*16,(MAPYOFT-2.9)*16,(x1+1.9)*16,(MAPYOFT-2.5)*16);
    setcolor(YELLOW);
    rectangle(10*16,6*16,20*16,26*16);
    settextstyle(0,0,2);
    outtextxy(5*16,13.5*16,"level");
    outtextxy(20.4*16,13.5*16,"speed");
    outtextxy((MAPXOFT+22)*16,(MAPYOFT+3)*16,"score");
    settextstyle(0,0,4);
    outtextxy(6.5*16,15*16,"0");
    outtextxy(21.9*16,15*16,"0");
    settextstyle(0,0,3);
    outtextxy((MAPXOFT+21)*16,(MAPYOFT+6)*16,sc);
}


void ajustment(void)            /*开始游戏前调整速度和高度*/
{
    int key,boo=1,left=1;
    setcolor(YELLOW);
    settextstyle(0,0,2);
    outtextxy(500,340,"PRESS");
    outtextxy(500,360,"ENTER");
    outtextxy(500,380," TO  ");
    outtextxy(500,400,"START");
    while(1)
    {
        if(TimerCounter>8)
        {
            TimerCounter=0;
            boo*=-1;
            if(boo==-1)
                setcolor(BLACK);
            else
                setcolor(YELLOW);
            if(left==1)
            {
                line(6*16,17*16,8.5*16,17*16);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -