zoukankan      html  css  js  c++  java
  • 推箱子游戏-C语言

    自己用C语言实现的推箱子的游戏,在写这个的期间浏览,查看了许多的博客和论坛。(写于大一下学期)

    这个游戏我用的是VS2010和EasyX图形库写的。

    如有错误,望指正。

    代码在最后。

    游戏的效果图

     

     游戏界面

     通关界面

    这个3.0是因为,有过2次大的修改。

    还有这个时间的数字是不动的,这里不太懂怎么弄倒计时数。

    源码

    // 推箱子3.0.cpp : 定义控制台应用程序的入口点。
    /**
    *共有 空心的 实心的 箱子 到目的地的箱子  人 在目的地上的人 目的地 7种区域
    *
    *
    */
    
    
    
    #include "stdafx.h"
    #include <Windows.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <graphics.h>//图形库头文件 EasyX库
    
    int map[9][11]={ 
        {1,1,1,1,1,1,1,1,1,1,1},
        {1,0,0,0,1,3,0,0,0,0,1},
        {1,0,0,0,1,0,0,0,0,0,1},
        {1,0,0,0,4,0,4,0,1,1,1},
        {1,3,0,0,0,5,0,0,0,3,1},
        {1,1,1,0,4,0,4,0,0,0,1},
        {1,0,0,0,0,0,1,0,0,0,1},
        {1,0,0,0,0,3,1,0,0,0,1},
        {1,1,1,1,1,1,1,1,1,1,1}
    };
    
    struct Imgposition{//定义贴图坐标的结构体
        int x;
        int y;
    };
    
    struct Imgposition abc[9][11]={//定义并赋值贴图坐标的结构体类型数组
        {{0,0},{32,0},{64,0},{96,0},{128,0},{160,0},{192,0},{224,0},{256,0},{288,0},{320,0}},
        {{0,32},{32,32},{64,32},{96,32},{128,32},{160,32},{192,32},{224,32},{256,32},{288,32},{320,32}},
        {{0,64},{32,64},{64,64},{96,64},{128,64},{160,64},{192,64},{224,64},{256,64},{288,64},{320,64}},
        {{0,96},{32,96},{64,96},{96,96},{128,96},{160,96},{192,96},{224,96},{256,96},{288,96},{320,96}},
        {{0,128},{32,128},{64,128},{96,128},{128,128},{160,128},{192,128},{224,128},{256,128},{288,128},{320,128}},
        {{0,160},{32,160},{64,160},{96,160},{128,160},{160,160},{192,160},{224,160},{256,160},{288,160},{320,160}},
        {{0,192},{32,192},{64,192},{96,192},{128,192},{160,192},{192,192},{224,192},{256,192},{288,192},{320,192}},
        {{0,224},{32,224},{64,224},{96,224},{128,224},{160,224},{192,224},{224,224},{256,224},{288,224},{320,224}},
        {{0,256},{32,256},{64,256},{96,256},{128,256},{160,256},{192,256},{224,256},{256,256},{288,256},{320,256}}
    
    };
    
    int i=4;int j=5;
    int *pi=&i;int *pj=&j;
    int playerposition=map[4][5]; //定义玩家的初始位置???
    IMAGE A,B,C,D,E,F,G,H,I,J,K,L;//存储贴图的指针
    
    
    void showcover(){//显示游戏封面
        putimage(96,18,&H);
        putimage(128,18,&I);
        putimage(160,18,&J);
        putimage(192,18,&K);
        putimage(60,70,&L);
    
        //outtextxy(240,295,_T("BY第企鹅"));
        outtextxy(15,295,_T("版本:3.0"));
    
        settextcolor(LIGHTRED);//更换文本颜色
        outtextxy(100,250,_T("按任意键进入游戏"));
        settextcolor(WHITE);//换回白色
        system("pause");
    }
    
    void showUI(){
        putimage(388,18,&H);//
        putimage(420,18,&I);//
        putimage(452,18,&J);//
        putimage(484,18,&K);//3.0
    }
    void showmessage(){
        {//显示玩家坐标
            LPCTSTR str;
            TCHAR str1[127]=_T("玩家坐标(");
            TCHAR str2[20];
            TCHAR str3[20];
    
            _stprintf(str2,_T("%d"),i+1);
            _stprintf(str3,_T("%d"),j+1);
            lstrcat(str1,str2);lstrcat(str1,_T(""));
            lstrcat(str1,str3);lstrcat(str1,_T(""));
            //玩家坐标(x,y)x为行数 y为列数。
            outtextxy(388,82,str1);
        }
        {//显示玩家状态
            if(map[i][j]==5){outtextxy(388,114,_T("玩家状态:False"));}
            else{outtextxy(388,114,_T("玩家状态:True  "));}
            
        }
        {//显示剩余数量
            int count=0;
            //检查四个箱子是否归位
            if(map[4][1]!=7){count++;}
            if(map[7][5]!=7){count++;}
            if(map[4][9]!=7){count++;}
            if(map[1][5]!=7){count++;}
            TCHAR str[20]=_T("剩余箱子:");
            TCHAR str1[20];
            _stprintf(str1,_T("%d"),count);
            //lstrcat(str,str1);
            outtextxy(388,146,str);
            settextcolor(RED);//更换文本颜色
            outtextxy(468,146,str1);
            settextcolor(WHITE);//换回白色
        }
        {//显示所用时间
            TCHAR str[20]=_T("时间:99 s");
            outtextxy(388,178,str);
        }
        {//显示操作说明
            TCHAR str1[50]=_T("操作:w,s,a,d,");
            TCHAR str2[50]=_T("r 重新游戏,h 帮助");
    
            outtextxy(388,235,str1);
            outtextxy(388,260,str2);
        }
    
    
    }
    
    void loadImage(){
        loadimage(&A,_T("素材//推箱子贴图//空白_推箱子贴图.png"),0,0,false);
        loadimage(&B,_T("素材//推箱子贴图//墙壁_推箱子贴图.png"),0,0,false);
        loadimage(&C,_T("素材//推箱子贴图//目的地_推箱子贴图.png"),0,0,false);
        loadimage(&D,_T("素材//推箱子贴图//箱子2_推箱子贴图.png"),0,0,false);
        loadimage(&E,_T("素材//推箱子贴图//玩家_推箱子贴图.png"),0,0,false);
        loadimage(&F,_T("素材//推箱子贴图//到达目的地的箱子_推箱子贴图.png"),0,0,false);
        loadimage(&G,_T("素材//推箱子贴图//玩家站在目的地上_推箱子贴图.png"),0,0,false);
        loadimage(&H,_T("素材//推箱子贴图//推_推箱子贴图.png"),0,0,false);
        loadimage(&I,_T("素材//推箱子贴图//箱_推箱子贴图.png"),0,0,false);
        loadimage(&J,_T("素材//推箱子贴图//子_推箱子贴图.png"),0,0,false);
        loadimage(&K,_T("素材//推箱子贴图//3.0_推箱子贴图.png"),0,0,false);
    
        loadimage(&L,_T("素材//推箱子贴图//封面.png"),0,0,false);
    }
    void showmap(){  //打印地图的函数
        for(int i=0;i<9;i++){
            for(int j=0;j<11;j++){
                switch (map[i][j]){
                case 0://printf("  ");
                    putimage(abc[i][j].x,abc[i][j].y,&A);
                    break;
                case 1://printf("■");
                    putimage(abc[i][j].x,abc[i][j].y,&B);
                    break;
                case 3://printf("☆");
                    putimage(abc[i][j].x,abc[i][j].y,&C);
                    break;
                case 4://printf("□");
                    putimage(abc[i][j].x,abc[i][j].y,&D);
                    break;
                case 5://printf("♀");
                    putimage(abc[i][j].x,abc[i][j].y,&E);
                    break;
                case 7://printf("★");
                    putimage(abc[i][j].x,abc[i][j].y,&F);
                    break;
                case 8://printf("♀");
                    putimage(abc[i][j].x,abc[i][j].y,&G);
                    break;
                }
            }//printf("
    ");
        }
    }
    
    
    void showmap_again(char ch,int *i,int *j){//修改地图元素的值   修改玩家的坐标
        void whoisyourdaddy();
        switch(ch){
        case 'w':
        case 'W':printf("%d",map[*i-1][*j]);
            if(map[*i-1][*j]==0||map[*i-1][*j]==3){
                map[*i-1][*j]+=5;
                map[*i][*j]-=5;
                *i-=1;//修改玩家的坐标
            }
            else if(map[*i-1][*j]==4||map[*i-1][*j]==7){
                if(map[*i-2][*j]==0||map[*i-2][*j]==3){
                    map[*i][*j]-=5;
                    if(map[*i-1][*j]!=7){map[*i-1][*j]=5;}else{map[*i-1][*j]=8;}
                    map[*i-2][*j]+=4;
                    *i-=1;//修改玩家的坐标
                }
            }break;
        case 's':
        case 'S':
            if(map[*i+1][*j]==0||map[*i+1][*j]==3){
                map[*i][*j]-=5;
                map[*i+1][*j]+=5;
                *i+=1;//修改玩家的坐标
            }
            else if(map[*i+1][*j]==4||map[*i+1][*j]==7){
                if(map[*i+2][*j]==0||map[*i+2][*j]==3){
                    map[*i][*j]-=5;
                    if(map[*i+1][*j]!=7){map[*i+1][*j]=5;}else{map[*i+1][*j]=8;}
                    map[*i+2][*j]+=4;
                    *i+=1;//修改玩家的坐标
                }
            }break;
        case 'a':
        case 'A':
            if(map[*i][*j-1]==0||map[*i][*j-1]==3){
                map[*i][*j]-=5;
                map[*i][*j-1]+=5;
                *j-=1;//修改玩家的坐标
            }
            else if(map[*i][*j-1]==4||map[*i][*j-1]==7){
                if(map[*i][*j-2]==0||map[*i][*j-2]==3){
                    map[*i][*j]-=5;
                    if(map[*i][*j-1]!=7){map[*i][*j-1]=5;}else{map[*i][*j-1]=8;}
                    map[*i][*j-2]+=4;
                    *j-=1;//修改玩家的坐标
                }
            }break;
        case 'd':
        case 'D':
            if(map[*i][*j+1]==0||map[*i][*j+1]==3){
                map[*i][*j]-=5;
                map[*i][*j+1]+=5;
                *j+=1;//修改玩家的坐标
            }
            else if(map[*i][*j+1]==4||map[*i][*j+1]==7){
                if(map[*i][*j+2]==0||map[*i][*j+2]==3){
                    map[*i][*j]-=5;
                    if(map[*i][*j+1]!=7){map[*i][*j+1]=5;}else{map[*i][*j+1]=8;}
                    map[*i][*j+2]+=4;
                    *j+=1;//修改玩家的坐标
                }
            }break;
        case 'H':
        case 'h':
            whoisyourdaddy();
            break;
        
        case 'R':
        case 'r':
                int map2[9][11]={
        {1,1,1,1,1,1,1,1,1,1,1},
        {1,0,0,0,1,3,0,0,0,0,1},
        {1,0,0,0,1,0,0,0,0,0,1},
        {1,0,0,0,4,0,4,0,1,1,1},
        {1,3,0,0,0,5,0,0,0,3,1},
        {1,1,1,0,4,0,4,0,0,0,1},
        {1,0,0,0,0,0,1,0,0,0,1},
        {1,0,0,0,0,3,1,0,0,0,1},
        {1,1,1,1,1,1,1,1,1,1,1}};
                for(int i=0;i<9;i++){//地图复原
                    for(int j=0;j<11;j++){
                        map[i][j]=map2[i][j];
                    }
                }
                *i=4,*j=5;//玩家位置复原
                //map[9][11]=map2[9][11];
                break;
    
        }
    }
    int checkgame(){
        //void gamehelp();
        if(map[4][1]==7&&map[7][5]==7&&map[4][9]==7&&map[1][5]==7){
            //获胜时刷新界面
            showUI(); showmessage();
            showmap();
            settextcolor(LIGHTGREEN);//更改文本颜色
            settextstyle(12,0,_T("宋体"));//更改文本字体
            outtextxy(388,210,_T("您获胜了!!!"));
            settextcolor(WHITE);
            Sleep(3000);
            system("cls");
            printf("您获胜了!!!
    ");
            system("pause");
            return 0;}
        else{return 1;}
    }
    
    void whoisyourdaddy(){//游戏一键完成
        map[4][1]=7,map[7][5]=7,map[4][9]=7,map[1][5]=7;//目的地置为7;
    
        map[3][4]=0,map[5][4]=0,map[5][6]=0,map[3][6]=0;//箱子置为0
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        
        initgraph(320, 320);//封面初始界面
        loadImage();//载入图片(自定义函数)
        showcover();//显示游戏封面
    
    
        //SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),12);//更改界面文本颜色
        int check=1;
        initgraph(552, 288);//游戏初始界面
        //initgraph(352, 288);//游戏界面大小
        while(check){
            //system("mode con cols=90 lines=30");  //窗口宽度高度 
            //system("cls");
            char contrue;//玩家的按键        
            showUI(); showmessage();
            ///*显示玩家位置的状态*/changetextcolor(FOREGROUND_BLUE);printf("玩家坐标(%d,%d)	    ",i+1,j+1);printf("♀的状态为%d
    ",map[i][j]);
            ///*游戏提示*/gamehelp();(自定义函数)
            showmap();//(自定义函数)
            
            //printf("您的操作(w,s,a,d):");
            contrue=getchar();
            showmap_again(contrue,pi,pj);//(自定义函数)
            ////printf("
    玩家坐标(%d,",i+1);
            ////printf("%d)
    ",j+1);
            check=checkgame();//检查游戏是否退出 (自定义函数)
            ///*内置外挂-一键完成*///whoisyourdaddy();
            
        }
        return 0;
    }

    代码看起来有点乱,因为不是同一个时间段写的。

    不知道多年以后看到这些,会有什么感想。

  • 相关阅读:
    [Android应用开发] 01.快速入门
    [wordpress使用]004_导入多媒体
    【Python3爬虫】使用异步协程编写爬虫
    【Python3爬虫】斗鱼弹幕爬虫
    Pycharm2018永久破解的办法
    【Python3爬虫】大众点评爬虫(破解CSS反爬)
    【Python3爬虫】用Python发送天气预报邮件
    【Python3爬虫】猫眼电影爬虫(破解字符集反爬)
    【Python3爬虫】微博用户爬虫
    【Python3爬虫】拉勾网爬虫
  • 原文地址:https://www.cnblogs.com/flyingpenguins/p/12907918.html
Copyright © 2011-2022 走看看