c语言编写童年游戏魔塔

制作不易,多多支持,让我们看看代码吧

#include<stdio.h>
#include<windows.h>
#include<string.h>
int i,j,x,y,key=0,attack=0,life=5,money=0,floor,time[7]={0};
int mk[8][8]={0},ma[8][8]={0},ml[8][8]={0},mA[8][8]={0},mB[8][8]={0},mC[8][8]={0},mD[8][8]={0},mM[8][8]={0};
char ch,n[8][8];
char n1[8][8],n2[8][8],n3[8][8],n4[8][8],n5[8][8],n6[8][8];
void entryspace(int x)
{
    for(i=0;i<x;i++)
        printf("\t");
}
void entryline(int x)
{
    for(i=0;i<x;i++)
        printf("\n");
}
void line()
{
    for(i=0;i<40;i++)
    printf("__");
    printf("\n");
}
void map1()
{
    char m[8][8]=
    {
            "#######",
            "# ###$#",
            "#*%#*K#",
            "#AA#AA#",
            "# K+K #",
            "#o### >",
            "#######"
    };
    x=5;
    y=1;
    for(i=0;i<8;i++)
    strcpy(n[i],m[i]);
}
void map2()
{
    char m[8][8]=
    {
             "#######",
             "# #%# #",
             "##*+%##",
             "##* %##",
             "# #B# #",
             ">  + o<",
             "#######"
             };
    x=5;
    y=5;
    for(i=0;i<8;i++)
    strcpy(n[i],m[i]);
}
void map3()
{
    char m[8][8]=
    {
             "#######",
             "#* BB%#",
             "#A# #%#",
             "# +%#B#",
             "# ###B#",
             "<oKAA >",
             "#######"
    };
    x=5;
    y=1;
    for(i=0;i<8;i++)
    strcpy(n[i],m[i]);
}
void map4()
{
    char m[8][8]=
    {
            "#>####",
            "#C#C%##",
            "# +B#*#",
            "#B#+C%#",
            "#B# ###",
            "#%#B%o<",
            "#######"
    };
    x=5;
    y=5;
    for(i=0;i<8;i++)
    strcpy(n[i],m[i]);
}
void map5()
{
    char m[8][8]=
    {
            "#<#####",
            "#o### #",
            "#D+C+D#",
            "#C### #",
            "#D#+D%#",
            "> #%#*#",
            "#######"
    };
    x=1;
    y=1;
    for(i=0;i<8;i++)
    strcpy(n[i],m[i]);
}
void map6()
{
    char m[8][8]=
    {
            "#######",
            "##MPM##",
            "##DMD##",
            "#DM+ *#",
            "# D#%*#",
            "<oD#*%#",
            "#######"
    };
    x=5;
    y=1;
    for(i=0;i<8;i++)
    strcpy(n[i],m[i]);
}
void head()
{
    entryspace(5);
    printf("让我们开始吧\n");
    line();
    printf("你需要通过‘w’,‘s’,‘a’,‘d’四个键开控制‘o’点\n");
    printf("K:钥匙\t+:关着的门\t&:开着的门\t*:生命值+5\t%%:攻击力+1\t>:向上的楼梯\n");
    printf("A:小怪兽(攻:5,金:1)  B:中级怪兽(攻:10,金:2)  C:大怪兽(攻:15,金:4)  \n");
    printf("D:巨大怪兽 (攻:20,金:6)   M:究极怪兽 (攻:40,金:5)$:商店 \n");
    line();    printf("生存法则:当你攻击怪兽时,生命会减去你的攻击力减去怪兽的攻击力,生命为零即失败!\n");
    printf("key=%2d  attack=%2d  life=%2d  money=%2d  floor=%2d\n",key,attack,life,money,floor);
    line();
}
void trawmap()
{
    entryline(2);
    for(i=0;i<8;i++)
    {
        printf("\t\t\t\t\t");
        puts(n[i]);
    }
}
void move()
{
    ch=getch();
    if(ch=='w'&&n[x-1][y]!='#')
    {
        if(n[x-1][y]=='+')
        {
            if(key>0)
            {
                n[x][y]=' ';
                x--;
                n[x][y]='o';
                key--;
            }
        }
        else
        {
            n[x][y]=' ';
            x--;
            n[x][y]='o';
        }
    }
    if(ch=='s'&&n[x+1][y]!='#')
    {
        if(n[x+1][y]=='+')
        {
            if(key>0)
            {
                n[x][y]=' ';
                x++;
                n[x][y]='o';
                key--;
            }
        }
        else
        {
            n[x][y]=' ';
            x++;
            n[x][y]='o';
        }

    }
    if(ch=='a'&&n[x][y-1]!='#')
    {
        if(n[x][y-1]=='+')
        {
            if(key>0)
            {
                n[x][y]=' ';
                y--;
                n[x][y]='o';
                key--;
            }
        }
        else
        {
            n[x][y]=' ';
            y--;
            n[x][y]='o';
        }
    }
    if(ch=='d'&&n[x][y+1]!='#')
    {
        if(n[x][y+1]=='+')
        {
            if(key>0)
            {
                n[x][y]=' ';
                y++;
                n[x][y]='o';
                key--;
            }
        }
        else
        {
            n[x][y]=' ';
            y++;
            n[x][y]='o';
        }

    }
}
void Key(int n,int k)
{
    if(x==n&&y==k&&mk[n][k]==0)
    {
        mk[n][k]=1;
        key++;
    }
}
void Attack(int n,int k)
{
    if(x==n&&y==k&&ma[n][k]==0)
    {
        ma[n][k]=1;
        attack++;
    }
}
void Life(int n, int k)
{
    if(x==n&&y==k&&ml[n][k]==0)
    {
        ml[n][k]=1;
        life+=5;
    }
}
void A(int n,int k)
{
    if(x==n&&y==k&&mA[n][k]==0)
    {
        mA[n][k]=1;
        if(attack<5)
        life-=5-attack;
        money++;
    }
}
void B(int n,int k)
{
    if(x==n&&y==k&&mB[n][k]==0)
    {
        mB[n][k]=1;
        if(attack<10)
            life-=10-attack;
        money+=2;
    }
}
void C(int n,int k)
{
    if(x==n&&y==k&&mC[n][k]==0)
    {
        mC[n][k]=1;
        if(attack<15)
           life-=15-attack;
        money+=4;
    }
}
void E(int n,int k)
{
    if(x==n&&y==k&&mD[n][k]==0)
    {
        mD[n][k]=1;
        if(attack<20)
           life-=20-attack;
        money+=6;
    }
}
void M(int n,int k)
{
    if(x==n&&y==k&&mM[n][k]==0)
    {
        mM[n][k]=1;
        if(attack<40)
           life-=40-attack;
        money+=5;
    }
}
void Door(int m,int k)
{
    if(n[m][k]==' ')
    n[m][k]='&';
}
void Store(int m,int k)
{
    if(x==m&&y==k)
    {
        printf("欢迎来到商店\n按1 K  $3\n按2 *  $4\n按3 %%  $5");
        ch=getch();
        if(ch=='1')
        {
            money-=3;
            if(money<0)
            {printf("钱不够了");
            money+=3;
            key--;
            }
            key++;
        }
        if(ch=='2')
        {
            money-=4;
            if(money<0)
            {printf("钱不够了");
            money+=4;
            life-=5;
            }
            life+=5;
        }
        if(ch=='3')
        {
            money-=5;
            if(money<0)
            {printf("钱不够了");
            money+=5;
            attack--;
            }
            attack++;
        }
    }
}
void U(int k,int m)
{
    if(n[k][m]==' ')
        n[k][m]='>';
}
void D(int k,int m)
{
    if(n[k][m]==' ')
        n[k][m]='<';
}
void love()
{
    char m[8][33]=
    {
            "################################",
            "# ### ## ##     #  # ### #   # #",
            "#  #  # # # # # # #  # # #   # #",
            "#  #  #   # # # ##   ###   #   #",
            "#  #   # #  ### # #  ##    #   #",
            "# ###   #       #  # # ##  #   #",
            "################################"
    };
    for(i=0;i<8;i++)
    {
        puts(m[i]);
    }
}
int main()
{
    printf("开始前请将输入法调成英文状态");
    entryline(5);
    printf("王子和公主在森林玩耍,突然大魔王来了,抓走了公主!!!");
    Sleep(2000);
    system("cls");
    entryline(5);
    printf("王子为了救公主,去了魔王所在的魔塔里,并决心与他一决生死!");
    Sleep(2000);
    system("cls");
    entryline(5);
    printf("现在你就是那位王子,已经在魔塔里了,去吧,动用你的智慧救出公主吧!");
    Sleep(2000);
    system("cls");
    floor=1;
    while(1)
    {
        if(floor==1)
        {    head();
            if(time[1]==0)
            {
            map1();
            }
            else
            {
                for(i=0;i<8;i++)
                    strcpy(n[i],n1[i]);
            }
            while(1)
            {
                trawmap();
                move();
                Key(4,2);
                Key(4,4);
                Key(2,5);
                Attack(2,2);
                Life(2,1);
                Life(2,4);
                A(3,1);
                A(3,2);
                A(3,4);
                A(3,5);
                Door(4,3);
                Store(1,5);
                if(life<=0)
                {
                    floor=0;
                    printf("你失败了!别气馁,再来一次,公主还在等着你救他呢!\n");
                    printf("关闭后重新调试即可\n");
                    Sleep(3000);
                    break;
                }
                if(n[1][5]==' ')
                    n[1][5]='$';
                system("cls");
                U(5,6);
                if(x==5&&y==6)
                {
                    floor=2;
                    n[5][6]='>';
                    n[5][5]='o';
                    x=5;
                    y=5;
                    for(i=0;i<8;i++)
                        strcpy(n1[i],n[i]);
                    time[1]=1;
                    break;
                }
                head();
            }
        }

        if(floor==2)
        {        head();
            if(time[2]==0)
            {
            map2();
            }
            else
            { 
                for(i=0;i<8;i++)
                    strcpy(n[i],n2[i]);
            }
            while(1)
            {
                trawmap();
                move();
                Life(2,2);
                Life(3,2);
                Attack(2,4);
                Attack(3,4);
                Attack(1,3);
                B(4,3);
                Door(5,3);
                Door(2,3);
                if(life<=0)
                {
                    floor=0;
                    printf("你失败了!别气馁,再来一次,公主还在等着你救他呢!\n");
                    printf("关闭后重新调试即可\n");
                    Sleep(3000);
                    break;
                }
                system("cls");
                U(5,0);
                D(5,6);
                if(x==5&&y==6)
                {
                    floor=1;
                    n[5][6]='<';
                    n[5][5]='o';
                    x=5;
                    y=5;
                    for(i=0;i<8;i++)
                        strcpy(n2[i],n[i]);
                    time[2]=1;
                    break;
                }
                if(x==5&&y==0)
                {
                    floor=3;
                    n[5][0]='>';
                    n[5][1]='o';
                    x=5;
                    y=1;
                    for(i=0;i<8;i++)
                        strcpy(n2[i],n[i]);
                    time[2]=1;
                    break;
                }
                head();
            }
        }
        if(floor==3)
        {
            head();
            if(time[3]==0)
            {
            map3();
            }
            else
            {
                for(i=0;i<8;i++)
                    strcpy(n[i],n3[i]);
            }
            while(1)
            {
                trawmap();
                move();
                Key(5,2);
                Attack(3,3);
                Attack(1,5);
                Attack(2,5);
                A(5,3);
                A(5,4);
                A(2,1);
                B(1,3);
                B(1,4);
                B(3,5);
                B(4,5);
                Life(1,1);
                Door(3,2);
                if(life<=0)
                {
                    floor=0;
                    printf("你失败了!别气馁,再来一次,公主还在等着你救他呢!\n");
                    printf("关闭后重新调试即可\n");
                    Sleep(3000);
                    break;
                }
                system("cls");
                U(5,6);
                D(5,0);
                if(x==5&&y==0)
                {
                    floor=2;
                    n[5][0]='<';
                    n[5][1]='o';
                    x=5;
                    y=1;
                    for(i=0;i<8;i++)
                        strcpy(n3[i],n[i]);
                    time[3]=1;
                    break;
                }
                if(x==5&&y==6)
                {
                    floor=4;
                    n[5][6]='>';
                    n[5][5]='o';
                    x=5;
                    y=5;
                    for(i=0;i<8;i++)
                        strcpy(n3[i],n[i]);
                    time[3]=1;
                    break;
                }
                head();
            }
        }
        if(floor==4)
        {
            head();
            if(time[4]==0)
            {
            map4();
            }
            else
            {
                for(i=0;i<8;i++)
                    strcpy(n[i],n4[i]);
            }
            while(1)
            {
                trawmap();
                move();
                Attack(5,4);
                Attack(1,4);
                Attack(3,5);
                Attack(5,1);
                B(5,3);
                B(2,3);
                B(3,1);
                B(4,1);
                C(1,1);
                C(1,3);
                C(3,4);
                Life(2,5);
                Door(2,2);
                Door(3,3);
                if(life<=0)
                {
                    floor=0;
                    printf("你失败了!别气馁,再来一次,公主还在等着你救他呢!\n");
                    printf("关闭后重新调试即可\n");
                    Sleep(3000);
                    break;
                }
                system("cls");
                U(0,1);
                D(5,6);
                if(x==5&&y==6)
                {
                    floor=3;
                    n[5][6]='<';
                    n[5][5]='o';
                    x=5;
                    y=5;
                    for(i=0;i<8;i++)
                        strcpy(n4[i],n[i]);
                    time[4]=1;
                    break;
                }
                if(x==0&&y==1)
                {
                    floor=5;
                    n[0][1]='>';
                    n[1][1]='o';
                    x=1;
                    y=1;
                    for(i=0;i<8;i++)
                        strcpy(n4[i],n[i]);
                    time[4]=1;
                    break;
                }
                head();
            }
        }
        if(floor==5)
        {
            head();
            if(time[5]==0)
            {
            map5();
            }
            else
            {
                for(i=0;i<8;i++)
                    strcpy(n[i],n5[i]);
            }
            while(1)
            {
                trawmap();
                move();
                Attack(5,3);
                Attack(4,5);
                Life(5,5);
                C(3,1);
                C(2,3);
                E(2,1);
                E(4,1);
                E(2,5);
                E(4,4);
                Door(2,2);
                Door(2,4);
                Door(4,3);
                if(life<=0)
                {
                    floor=0;
                    printf("你失败了!别气馁,再来一次,公主还在等着你救他呢!\n");
                    printf("关闭后重新调试即可\n");
                    Sleep(3000);
                    break;
                }
                system("cls");
                D(0,1);
                U(5,0);
                if(x==0&&y==1)
                {
                    floor=4;
                    n[0][1]='<';
                    n[1][1]='o';
                    x=1;
                    y=1;
                    for(i=0;i<8;i++)
                        strcpy(n5[i],n[i]);
                    time[5]=1;
                    break;
                }
                if(x==5&&y==0)
                {
                    floor=6;
                    n[5][0]='<';
                    n[5][1]='o';
                    x=5;
                    y=1;
                    for(i=0;i<8;i++)
                        strcpy(n5[i],n[i]);
                    time[5]=1;
                    break;
                }
                head();
            }
        }
        if(floor==6)
        {
            head();
            if(time[6]==0)
            {
            map6();
            }
            else
            {
                for(i=0;i<8;i++)
                    strcpy(n[i],n6[i]);
            }
            while(1)
            {
                trawmap();
                move();
                Attack(4,4);
                Attack(5,5);
                Life(3,5);
                Life(4,5);
                Life(5,4);
                M(1,2);
                M(1,4);
                M(3,2);
                M(2,3);
                Door(3,3);
                E(3,1);
                E(4,2);
                E(5,2);
                E(2,2);
                E(2,4);
                if(n[1][3]==' ')
                    n[1][3]='P';
                if(n[1][2]!='M'&&n[1][4]!='M'&&n[2][3]!='M')
                {
                    printf("我亲爱的公主,我终于找到你了!");
                    love();
                    Sleep(3000);
                    floor=7;
                    break;
                }
                if(life<=0)
                {
                    floor=0;
                    printf("你被怪兽击败了!别气馁,再来一次,公主还在等着你救他呢!\n");
                    printf("关闭后重新调试即可\n");
                    Sleep(3000);
                    break;
                }
                system("cls");
                D(5,0);
                if(x==5&&y==0)
                {
                    floor=5;
                    n[5][0]='<';
                    n[5][1]='o';
                    x=5;
                    y=1;
                    for(i=0;i<8;i++)
                        strcpy(n6[i],n[i]);
                    time[6]=1;
                    break;
                }

                head();
            }
        }
        if(floor==0||floor==7)
            break;
    }
    
    return 0;
}

  • 11
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一个简单的魔塔游戏C语言代码示例,但是需要注意的是,这只是一个基础版本,可能不具备完整的游戏体验。 ```c #include <stdio.h> #include <stdlib.h> #include <time.h> #define MAP_SIZE 15 #define LEVEL_NUM 10 int map[MAP_SIZE][MAP_SIZE]; int level = 1; int hp = 100; int attack = 10; void init_map() { int i, j; for (i = 0; i < MAP_SIZE; i++) { for (j = 0; j < MAP_SIZE; j++) { if (i == 0 || i == MAP_SIZE - 1 || j == 0 || j == MAP_SIZE - 1) { map[i][j] = 1; } else { map[i][j] = 0; } } } } void print_map() { int i, j; for (i = 0; i < MAP_SIZE; i++) { for (j = 0; j < MAP_SIZE; j++) { if (map[i][j] == 0) { printf(" "); } else if (map[i][j] == 1) { printf("#"); } else if (map[i][j] == 2) { printf("S"); } else if (map[i][j] == 3) { printf("M"); } else if (map[i][j] == 4) { printf("T"); } else if (map[i][j] == 5) { printf("E"); } else if (map[i][j] == 6) { printf("B"); } } printf("\n"); } printf("Level: %d, HP: %d, Attack: %d\n", level, hp, attack); } int get_random(int min, int max) { srand(time(NULL)); return rand() % (max - min + 1) + min; } void generate_monster() { int i, j; do { i = get_random(1, MAP_SIZE - 2); j = get_random(1, MAP_SIZE - 2); } while (map[i][j] != 0); map[i][j] = 3; } void generate_treasure() { int i, j; do { i = get_random(1, MAP_SIZE - 2); j = get_random(1, MAP_SIZE - 2); } while (map[i][j] != 0); map[i][j] = 4; } void generate_exit() { int i, j; do { i = get_random(1, MAP_SIZE - 2); j = get_random(1, MAP_SIZE - 2); } while (map[i][j] != 0); map[i][j] = 5; } void generate_boss() { int i, j; do { i = get_random(1, MAP_SIZE - 2); j = get_random(1, MAP_SIZE - 2); } while (map[i][j] != 0); map[i][j] = 6; } void move(int x, int y) { if (map[x][y] == 1) { printf("You hit a wall!\n"); } else if (map[x][y] == 2) { printf("You find a sword, attack + 5!\n"); attack += 5; map[x][y] = 0; } else if (map[x][y] == 3) { int monster_hp = get_random(50, 100); printf("You encounter a monster, HP: %d!\n", monster_hp); while (hp > 0 && monster_hp > 0) { printf("Your HP: %d, Monster HP: %d\n", hp, monster_hp); monster_hp -= attack; if (monster_hp <= 0) { printf("You kill the monster!\n"); map[x][y] = 0; break; } hp -= get_random(10, 20); if (hp <= 0) { printf("You are dead!\n"); exit(0); } } } else if (map[x][y] == 4) { printf("You find a treasure, HP + 20!\n"); hp += 20; map[x][y] = 0; } else if (map[x][y] == 5) { printf("You find the exit, level up!\n"); level++; hp = 100; attack += 5; if (level == LEVEL_NUM) { printf("Congratulations, you win!\n"); exit(0); } init_map(); generate_monster(); generate_treasure(); generate_exit(); generate_boss(); } else if (map[x][y] == 6) { int boss_hp = 200; printf("You encounter the boss, HP: %d!\n", boss_hp); while (hp > 0 && boss_hp > 0) { printf("Your HP: %d, Boss HP: %d\n", hp, boss_hp); boss_hp -= attack; if (boss_hp <= 0) { printf("You kill the boss, level up!\n"); level++; hp = 100; attack += 5; if (level == LEVEL_NUM) { printf("Congratulations, you win!\n"); exit(0); } init_map(); generate_monster(); generate_treasure(); generate_exit(); generate_boss(); break; } hp -= get_random(20, 30); if (hp <= 0) { printf("You are dead!\n"); exit(0); } } } else { map[x][y] = 2; } } int main() { init_map(); generate_monster(); generate_treasure(); generate_exit(); generate_boss(); while (1) { print_map(); printf("Please input the direction you want to go (w/a/s/d): "); char direction; scanf("%c", &direction); getchar(); int x = 0, y = 0; switch (direction) { case 'w': x = -1, y = 0; break; case 'a': x = 0, y = -1; break; case 's': x = 1, y = 0; break; case 'd': x = 0, y = 1; break; default: printf("Invalid input!\n"); continue; } move(x + 1, y + 1); } return 0; } ``` 以上代码实现了一个简单的魔塔游戏,包括地图初始化、怪物、宝藏、出口和Boss的生成,以及人物的移动和战斗等基本功能。您可以根据自己的需求进行修改和完善。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值