C++小游戏

1.贪吃蛇

#include<cstdio>//此代码必须由Dev-C++运行(可在腾讯软件中心下载) //
#include<windows.h>//由上下左右键操做 ,按空格键开始,其余用 上下左右键操做 //
#define UP 1
#define LEFT 2
#define DOWN 3
#define RIGHT 4
#define HEAD 5
#define WALL 22
struct botton{
int x;
int y;
int length;
};
struct Snake{
Snake(int a,bool b,int c);
bool life;
int Flag ;
int Length ;
int Sum ;
int color;
int speed;
POINT Head ;
POINT End ;
void Smarter() ;
void Move() ;
void Go(bool iiSmart,bool iiSmarter,bool iiMove,int up,int left,int down,int right) ;
};
void GradeIn() ;
void LevelChoose() ;
void MenuPrint() ;
void Menu() ;
void GradePrint() ;
void Setting() ;
void Prepare() ;
void Walk(int ,long&,long&) ;
void Dead() ;
void Foods() ;
void GradeOut(int) ;
void Check() ;
void Pedge(int,int,int,bool) ;
int Pposition(int,int) ;
int Pcolor(int,int) ;
char *Psame(int,char);
char *Pempty(int) ;
POINT Food ;
Snake Person(10,true,1) ;
Snake Computer(1,false,1) ;
Snake Another(14,false,1) ;
int Map[WALL][WALL] ={} ;
int Level = 0 ;
int Score[4] ={} ;
bool iBegin = true ;
bool iMenu = false ;
bool iDead = true ;
bool iFor = false ;
bool iSmart = false ;
bool iCheck = false ;
char wLevel[4][5] ={"简单","普通","困难","炼狱"} ;
Snake::Snake(int a,bool b,int c)
{
color=a;
life=b;
speed=c;
}
/* 数据导入 */
void GradeIn()
{
if ( fopen("score.txt","rb")){
fscanf(fopen("score.txt","rb"),"%d%d%d%d",&Score[0],&Score[1],&Score[2]),&Score[3] ;
}
}
/* 菜单 */
void Menu()
{
int one=0 ;
POINT edge[5]={{16,7},{13,10},{13,13},{23,13},{16,16}} ;
int length[5]={12,18,10,8,12} ;
Print:
system("cls") ;
printf("\n\n\n%s贪%s吃%s蛇\n\n\n\n",Pempty(17),Pempty(2),Pempty(2),Pcolor( 15, 3)) ;
printf("\n%s开始游戏\n\n",Pempty(18)) ;
printf("\n%s游戏难度 :%s\n\n",Pempty(15),wLevel[Level]) ;
printf("\n%s排行榜%s设置\n\n",Pempty(15),Pempty(4)) ;
printf("\n%s退出游戏",Pempty(18)) ;
Pedge(edge[one].x,edge[one].y,length[one],true);
Scan:
Sleep(125) ;
if ( GetAsyncKeyState(' ')||GetAsyncKeyState(VK_RETURN)){
void (*name[])()={Prepare,LevelChoose,GradePrint,Setting} ;
if(one<4){
name[one]() ;
}
if(one==0||one==4){
iBegin=(one==4)?false:true ;
return ;
}
one=0 ;
}
else if ((GetAsyncKeyState('S')||GetAsyncKeyState(VK_DOWN))&& one!=4){
one+=(one==2)?2:1 ;
}
else if ((GetAsyncKeyState('W')||GetAsyncKeyState(VK_UP))&& one!=0){
one-=(one==3)?2:1 ;
}
else if ((GetAsyncKeyState('D')||GetAsyncKeyState(VK_RIGHT))&& one==2){
one++ ;
}
else if ((GetAsyncKeyState('A')||GetAsyncKeyState(VK_LEFT))&& one==3){
one-- ;
}
else{
goto Scan ;
}
for(int i=0;i<5;i++){
Pedge(edge[i].x,edge[i].y,length[i],false);
}
goto Print ;
}
/* 难度选择 */
void LevelChoose()
{
Print :
system("cls") ;
printf("\n\n\n%s困 难 度 选 择\n\n\n\n",Pempty(20)) ;
for(int i=0 ;i<4 ;i++){
printf("%s%s\n\n\n",Pempty(27),wLevel[i]) ;
}
for(int i=0 ;i<2 ;i++){
printf("--------",Pposition(25,(Level+2)*3+i*2) ) ;
printf("#",Pposition(25+i*7,(Level+2)*3+1)) ;
}
Scan:
Sleep(125) ;
if ((GetAsyncKeyState('S')||GetAsyncKeyState(VK_DOWN))&& Level != 3){
Level++ ;
}
else if ((GetAsyncKeyState('W')||GetAsyncKeyState(VK_UP))&& Level != 0){
Level-- ;
}
else if ( GetAsyncKeyState(' ')||GetAsyncKeyState(VK_RETURN)){
return ;
}
else{
goto Scan ;
}
goto Print ;
}

