C++控制台游戏——经典类:

  • 俄罗斯方块1(上下左右键控制):
//上下左右键控制
#include<bits/stdc++.h>
#include<windows.h>
int a[24][17],i,j,tim=800,ti=800,shape=0,b,bn,ta[4][4],turn[4][4],nex[4][4],nextshape,add=0,score=0,speed=1,ok=1,mouse=0,best=0;
void Place(const int x, const int y)
{
COORD PlaceCursorHere;
PlaceCursorHere.X = y;
PlaceCursorHere.Y = x;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), PlaceCursorHere);
return;
}
void color(int x)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),x);
}
void appear()
{
for(i=3; i<=22; i++) for(j=1; j<=15; j++) if(a[i][j]!=0)
        {
            Place(i-2,2*j);
            printf("■");
        }
}
void disappear()
{
for(i=3; i<=22; i++) for(j=1; j<=15; j++) if(a[i][j]==2)
        {
            Place(i-2,2*j);
            printf(" ");
        }
}
int search(int x0,int y0,int x,int y)
{
POINT pt;
HWND h=GetForegroundWindow();
GetCursorPos(&pt);
ScreenToClient(h,&pt);
if(pt.x>=x0&&pt.y>=y0&&pt.x<=x&&pt.y<=y)
{
    if(mouse!=0)
    {
        Sleep(100);
        return 2;
    }
    else return 1;
}
else return 0;
}
void button(int x)
{
int m=x*10;
if(x<3)
{
    Place(22,m);
    printf("┌┄┄┄┐");
    Place(23,m);
    {
        if(x==0)
        {
            if(ok!=0) printf("┆开始 ┆");
            else printf("┆暂停 ┆");
        }
        if(x==1) printf("┆重置 ┆");
        if(x==2) printf("┆退出 ┆");
    }
    Place(24,m);
    printf("└┄┄┄┘");
}
else
{
    Place(9,m-33);
    printf("┌┄┄┐");
    Place(10,m-33);
    {
        if(x==4) printf("┆是┆");
        else printf("┆否┆");
    }
    Place(11,m-33);
    printf("└┄┄┘");
}
}
void menu(int x)
{
int k,l;
if(x==0)
{
    if(ok!=0)
    {
        ok=0;
        return;
    }
    else while(1)
        {
            mouse=GetAsyncKeyState(VK_LBUTTON);
            Place(9,13);
            printf("暂停");
            if(search(7,360,71,390)==2) break;
            Sleep(50);
        }
    Place(9,12);
    for(j=1; j<=6; j++) printf(" "); //appear();
}
else
{
    for(i=1; i<=20; i++) for(j=2; j<=30; j++)
        {
            Place(i,j);
            printf(" ");
        }
    Place(7,2);
    for(i=1; i<=15; i++) printf("┄");
    Place(12,2);
    for(i=1; i<=15; i++) printf("┄");
    Place(8,6);
    printf("你想要 ");
    if(x==1) printf("重置?");
    if(x==2) printf("exit?");
    button(4);
    button(5);
    while(1)
    {
        mouse=GetAsyncKeyState(VK_LBUTTON);
        for(i=0; i<=1; i++)
        {
            k=search(63+i*80,150,110+i*80,180);
            if(k!=2)
            {
                if(k==1) color(15);
                else color(7);
                button(i+4);
            }
            else
            {
                for(l=7; l<=12; l++) for(j=2; j<=30; j++)
                    {
                        Place(l,j);
                        printf(" ");
                    }
                if(i==0)
                {
                    if(x==1) ok=2;
                    else exit(0);
                }
                return;
            }
        }
        Sleep(50);
    }
}
}
void click()
{
for(i=0; i<=2; i++)
{
    mouse=GetAsyncKeyState(VK_LBUTTON);
    int k=search(i*80+7,360,i*80+71,390);
    if(k!=2)
    {
        if(k==1) color(15);
        else color(7);
        button(i);
        color(7);
    }
    else
    {
        menu(i);
        return;
    }
}
Sleep(50);
}
void ss()
{
Place(9,39);
printf("%d",score);
if(score>=speed*100)
{
    speed++;
    ti=ti-200;
    if(ti<0) ti=0;
    else
    {
        for(i=1; i<=4; i++)
        {
            Place(9,12);
            printf("加速!");
            Sleep(70);
            Place(9,12);
            for(j=1; j<=9; j++) printf(" ");
            Sleep(70);
        }
        Sleep(200);
    }
}
if(best<score) best=score;
appear();
Place(12,39);
printf("%d",speed);
Place(15,39);
printf("%d",best);
}
void replace()
{
for(i=1; i<=20; i++) for(j=2; j<=30; j++)
    {
        Place(i,j);
        printf(" ");
    }
for(i=0; i<=22; i++) for(j=1; j<=15; j++) a[i][j]=0;
for(i=1; i<=15; i++) a[23][i]=1;
for(i=1; i<=23; i++)
{
    a[i][0]=1;
    a[i][16]=1;
}
ss();
tim=800;
ti=800;
shape=0;
add=0;
score=0;
speed=1;
}
void change(int x)
{
int q=0,l;
for(l=1; l<=x; l++)
{
    for(j=0; j<=2+add; j++)
    {
        for(i=2+add; i>=0; i--)
        {
            turn[j][q]=ta[i][j];
            q++;
            if(q>2+add) q=0;
        }
    }
    for(i=0; i<=3; i++) for(j=0; j<=3; j++) ta[i][j]=turn[i][j];
}
}
void born()
{
int x,q=0;
srand(time(NULL));
bn=b;
shape=nextshape;
x=rand()%12+1;
if(b==5&&x>1)
{
    x--;
    add=1;
}
for(i=0; i<=2+add; i++) for(j=x; j<=x+2+add; j++)
    {
        a[i][j]=nex[i][q];
        q++;
        if(q>2+add) q=0;
    }
add=0;
}
void next()
{
srand(time(NULL));
b=rand()%7;
nextshape=rand()%4;
add=0;
for(i=3; i<=6; i++) for(j=37; j<=45; j++)
    {
        Place(i,j);
        printf(" ");
    }
for(i=0; i<=3; i++) for(j=0; j<=3; j++) nex[i][j]=turn[i][j]=ta[i][j]=0;
ta[1][1]=2;
if(b==0) ta[1][2]=ta[1][0]=ta[0][1]=2;
if(b==1) ta[1][2]=ta[1][0]=ta[0][0]=2;
if(b==2) ta[1][2]=ta[1][0]=ta[0][2]=2;
if(b==3) ta[1][0]=ta[0][1]=ta[0][2]=2;
if(b==4) ta[0][0]=ta[0][1]=ta[1][2]=2;
if(b==5)
{
    ta[1][0]=ta[1][2]=ta[1][3]=2;
    add=1;
}
if(b==6) ta[0][0]=ta[0][1]=ta[1][0]=2;
if(nextshape>0&&b!=6)
{
    change(nextshape);
    for(i=0; i<=3; i++) for(j=0; j<=3; j++)
        {
            nex[i][j]=turn[i][j];
            if(nex[i][j]==2)
            {
                Place(i+3,j*2+37);
                printf("■");
            }
        }
}
else
{
    for(i=0; i<=3; i++) for(j=0; j<=3; j++)
        {
            nex[i][j]=ta[i][j];
            if(nex[i][j]==2)
            {
                Place(i+3,j*2+37);
                printf("■");
            }
        }
}
}
void clear()
{
int c=0,f=0,l[23],s=0,k;
for(i=3; i<=22; i++)
{
    l[i]=0;
    for(j=1; j<=15; j++) c=a[i][j]+c;
    if(c==15)
    {
        for(j=1; j<=15; j++) a[i][j]=0;
        for(k=i-1; k>=2; k--) for(j=1; j<=15; j++) a[k+1][j]=a[k][j];
        f++;
        l[i]=1;
        s=5;
    }
    c=0;
}
score=score+f*10;
while(f>1)
{
    score=score+f*5;
    f--;
}
f=0;
while(s>0)
{
    for(i=22; i>=3; i--) if(l[i]==1)
        {
            Place(i-2,2);
            for(j=1; j<=15; j++) printf("■");
        }
    Sleep(70);
    for(i=22; i>=3; i--) if(l[i]==1)
        {
            Place(i-2,2);
            for(j=1; j<=30; j++) printf(" ");
        }
    Sleep(70);
    s--;
}
for(i=3; i<=22; i++) for(j=1; j<=15; j++)
    {
        Place(i-2,2*j);
        printf(" ");
    }
appear();
}
void control()
{
int up,down,right,left,c=0,d=0,x,y,no=0,k,l=0,q=0;
k=shape;
add=0;
up=GetAsyncKeyState(VK_UP);
down=GetAsyncKeyState(VK_DOWN);
right=GetAsyncKeyState(VK_RIGHT);
left=GetAsyncKeyState(VK_LEFT);
if(down!=0)
{
    tim=0;
}
if(left!=0||right!=0)
{
    Sleep(100);
    disappear();
    for(i=0; i<=22; i++) for(j=1; j<=15; j++)
        {
            if(a[i][j]==2&&a[i][j-1]!=1) c++;
            if(a[i][j]==2&&a[i][j+1]!=1) d++;
        }
    for(i=0; i<=22; i++) for(j=1; j<=15; j++) if(left!=0&&c==4&&a[i][j]==2)
            {
                a[i][j-1]=a[i][j];
                a[i][j]=0;
            }
    for(i=0; i<=22; i++) for(j=15; j>=1; j--) if(right!=0&&d==4&&a[i][j]==2)
            {
                a[i][j+1]=a[i][j];
                a[i][j]=0;
            }
    appear();
}
if(up!=0&&bn!=6)
{
    Sleep(150);
    disappear();
    for(i=0; i<=22; i++)
    {
        for(j=1; j<=15; j++) if(a[i][j]==2)
            {
                x=i;
                no=1;
                break;
            }
        if(no==1) break;
    }
    no=0;
    for(j=1; j<=15; j++)
    {
        for(i=0; i<=22; i++) if(a[i][j]==2)
            {
                y=j;
                no=1;
                break;
            }
        if(no==1) break;
    }
    no=0;
    if(k==1) y--;
    if(k==2) x--;
    add=0;
    if(bn==5)
    {
        add=1;
        if(k==0||k==2) x--;
        if(k==1||k==3) y--;
    }
    for(i=0; i<=3; i++) for(j=0; j<=3; j++) ta[i][j]=a[x+i][y+j];
    if(bn>=0&&bn<5)
    {
        if(k==0) ta[2][0]=0;
        if(k==1) ta[0][0]=0;
        if(k==2) ta[0][2]=0;
        if(k==3) ta[2][2]=0;
    }
    if(bn==5)
    {
        if(k==0) ta[0][3]=ta[2][0]=ta[2][1]=ta[3][1]=ta[3][0]=0;
        if(k==1) ta[0][0]=ta[1][0]=ta[0][1]=ta[1][1]=ta[3][3]=0;
        if(k==2) ta[3][0]=ta[0][2]=ta[0][3]=ta[1][2]=ta[1][3]=0;
        if(k==3) ta[0][0]=ta[2][2]=ta[2][3]=ta[3][2]=ta[3][3]=0;
    }
    for(i=0; i<=2+add; i++) for(j=0; j<=2+add; j++) if(ta[i][j]!=1) l++;
    if(l==9+add*7)
    {
        change(1);
        for(i=0; i<=22; i++) for(j=1; j<=15; j++) if(a[i][j]==2) a[i][j]=0;
        for(i=0; i<=2+add; i++) for(j=0; j<=2+add; j++) if(turn[i][j]==2) a[x+i][y+j]=turn[i][j];
        shape++;
    }
    if(shape>3) shape=0;
    appear();
}
}
int main()
{
int k,start,finish,d;
printf("┌");
for(i=1; i<=15; i++) printf("┄");
printf("┐\n");
for(i=1; i<=20; i++) printf("┆\n");
printf("└");
for(i=1; i<=15; i++) printf("┄");
printf("┘");
for(i=1; i<=20; i++)
{
    Place(i,32);
    printf("┆\n");
}
Place(2,37);
printf("下一个");
Place(8,37);
printf("得分");
Place(14,37);
printf("最高分");
Place(11,37);
printf("速度");
for(i=0; i<=2; i++) button(i);
while(ok==1) click();
while(ok!=1)
{
    replace();
    next();
    while(1)
    {
        born();
        next();
        while(1)
        {
            appear();
            k=0;
            start=clock();
            while(1)
            {
                finish=clock();
                d=finish-start;
                if(d>=tim) break;
                control();
                click();
                if(ok==2) break;
            }
            if(ok==2) break;
            disappear();
            for(i=22; i>=0; i--) for(j=1; j<=15; j++) if(a[i][j]==2&&a[i+1][j]!=1) k++;
            if(k==4)
            {
                for(i=22; i>=0; i--) for(j=1; j<=15; j++) if(a[i][j]==2)
                        {
                            a[i+1][j]=2;
                            a[i][j]=0;
                        }
            }
            else
            {
                for(i=22; i>=0; i--) for(j=1; j<=15; j++) if(a[i][j]==2) a[i][j]=1;
                break;
            }
            tim=ti;
            appear();
        }
        if(ok==2) break;
        appear();
        clear();
        ss();
        for(i=1; i<=15; i++) if(a[2][i]==1)
            {
                ok=1;
                break;
            }
    }
    if(ok==2)
    {
        ok=0;
        continue;
    }
    for(i=1; i<=20; i++) for(j=2; j<=30; j++)
        {
            Place(i,j);
            printf(" ");
        }
    Place(9,12);
    printf("游戏结束!");
    while(ok==1) click();
}
return 0;
}
  • 俄罗斯方块2(J,L左移右移;D变换方向):
