当前位置:   article > 正文

游戏——吃豆人_"if (k%80==0){color (13); gotoxy(3,12); printf(\"│

"if (k%80==0){color (13); gotoxy(3,12); printf(\"│\");}有什么用"

暑假自己写了个c++的吃豆人,刚转 c++ 几周,就当练练语法吧~
游戏界面(玩的时候注意把上下拉长一点)
一共写了三个怪物:
红色:开局就出来,目标直接对准玩家!
粉色:玩家走了8步以后出来,目标对着玩家前方两格位置
蓝色:玩家吃了八个豆子之后出来,目标对着玩家后方两格位置

这里我并没有设置的“大力丸”,而是用按键“g”代替。
方向键:

怪物加速怪物减速玩家加速玩家减速大力模式暂停
52139764g0
ikjleqdags

也就是说,你只要按了”g”键,怪物们就会都躲着你了!

下面贴上代码!

#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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,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);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/122821
推荐阅读
相关标签
  

闽ICP备14008679号