void GradePrint()
{
system("cls") ;
printf("\n\n\n%s排 行 榜\n\n",Pempty(18)) ;
for(int i=0 ;i<3 ;i++){
printf("%s第%d名: %d\n\n",Pempty(17) ,i+1,Score[i]) ;
}
printf("\n\n\n%s知道了\n\n",Pempty(19)) ;
Pedge(17,13,10,true);
for(Sleep(125) ; (GetAsyncKeyState(' ') ||GetAsyncKeyState(VK_RETURN))== false ;Sleep(125)) ;
}

void Setting()
{
bool iScore=false ;
bool iOut=false ;
bool *str[8]={&iDead,&iFor,&iSmart,&Computer.life,&Another.life,&iScore,&iCheck,&iOut} ;
char Name[8][9]={"死亡限制","循环地图","智能辅助","人机模式","双人模式","缓存清理","检查模式","退出设置"} ;
int one=0 ;
Print:
system("cls") ;
printf("\n\n%s设%s置\n\n",Pempty(25),Pempty(4)) ;
for(int i=0 ;i<8 ;i++){
printf("%s%s%s",Pempty(20),Name[i],Pempty(8)) ;
printf("%s\n\n",(*str[i])?" 开":"关",Pcolor((*str[i])?3:15,(*str[i])?15:3)) ;
Pcolor(15,3) ;
}
for(int i=0 ;i<2 ;i++){
printf("--------",Pposition(34,one*2+3+i*2)) ;
printf("#",Pposition(34+i*7,one*2+4) ) ;
}
Scan:
Sleep(125) ;
if((GetAsyncKeyState('S')||GetAsyncKeyState(VK_DOWN))&& one < 7){
one++ ;
}
else if((GetAsyncKeyState('W')||GetAsyncKeyState(VK_UP))&& one > 0){
one-- ;
}
else if( GetAsyncKeyState(' ')||GetAsyncKeyState(VK_RETURN)){
*str[one]=(*str[one])?false:true ;
}
else{
goto Scan ;
}
if(iScore){;
Score[0] = Score[1] = Score[2] = Score[3]= 0 ;
GradeOut(0);
}
if(iOut){
return ;
}
goto Print ;
}
void Check()
{
for(int i=0 ;i<WALL ;i++){
for(int j=0 ;j<WALL ;j++){
fprintf(fopen("check.txt","a"),"%c",Map[i][j]+32) ;
}
fprintf(fopen("check.txt","a"),"\n") ;
}
}
/* 主函数 */
int main()
{
system("color 3f&mode con cols=44 lines=24&title 贪吃蛇") ;
GradeIn() ;
Menu:
Menu() ;
while( iBegin){
if(Another.life==false){
Person.Go(iSmart,false,false,VK_UP,VK_LEFT,VK_DOWN,VK_RIGHT) ;
}
Person.Go(iSmart,false,true,'W','A','S','D') ;
Computer.Go(true,true,true,0,0,0,0) ;
Another.Go(iSmart,false,true,VK_UP,VK_LEFT,VK_DOWN,VK_RIGHT) ;
if ( iDead&&(Map[Person.Head.y][Person.Head.x] != HEAD || ( Computer.life&& Map[Computer.Head.y][Computer.Head.x] != HEAD) || ( Another.life&& Map[Another.Head.y][Another.Head.x] != HEAD))){
Dead() ;
}
if ( iMenu){
goto Menu ;
}
Sleep(( Level < 3) ? ( 150 - ( Level * 50)) : ( rand() % 80)) ;
if(iCheck){
Check() ;
}
}
return 0 ;
}
/*方向 */
void Snake::Go(bool iiSmart,bool iiSmarter,bool iiMove,int up,int left,int down,int right)
{
if(life){
Flag =((GetAsyncKeyState(left))&& Flag!=RIGHT)?LEFT:Flag ;
Flag = ((GetAsyncKeyState(right))&& Flag!=LEFT)?RIGHT:Flag ;
Flag = ((GetAsyncKeyState(up))&& Flag!=DOWN)?UP:Flag ;
Flag = ((GetAsyncKeyState(down))&& Flag!=UP)?DOWN:Flag ;
iMenu = (GetAsyncKeyState('R'))?true:false ;
if(iiSmarter){
Smarter() ;
}
if(iiSmart){
for(int i=0 ;i<16 ;i++){
if((Flag==LEFT&&Map[Head.y][Head.x-1])||(Flag==DOWN&&Map[Head.y+1][Head.x])||(Flag==RIGHT&&Map[Head.y][Head.x+1])||(Flag==UP&&Map[Head.y-1][Head.x])){
Flag=rand()%4+1 ;
}
}
}
if(iiMove){
Move() ;
}
}
}
/* 智能方向 */
void Snake::Smarter()
{
Map[Food.y][Food.x]=-2 ;
for(int nem=0 ;nem<50&&Map[Head.y-1][Head.x]!=-2&&Map[Head.y+1][Head.x]!=-2&&Map[Head.y][Head.x-1]!=-2&&Map[Head.y][Head.x+1]!=-2 ;nem++){
for(int i=1 ;i<WALL-1 ;i++){
for(int j=1 ;j<WALL-1 ;j++){
if(Map[i][j]==-2){
if(Map[i-1][j]==0){
Map[i-1][j]=-1 ;
}
if(Map[i+1][j]==0){
Map[i+1][j]=-1 ;
}
if(Map[i][j-1]==0){
Map[i][j-1]=-1 ;
}
if(Map[i][j+1]==0){
Map[i][j+1]=-1 ;
}
}
}
}
if(iFor){
for(int i=0 ;i<WALL ;i++){
for(int j=0 ;j<WALL ;j+=(i==0||i==WALL-1)?1:WALL-1){
if(Map[i][j]==-1){
Map[(i==0||i==WALL-1)?abs(i-WALL+1):i][(j==0||j==WALL-1)?abs(j-WALL+1):j] = (Map[(i==0||i==WALL-1)?abs(i-WALL+1):i][(j==0||j==WALL-1)?abs(j-WALL+1):j]==0)?-2:Map[(i==0||i==WALL-1)?abs(i-WALL+1):i][(j==0||j==WALL-1)?abs(j-WALL+1):j] ;
Map[(i==0||i==WALL-1)?abs(i-WALL+2):i][(j==0||j==WALL-1)?abs(j-WALL+2):j] = (Map[(i==0||i==WALL-1)?abs(i-WALL+2):i][(j==0||j==WALL-1)?abs(j-WALL+2):j]==0)?-2:Map[(i==0||i==WALL-1)?abs(i-WALL+2):i][(j==0||j==WALL-1)?abs(j-WALL+2):j] ;
}
}
}
}
for(int i=0 ;i<WALL ;i++){
for(int j=0 ;j<WALL ;j++){
Map[i][j]=(Map[i][j]==-1)?-2:Map[i][j] ;
}
}
}
if(Map[Head.y-1][Head.x]==-2){
Flag=UP ;
}
else if(Map[Head.y+1][Head.x]==-2){
Flag=DOWN ;
}
else if(Map[Head.y][Head.x-1]==-2){
Flag=LEFT ;
}
else if(Map[Head.y][Head.x+1]==-2){
Flag=RIGHT ;
}
for(int i=0 ;i<WALL ;i++){
for(int j=0 ;j<WALL ;j++){
if(Map[i][j]==-2){
Map[i][j]=0 ;
}
}
}
}
/* 打印 */
int Pposition ( int x ,int y)
{
COORD Pos ={ x,y } ;
HANDLE Handle = GetStdHandle(STD_OUTPUT_HANDLE) ;
SetConsoleCursorPosition(Handle, Pos) ;
CONSOLE_CURSOR_INFO CurSor ;
GetConsoleCursorInfo(Handle,&CurSor) ;
CurSor.bVisible = false ;
SetConsoleCursorInfo(Handle,&CurSor) ;
return 0 ;
}
/* 颜色 */
int Pcolor( int a ,int b)
{
HANDLE Handle=GetStdHandle(STD_OUTPUT_HANDLE) ;
SetConsoleTextAttribute(Handle, a + b * 0x10) ;
return 0 ;
}
/* 批量 */
char * Psame (int n,char word)
{
char * sentence=new char [n+2] ;
for(int i=0 ;i<n ;i++){
sentence[i]=word ;
}
sentence[n]=0 ;
return sentence ;
}
/* 空格 */
char * Pempty(int n)
{
return Psame(n,' ');
}
/* 边框 */
void Pedge(int x,int y,int length,bool saw)
{
for(int i=0;i<2;i++){
printf("%s",Psame(length,'-'),Pposition(x,y+i*2),Pcolor(saw?15:3,3));
printf("#",Pposition(x+i*(length-1),y+1));
}
}
/* 运行 */
void Prepare()
{
Person.Flag = RIGHT ;
Person.End.x = Person.Head.x = 3 ;
Person.End.y = Person.Head.y = 10 ;
Person.Sum = -3 ;
Person.Length = 0 ;
Computer.Flag = LEFT ;
Computer.End.x = Computer.Head.x = 18 ;
Computer.End.y = Computer.Head.y = 10 ;
Computer.Sum = -3 ;
Computer.Length = 0 ;
Another.Flag =(Computer.life)?DOWN : LEFT ;
Another.End.x = Another.Head.x =(Computer.life)? 11:18 ;
Another.End.y = Another.Head.y =(Computer.life)? 3:10 ;
Another.Sum = -3 ;
Another.Length = 0 ;
iMenu = false ;
system("cls") ;
for( int i = 0 ; i < WALL ; i++){
for( int j = 0 ; j < WALL ; ++j){
if( i == 0 || i == WALL -1 || j == 0 || j == WALL -1){
Map[i][j] = (iFor)?0:WALL ;
Pcolor( 15,3) ;
}
else{
Map[i][j] = 0 ;
Pcolor( 3 , 3) ;
}
printf("■") ;
}
}
Foods() ;
}
/* 走位 */
void Walk(int Flag,long&x,long&y)
{
switch(Flag){
case LEFT:{
x-=(iFor&&x==1)?-19:1 ;
}return ;
case RIGHT:{
x+=(iFor&&x==20)?-19:1 ;
}return ;
case UP:{
y-=(iFor&&y==1)?-19:1 ;
}return ;
case DOWN:{
y+=(iFor&&y==20)?-19:1 ;
}return ;
}
}
/* 移动 */
void Snake::Move()
{
printf("●",Pcolor(color,3) ,Pposition(End.x*2,End.y) ) ;
Map[Head.y][Head.x] = Flag ;
Walk(Flag,Head.x,Head.y) ;
printf("●",Pcolor( color ,3),Pposition( Head.x * 2 ,Head.y)) ;
Map[Head.y][Head.x] += HEAD ;
if( Head.y==Food.y&& Head.x==Food.x){
Length++ ;
Foods() ;
}
if( Sum==Length){
int Temp = Map[End.y][End.x] ;
Map[End.y][End.x] = 0 ;
printf("■",Pcolor( 3 ,3), Pposition( End.x * 2 ,End.y) ) ;
Walk(Temp,End.x,End.y) ;
}
else{
Sum++ ;
}
}
/* 判断 */
void Dead()
{
GradeOut(Person.Length+Another.Length) ;
printf(" Game Over !",Pcolor( 15, 3) ,Pposition( 16 , 9)) ;
Sleep(1500) ;
char wDead[3][9] ={"重新开始","返回菜单","退出游戏"} ;
int one = iBegin = false ;
Print:
system("cls") ;
printf("\n\n\n\n%s游 戏 结 束\n",Pempty(20)) ;
if(Another.life&&Person.Length>Another.Length){
printf("%s左 边 赢 了",Pempty(20)) ;
}
else if(Another.life&&Person.Length<Another.Length){
printf("%s右 边 赢 了",Pempty(20)) ;
}
printf("\n%s分数 : %d\n\n\n\n" ,Pempty(22),Person.Length+Another.Length) ;
for( int i = 0 ; i < 3 ; i++){
printf("%s%s\n\n\n",Pempty(23),wDead[i]) ;
}
for(int i=0 ;i<2 ;i++){
printf("------------",Pposition(21,one*3+9+i*2)) ;
printf("#",Pposition(21+i*11,one*3+10) ) ;
}
Scan:
Sleep(125) ;
if((GetAsyncKeyState('W')||GetAsyncKeyState(VK_UP))&& one != 0){
one-- ;
}
else if((GetAsyncKeyState('S')||GetAsyncKeyState(VK_DOWN))&& one != 2){
one++ ;
}
else if( GetAsyncKeyState(' ')||GetAsyncKeyState(VK_RETURN)){
switch(one){
case 0:{
iBegin = true ;
Prepare() ;
}break ;
case 1:{
iBegin = iMenu = true ;
}break ;
}
return ;
}
else{
goto Scan ;
}
goto Print ;
}
/* 果实 */
void Foods()
{
do{
Food.x = rand() % ( WALL - 2) +1 ;
Food.y = rand() % ( WALL - 2) +1 ;
}while( Map[Food.y][Food.x]!=0) ;
printf("★",Pcolor( 4 , 3),Pposition( Food.x * 2 ,Food.y)) ;
}
/* 分数 */
void GradeOut(int grade)
{
for(int i=2 ;i>=0 ;i--){
if(Score[i]<grade){
Score[i+1] = Score[i] ;
Score[i] = grade ;
}
}
fprintf(fopen("score.txt","w+"),"%d\n%d\n%d",Score[0],Score[1],Score[2]) ;
}