//J,L左移右移;D变换方向
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <windows.h>
#include <ctime>
#include <conio.h>
#include <iostream>
using namespace std;
#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define inf 2147483647
struct type_block
{
int a[10][10];
int color;
int size;
}now, nextA, nextB, nextC, hold;
int sblock[20][10][10] = {{{7, 0, 0, 0}, {0, 0, 1, 1}, {0, 1, 1, 0}, {0, 0, 0, 0}},
{{8, 0, 0, 0}, {0, 1, 1, 0}, {0, 0, 1, 1}, {0, 0, 0, 0}},
{{9, 0, 0, 0}, {1, 1, 1, 1}, {0, 0, 0, 0}, {0, 0, 0, 0}},
{{10, 0, 0, 0}, {0, 0, 0, 1}, {0, 1, 1, 1}, {0, 0, 0, 0}},
{{13, 0, 0, 0}, {0, 1, 0, 0}, {0, 1, 1, 1}, {0, 0, 0, 0}},
{{5, 0, 0, 0}, {0, 1, 1, 0}, {0, 1, 1, 0}, {0, 0, 0, 0}},
{{16, 0, 0, 0}, {0, 0, 1, 0}, {0, 1, 1, 1}, {0, 0, 0, 0}},
{{0, 0, 0, 0}, {0, 1, 0, 0}, {0, 1, 1, 0}, {0, 0, 1, 0}},
{{1, 0, 0, 0}, {0, 0, 1, 0}, {0, 1, 1, 0}, {0, 1, 0, 0}},
{{2, 1, 0, 0}, {0, 1, 0, 0}, {0, 1, 0, 0}, {0, 1, 0, 0}},
{{11, 0, 1, 0}, {0, 0, 1, 0}, {0, 0, 1, 1}, {0, 0, 0, 0}},
{{12, 0, 0, 0}, {0, 1, 1, 1}, {0, 1, 0, 0}, {0, 0, 0, 0}}, //11
{{3, 0, 1, 1}, {0, 0, 0, 1}, {0, 0, 0, 1}, {0, 0, 0, 0}},
{{14, 1, 1, 0}, {0, 1, 0, 0}, {0, 1, 0, 0}, {0, 0, 0, 0}},
{{15, 1, 1, 1}, {0, 0, 0, 1}, {0, 0, 0, 0}, {0, 0, 0, 0}},
{{4, 0, 0, 1}, {0, 0, 0, 1}, {0, 0, 1, 1}, {0, 0, 0, 0}},
{{17, 0, 0, 0}, {0, 0, 1, 0}, {0, 0, 1, 1}, {0, 0, 1, 0}},
{{18, 0, 0, 0}, {0, 0, 0, 0}, {0, 1, 1, 1}, {0, 0, 1, 0}}, //17
{{6, 0, 0, 0}, {0, 0, 1, 0}, {0, 1, 1, 0}, {0, 0, 1, 0}}};
HANDLE hOut = GetStdHandle (STD_OUTPUT_HANDLE);
int map[22][12];
int mode = 1;
int fraction = 0;
int positionX, positionY;
bool locked;
int keytime = 100000000;
bool keytimeflag;
int sleeptime = 0;
bool holdflag = 1;
int passcondition = 200;
int addlinetime = 10000, addlineflag;
int locktime = 1000;
int keydownflag;
int xray = 0;
int firstwin = 1;
int exfraction = 0;
int exgamestarttime;
int blind = 0;
int lockdelay = 300;
int clockms, stclockms;
double blockpersecond, blockperminute;
int blocknum;
void gotoxy (int x, int y);
void welcomepage ();
void reset ();
void choosedifficulty ();
void ready_go ();
void updatedata ();
void updatetime ();
type_block roundblock ();
void printblock (type_block m_block, int x, int y);
void clearblock (type_block m_block, int x, int y);
int checkblock (type_block m_block, int x, int y);
type_block myup (type_block m_block);
type_block mydown (type_block m_block);
void checkkey ();
void checkline ();
void addline ();
void gameover ();
void win ();
void easy_extra_game ();
void master_extra_game ();
void shirase_extra_game ();
int main()
{
welcomepage ();
reset ();
choosedifficulty();
ready_go ();
clearblock (nextA, 34, 4); clearblock (nextB, 38 + nextA.size, 4); clearblock (nextC, 42 + nextA.size + nextB.size, 4);
now = nextA; nextA = nextB; nextB = nextC; nextC = roundblock();
printblock (nextA, 34, 4); printblock (nextB, 38 + nextA.size, 4); printblock (nextC, 42 + nextA.size + nextB.size, 4);
positionX = 0; positionY = 4; locked = 0;
keytime = clock(); keytimeflag = 1;
addlineflag = clock (); stclockms = clock ();
    while (1)
    {
        updatedata (); updatetime ();
        if (locked)
        {
            ++blocknum;
            SetConsoleTextAttribute(hOut, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
            now.color = -xray * 2;
            printblock (now, positionY * 2 + 8, positionX + 1);
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    if (!(i + j))
                        continue;
                    if (now.a[i][j]) 
                        map[positionX + i][positionY + j] = 1;
                }
            }
            checkline ();
            Sleep (lockdelay);
            updatetime ();
            if (blind)
                clearblock (now, positionY * 2 + 8, positionX + 1);
            locked = 0;
            clearblock (nextA, 34, 4); clearblock (nextB, 38 + nextA.size, 4); clearblock (nextC, 42 + nextA.size + nextB.size, 4);
            now = nextA; nextA = nextB; nextB = nextC; nextC = roundblock();
            printblock (nextA, 34, 4); printblock (nextB, 38 + nextA.size, 4); printblock (nextC, 42 + nextA.size + nextB.size, 4);
            positionX = 0; positionY = 4;
            if (!checkblock (now, positionX, positionY))
                gameover ();
            if (fraction % 100 != 99 && fraction != passcondition)
                ++fraction;
            SetConsoleTextAttribute(hOut, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
            gotoxy (56, 10);
            printf ("%4d", fraction);
            gotoxy (57, 11);
            printf ("---");
            gotoxy (56, 12);
            int lssc = fraction % 100 ? (fraction / 100 + 1) * 100 : fraction;
            if (mode == 1 && lssc >= 200) lssc = 200;
            if (mode == 2 && lssc >= 999) lssc = 900;
            if (mode == 3 && lssc >= 1300) lssc = 1300;
            printf ("%4d", lssc);
            keytime = clock(); holdflag = 1;
            if (clock () - addlineflag > addlinetime)
            {
                addlineflag = clock ();
                addline ();
            }
            keydownflag = 0;
        }
        if (checkblock (now, positionX + 1, positionY))
        while (checkblock (now, positionX + 1, positionY))
        {
            ++positionX; updatetime ();
            if (sleeptime)
            {
                printblock (now, positionY * 2 + 8, positionX + 1);
                int sttime = clock (), timeover;
                while ((timeover = (clock () - sttime < sleeptime)) && !kbhit());
                clearblock (now, positionY * 2 + 8, positionX + 1);
                if (timeover)
                    checkkey ();
            }
            checkkey ();
        }
        printblock (now, positionY * 2 + 8, positionX + 1);
        Sleep (100);
        clearblock (now, positionY * 2 + 8, positionX + 1);
        checkkey ();
    }
}
void gotoxy (int x, int y)
{
COORD pos;
pos.X = x; pos.Y = y;
SetConsoleCursorPosition (hOut, pos);
}
void welcomepage ()
{
puts ("                                                                               ");
puts ("   ■■■■■■■■■■                                                        ");
puts ("   ■■■■■■■■■■                                                        ");
puts ("           ■■                                                                ");
puts ("           ■■                                                                ");
puts ("           ■■                                                                ");
puts ("           ■■          ■■■■      ■     ■         ■    ■■■          ");
puts ("           ■■          ■    ■   ■■■■  ■ ■■■      ■      ■        ");
puts ("           ■■          ■■■■      ■     ■■       ■  ■                ");
puts ("           ■■          ■            ■     ■         ■    ■■■          ");
puts ("           ■■          ■■■■      ■     ■         ■          ■        ");
puts ("                                                                     ■        ");
puts ("                                                             ■■■■          ");
puts ("                                                                               ");
puts ("     ------------------------------------------------------------------        ");
puts ("                             CHAMPION                                  ");
puts ("                                                                               ");
puts ("                                                                               ");
puts ("                                                                               ");
puts ("                                                                               ");
puts ("                                                                               ");
SetConsoleTextAttribute(hOut, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY);
int modnum = 0;
while (1)
{
    gotoxy (22, 20);
    if (modnum)
        puts ("PUSH START BUTTOM");
    else
        puts ("                                  ");
    int sttime = clock (), timeover;
    while ((timeover = (clock () - sttime < 700)) && !kbhit());
    if (timeover)
    {
        if (_getch () == ' ')
            break;
    }
    modnum ^= 1;
}
gotoxy (0, 0);
for (int i = 1; i <= 25; ++i)
    puts ("                                                                               ");
gotoxy (0, 0);
}
void reset ()
{
srand (time (0));
SetConsoleTextAttribute(hOut, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
puts ("");
puts ("        ■■■■■■■■■■■■");
puts ("        ■                    ■");
puts ("        ■                    ■");
puts ("        ■                    ■");
puts ("        ■                    ■");
puts ("        ■                    ■");
puts ("        ■                    ■");
puts ("        ■                    ■");
puts ("        ■                    ■");
puts ("        ■                    ■");
puts ("        ■                    ■");
puts ("        ■                    ■");
puts ("        ■                    ■");
puts ("        ■                    ■");
puts ("        ■                    ■");
puts ("        ■                    ■");
puts ("        ■                    ■");
puts ("        ■                    ■");
puts ("        ■                    ■");
puts ("        ■                    ■");
puts ("        ■                    ■");
puts ("        ■■■■■■■■■■■■");
nextA = roundblock (); nextB = roundblock (); nextC = roundblock ();
for (int i = 34; i <= 34 + nextA.size; ++i)
{
    gotoxy (i, 4);
    for (int j = 4; j <= 4 + nextA.size; ++j)
        printf (" ");
}
printblock (nextA, 34, 4);
for (int i = 38 + nextA.size; i <= 38 + nextA.size + nextB.size; ++i)
{
    gotoxy (i, 4);
    for (int j = 4; j <= 4 + nextB.size; ++j)
        printf (" ");
}
printblock (nextB, 38 + nextA.size, 4);
for (int i = 42 + nextA.size + nextB.size; i <= 42 + nextA.size + nextB.size + nextC.size; ++i)
{
    gotoxy (i, 4);
    for (int j = 4; j <= 4 + nextC.size; ++j)
        printf (" ");
}
printblock (nextC, 42 + nextA.size + nextB.size, 4);
for (int i = 1; i <= 20; ++i)
    map[i][0] = map[i][11] = 1;
CONSOLE_CURSOR_INFO CursorInfo;  
GetConsoleCursorInfo (hOut, &CursorInfo);
CursorInfo.bVisible = 0;
SetConsoleCursorInfo (hOut, &CursorInfo);
SetConsoleTextAttribute(hOut, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
gotoxy (56, 10);
printf ("%4d", fraction);
gotoxy (57, 11);
printf ("---");
gotoxy (56, 12);
if (fraction)
    printf ("%4d", fraction % 100 ? (fraction / 100 + 1) * 100 : fraction);
else
    printf (" 100");
SetConsoleTextAttribute(hOut, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
gotoxy (45, 20);
printf ("00:00:00");
}
void choosedifficulty ()
{
while (1) 
{
    gotoxy (0, 0);
    switch (mode)
    {
        case 1:
                SetConsoleTextAttribute(hOut, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
                puts ("");
                puts ("        ■■■■■■■■■■■■");
                puts ("        ■                    ■");
                puts ("        ■                    ■");
                puts ("        ■                    ■");
                puts ("        ■                    ■");
                puts ("        ■                    ■");
                puts ("        ■                    ■");
                puts ("        ■                    ■");
                puts ("        ■                    ■");
                puts ("        ■                    ■");
                puts ("        ■                    ■");
                puts ("        ■                    ■");
                puts ("        ■                    ■");
                puts ("        ■                    ■");
                puts ("        ■                    ■");
                puts ("        ■                    ■");
                puts ("        ■                    ■");
                puts ("        ■                    ■");
                puts ("        ■                    ■");
                puts ("        ■                    ■");
                puts ("        ■                    ■");
                puts ("        ■■■■■■■■■■■■");
            gotoxy (18, 9);
            SetConsoleTextAttribute(hOut, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
            puts (" EASY  ");
            gotoxy (10, 16);
            printf ("慢速的经典俄罗斯");
            gotoxy (10, 17);
            printf ("方块,适合初学者.");
            break;
        case 2:
                SetConsoleTextAttribute(hOut, FOREGROUND_BLUE | FOREGROUND_INTENSITY);
                puts ("");
                puts ("        ■■■■■■■■■■■■");
                puts ("        ■                    ■");
                puts ("        ■                    ■");
                puts ("        ■                    ■");
                puts ("        ■                    ■");
                puts ("        ■                    ■");
                puts ("        ■                    ■");
                puts ("        ■                    ■");
                puts ("        ■                    ■");
                puts ("        ■                    ■");
                puts ("        ■                    ■");
                puts ("        ■                    ■");
                puts ("        ■                    ■");
                puts ("        ■                    ■");
                puts ("        ■                    ■");
                puts ("        ■                    ■");
                puts ("        ■                    ■");
                puts ("        ■                    ■");
                puts ("        ■                    ■");
                puts ("        ■                    ■");
                puts ("        ■                    ■");
                puts ("        ■■■■■■■■■■■■");
            gotoxy (17, 9);
            SetConsoleTextAttribute(hOut, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
            puts ("MASTER ");
            gotoxy (10, 16);
            printf ("中速的街机俄罗斯");
            gotoxy (10, 17);
            printf ("方块,适合中级者.");
            break;
        case 3:
                SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_INTENSITY);
                puts ("");
                puts ("        ■■■■■■■■■■■■");
                puts ("        ■                    ■");
                puts ("        ■                    ■");
                puts ("        ■                    ■");
                puts ("        ■                    ■");
                puts ("        ■                    ■");
                puts ("        ■                    ■");
                puts ("        ■                    ■");
                puts ("        ■                    ■");
                puts ("        ■                    ■");
                puts ("        ■                    ■");
                puts ("        ■                    ■");
                puts ("        ■                    ■");
                puts ("        ■                    ■");
                puts ("        ■                    ■");
                puts ("        ■                    ■");
                puts ("        ■                    ■");
                puts ("        ■                    ■");
                puts ("        ■                    ■");
                puts ("        ■                    ■");
                puts ("        ■                    ■");
                puts ("        ■■■■■■■■■■■■");
            gotoxy (16, 9);
            SetConsoleTextAttribute(hOut, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
            puts ("SHIRASE");
            gotoxy (10, 16);
            printf ("高速俄罗斯方块,");
            gotoxy (10, 17);
            printf ("挑战速度的极限.");
            break;
    }
    char key = _getch ();
    if (key == 'i' && mode != 1)
        --mode;
    if (key == 'k' && mode != 3)
        ++mode;
    gotoxy (10, 16);
    printf ("                 ");
    gotoxy (10, 17);
    printf ("                 ");
    if (key == ' ')
        break;
}
gotoxy (16, 9);
printf ("          ");
} 
void ready_go ()
{
SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
Sleep (1000);
gotoxy (18, 8);
puts ("READY");
Sleep (800);
gotoxy (18, 8);
puts (" GO! ");
Sleep (800);
gotoxy (18, 8);
puts ("     ");
}
void updatedata ()
{
switch (mode)
{
    case 1:
        addlinetime = inf; lockdelay = 300;
        if (fraction >= 0)
        {
            sleeptime = 2000 - fraction * 5;
            locktime = 800;
        }
        else if (fraction >= 100)
        {
            sleeptime = 1000 - fraction * 5 / 2;
            locktime = 700;
        }
        if (fraction >= 200)
            win ();
        break;
    case 2:
        addlinetime = inf; lockdelay = 300;
        if (fraction >= 0)
        {
            sleeptime = 1000 - fraction * 3;
            locktime = 700;
        }
        if (fraction >= 100)
        {
            sleeptime = 1000 - fraction * 3;
            locktime = 600;
        }
        if (fraction >= 200)
        {
            sleeptime = 1000 - fraction * 3;
            locktime = 500;
            if (fraction >= 300)
                locktime = 700;
            else if (fraction >= 400)
                locktime = 600;
        }
        if (fraction >= 999)
            win ();
        break;
    case 3:
        addlinetime = inf; lockdelay = 300 - fraction / 400 * 50; sleeptime = 0;
        locktime = 240 - fraction / 300 * 40;
        if (fraction >= 600)
            addlinetime = 10000;
        if (fraction >= 800)
            addlinetime = 7000;
        if (fraction >= 1000)
        {
            addlinetime = inf;
            xray = 1;
        }
        if (fraction >= 1300)
            win ();
}
}
void updatetime ()
{
SetConsoleTextAttribute(hOut, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
gotoxy (45, 20); clockms = clock () - stclockms;
printf ("%02d:%02d:%02d", clockms / 60000 % 100, clockms / 1000 % 60, clockms % 1000 / 10);
}
type_block roundblock ()
{
type_block c;
int kind = rand () % 7;
while (kind < 2 && fraction <= 6)
    kind = rand () % 7;
c.size = 4; c.color = kind + 1;
if (xray) c.color = -2;
for (int i = 0; i < c.size; ++i)
    for (int j = 0; j < c.size; ++j)
        c.a[i][j] = sblock[kind][i][j];
return c;
}
void printblock (type_block m_block, int x, int y)
{
switch (m_block.color)
{
    case 1:
        SetConsoleTextAttribute (hOut, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
        break;
    case 2:
        SetConsoleTextAttribute (hOut, FOREGROUND_RED | FOREGROUND_INTENSITY);
        break;
    case 3:
        SetConsoleTextAttribute (hOut, FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
        break;
    case 4:
        SetConsoleTextAttribute (hOut, FOREGROUND_GREEN | FOREGROUND_RED);
        break;
    case 5:
        SetConsoleTextAttribute (hOut, FOREGROUND_BLUE | FOREGROUND_INTENSITY);
        break;
    case 6:
        SetConsoleTextAttribute (hOut, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY);
        break;
    case 7:
        SetConsoleTextAttribute (hOut, FOREGROUND_RED | FOREGROUND_BLUE);
        break;
    case -1:
        SetConsoleTextAttribute (hOut, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED);
        break;
    case -2:
        SetConsoleTextAttribute (hOut, FOREGROUND_GREEN);
        break;
    default:
        SetConsoleTextAttribute (hOut, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY);
        break;
}
for (int i = 0; i < m_block.size; ++i)
{
    for (int j = 0 + !i; j < m_block.size; ++j)
        if (m_block.a[i][j])
        {
            gotoxy (x + j * 2, y + i);
            if (xray)
                puts ("[]");
            else
                puts ("■");
        }
}
}
void clearblock (type_block m_block, int x, int y)
{
for (int i = 0; i < m_block.size; ++i)
{
    for (int j = 0 + !i; j < m_block.size; ++j)
        if (m_block.a[i][j])
        {
            gotoxy (x + j * 2, y + i);
            puts ("  ");
        }
}
}
int checkblock (type_block m_block, int x, int y)
{
for (int i = 0; i < m_block.size; ++i)
    for (int j = 0; j < m_block.size; ++j)
    {
        if (!(i + j))
            continue;
        if (m_block.a[i][j] && (map[x + i][y + j] || x + i > 20 || y + j < 1 || y + j > 10))
            return 0;
    }
return 1;
}
type_block myup (type_block m_block)
{
type_block c;
int kind = m_block.a[0][0];
for (int i = 0; i < m_block.size; ++i)
    for (int j = 0; j < m_block.size; ++j)
        c.a[i][j] = sblock[kind][i][j];
c.size = m_block.size; c.color = m_block.color;
return c;
}
type_block mydown (type_block m_block)
{
type_block c = m_block;
for (int t = 1; t <= 3; ++t)
    c = myup (c);
return c;
}
void checkkey ()
{
if (checkblock (now, positionX + 1, positionY) && keydownflag < (sleeptime > 100 ? 0 : 4))
{
    ++keydownflag;
    if (KEY_DOWN('J'))
    {
        if (checkblock (now, positionX, positionY - 1))
            --positionY;
    }
    if (KEY_DOWN('L'))
    {
        if (checkblock (now, positionX, positionY + 1))
            ++positionY;
    }
}
if (kbhit())
        {
            keytime = clock ();
            char key = _getch();
            if (key == 'j')
            {
                if (checkblock (now, positionX, positionY - 1))
                    --positionY;
            }
            if (key == 'l')
            {
                if (checkblock (now, positionX, positionY + 1))
                    ++positionY;
            }
            if (key == 's')
            {
                if (!holdflag)
                    return;
                if (hold.a[0][0])
                {
                    clearblock (hold, 60, 5);
                    type_block t = now; now = hold; hold = t;
                    printblock (hold, 60, 5);
                    positionX = 0; positionY = 4; keytime = clock();
                    holdflag = 0;
                }
                else
                {
                    SetConsoleTextAttribute(hOut, FOREGROUND_BLUE | FOREGROUND_INTENSITY);
                    gotoxy (60, 4);
                    puts ("Hold");
                    clearblock (hold, 60, 5);
                    hold = now;
                    printblock (hold, 60, 5);
                    clearblock (nextA, 34, 4); clearblock (nextB, 38 + nextA.size, 4); clearblock (nextC, 42 + nextA.size + nextB.size, 4);
                    now = nextA; nextA = nextB; nextB = nextC; nextC = roundblock();
                    printblock (nextA, 34, 4); printblock (nextB, 38 + nextA.size, 4); printblock (nextC, 42 + nextA.size + nextB.size, 4);
                    positionX = 0; positionY = 4; keytime = clock();
                }
            }
            if (key == 'f')
            {
                type_block newnow = myup (now);
                if (checkblock (newnow, positionX, positionY))
                    now = newnow;
                else if (checkblock (newnow, positionX - 1, positionY))
                {
                    now = newnow;
                    --positionX;
                }
                else if (checkblock (newnow, positionX - 2, positionY)) 
                {
                    now = newnow;
                    positionX -= 2;
                }
                else if (checkblock (newnow, positionX, positionY - 1))
                {
                    now = newnow;
                    --positionY;
                }
                else if (checkblock (newnow, positionX, positionY + 1))
                {
                    now = newnow;
                    ++positionY;
                }
            }
            if (key == 'd')
            {
                type_block newnow = mydown (now);
                if (checkblock (newnow, positionX, positionY))
                    now = newnow;
                else if (checkblock (newnow, positionX - 1, positionY))
                {
                    now = newnow;
                    --positionX;
                }
                else if (checkblock (newnow, positionX - 2, positionY)) 
                {
                    now = newnow;
                    positionX -= 2;
                }
                else if (checkblock (newnow, positionX, positionY - 1))
                {
                    now = newnow;
                    --positionY;
                }
                else if (checkblock (newnow, positionX, positionY + 1))
                {
                    now = newnow;
                    ++positionY;
                }
            }
            if (KEY_DOWN(' '))
            {
                for (int i = 1; i <= 20; ++i)
                    if (checkblock (now, positionX + 1, positionY))
                        ++positionX;
                locked = 1;
            }
        }
        if (clock() - keytime > locktime && !checkblock (now, positionX + 1, positionY))
            locked = 1;
}
void checkline ()
{
bool tf = 0;
for (int i = 1; i <= 20; ++i)
{
    int x = 0;
    for (int j = 1; j <= 10; ++j)
        if (map[i][j])
            ++x;
    if (x == 10)
    {
        tf = 1;
        for (int k = i; k >= 1; --k)
            for (int l = 1; l <= 10; ++l)
                map[k][l] = map[k - 1][l];
        SetConsoleTextAttribute (hOut, FOREGROUND_RED);
        gotoxy (10, i + 1);
        for (int k = 1; k <= 10; ++k)
        {
            map[1][k] = 0;
            printf ("..");
        }
        if (firstwin)
            ++fraction;
    }
}
if (tf)
{
    Sleep (100);
    if (xray)
        SetConsoleTextAttribute (hOut, FOREGROUND_GREEN);
    else
        SetConsoleTextAttribute (hOut, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY);
    for (int i = 1; i <= 20; ++i)
    {
        gotoxy (10, i + 1);
        for (int j = 1; j <= 10; ++j)
            if (map[i][j] && !blind)
                if (xray)
                    printf ("[]");
                else
                    printf ("■");
            else
                printf ("  ");
    }
}
}
void addline ()
{
for (int i = 1; i <= 19; ++i)
    for (int j = 1; j <= 10; ++j)
        map[i][j] = map[i + 1][j];
for (int i = 1; i <= 10; ++i)
    map[20][i] = map[19][i];
if (xray)
    SetConsoleTextAttribute (hOut, FOREGROUND_GREEN);
else
    SetConsoleTextAttribute (hOut, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY);
    for (int i = 1; i <= 20; ++i)
    {
        gotoxy (10, i + 1);
        for (int j = 1; j <= 10; ++j)
            if (map[i][j] && !(i != 1 && blind))
                if (xray)
                    printf ("[]");
                else
                    printf ("■");
            else
                printf ("  ");
    }
if (blind)
{
    Sleep (200);
    gotoxy (10, 2);
    printf ("                    ");
}
}
void gameover ()
{
clockms = clock () - stclockms;
blockpersecond = (double) blocknum / clockms * 1000; blockperminute = blockpersecond * 60.0;
for (int i = 20; i >= 1; --i)
{
    for (int j = 1; j <= 10; ++j)
    {
        gotoxy (j * 2 + 8, i + 1);
        puts ("  ");
    }
    Sleep (150);
}
Sleep (2000);
SetConsoleTextAttribute(hOut, FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_INTENSITY);
gotoxy (14, 10);
puts ("  暂定段位");
gotoxy (13, 19);
printf ("BPS  %lf", blockpersecond);
gotoxy (13, 20);
printf ("BPM  %lf", blockperminute);
for (int i = 1; i <= 29; ++i)
{
    if (i % 2)
        SetConsoleTextAttribute(hOut, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY);
    else
        SetConsoleTextAttribute(hOut, FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED);
    gotoxy (19, 13);
    switch (mode)
    {
        case 1:
            gotoxy (18, 13);
            printf ("H%d", fraction + exfraction * 10 + rand () % 10);
            break;
        case 2:
            if (fraction >= 999)
                printf ("S13");
            else if (fraction >= 950)
                printf ("S12");
            else if (fraction >= 900)
                printf ("S11");
            else if (fraction >= 850)
                printf ("S10");
            else if (fraction >= 800)
                printf ("S9");
            else if (fraction >= 750)
                printf ("S8");
            else if (fraction >= 700)
                printf ("S7");
            else if (fraction >= 650)
                printf ("S6");
            else if (fraction >= 600)
                printf ("S5");
            else if (fraction >= 550)
                printf ("S4");
            else if (fraction >= 500)
                printf ("S3");
            else if (fraction >= 450)
                printf ("S2");
            else if (fraction >= 400)
                printf ("S1");
            else if (fraction >= 350)
                printf (" 1");
            else if (fraction >= 300)
                printf (" 2");
            else if (fraction >= 250)
                printf (" 3");
            else if (fraction >= 200)
                printf (" 4");
            else if (fraction >= 150)
                printf (" 5");
            else if (fraction >= 100)
                printf (" 6");
            else if (fraction >= 50)
                printf (" 7");
            else printf (" 8");
            break;
        case 3:
            if (fraction >= 1300)
                printf ("S13");
            else if (fraction >= 1200)
                printf ("S12");
            else if (fraction >= 1100)
                printf ("S11");
            else if (fraction >= 1000)
                printf ("S10");
            else if (fraction >= 900)
                printf ("S9");
            else if (fraction >= 800)
                printf ("S8");
            else if (fraction >= 700)
                printf ("S7");
            else if (fraction >= 600)
                printf ("S6");
            else if (fraction >= 500)
                printf ("S5");
            else if (fraction >= 400)
                printf ("S4");
            else if (fraction >= 300)
                printf ("S3");
            else if (fraction >= 200)
                printf ("S2");
            else if (fraction >= 100)
                printf ("S1");
            else
            {
                gotoxy (15, 13);
                printf ("Let's go");
                gotoxy (15, 14);
                printf ("better");
                gotoxy (15, 15);
                printf ("next time!");
            }
            break;
    }
    Sleep (120);
}
Sleep (2000);
system ("pause>nul");
exit (0);
}
void win ()
{
if (firstwin)
{
    for (int i = 20; i >= 1; --i)
    {
        for (int j = 1; j <= 10; ++j)
        {
            gotoxy (j * 2 + 8, i + 1);
            puts ("  ");
            map[i][j] = 0;
        }
        Sleep (150);
    }
    Sleep (1000);
    firstwin = 0;
    switch (mode)
    {
        case 1:
            easy_extra_game ();
            break;
        case 2:
            master_extra_game ();
            break;
        case 3:
            shirase_extra_game ();
            break;
        default:
            break;
    }
    return;
}
gotoxy (15, 9);
switch (mode)
{
    case 1:
        SetConsoleTextAttribute(hOut, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
        puts ("  EASY MODE ");
        break;
    case 2:
        SetConsoleTextAttribute(hOut, FOREGROUND_BLUE | FOREGROUND_INTENSITY);
        puts (" MASTER MODE");
        break;
    case 3:
        SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_INTENSITY);
        puts ("SHIRASE MODE");
        break;
}
gotoxy (15, 10);
puts ("  ALL CLEAR  ");
Sleep (5000);
gotoxy (15, 9);
puts ("             ");
gotoxy (15, 10);
puts ("             ");
gameover ();
}
void easy_extra_game ()
{
sleeptime = 0; exgamestarttime = clock (); locktime = 300; keytime = clock ();
if (mode == 3)
    locktime = 200;
while (clock () - exgamestarttime < 40000)
    {
        if (locked)
        {
            ++exfraction;
            SetConsoleTextAttribute(hOut, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
            now.color = -xray * 2;
            printblock (now, positionY * 2 + 8, positionX + 1);
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    if (!(i + j))
                        continue;
                    if (now.a[i][j]) 
                        map[positionX + i][positionY + j] = 1;
                }
            }
            checkline ();
            Sleep (lockdelay);
            if (blind)
                clearblock (now, positionY * 2 + 8, positionX + 1);
            locked = 0;
            clearblock (nextA, 34, 4); clearblock (nextB, 38 + nextA.size, 4); clearblock (nextC, 42 + nextA.size + nextB.size, 4);
            now = nextA; nextA = nextB; nextB = nextC; 
            if (mode == 3 && !rand () % 4)
                xray = 0;
            nextC = roundblock();
            xray = 1;
            printblock (nextA, 34, 4); printblock (nextB, 38 + nextA.size, 4); printblock (nextC, 42 + nextA.size + nextB.size, 4);
            positionX = 0; positionY = 4;
            if (!checkblock (now, positionX, positionY))
                gameover ();
            SetConsoleTextAttribute(hOut, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
            gotoxy (56, 10);
            printf ("%4d", fraction);
            gotoxy (57, 11);
            printf ("---");
            gotoxy (56, 12);
            int lssc = fraction % 100 ? (fraction / 100 + 1) * 100 : fraction;
            if (mode == 1 && lssc >= 200) lssc = 200;
            if (mode == 2 && lssc >= 999) lssc = 900;
            if (mode == 3 && lssc >= 1300) lssc = 1300;
            printf ("%4d", lssc);
            keytime = clock(); holdflag = 1;
            if (clock () - addlineflag > addlinetime)
            {
                addlineflag = clock ();
                addline ();
            }
            keydownflag = 0;
        }
        if (checkblock (now, positionX + 1, positionY))
        while (checkblock (now, positionX + 1, positionY))
        {
            ++positionX;
            if (sleeptime)
            {
                printblock (now, positionY * 2 + 8, positionX + 1);
                int sttime = clock (), timeover;
                while ((timeover = (clock () - sttime < sleeptime)) && !kbhit());
                clearblock (now, positionY * 2 + 8, positionX + 1);
                if (timeover)
                    checkkey ();
            }
            checkkey ();
        }
        printblock (now, positionY * 2 + 8, positionX + 1);
        Sleep (100);
        clearblock (now, positionY * 2 + 8, positionX + 1);
        checkkey ();
    }
if (xray)
    SetConsoleTextAttribute (hOut, FOREGROUND_GREEN);
else
    SetConsoleTextAttribute (hOut, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY);
    for (int i = 1; i <= 20; ++i)
    {
        gotoxy (10, i + 1);
        for (int j = 1; j <= 10; ++j)
            if (map[i][j])
                if (xray)
                    printf ("[]");
                else
                    printf ("■");
            else
                printf ("  ");
    }
win ();
}
void master_extra_game ()
{
blind = 1;
easy_extra_game ();
}
void shirase_extra_game ()
{
blind = 1;
easy_extra_game ();
}
  • 俄罗斯方块3(下落有BUG,不会死):
    //BUG:清除一行后上方的方块会重力下落
    #include <iostream>
    #include <windows.h>
    #include <vector>
    #include <mmsystem.h>
    #pragma comment(lib, "winmm.lib")
    using namespace std;
    #define GameW 10
    #define GameH 20
    const int CtrlLeft = GameW*2+4 + 3;
    struct Point {
    Point(){}
    Point(int x, int y) {_x = x, _y = y;}
    int _x, _y;
    };
    HANDLE g_hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
    HANDLE g_hInput  = GetStdHandle(STD_INPUT_HANDLE);
    Point g_ptCursor(0,0);
    BOOL isChecking = FALSE;
    BOOL g_bGameOver = FALSE;
    int g_nGameBack[GameH][GameW], Case;
    int nowKeyInfo = -1;
    int g_nDiff = 1;
    int g_nLife = 2;
    int g_nScore = 0;
    void SetCursor(COORD cd) {
    SetConsoleCursorPosition(g_hOutput, cd);
    }
    void SetCursor(int x, int y){
    COORD cd = {x, y};
    SetCursor(cd);
    }
    void SetBlockCursor(int x, int y){
    COORD cd = {2*x + 2, y + 1};
    SetCursor(cd);
    }
    void SetBack(int x, int y, BOOL bk) {
    SetBlockCursor(x, y);
    if (bk) 
        printf("%s", "■");
    else
        printf(" ");
    }
    bool Out(int x, int y) {
    return x < 0 || y < 0 || x >= GameW || y >= GameH; 
    }
    struct xBlock {
    public:
     int len;
    int nowRotateID;
    BOOL mask[4][4][4];
    static vector <xBlock> List;
    xBlock() { len = 0; }
    xBlock(int l, char *str) {
        int i, j, k;
        len = l;
        memset(mask, FALSE, sizeof(mask));
        for(i = 0; i < l; i++) {
            for(j = 0; j < l; j++) {
                mask[0][i][j] = str[i*l + j] - '0';
            }
        }
        for(k = 1; k < 4; k++) {
            for(i = 0; i < len; i++) {
                for(j = 0; j < len; j++) {
                    mask[k][i][j] = mask[k-1][j][len-1-i];
                }
            }
        }
        nowRotateID = rand() % 4;
    }
    void rotate() {
        nowRotateID ++;
        if (nowRotateID >= 4)
            nowRotateID = 0;
    }
    BOOL getUnit(int x, int y, int roID) {
        if (roID == -1) {
            roID = nowRotateID;
        }
        return mask[roID][y][x];
    }
    };
    vector <xBlock> xBlock::List;
    class Block {
    public:
     int x, y;
    int ID;
    xBlock bk;
    void reset(xBlock *pbk) {
        bk = *pbk;
        x = 4, y = 0;
        ID = ++ Case;
        if(collide(0,0)) {
            lifeDown();
        }
        draw();
        *pbk = xBlock::List[rand() % xBlock::List.size()];
    }
    void lifeDown() {
        int i, j;
        for(i = 0; i < GameH; i++) {
            for(j = 0; j < GameW; j++) {
                SetBack(j, i, TRUE);
                Sleep(10);
            }
        }
        if(g_nLife) {
            g_nLife --;
            for(i = g_nLife; i < 6; i++) {
                SetCursor(CtrlLeft + i, 15);
                printf("%c", ' ');
            }
            for(i = GameH-1; i >= 0; i--) {
                for(j = GameW-1; j >= 0; j--) {
                    SetBack(j, i, FALSE);
                    Sleep(10);
                    g_nGameBack[i][j] = 0;
                }
            }
        }else {
            g_bGameOver = TRUE;
        }
    }
    void erase() {
        int i, j;
        for(i = 0; i < bk.len; i++) {
            for(j = 0; j < bk.len; j++) {
                if (bk.getUnit(j, i, -1)) {
                    if(!Out(j+x, i+y) && g_nGameBack[i+y][j+x]) {
                        SetBack(j+x, i+y, FALSE);
                        g_nGameBack[i+y][j+x] = 0;
                    }
                }
            }
        }
    }
    void draw() {
        int i, j;
        for(i = 0; i < bk.len; i++) {
            for(j = 0; j < bk.len; j++) {
                if (bk.getUnit(j, i, -1)) {
                    if(!Out(j+x, i+y) && !g_nGameBack[i+y][j+x]) {
                        SetBack(j+x, i+y, TRUE);
                        g_nGameBack[i+y][j+x]  = ID;
                    }
                }
            }
        }
    }
    void draw(int x, int y) {
        int i, j;
        for(i = 0; i < 4; i++) {
            for(j = 0; j < 4; j++) {
                SetCursor(x + 2*j, y + i);
                if (bk.getUnit(j, i, -1)) {    
                    printf("%s", "■");
                }else 
                    printf(" ");
            }
        }
    }
    bool collide(int dx, int dy, int roID = -1) {
        int i, j;
        for(i = 0; i < bk.len; i++) {
            for(j = 0; j < bk.len; j++) {
                if (bk.getUnit(j, i, roID)) {
                    Point ptPos(j + x + dx, i + y + dy);
                    if(Out(ptPos._x, ptPos._y)
                    || g_nGameBack[ptPos._y][ptPos._x] && ID != g_nGameBack[ptPos._y][ptPos._x]) {
                        return TRUE;
                    }
                }
            }
        }
        return FALSE;
    }
    void rotate(int nTimes = 1) {
        int nextro = (bk.nowRotateID + nTimes) % 4;
        if(collide(0, 0, nextro)) {
            return ;
        }
        Beep(12000, 50);
        erase();
        bk.nowRotateID = nextro;
        draw();
    }
    BOOL changepos(int dx, int dy) {
        if(collide(dx, dy)) {
            return FALSE;
        }
        erase();
        x += dx;
        y += dy;
        draw();
        return TRUE;
    }
    };
    void GameInit() {
    CONSOLE_CURSOR_INFO cursor_info;
    cursor_info.bVisible = FALSE;
    cursor_info.dwSize   = 100;
    SetConsoleCursorInfo(g_hOutput, &cursor_info);
    xBlock::List.push_back(xBlock(3, "010111000"));
    xBlock::List.push_back(xBlock(3, "110110000"));
    xBlock::List.push_back(xBlock(3, "111001000"));
    xBlock::List.push_back(xBlock(3, "111100000"));
    xBlock::List.push_back(xBlock(3, "110011000"));
    xBlock::List.push_back(xBlock(3, "011110000"));
    xBlock::List.push_back(xBlock(4, "1000100010001000"));
    }
    void DrawFrame(int x, int y, int nWidth, int nHeight) {
    int i;
    for(i = 0; i < nWidth; i++) {
        SetCursor(x + 2*i + 2, y);
        printf("%s", "一");
        SetCursor(x + 2*i + 2, y + nHeight+1);
        printf("%s", "┄");
    }
    for(i = 0; i < nHeight; i++) {
        SetCursor(x, y + i + 1);
        printf("%s", "┆");
        SetCursor(x + nWidth*2+2, y + i + 1);
        printf("%s", "┆");
    }        
    SetCursor(x, y);
    printf("%s", "┌");    
    SetCursor(x, y + nHeight+1);
    printf("%s", "└");
    SetCursor(x + nWidth*2+2, y);
    printf("%s", "┐");    
    SetCursor(x + nWidth*2+2, y + nHeight+1);
    printf("%s", "┘");
    }
    void MissionInit() {
    memset(g_nGameBack, FALSE, sizeof(g_nGameBack));
    Case = 1;
    int i;
    DrawFrame(0, 0, GameW, GameH);
    DrawFrame(GameW*2+4, 0, 4, GameH);
    SetCursor(CtrlLeft, 2);
    printf("Next");
    SetCursor(CtrlLeft, 8);
    printf("Speed");
    for(i = 0; i < g_nDiff; i++) {
        SetCursor(CtrlLeft + i, 9);
        printf("%c", 1);
    }
    SetCursor(CtrlLeft, 11);
    printf("Score");
    SetCursor(CtrlLeft, 12);
    printf("%d", g_nScore);
    SetCursor(CtrlLeft, 14);
    printf("Life");
    for(i = 0; i < g_nLife; i++) {
        SetCursor(CtrlLeft + i, 15);
        printf("%c", 3);
    }
    SetCursor(CtrlLeft-1, 19);
    printf("@Metaphu");
    SetCursor(CtrlLeft-1, 20);
    printf("Revised");
    }
    void Check() {
    isChecking = TRUE;
    int i, j, k;
    vector <int> line;
    for(i = 0; i < GameH; i++) {
        for(j = 0; j < GameW; j++) {
            if(!g_nGameBack[i][j])
                break;
        }
        if(j == GameW) {
            line.push_back(i);
        }
    }
    if(line.size()) {
        int nCount = 7;
        while(nCount --) {
            for(i = 0; i < line.size(); i++) {
                for(j = 0; j < GameW; j++) {
                    SetBack(j, line[i], nCount&1);
                }
            }
            Sleep(70);
        }
        for(i = 0; i < line.size(); i++) {
            for(j = 0; j < GameW; j++) {
                g_nGameBack[line[i]][j] = 0;
            }
        }
        for(i = 0; i < GameW; i++) {
            int next = GameH-1;
            for(j = GameH-1; j >= 0; j--) {
                for(k = next; k >= 0; k--) {
                    if(g_nGameBack[k][i]) 
                        break;
                }
                next = k - 1;
                BOOL is = (k >= 0);
                SetBack(i, j, is);
                g_nGameBack[j][i] = is;
            }
        }
        g_nScore += 2*line.size()-1;
        SetCursor(CtrlLeft, 12);
        printf("%d", g_nScore);
        if( g_nScore >= g_nDiff * g_nDiff * 10) {
            if(g_nDiff <= 6)
                g_nDiff ++;
        }
        if( g_nScore >= 50 * (g_nLife+1)) {
            if(g_nLife <= 6)
                g_nLife ++;
        }
    }
    isChecking = FALSE;
    }
    int main() {
    Block* obj = new Block();
    Block* buf = new Block();
    BOOL bCreateNew = FALSE;
    int nTimer = GetTickCount();
    int LastKeyDownTime = GetTickCount();
    GameInit();
    MissionInit();
    buf->bk = xBlock::List[rand() % xBlock::List.size()];
    while(1) {
        if(!bCreateNew) {
            bCreateNew = TRUE;
            obj->reset(&buf->bk);
            if(g_bGameOver)
                break;
            buf->draw(CtrlLeft - 1, 4);
        }
        if (GetTickCount() - nTimer >= 1000 / g_nDiff) {
            nTimer = GetTickCount();
            if (!obj->collide(0, 1))
                obj->changepos(0, 1);
            else {
                Check();
                bCreateNew = FALSE;
            }
        }
        if (GetTickCount() - LastKeyDownTime >= 100) {
            if(FALSE == isChecking) {
                LastKeyDownTime = GetTickCount();
                if (GetAsyncKeyState(VK_UP)) {
                    obj->rotate();
                }
                if (GetAsyncKeyState(VK_LEFT)) {
                    obj->changepos(-1, 0);
                }
                if (GetAsyncKeyState(VK_RIGHT)) {
                    obj->changepos(1, 0);
                }
                if (GetAsyncKeyState(VK_DOWN)) {
                    if( FALSE == obj->changepos(0, 2) )
                        obj->changepos(0, 1);
                }
            }
        }
    }
    SetCursor(8, 10);
    printf("Game Over");
    SetCursor(0, GameH+3);
    printf("按ESC键退出游戏");
    while(1) {
        if (GetAsyncKeyState(VK_ESCAPE))
            break;
    }
    return 0;
    }


  • flappy_bird2:
#include<bits/stdc++.h>
#include<Windows.h>
#define PR_Box printf("■")
#define PR_Gold printf("★")
#define PR_Ag printf("☆")
#define PR_FBird printf("Ю")
#define PR_DBird printf("Ф")
#define PR_Land printf("┳┳┯")
#define PR_Bg_TL printf("╔")
#define PR_Bg_TR printf("╗")
#define PR_Bg_DL printf("╚")
#define PR_Bg_DR printf("╝")
#define PR_Bg_X printf("═")
#define PR_Bg_Y printf("║")
#define PR_Blank printf(" ");
int Grade=1,C_Gold=0,C_Ag=0,Score=0,Delay_time=1000,Max_blank=9,Distance=18;
struct Birds {
int x,y;
int condition;
};
Birds*Bird=(Birds*)malloc(sizeof(Birds));
struct Bg {
int x,y;
int l_blank;
int reward[9];
Bg*pri;
Bg*next;
};
Bg*Bg1=new Bg[sizeof(Bg)];
void Position(int x,int y) {
COORD pos= {x-1,y-1};
HANDLE Out=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(Out,pos);
}
void CreatBird() {
Bird->x=41;
Bird->y=10;
Bird->condition=0;
}
void CreatBg() {
Bg*Bg2=(Bg*)malloc(sizeof(Bg));
Bg1->x=90;
Bg1->y=8;
Bg2->x=Bg1->x+Distance;
Bg2->y=9;
Bg1->l_blank=Max_blank-Grade;
Bg2->l_blank=Max_blank-Grade;
Bg1->next=Bg2;
Bg1->pri=Bg2;
Bg2->next=Bg1;
Bg2->pri=Bg1;
}
void InsertBg(Bg*p) {
int temp;
Bg*Bgs=(Bg*)malloc(sizeof(Bg));
Bgs->x=p->pri->x+Distance;
Bgs->l_blank=Max_blank-Grade;
srand((int)time(0));
temp=rand();
if(temp%2==0) {
    if((temp%4+p->pri->y+Max_blank-Grade)<21)Bgs->y=p->pri->y+temp%4;
    else Bgs->y=p->pri->y;
} else {
    if((p->pri->y-temp%4)>2)Bgs->y=p->pri->y-temp%4;
    else Bgs->y=p->pri->y;
}
Bgs->pri=p->pri;
Bgs->next=p;
p->pri->next=Bgs;
p->pri=Bgs;
}
void Check_Bg(Bg*q) {
Bg*p=q;
int i=0,temp;
while(++i<=5) {
    if(p->x>-4)p=p->next;
    else {
        srand((int)time(0));
        temp=rand();
        if(temp%2==0) {
            if((temp%4+p->y+Max_blank-Grade)<21)p->y=p->y+temp%4;
            else p->y=p->y;
            p->x=p->pri->x+Distance;
            p->l_blank=Max_blank-Grade;
        } else {
            if((p->y-temp%4)>2)p->y=p->y-temp%4;
            else p->y=p->y;
            p->x=p->pri->x+Distance;
            p->l_blank=Max_blank-Grade;
        }
    }
}
}
void Loop_Bg(Bg*q) {
Bg*p=q;
int i=0;
while(++i<=5) {
    p->x=p->x-1;
    p=p->next;
    if(Bird->x==p->x) {
        Score+=1;
        if(Score%4==0&&Grade<4)Grade++;
    }
}
}
void Prt_Bg(Bg*q) {
Bg*p=q;
int i=0,k,j;
while(++i<=5) {
    if(p->x>0&&p->x<=78) {
        for(k=2; k<p->y; k++) {
            Position(p->x+1,k);
            PR_Box;
            PR_Box;
            PR_Blank
        }
        Position(p->x,p->y);
        PR_Box;
        PR_Box;
        PR_Box;
        PR_Blank;
        Position(p->x,p->y+p->l_blank);
        PR_Box;
        PR_Box;
        PR_Box;
        PR_Blank;
        k=k+p->l_blank+1;
        for(k; k<=22; k++) {
            Position(p->x+1,k);
            PR_Box;
            PR_Box;
            PR_Blank;
        }
        Position(p->x,23);
        for(k=1; k<Distance/3-2; k++)PR_Land;
    }
    p=p->next;
    if(p->x==0) {
        for(j=2; j<p->y; j++) {
            Position(p->x+1,j);
            PR_Blank;
            PR_Blank;
        }
        Position(p->x+1,p->y);
        PR_Blank;
        PR_Blank;
        PR_Blank;
        Position(p->x+1,p->y+Max_blank-Grade);
        PR_Blank;
        PR_Blank;
        PR_Blank;
        j=j+Max_blank-Grade+1;
        for(j; j<=22; j++) {
            Position(p->x+1,j);
            PR_Blank;
            PR_Blank;
        }
    }
}
}
void PrtBg() {
int i;
Position(1,1);
PR_Bg_TL;
Position(79,1);
PR_Bg_TR;
Position(1,24);
PR_Bg_DL;
Position(79,24);
PR_Bg_DR;
for(i=3; i<=78; i+=2) {
    Position(i,1);
    PR_Bg_X;
    Position(i,24);
    PR_Bg_X;
}
}
void PrtBird() {
Position(Bird->x,Bird->y-1);
PR_Blank;
Position(Bird->x,Bird->y);
PR_FBird;
Position(38,2);
printf("Score:%d",Score);
}
int CheckYN(Bg*q) {
Bg*p=q;
int i=0;
while(++i<=5) {
    if(Bird->y>23)return 0;
    if(Bird->x==p->x&&Bird->y<=p->y)return 0;
    if((Bird->x==p->x||Bird->x==p->x+1||Bird->x==p->x+2)&&Bird->y==p->y)return 0;
    if(Bird->x==p->x&&Bird->y>p->y+p->l_blank)return 0;
    if((Bird->x==p->x||Bird->x==p->x+1||Bird->x==p->x+2)&&Bird->y==p->y+p->l_blank)return 0;
    p=p->next;
}
return 1;
}
void Prtfirst() {
printf("══════════════════════════════════════\n");
printf(" ■■ ■■\n");
printf(" ■■ ■■\n");
printf(" ■■ ■■\n");
printf(" ■■ ■■\n");
printf(" ■■ ■■ C++语言版 Flappy Bird\n");
printf(" ■■ ■■ 瞎搞人:侦探鼠\n");
printf(" ■■ ■■ 瞎搞日期:2019.7.3\n");
printf(" ■■ ■■ 耗时:2.46小时\n");
printf(" ■■■ ■■ 游戏说明:\n");
printf(" ■■ 1-按上箭头使鸟起飞\n");
printf(" ■■ 2-等级越高,难度越大!\n");
printf(" Ю ■■■\n");
printf("\n");
printf(" \n\n\n\n\n\n\n\n");
printf(" ┳┳┯┳┳┯┳┳┯┳┳┯┳┳┯┳┳┯┳┳┯┳┳┯┳┳┯┳┳┯┳┳┯┳┳┯┳\n");
system("pause");
Position(1,1);
int i=0;
while(i++<40*25)PR_Blank;
}
int main() {
int i=0;
char ch;
Prtfirst();
PrtBg();
CreatBg();
InsertBg(Bg1);
InsertBg(Bg1);
InsertBg(Bg1);
CreatBird();
while(1) {
    if(!CheckYN(Bg1))break;
    Check_Bg(Bg1);
    Prt_Bg(Bg1);
    PrtBird();
    Loop_Bg(Bg1);
    Bird->y=Bird->y+1;
    if(GetAsyncKeyState(VK_UP)) {
        Position(Bird->x,Bird->y-1);
        PR_Blank;
        Bird->y=Bird->y-4;
    }
    while(i++<500);
    {
        Sleep(100);
    }
    i=0;
}
Position(38,10);
printf("Game Over!");
Position(1,25);
system("pause");
}
  • 小鸟快飞flappy_bird2:
//空格控制
#include <cstdio>
#include <ctime>
#include <conio.h>
#include <windows.h>
#include <cstdlib>
#include <cstring>
using namespace std;
//==================================================================
#define hest 100            //跳的速度(点击到最高点有 (250/hest) 秒) 
#define mo 2000             //跳的高度(每次点击跳到最高多 (((1+hest)*hest+hest)/mo) 格) 
#define hei 20              //屏幕高度 
#define wid 13              //屏幕宽度 
#define det v*2-1
#define gap 5               //柱子间上下的空隙长度 
#define len 3               //柱子的宽度 
#define T  25               //柱子移动时间单位      T t移动一格
#define TT 500/T            //柱子出现频率            TT T出现一个 
//==================================================================
void color(int a)//颜色函数
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
}
void gotoxy(int x,int y)//位置函数(列为x 行为y)
{
COORD pos;
pos.X=2*x;
pos.Y=hei-y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
int v,h,hx,hy,t,tt,gg,quit,score,plused,wall[hei+5][wid+len+10];
/*
t:  时间基本单位          1t=4ms
tt: 辅助作用                柱子每 移动T格tt++ 
quit                        记录是否退出游戏 
plused                      辅助用作计分 
wall[i][j]  第 i 行第 j 列的柱子情况(0为空 1为有但还没打印 2为打印过了) 
*/ 
void reset(){
system("cls");
color(4);
for (int i=0; i<=wid; i++){gotoxy(i,-1); printf("※");}
gg=0;
hx=5;
hy=hei/2;
h=hy*mo;
t=0;
tt=-5; 
v=hest;
score=0;
memset(wall,0,sizeof(wall));
color(14); gotoxy(hx,hy); printf("◎");
}
void move_bird(){
h+=(v*2-1);
if (h>hei*mo) h=hei*mo;
if (h/mo!=hy){
    gotoxy(hx,hy); printf("  ");
    hy=h/mo;
    if (hy<0) hy=h=0;//gg=1;
    if (wall[hy][hx]) gg=1;
    gotoxy(hx,hy);
    color(14); printf("◎");
}
}
void move_wall(){
for (int i=0; i<=hei; i++){
    int k=0,pp=0;
    while (k<wid+5){
        color(2); 
        for (; k<=wid+5 && !wall[i][k]; k++);
        if (k+len/2==hx){
            if (!plused) plused=1, score++;
            if (!pp) color(15),pp=1;
        }
        if (k>=0 && wall[i][k]) wall[i][k-1]=1;
        k-=2; if (k<-1) k=-1;
        while (wall[i][++k]){
            if (hx==k && hy==i) gg=1;
            if (k<=wid && wall[i][k]==1){wall[i][k]=2; gotoxy(k,i); printf("█");}
        }
        wall[i][k-1]=0; gotoxy(k-1,i); printf("  ");
    }
}
}
void new_wall(){
srand(time(NULL));
int dd=(rand()%(hei-gap-1)+1),uu=dd+gap+1;
for (int i=1; i<=len; i++) wall[dd][i+wid]=1;
for (int i=dd-1; i>=0; i--) for (int j=2; j<len; j++) wall[i][j+wid]=1;
for (int i=1; i<=len; i++) wall[uu][i+wid]=1;
for (int i=uu+1; i<=hei; i++) for (int j=2; j<len; j++) wall[i][j+wid]=1;
plused=0;
}
void game(){
while (!gg){
    v--;
    if(kbhit()) {char ch=getch(); v=hest;}
    Sleep(4); t=(t+1)%T;
    if (tt==TT) tt=0, new_wall();
    if (t==0) tt++, move_wall();
    move_bird();
    color(8);
    gotoxy(wid/2,2*hei/3);
    printf("%d",score);
}
gotoxy(wid/2,-3); color(15); printf("GG~");
gotoxy(wid/2-3,-5); printf("final score: %d",score);
gotoxy(hx,hy); color(12); printf("◎");
}
int main(){
char ch;
while (ch!='*'){
    reset();
    ch=getch();
    game();
    Sleep(1500);
    while (kbhit()) ch=getch();
    Sleep(1000);
    ch=getch();
}
}
  • 贪吃蛇无限版:
#include <cstdio>
#include <iostream>
#include <ctime>
#include <conio.h>
#include <windows.h>       //停顿:Sleep(); 
#include <cstdlib>         //清屏:system("cls");
#include <cstring>         //1:'◎' 2:'⊙' 3:'▲' 4:'◆' 5:'■'
using namespace std;
int a[100][100], dir[100][100];
//string guo[5]={'▲','◆','■','▼','★'};
int fx[4][2] = { {0,-1},{1,0},{0,1},{-1,0} }; //ap来记录奖励块的分   
int n, m, ff, f = 1, t = 50, T = 50, ap = 10, egg[2], danshu, headx, heady, endx, endy, point = 0, apple = 0, fangx, stopp = 0, panduan = 0;  //ff用来判断  panduan 0:中途退出;1:咬到自己;2:重置  
int lastfx;
int zuobi = 0;
int fff[110][110];
string print[4][4] = { {"═","╔","  ","╚"},{"╝","║","╚","  "},{"  ","╗","═","╝"},{"╗"," ","╔","║"} };
void color(int a)//颜色函数
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), a);
}
void gotoxy(int x, int y)//位置函数(行为x 列为y)
{
COORD pos;
pos.X = 2 * y;
pos.Y = x;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
void reset() {
system("cls");
color(7); printf("                   ★"); color(10); printf("贪吃蛇"); color(11); printf("★\n\n"); color(7);
printf("⊙打印若有错位请调整窗口大小或按r重输小点的数⊙\n\n  请输入场地大小(格式:行数 列数) 推荐(20 20) \n\n>>");
scanf("%d%d", &n, &m);
system("cls");
color(14);
for (int i = 1; i <= m; i++) { gotoxy(0, i); printf("※"); gotoxy(n + 1, i); printf("※"); }
for (int i = 1; i <= n; i++) { gotoxy(i, 0); printf("※"); gotoxy(i, m + 1); printf("※"); }
color(10);
memset(fff, 0, sizeof(fff));
gotoxy(0, 0); printf("┍");
gotoxy(n + 1, 0); printf("┕");
gotoxy(0, m + 1); printf("┑");
gotoxy(n + 1, m + 1); printf("┙");
color(7);
gotoxy(0, m + 5); printf("方向:a/s/d/w | 1/2/3/5");
gotoxy(1, m + 5); printf("暂停:p       |       0");
gotoxy(2, m + 5); printf("减速:q/j     |     4/7");
gotoxy(3, m + 5); printf("加速:e/k     |     6/9");
gotoxy(4, m + 5); printf("重置:r       |       .");
gotoxy(5, m + 5); printf("地图:h       |       +");
headx = endx = endy = 1; heady = 3;
f = 1; ff = 3;
memset(a, 1, sizeof(a));
memset(dir, 0, sizeof(dir));
a[1][1] = a[1][2] = a[1][3] = 0;
gotoxy(1, 1); printf("⊙");
gotoxy(1, 2); printf("⊙");
gotoxy(1, 3); printf("◎");
dir[1][1] = dir[1][2] = dir[1][3] = 2;
apple = 0; fangx = 2; point = 0; danshu = 0; lastfx = 2; stopp = 0;
if (zuobi) { color(12); gotoxy(12, m + 5); printf("作弊可耻!"); gotoxy(m + 4, 0); printf("做了弊还没路走那就救不了你了,乖乖重来吧......"); color(7); }
}
void haha() {
char ch = getch();
if ((ch == 'a' || ch == '1') && dir[headx][heady] != 2) { fangx = 0; return; }
if ((ch == 's' || ch == '2') && dir[headx][heady] != 3) { fangx = 1; return; }
if ((ch == 'd' || ch == '3') && dir[headx][heady] != 0) { fangx = 2; return; }
if ((ch == 'w' || ch == '5') && dir[headx][heady] != 1) { fangx = 3; return; }
if (ch == 'p' || ch == '0') { stopp = (stopp + 1) % 2; return; }
if (ch == 'q' || ch == '4') { T += 10; if (T >= 1005) T = 1005; return; }
if (ch == 'e' || ch == '6') { T -= 10; if (T <= 1) T = 2; return; }
if (ch == 'j' || ch == '7') { T += 1; if (T >= 1005) T = 1005; return; }
if (ch == 'k' || ch == '9') { T -= 1; if (T <= 1) T = 2; return; }
if (ch == 'r' || ch == '.') { ff = 2; f = 0; panduan = 0; return; }
if (ch == 'h' || ch == '8') {
    zuobi += 1; zuobi %= 2;
    if (zuobi) { color(12); gotoxy(12, m + 5); printf("作弊可耻!"); gotoxy(m + 4, 0); printf("做了弊还没路走那就救不了你了,乖乖重来吧......"); color(7); }
    else { printf("                "); gotoxy(m + 4, 0); printf("                                                   "); }
}
if (ch == 'g' || ch == '+') {
    gotoxy(n + 50, 0);
    printf("现在切换到地图编辑模式\n\nq/4 为开始放置模式(光标移动过的痕迹会留下障碍物)\n\ne/6 为取消放置障碍物模式\n\nj/0 为橡皮模式(再按一次退出橡皮模式)\n\n按 h/+ 继续游戏\n\n");
    printf("\n按任意键开始编辑地图");
    color(13);
    int x = 1, y = 1, dfx, moshi = 0;
    ch = getch(); gotoxy(0, 0); gotoxy(1, 1);
    while (ch != 'h' && ch != '+') {
        if (ch == 'a' || ch == '1') dfx = 0;
        else if (ch == 's' || ch == '2') dfx = 1;
        else if (ch == 'd' || ch == '3') dfx = 2;
        else if (ch == 'w' || ch == '5') dfx = 3;
        else dfx = 5;
        if (ch == 'q' || ch == '4') moshi = 1;
        if (ch == 'e' || ch == '6') moshi = 0;
        if (ch == 'j' || ch == '0') if (moshi - 2) moshi = 2; else moshi = 0;
        if (dfx <= 4) {
            if (x + fx[dfx][0] > 0 && x + fx[dfx][0] <= n && y + fx[dfx][1] > 0 && y + fx[dfx][1] <= m) { x += fx[dfx][0]; y += fx[dfx][1]; }
            if (x > 0 && x <= n && y > 0 && y <= m) {
                gotoxy(x, y);
                if (moshi == 1 && a[x][y] && !fff[x][y]) { a[x][y] = 0; fff[x][y] = 1; printf("□"); }
                if (moshi == 2 && fff[x][y]) { a[x][y] = 1; fff[x][y] = 0; printf("  "); }
                gotoxy(x, y);
            }
        }
        ch = getch();
    }
    color(7);
}
}
void findegg() {
srand(time(NULL));
int aa = 1;
while (!a[egg[0]][egg[1]]) { egg[0] = (rand() % (n)) + 1; egg[1] = (rand() % m) + 1; }
a[egg[0]][egg[1]] = 0;
point += ap;
gotoxy(egg[0], egg[1]); if (!apple) { printf("★"); ap = 8000 / T; if (ap >= 200) ap = 200; }
else { printf("●"); ap = 10; }
apple++; apple = apple % 10;
gotoxy(n + 2, 0); printf("距离下一个奖励蛋还有 "); color(11); cout << 9 - (8 + apple) % 10; color(7); printf(" 个");
danshu++;
return;
}
int gg() {
if (ff == 0) return(0);
if (ff == 1) { gotoxy(n + 2, 1); Sleep(3000); gotoxy(n + 26, 1); gotoxy(n + 10, 0); printf("哦豁~ 你咬到自己了o( ̄▽ ̄)d\n\n\n你共吃蛋 %d 个\n\n\n你共得分 %d 分,恭喜!\n\n\n等一下就继续", danshu, point); Sleep(5000); if (kbhit) return(1); }
if (ff == 2) return(1);
return 0;
}
void dong() {
gotoxy(11, m + 3); printf("当前的蛋有 %d 分            ", ap);
if (ap - 3 > 10) ap -= 3;
if (ap != 10) {
    gotoxy(egg[0], egg[1]);
    color(ap % 10);
    printf("★");
    color(7);
}
int x, y;
x = (headx + fx[fangx][0] - 1 + n) % (n)+1; y = (heady + fx[fangx][1] - 1 + m) % (m)+1;
dir[headx][heady] = fangx;
if (x == egg[0] && y == egg[1]) {
    gotoxy(headx, heady); cout << print[lastfx][fangx];
    dir[headx][heady] = fangx;
    headx = x; heady = y; a[x][y] = 0;
    gotoxy(headx, heady); printf("◎");
    findegg();
}
else if (a[x][y]) {
    gotoxy(headx, heady); cout << print[lastfx][fangx];
    headx = x; heady = y; a[x][y] = 0;
    gotoxy(headx, heady); printf("◎");
    gotoxy(endx, endy); printf("  "); a[endx][endy] = 1;
    x = endx; y = endy;
    endx += fx[dir[x][y]][0]; endx = (endx - 1 + n) % (n)+1;
    endy += fx[dir[x][y]][1]; endy = (endy - 1 + m) % (m)+1;
    dir[x][y] = -1;
}
else if (!zuobi) { f = 0; ff = 1; }
dir[headx][heady] = fangx;
lastfx = fangx;
return;
}
int main() {
while (1) {
    srand(time(NULL));
    reset();
    egg[0] = (rand() % n - 1) + 2; egg[1] = (rand() % m) + 1;
    a[egg[0]][egg[1]] = 0;
    gotoxy(egg[0], egg[1]); if (apple) { printf("★"); ap = 100; }
    else printf("●");
    while (f) {
        if (kbhit()) haha();
        gotoxy(9, m + 5); printf("吃蛋:%d 个", danshu);
        gotoxy(8, m + 5); printf("速度: %d   ", 1005 - T);
        //while (stopp) {Sleep(10); if (kbhit()) haha();}
        Sleep(1); t = (t - 1 + T) % T;
        if (t == 0 && !stopp) dong();
        gotoxy(7, m + 5); printf("得分:%d 分", point);
        gotoxy(12, m + 5);
    }
    if (!gg()) return 0;
}
return 0;
}
  • 贪吃蛇经典版(有墙壁):
//WASD控制/开始游戏
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <windows.h>
#define W 32
#define H 22
const int SIZE_SK = 30*20;
HANDLE hout;
COORD pos= {0, 0};
CONSOLE_CURSOR_INFO cur_info = {1, 0};
struct snake {
short x, y; //节点坐标
short md; //节点运动方向
short num; //节点内容
int col; //节点颜色
} snake[30*20];
const short dx[4] = {-1, 1, 0, 0}; //方向?
const short dy[4] = {0, 0, -1, 1}; //方向?
short gamemap[W][H];
short head, tail, score=0;
void initmap(int tm);
void createFood(void);
int getkeys(void);
int move(int idx);
void pntSnake(int i);
void drawEdge(void);
void gameinfo(char *s, int n);
int main(void)
{
int tm = 150; //刷新间隔150毫秒?
hout = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorInfo(hout, &cur_info); //隐藏光标?
system("COLOR 2f");
initmap(tm);
return 0;
}
void initmap(int tm)
{
short x, y, i, j;
int index, tmp, ret_m;
while (1){
head=0;
tail=-1;
score=0;
//初始化蛇节点随机颜色
srand(time(0));
for (i=0; i<SIZE_SK; i++)
snake[i].col = 0x20;
//初始化地图数组
for (i=0; i<W; i++)
for (j=0; j<H; j++)
gamemap[i][j] = 0;
system("cls");
//绘制边界?
drawEdge();
//打印蛇头
srand(time(0));
do {
x = rand()%(W-2) + 1;
y = rand()%(H-2) + 1;
} while (gamemap[x][y] != 0); //只在空白处生成?
snake[head].x = x;
snake[head].y = y;
gamemap[x][y] = snake[head].num = 1;
pos.X = x*2;
pos.Y = y;
SetConsoleCursorPosition(hout, pos);
printf("█");
//打印食物
createFood();
while ((index=getkeys()) == 4)
Sleep(500);
//循环开始
while (1){
ret_m = move(index);
if (ret_m == 1){
pos.X = 0; pos.Y = H;
SetConsoleCursorPosition(hout, pos);
printf("哎呀,挂了哦 ^_^");
break;
}
else if (ret_m == 2){
pos.X = 0; pos.Y = H;
SetConsoleCursorPosition(hout, pos);
printf("哎呀,超神了 ^_^");
break;
}
Sleep(tm);
if ((tmp = getkeys()) != 4)
index = tmp;
}
//菜单代码
puts(" 按w\\s\\a\\d 继续玩,退出请关闭我");
getch();
}
}
void createFood(void)
{
short x, y;
srand(time(0));
do {
x = rand()%(W-2) + 1;
y = rand()%(H-2) + 1;
} while (gamemap[x][y] != 0);
gamemap[x][y] = 2;
pos.X = x*2; pos.Y = y;
SetConsoleCursorPosition(hout, pos);
SetConsoleTextAttribute(hout, 0xccccc);
printf("█");
}
int getkeys(void) //获取方向键?
{
char ch;
while(kbhit()){
ch = getch();
if(ch == 'w')
return 2;
if(ch == 's')
return 3;
if(ch == 'a')
return 0;
if(ch == 'd')
return 1;
while (kbhit());
}
return 4;
}
int move(int idx)
{
int newx, newy, pt_tail, i;
newx = snake[head].x + dx[idx];
newy = snake[head].y + dy[idx];
switch (gamemap[newx][newy]){
case 0:
if ((head = ++head%SIZE_SK) != tail){ //不相等即蛇节点队列未满
snake[head].x = newx;
snake[head].y = newy;
snake[head].num = 1;
gamemap[newx][newy] = 1;
tail = ++tail%SIZE_SK;
snake[tail].num = 0; //清除尾部,打印空字符
gamemap[snake[tail].x][snake[tail].y] = 0;
snake[head].md = idx; //存储移动方向?
pt_tail = tail - 1;
}
else
return 2; //回合结束标记2代表蛇填满空间
break;
case 2:
if ((head = ++head%SIZE_SK) != tail){ //不相等即蛇节点队列未满
snake[head].x = newx; //head+1后存储新坐标为蛇头?
snake[head].y = newy;
snake[head].num = 1;
gamemap[newx][newy] = 1;
snake[head].md = idx; //存储移动方向
pt_tail = tail = tail%SIZE_SK;
score++;
createFood();
}
break;
case 3:
return 1; //遇到墙壁
case 1:
return 1; //遇到自身
default: ;
}
//打印蛇
if (pt_tail == -1)
pt_tail++;
if (pt_tail < head){ //尾巴在头部后面(这两者是数组索引)?
for (i=pt_tail; i<=head; i++){
pntSnake(i); //打印?
}
}
else{
for (i=pt_tail; i<SIZE_SK; i++)
pntSnake(i);
for (i=0; i<=head; i++)
pntSnake(i);
}
gameinfo(" 分数", score);
return 0;
}
void pntSnake(int i)
{
//转换x坐标:内部是连续整数,打印时一个字符占位2,所以要2x
pos.X = snake[i].x * 2;
pos.Y = snake[i].y;
SetConsoleCursorPosition(hout, pos);
if (snake[i].num == 0){
putchar('\0'); //清除蛇尾
putchar('\0');}
else if (snake[i].num == 1){
//下面函数第二个参数低4位控制前景色,高4位控制背景色,共8位?
SetConsoleTextAttribute(hout, snake[i].col);
printf("█");
}
}
void drawEdge(void)
{
int i;
for (i=0; i<W; i++){
pos.X = i*2; pos.Y = 0;
SetConsoleCursorPosition(hout, pos);
printf("█");
gamemap[i][pos.Y] = 3;
pos.Y = H-1;
SetConsoleCursorPosition(hout, pos);
printf("█");
gamemap[i][H-1] = 3;
}
for (i=1; i<H-1; i++){
pos.X = 0; pos.Y = i;
SetConsoleCursorPosition(hout, pos);
printf("█");
gamemap[pos.X][i] = 3;
pos.X = (W-1)*2;
SetConsoleCursorPosition(hout, pos);
printf("█");
gamemap[W-1][i] = 3;
}
}
void gameinfo(char *s, int n)
{
pos.X = 0; pos.Y = H;
SetConsoleCursorPosition(hout, pos);
SetConsoleTextAttribute(hout, 0x2f);
printf(" 贪吃蛇");
printf(" %s: %d", s, n);
}
  • 吃豆人:
#include <cstdio>
#include <iostream>
#include <ctime>
#include <conio.h>
#include <windows.h>       //停顿:Sleep(); 
#include <cstdlib>         //清屏:system("cls");
#include <cstring>
using namespace std;
const int n=809;
struct Point {int x,y;};
int dali;
int fx[4]={-1,27,1,-27};
int fxfx[4][2]={{0,-1},{1,0},{0,1},{-1,0}};
int dis[1000][1000]; //0:墙 1:有分的路 2:没分的路 3:怪物的家 
int changdi[30][27]={
{0,0,0,0,0,0,0,0,0,0,0,0,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,0,1,1,1,1,1,1,1,1,1,1,1,1,0},
{0,1,0,0,0,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,0,0,0,1,0},
{0,1,0,0,0,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,0,0,0,1,0},
{0,1,0,0,0,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,0,0,0,1,0},
{0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
{0,1,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,1,0},
{0,1,1,1,1,1,1,0,0,1,1,1,1,0,1,1,1,1,0,0,1,1,1,1,1,1,0},
{0,0,0,0,0,0,1,0,0,0,0,0,2,0,2,0,0,0,0,0,1,0,0,0,0,0,0},
{0,0,0,0,0,0,1,0,0,0,0,0,2,0,2,0,0,0,0,0,1,0,0,0,0,0,0},
{0,0,0,0,0,0,1,0,0,2,2,2,2,2,2,2,2,2,0,0,1,0,0,0,0,0,0},
{0,0,0,0,0,0,1,0,0,2,0,0,0,3,0,0,0,2,0,0,1,0,0,0,0,0,0},
{0,0,0,0,0,0,1,0,0,2,0,3,3,3,3,3,0,2,0,0,1,0,0,0,0,0,0},
{2,2,2,2,2,2,1,2,2,2,0,3,3,3,3,3,0,2,2,2,1,2,2,2,2,2,2},
{0,0,0,0,0,0,1,0,0,2,0,3,3,3,3,3,0,2,0,0,1,0,0,0,0,0,0},
{0,0,0,0,0,0,1,0,0,2,0,0,0,0,0,0,0,2,0,0,1,0,0,0,0,0,0},
{0,0,0,0,0,0,1,0,0,2,2,2,2,2,2,2,2,2,0,0,1,0,0,0,0,0,0},
{0,0,0,0,0,0,1,0,0,2,0,0,0,0,0,0,0,2,0,0,1,0,0,0,0,0,0},
{0,0,0,0,0,0,1,0,0,2,0,0,0,0,0,0,0,2,0,0,1,0,0,0,0,0,0},
{0,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,0},
{0,1,0,0,0,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,0,0,0,1,0},
{0,1,0,0,0,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,0,0,0,1,0},
{0,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,0},
{0,0,0,1,0,0,1,0,0,1,0,0,0,0,0,0,0,1,0,0,1,0,0,1,0,0,0},
{0,0,0,1,0,0,1,0,0,1,0,0,0,0,0,0,0,1,0,0,1,0,0,1,0,0,0},
{0,1,1,1,1,1,1,0,0,1,1,1,1,0,1,1,1,1,0,0,1,1,1,1,1,1,0},
{0,1,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,1,0},
{0,1,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,1,0},
{0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
};
int x,x1,x2,x3,x4,y,y1,y2,y3,y4;
int now,now1,now2,now3,now4;
int g1,g2,g3,g4;
int fangx,nextfx,last1,last2,last3,last4;
int fenshu,guozi,guaitimer;
int T1,T2,t1,t2,stopped; //T:计时 t1:玩家速度 t2:怪物速度 
int f=0; //f:{0:继续 1:被吃 2:赢了 3:输了}
int beichi;
void color(int a)//颜色函数
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
}
void gotoxy(int x,int y)//位置函数(行为x 列为y)
{
COORD pos;
pos.X=2*y;
pos.Y=x;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
void begin(){
system("cls");
color(11); printf("       ★"); color(10); printf("吃豆人"); color(11); printf("★\n\n"); color(7);
printf("     请将窗口开至"); color(11); printf("全屏\n"); color(7);
printf("  正在初始化,请耐心等待"); 
for (int i=0; i<=n; i++) for (int j=1; j<=n; j++) dis[i][j]=900;
 for (int i=0; i<=n; i++){
    for (int j=0; j<=3; j++){
        if (i+fx[j]>=0 && i+fx[j]<=n){
            int k=i+fx[j],xx=k/27,yy=k%27,kk;
            if (changdi[i/27][i%27] && changdi[xx][yy]) dis[i][k]=kk=1;
        }
    }
}
 for (int k=0; k<=n; k++)if(changdi[k]){
    for (int i=0; i<=n; i++)if(changdi[i])
        for (int j=0; j<=n; j++)if(changdi[j])
            if (dis[i][j]>dis[i][k]+dis[k][j]) dis[i][j]=dis[i][k]+dis[k][j];
    if (k%80==0){color (13); gotoxy(3,12); printf("│");}
    if (k%80==20){color(13); gotoxy(3,12); printf("╱");}
    if (k%80==40){color(13); gotoxy(3,12); printf("─");}
    if (k%80==60){color(13); gotoxy(3,12); printf("╲");}
    if (k%60==0){color(11); gotoxy(5,k/60); printf("●");}
}
}
void shuru(){
char ch=getch();
if (ch=='1' | ch=='j') if (changdi[x+fxfx[0][0]][y+fxfx[0][1]]==1|changdi[x+fxfx[0][0]][y+fxfx[0][1]]==2) fangx=nextfx=0; else nextfx=0;
else if (ch=='2' | ch=='k') if (changdi[x+fxfx[1][0]][y+fxfx[1][1]]==1|changdi[x+fxfx[1][0]][y+fxfx[1][1]]==2) fangx=nextfx=1; else nextfx=1;
else if (ch=='3' | ch=='l') if (changdi[x+fxfx[2][0]][y+fxfx[2][1]]==1|changdi[x+fxfx[2][0]][y+fxfx[2][1]]==2) fangx=nextfx=2; else nextfx=2;
else if (ch=='5' | ch=='i') if (changdi[x+fxfx[3][0]][y+fxfx[3][1]]==1|changdi[x+fxfx[3][0]][y+fxfx[3][1]]==2) fangx=nextfx=3; else nextfx=3;
else if (ch=='0' | ch=='s') stopped=(stopped+1)%2;
else if (ch=='4' | ch=='a') t1++;
else if (ch=='7' | ch=='q') t2++;
else if ((ch=='6' | ch=='d') && t1-1>0) t1--; 
else if ((ch=='9' | ch=='e') && t2-1>0) t2--;
else if (ch=='g') dali=(dali+1)%2;
}
void reset(){
system("cls"); color(7);
gotoxy(2,30); printf("控制方向:1/2/3/5");
gotoxy(4,30); printf("你的速度:4/6");
gotoxy(6,30); printf("怪物速度:7/9");
x=22; y=13; 
x1=x2=x3=x4=14; y1=11; y2=12; y3=14; y4=15;
now=607; now1=389; now2=390; now3=392; now4=393;
for (int k=0; k<=n; k++){
    int i=k/27,j=k%27;
    gotoxy(i,j);
    if (changdi[i][j]==1){color(7); printf("•");}
    else if (!changdi[i][j]){color(1); printf("■");}
    if (j=26){gotoxy(i,27); color(7); printf("%d",i);} ////////////////////////////////////////////////////
}
gotoxy(0,0);
gotoxy(x,y); color(14); printf("●");
gotoxy(x1,y1); color(4); printf("◆");
gotoxy(x2,y2); color(5); printf("◆");
gotoxy(x3,y3); color(3); printf("◆");
gotoxy(x4,y4); color(2); printf("◆");
fangx=0; T1=T2=guaitimer=0; t1=75; t2=100;stopped=0; fenshu=0; guozi=237; g1=g2=g3=g4=0; dali=0;
gotoxy(14,30); printf("  ");
}
void move1(){
int xx,yy;
xx=x+fxfx[nextfx][0]; yy=y+fxfx[nextfx][1];
if (changdi[xx][yy]){
    if (changdi[xx][yy]==1){fenshu+=1; changdi[xx][yy]=2;}
    color(14);
    gotoxy(x,y); printf("  ");
    gotoxy(xx,yy); if (!dali) printf("◎"); else printf("☆");
    now=x*27+y; x=xx; y=yy;
    fangx=nextfx;
}
else{
    if (x==13 && y==0 && fangx==0){xx=x; yy=26;}
    else if (x==13 && y==26 && fangx==2){xx=x; yy=0;}
    else{xx=x+fxfx[fangx][0]; yy=y+fxfx[fangx][1];}
    if (changdi[xx][yy]){
        if (changdi[xx][yy]==1){fenshu+=1; changdi[xx][yy]=2;}
        color(14);
        gotoxy(x,y); printf("  ");
        gotoxy(xx,yy); if (!dali) printf("◎"); else printf("☆");
        now=x*27+y; x=xx; y=yy;
     }
 }
color(7);
//gotoxy(15,28); printf("(%d,%d)     ",x,y); gotoxy(16,28); printf("now:%d     ",now); gotoxy(17,28); printf("%d (%d,%d) ",fangx,fxfx[fangx][0],fxfx[fangx][1]); gotoxy(18,28); printf("(%d,%d) changdi:%d  ",xx,yy,changdi[xx][yy]);
}
void move2(){
int haha,minhaha,xx,yy,chi=0;
if (g1){
    minhaha=2147483647;
    if (now1%27==0 | now1%27==26) haha=last1;
    else if (!dali){
        for (int i=0; i<=3; i++)
            if (changdi[(now1+fx[i])/27][(now1+fx[i])%27] && i!=last1 && minhaha>dis[now1+fx[i]][now]) 
                {minhaha=dis[now1+fx[i]][now]; haha=i;}
    }
    else{
        minhaha=-minhaha;
        for (int i=0; i<=3; i++)
            if (changdi[(now1+fx[i])/27][(now1+fx[i])%27] && i!=last1 && minhaha<dis[now1+fx[i]][now]) 
                {minhaha=dis[now1+fx[i]][now]; haha=i;}
    }
    xx=now1/27; yy=now1%27; gotoxy(xx,yy); 
    if (changdi[xx][yy]==1) printf("•");else printf("  "); 
    now1+=fx[haha]; last1=(haha+2)%4;
    xx=now1/27; yy=now1%27; gotoxy(xx,yy); color(4); printf("◆"); color(7);
    if (xx==x && yy==y){
        if (!dali) chi+=1;
        else {
            guozi+=50;
            fenshu+=50;
            last1=0;
            gotoxy(now1/27,now1%27); 
            if (changdi[now1/27][now1%27]==1) printf("•"); else printf("  ");
            now1=389;
        }
    }
}
if (g2){
    int k;
    minhaha=2147483647;
    if (fangx==0 | fangx==2){
        k=y+(fxfx[fangx][1])*3;
        while (k>25 | !changdi[x][k]) k--;
        while (k<1 | !changdi[x][k]) k++;
    } else{
        k=x+(fxfx[fangx][0])*3;
        while (k>28 | !changdi[k][y]) k--;
        while (k<1 | !changdi[k][y]) k++; 
    } 
    if (fangx==0 | fangx==2) k=x*27+k; else k=k*27+y;
     if (now2%27==0 | now2%27==26) haha=last2;
    else if (!dali)
        for (int i=0; i<=3; i++){
            if (changdi[(now2+fx[i])/27][(now2+fx[i])%27] && i!=last2 && minhaha>dis[now2+fx[i]][k])  
                {minhaha=dis[now2+fx[i]][k]; haha=i;}
        }       
    else{
        minhaha=-minhaha;
        for (int i=0; i<=3; i++){
            if (changdi[(now2+fx[i])/27][(now2+fx[i])%27] && i!=last2 && minhaha<dis[now2+fx[i]][k])  
                {minhaha=dis[now2+fx[i]][k]; haha=i;}
        }   
    }
     xx=now2/27; yy=now2%27; gotoxy(xx,yy); 
    if (changdi[xx][yy]==1) printf("•");else printf("  "); 
    now2+=fx[haha]; last2=(haha+2)%4; gotoxy(18,30);
    xx=now2/27; yy=now2%27; gotoxy(xx,yy); color(5); printf("◆"); color(7);
    if (xx==x && yy==y){
        if (!dali) chi+=1;
        else {
            guozi+=50;
            fenshu+=50;
            last2=0;
            gotoxy(now2/27,now2%27); 
            if (changdi[now2/27][now2%27]==1) printf("•"); else printf("  ");
            now2=390;
        }
    }
}
if (g3){
    int k;
    minhaha=2147483647;
    if (fangx==0 | fangx==2){
        k=y+(fxfx[(fangx+1)%4][1])*3;
        while (k>25 | !changdi[x][k]) k--;
        while (k<1 | !changdi[x][k]) k++;
    } else{
        k=x+(fxfx[(fangx+1)%4][0])*3;
        while (k>28 | !changdi[k][y]) k--;
        while (k<1 | !changdi[k][y]) k++; 
    } 
    if (fangx==0 | fangx==2) k=x*27+k; else k=k*27+y;
     if (now3%27==0 | now3%27==26) haha=last3;
    else if (!dali)
        for (int i=0; i<=3; i++){
            if (changdi[(now3+fx[i])/27][(now3+fx[i])%27] && i!=last3 && minhaha>dis[now3+fx[i]][k])  
                {minhaha=dis[now3+fx[i]][k]; haha=i;}
        }   
    else {
        minhaha=-minhaha;
        for (int i=0; i<=3; i++){
            if (changdi[(now3+fx[i])/27][(now3+fx[i])%27] && i!=last3 && minhaha<dis[now3+fx[i]][k])  
                {minhaha=dis[now3+fx[i]][k]; haha=i;}
        }   
    }   
     xx=now3/27; yy=now3%27; gotoxy(xx,yy); 
    if (changdi[xx][yy]==1) printf("•");else printf("  "); 
    now3+=fx[haha]; last3=(haha+2)%4; gotoxy(18,30);
    xx=now3/27; yy=now3%27;         
    gotoxy(xx,yy); color(3); printf("◆"); color(7);
    if (xx==x && yy==y){
        if (!dali) chi+=1;
        else {
            guozi+=50;
            fenshu+=50;
            last3=0;
            gotoxy(now3/27,now3%27); 
            if (changdi[now3/27][now3%27]==1) printf("•"); else printf("  ");
            now3=341;
        }
    }
}
if (chi) beichi++;
}
int main(){
begin();
int jixu=1;
reset();
 string bb[4]={"●","①","②","③"}; color(7);
gotoxy(12,12); printf("倒计时"); color(12);
for (int i=3; i>=0; i--){if (i==0) color(11);gotoxy(13,13); cout<<bb[i]; Sleep(1000);}
gotoxy(12,12); printf("      "); gotoxy(13,13); printf(" "); color(7);
while (!f){
    Sleep(1);
    gotoxy(10,30); printf("得分:%d   ",fenshu);
    gotoxy(8,30); printf("怪物速度:%d   ",300-t2);
    gotoxy(9,30); printf("你的速度:%d   ",300-t1);
    gotoxy(15,30); printf("被吃次数:%d ",beichi);
    if (kbhit()) shuru();
    if (stopped) continue;
    T1=(T1+1)%t1; T2=(T2+1)%t2;
    if (T1%t1==0 && now+fx[fangx]>0 && now+fx[fangx]<n) move1();
    if (T2%t2==0){
        if (guaitimer<=8){
            if (guaitimer==0) g1=1;
            if (guaitimer==8) g2=1;
            guaitimer++;
        }
        if (!g3 && fenshu>=30) g3=1;
        move2();    
    }
    if (fenshu==guozi)f=2;
}
if (f=2) {
    Sleep(3000);
    system("cls");
    printf("恭喜你吃完了!\n你一共被怪物吃掉了 %d 次",beichi);
    Sleep(3000);
    char ying;
    scanf("%c",&ying);
}
}
  • 随机迷宫
//源代码转自:https://vijos.org/discuss/5d65f0b4f413620449abf6f2#1566961910
//新代码转自:https://www.luogu.com.cn/blog/369209/sui-ji-mi-gong
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <time.h>
#define Height 25 //迷宫的高度,必须为奇数
#define Width 25 //迷宫的宽度,必须为奇数
#define Wall 1
#define Road 0
#define Start 2
#define End 3
#define Esc 5
#define Up 1
#define Down 2
#define Left 3
#define Right 4
int map[Height+2][Width+2];
void gotoxy(int x,int y) { //移动坐标
    COORD coord;
    coord.X=x;
    coord.Y=y;
    SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), coord );
}
void hidden() { //隐藏光标
    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_CURSOR_INFO cci;
    GetConsoleCursorInfo(hOut,&cci);
    cci.bVisible=0;//赋1为显示,赋0为隐藏
    SetConsoleCursorInfo(hOut,&cci);
}
void create(int x,int y) { //随机生成迷
    int c[4][2]= {0,1,1,0,0,-1,-1,0}; //四个方向
    int i,j,t;
//将方向打乱
    for(i=0; i<4; i++) {
        j=rand()%4;
        t=c[i][0];
        c[i][0]=c[j][0];
        c[j][0]=t;
        t=c[i][1];
        c[i][1]=c[j][1];
        c[j][1]=t;
    }
    map[x][y]=Road;
    for(i=0; i<4; i++)
        if(map[x+2*c[i][0]][y+2*c[i][1]]==Wall) {
            map[x+c[i][0]][y+c[i][1]]=Road;
            create(x+2*c[i][0],y+2*c[i][1]);
        }
}
int get_key() { //接收按键
    char c;
    while(c=getch()) {
        if(c==27) return Esc; //Esc
        if(c!=-32)continue;
        c=getch();
        if(c==72) return Up; //上
        if(c==80) return Down; //下
        if(c==75) return Left; //左
        if(c==77) return Right; //右
    }
    return 0;
}
void paint(int x,int y) { //画迷宫
    gotoxy(2*y-2,x-1);
    switch(map[x][y]) {
        case Start:
            printf("入");
            break; //画入口
        case End:
            printf("出");
            break; //画出口
        case Wall:
            printf("▇");
            break; //画墙
        case Road:
            printf("  ");
            break; //画路
    }
}
void game() {
    int x=2,y=1; //玩家当前位置,刚开始在入口处
    int c; //用来接收按键
    while(1) {
        gotoxy(2*y-2,x-1);
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED);
        printf("▇▇"); //画出玩家当前位置
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |FOREGROUND_BLUE|FOREGROUND_RED|FOREGROUND_GREEN);

        if(map[x][y]==End) { //判断是否到达出口
            gotoxy(30,24);
            printf("到达终点,按任意键结束");
            getch();
            break;
        }
        c=get_key();
        if(c==Esc) {
            gotoxy(0,24);
            break;
        }
        switch(c) {
            case Up: //向上走
                if(map[x-1][y]!=Wall) {
                    paint(x,y);
                    x--;
                }
                break;
            case Down: //向下走
                if(map[x+1][y]!=Wall) {
                    paint(x,y);
                    x++;
                }
                break;
            case Left: //向左走
                if(map[x][y-1]!=Wall) {
                    paint(x,y);
                    y--;
                }
                break;
            case Right: //向右走
                if(map[x][y+1]!=Wall) {
                    paint(x,y);
                    y++;
                }
                break;
        }
    }
}
int main() {
    int i,j;
    srand((unsigned)time(NULL)); //初始化随即种子
    hidden(); //隐藏光标
    for(i=0; i<=Height+1; i++)
        for(j=0; j<=Width+1; j++)
            if(i==0||i==Height+1||j==0||j==Width+1) //初始化迷宫
                map[i][j]=Road;
            else map[i][j]=Wall;
    create(2*(rand()%(Height/2)+1),2*(rand()%(Width/2)+1)); //从随机一个点开始生成迷宫,该点行列都为偶数
    for(i=0; i<=Height+1; i++) { //边界处理
        map[i][0]=Wall;
        map[i][Width+1]=Wall;
    }
    for(j=0; j<=Width+1; j++) { //边界处理
        map[0][j]=Wall;
        map[Height+1][j]=Wall;
    }
    map[2][1]=Start; //给定入口
    map[Height-1][Width]=End; //给定出口
    for(i=1; i<=Height; i++)
        for(j=1; j<=Width; j++) //画出迷宫
            paint(i,j);
    game(); //开始游戏
    getch();
    return 0;
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值