2.俄罗斯方块

//上下左右键控制
#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;
}
 

3.

#include<iostream>
#include<cstdio>
#include<windows.h>
#include<conio.h>
using namespace std;
int SIZ = 20;
HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE);
COORD coord;
HANDLE hCon;
enum Color { DARKBLUE = 1, DARKGREEN, DARKTEAL, DARKRED, DARKPINK, DARKYELLOW, GRAY, DARKGRAY, BLUE, GREEN, TEAL, RED, PINK, YELLOW, WHITE };
void SetColor(Color c) {
if(hCon == NULL)
    hCon = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hCon, c);
}
SYSTEMTIME sys;
//sys.wYear,sys.wMonth,sys.wDay,sys.wHour,sys.wMinute, sys.wSecond,sys.wMilliseconds,sys.wDayOfWeek
struct PLAYER {
int x,y;
int hp;
int gun;
int direct;
} p1,p2;
int map[1005][1005];
int abs(int x) {
if(x < 0) return -x;
return x;
}
void locate(int x,int y) {
coord.X=y - 1;
coord.Y=x - 1;
SetConsoleCursorPosition(hout,coord);
}
void print_map() {
locate(1,1);
SetColor(GRAY);
for(int i = 1; i <= SIZ; i++) cout<<"■";
locate(SIZ,1);
for(int i = 1; i <= SIZ; i++) cout<<"■";
for(int i = 2; i < SIZ; i++) {
    locate(i,1);
    cout<<"■";
    locate(i,SIZ*2-1);
    cout<<"■";
}
locate(SIZ+1,1);
SetColor(WHITE);
}
void create_tree(int x,int y) {
map[x][y] = map[x+1][y] = map[x-1][y] = map[x][y+1] = map[x][y-1] = 2;
}
void use_map(int x) {
if(x == 1) {
    SIZ = 20;
    SetColor(DARKGREEN);
    map[16][6]=map[15][6]=map[17][6]=map[16][7]=map[16][5]=map[14][13]=map[13][12]=map[13][13]=2;
    for(int i = 2; i < SIZ; i++) {
        for(int j = 2; j < SIZ; j++) {
            if(map[i][j] == 2) {
                locate(i,j*2-1);
                cout<<"■";
            }
        }
    }
    SetColor(GRAY);
    for(int i = 5; i <= 15; i++) {
        map[i][i] = 1;
        locate(i,i*2-1);
        cout<<"■";
    }
    SetColor(WHITE);
} else if(x == 2) {
    SIZ = 30;
    SetColor(GRAY);
    for(int i = 4; i <= 26; i++) {
        if(i == 13 || i == 14 ||i == 15) continue;
        map[i][4] = map[4][i] = map[26][i] = map[i][26] = 1;
    }
    for(int i = 1; i <= SIZ; i++) {
        for(int j = 1; j <= SIZ; j++) {
            if(map[i][j] == 1) {
                locate(i,j*2-1);
                cout<<"■";
            }
        }
    }
    SetColor(DARKGREEN);
    for(int i = 10; i<=20; i++) {
        if(i == 13 || i == 17) continue;
        map[i][10] = map[10][i] = map[20][i] = map[i][20] = 2;
    }
    create_tree(5,5);
    create_tree(18,18);
    for(int i = 1; i <= SIZ; i++) {
        for(int j = 1; j <= SIZ; j++) {
            if(map[i][j] == 2) {
                locate(i,j*2-1);
                cout<<"■";
            }
        }
    }
    SetColor(WHITE);
}
}
void cleanbody(int x,int y);
void putbody(int x,int y,int z);
void player_init() {
p1.hp = p2.hp = 300;
p1.gun = p2.gun = 1;
p1.direct = 4;
p2.direct = 2;
p1.x = 2;
p1.y = 2;
p2.x = SIZ - 1;
p2.y = SIZ - 1;
putbody(p1.x,p1.y,1);
putbody(p2.x,p2.y,2);
}
void mapinit() {
for(int i = 1; i <= SIZ; i++) {
    map[i][1] = map[1][i] = map[SIZ][i] = map[i][SIZ] = 1;
}
}
void init() {
printf("Use Which Map?\n");
int x;
cin>>x;
system("cls");
use_map(x);
mapinit();
print_map();
player_init();
}
void putbody(int x,int y,int z) {
if(z == 1) SetColor(BLUE);
else if(z == 2) SetColor(RED);
locate(x,y*2-1);
cout<<"■";
SetColor(WHITE);
}
void cleanbody(int x,int y) {
locate(x,y*2-1);
cout<<" ";
}
/*
LIST
direct:
    w 1
    a 2
    s 3
    d 4
gun:
    usp 1
    mimigun 2
    awp 3
block:
    void 0
    stone 1
    tree 2
    player 3
    clip 4
*/
bool judge(int x,int y) {
if(map[x][y] == 1) return false;
if(map[x][y] == 2) return false;
if(map[x][y] == 3) return false;
return true;
}
bool judge_gun(int x,int y) {
if(map[x][y] == 1) return 0;
if(map[x][y] == 2) return 0;
if(map[x][y] == 3) {
    if(p1.x == x && p1.y == y) p1.hp -= 50;//此处暂时不管威力
    else p2.hp -= 50;
    return 0;
}
return 1;
}
int cnt;
struct Clip {
int x,y;
int derect;
int force;
int start;
bool flag;
} clip[1000000];
void create_clip(int y,int x,int a,int b) {
int X,Y;
if(y == 1) {
    if(!judge_gun(a-1,b)) return;
    X = a-1;
    Y = b;
} else if(y == 2) {
    if(!judge_gun(a,b-1)) return;
    X = a;
    Y = b-1;
} else if(y == 3) {
    if(!judge_gun(a+1,b)) return;
    X = a+1;
    Y = b;
} else if(y == 4) {
    if(!judge_gun(a,b+1)) return;
    X = a;
    Y = b+1;
}
cnt++;
GetLocalTime( &sys );
clip[cnt].start = sys.wMilliseconds + sys.wSecond * 60 + sys.wHour * 3600;
clip[cnt].x = X;
clip[cnt].y = Y;
if(x == 1) {
    clip[cnt].derect = p1.direct;
} else if(x == 2) {
    clip[cnt].derect = p2.direct;
}
}
void shoot(int x) {
if(x == 1) {
    create_clip(p1.direct,1,p1.x,p1.y);
} else if(x == 2) {
    create_clip(p2.direct,2,p2.x,p2.y);
}
}
void clean_clip(int x,int y) {
locate(x,y*2-1);
cout<<"  ";
locate(1,1);
}
void print_clip(int x,int y,int i) {
if(clip[i].flag) {
    clean_clip(x,y);
    return;
}
locate(x,y*2-1);
SetColor(YELLOW);
cout<<"''";
locate(1,1);
//  system("pause");
}
void clipmove() {
GetLocalTime( &sys );
int t = sys.wMilliseconds + sys.wSecond * 60 + sys.wHour * 3600;
for(int i = 1; i <= cnt; i++) {
    if(clip[i].flag) continue;
    if(abs(clip[i].start - t) > 50) {
        clip[i].start = t;
        int x = clip[i].x;
        int y = clip[i].y;
        if(clip[i].derect==1) {
            if(!judge_gun(clip[i].x-1,clip[i].y)) {
                clip[i].flag = 1;
                clean_clip(x,y);
                continue;
            }
            clean_clip(clip[i].x,clip[i].y);
            clip[i].x--;
            print_clip(clip[i].x,clip[i].y,i);
        } else if(clip[i].derect==2) {
            if(!judge_gun(clip[i].x,clip[i].y-1)) {
                clip[i].flag = 1;
                clean_clip(x,y);
                continue;
            }
            clean_clip(clip[i].x,clip[i].y);
            clip[i].y--;
            print_clip(clip[i].x,clip[i].y,i);
        } else if(clip[i].derect==3) {
            if(!judge_gun(clip[i].x+1,clip[i].y)) {
                clip[i].flag = 1;
                clean_clip(x,y);
                continue;
            }
            clean_clip(clip[i].x,clip[i].y);
            clip[i].x++;
            print_clip(clip[i].x,clip[i].y,i);
        } else if(clip[i].derect==4) {
            if(!judge_gun(clip[i].x,clip[i].y+1)) {
                clip[i].flag = 1;
                clean_clip(x,y);
                continue;
            }
            clean_clip(clip[i].x,clip[i].y);
            clip[i].y++;
            print_clip(clip[i].x,clip[i].y,i);
        }
    }
}
}
void judge_hp() {
int x = p1.hp;
int y = p2.hp;
if(x<0 && y<0 && x > y) swap(x,y);
if(x <= 0) {
    locate(1,1);
    system("cls");
    printf("GAME OVER!\nTHE WINNER IS P2!");
    Sleep(5000);
    printf("\n-MADE BY Floatiy-");
    exit(0);
} else if(y <= 0) {
    locate(1,1);
    system("cls");
    printf("GAME OVER!\nTHE WINNER IS P1!");
    Sleep(5000);
    printf("\n-MADE BY Floatiy-");
    exit(0);
}
}
void prog() {
char ch;
while(true) {
    if(kbhit()) {
        ch=getch();
        if(ch == 'w' && judge(p1.x-1,p1.y)) {
            p1.direct = 1;
            cleanbody(p1.x,p1.y);
            map[p1.x][p1.y] = 0;
            putbody(--p1.x,p1.y,1);
            map[p1.x][p1.y] = 3;
        } else if(ch == '8' && judge(p2.x-1,p2.y)) {
            p2.direct = 1;
            cleanbody(p2.x,p2.y);
            map[p2.x][p2.y] = 0;
            putbody(--p2.x,p2.y,2);
            map[p2.x][p2.y] = 3;
        } else if(ch == 'a' && judge(p1.x,p1.y-1)) {
            p1.direct = 2;
            cleanbody(p1.x,p1.y);
            map[p1.x][p1.y] = 0;
            putbody(p1.x,--p1.y,1);
            map[p1.x][p1.y] = 3;
        } else if(ch == '4' && judge(p2.x,p2.y-1)) {
            p2.direct = 2;
            cleanbody(p2.x,p2.y);
            map[p2.x][p2.y] = 0;
            putbody(p2.x,--p2.y,2);
            map[p2.x][p2.y] = 3;
        } else if(ch == 's' && judge(p1.x+1,p1.y)) {
            p1.direct = 3;
            cleanbody(p1.x,p1.y);
            map[p1.x][p1.y] = 0;
            putbody(++p1.x,p1.y,1);
            map[p1.x][p1.y] = 3;
        } else if(ch == '5' && judge(p2.x+1,p2.y)) {
            p2.direct = 3;
            cleanbody(p2.x,p2.y);
            map[p2.x][p2.y] = 0;
            putbody(++p2.x,p2.y,2);
            map[p2.x][p2.y] = 3;
        } else if(ch == 'd' && judge(p1.x,p1.y+1)) {
            p1.direct = 4;
            cleanbody(p1.x,p1.y);
            map[p1.x][p1.y] = 0;
            putbody(p1.x,++p1.y,1);
            map[p1.x][p1.y] = 3;
        } else if(ch == '6' && judge(p2.x,p2.y+1)) {
            p2.direct = 4;
            cleanbody(p2.x,p2.y);
            map[p2.x][p2.y] = 0;
            putbody(p2.x,++p2.y,2);
            map[p2.x][p2.y] = 3;
        } else if(ch == '0') {
            shoot(2);
        } else if(ch == ' ') {
            shoot(1);
        }
        Sleep(20);
    }
    clipmove();
    judge_hp();
}
}
void welcome() {
printf("操作方法:\n玩家1 wasd控制移动,空格攻击 只能往右侧打子弹\n玩家2 数字小键盘4568控制移动,0攻击 只能往左侧打子弹\n");
Sleep(2000);
}
int main() {
welcome();
GetLocalTime( &sys );
init();
prog();
return 0;
}//试试就好
 

  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值