| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1696 人关注过本帖
标题:我在写一个2D的小游戏时遇到了一个BUG不会修,求各位大佬指教
只看楼主 加入收藏
狮子头
Rank: 2
等 级:论坛游民
帖 子:17
专家分:10
注 册:2021-5-22
结帖率:66.67%
收藏
已结贴  问题点数:10 回复次数:4 
我在写一个2D的小游戏时遇到了一个BUG不会修,求各位大佬指教
代码如下:
程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#include <time.h>
#include <string.h>




HANDLE hout;
int ti,tim,tin,tie,tik;
int yz,xz;
char get1[43]={0},get2[86]={0},get3[559]={0};
void gotoxy(int x,int y)              
{
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD pos;
    pos.X = x;
    pos.Y = y;
    SetConsoleCursorPosition(handle,pos);
}
void printmap()
{
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN); 
    gotoxy(0,25);
    printf("############################################################################");
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN|FOREGROUND_RED);
    printf("####################################################################################################################################################################################################################################");
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN|FOREGROUND_RED|FOREGROUND_BLUE);
    printf("############################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################");
    gotoxy(3,25);
}

void printp()
{
    xz=3;
    yz=25;
    yz=yz-4;
    system("cls");
    printmap();
    gotoxy(xz,yz);
    printf("0\b");
    yz=yz+1;
    gotoxy(xz,yz);
    printf("\b\b-[0]-");
    yz=yz+1;
    gotoxy(xz,yz);
    printf("\b| |");
    gotoxy(3,25);
}
//获得输入
char getInput()
{
    int ch;//输入字符串
     
    COORD coord;//屏幕上的坐标
    CONSOLE_SCREEN_BUFFER_INFO csbi;//控制台屏幕缓冲区信息
 
    coord.X=10;
    coord.Y=10;

 
    ch=getch();

 
    //0x0d表示回车,0XE0表示上下左右等键的键码
    while(ch==0xE0||ch==0x0d||ch=='w'||ch=='s'||ch=='a'||ch=='d')
    {
        GetConsoleScreenBufferInfo(hout,&csbi);
        coord.X=csbi.dwCursorPosition.X;//得到坐标X的值
        coord.Y=csbi.dwCursorPosition.Y;//得到坐标Y的值
 
        ch=getch();

 
         if(ch=='a')
         {
             if(coord.X!=0)
            {
                xz=coord.X;
                yz=coord.Y;
                gotoxy(0,0);
                printf("挖掘中...");
                Sleep(600);
                printf("\b\b\b\b\b\b\b\b\b         ");
                gotoxy(xz,yz);
                coord.X--;
                xz=coord.X;
                yz=coord.Y;
                yz=yz-4;
                gotoxy(xz,yz);
                printf("\b\b  0   ");
                yz=yz+1;
                gotoxy(xz,yz);
                printf("\b\b-[0]-    ");
                yz=yz+1;
                gotoxy(xz,yz);
                printf("\b\b | |  ");
            }
        }
        if(ch=='d')
        {
            if(coord.X!=79)
            {
                xz=coord.X;
                yz=coord.Y;
                gotoxy(0,0);
                printf("挖掘中...");
                Sleep(1500);
                printf("\b\b\b\b\b\b\b\b\b         ");
                gotoxy(xz,yz);
                coord.X++;
                xz=coord.X;
                yz=coord.Y;
                yz=yz-4;
                gotoxy(xz,yz);
                printf("\b\b   0  \b\b\b");
                yz=yz+1;
                gotoxy(xz,yz);
                printf("\b\b\b\b   -[0]-");
                yz=yz+1;
                gotoxy(xz,yz);
                printf("\b\b\b   | | \b");
                coord.X++;
            }
            else
            {
                coord.X=0;
                coord.Y++;
            }
        }
        if(ch=='w')
        {
            xz=coord.X;
            yz=coord.Y;
            yz=yz-5;
            gotoxy(xz,yz);
            printf("0\b");
            yz=yz+1;
            gotoxy(xz,yz);
            printf("\b\b-[0]-");
            yz=yz+1;
            gotoxy(xz,yz);
            printf("\b\b | | ");
            yz=yz+1;
            gotoxy(xz,yz);
            printf("\b\b  # ");
            coord.Y--;
        } 
        if(ch=='s')
        {
            xz=coord.X;
            yz=coord.Y;
            gotoxy(0,0);
            printf("挖掘中...");
            Sleep(1600);
            printf("\b\b\b\b\b\b\b\b\b         ");
            gotoxy(xz,yz);
            xz=coord.X;
            yz=coord.Y;
            gotoxy(xz-2,yz-4);
            printf("     ");
            gotoxy(xz-2,yz-3);
            printf("     ");
            gotoxy(xz-2,yz-2);
            printf("     ");
            gotoxy(xz-2,yz-1);
            printf("     ");
            gotoxy(xz,yz);
            printf("\b\b  0  \b\b\b\b\b");
            yz=yz+1;
            gotoxy(xz,yz);
            printf("\b\b-[0]-");
            yz=yz+1;
            gotoxy(xz,yz);
            printf("\b\b | | ");
            coord.Y++;
            coord.Y++;
            coord.Y++;
            coord.Y++;
        }
        //
        if(ch==0x4b)
        {
            if(coord.X!=0)
            {
                coord.X--;
                xz=coord.X;
                yz=coord.Y;
                yz=yz-4;
                gotoxy(xz,yz);
                printf("0   ");
                yz=yz+1;
                gotoxy(xz,yz);
                printf("\b\b-[0]-    ");
                yz=yz+1;
                gotoxy(xz,yz);
                printf("\b| |  ");
            }
        }

 
        //
        else if(ch==0x4d)
        {
            if(coord.X!=79)
            {
                coord.X++;
                xz=coord.X;
                yz=coord.Y;
                yz=yz-4;
                gotoxy(xz,yz);
                printf("\b\b   0\b");
                yz=yz+1;
                gotoxy(xz,yz);
                printf("\b\b\b\b   -[0]-");
                yz=yz+1;
                gotoxy(xz,yz);
                printf("\b\b\b   | |");
                coord.X++;
            }
            else if(coord.X==19)
            {
                xz=coord.X;
                yz=coord.Y;
                xz=xz-3;
                gotoxy(xz,yz);
            }
        }

 
        SetConsoleCursorPosition(hout,coord);
        ch=getch();

 

 
    }
    return ch;

 

 
    }

 
int main()
{
    loop:;
    CONSOLE_CURSOR_INFO cursor_info = {1,0};
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
    COORD coord;//屏幕上的坐标
    CONSOLE_SCREEN_BUFFER_INFO csbi;//控制台屏幕缓冲区信息
    coord.X=10;
    coord.Y=10;
    GetConsoleScreenBufferInfo(hout,&csbi);
    coord.X=csbi.dwCursorPosition.X;//得到坐标X的值
    coord.Y=csbi.dwCursorPosition.Y;//得到坐标Y的值
    system("mode con cols=76 lines=42");
    char hand;
    char ch;
    system("cls"); 
    printp();
    hout=GetStdHandle(STD_OUTPUT_HANDLE);
    //从键盘获取输入,如果是方向键则执行方向功能,如果是回车键则换行,如果是字符则输出
    while(1)
    {
        ch=getInput();
        if (ch==' ')
        {
            COORD coord;//屏幕上的坐标
            CONSOLE_SCREEN_BUFFER_INFO csbi;//控制台屏幕缓冲区信息
            coord.X=10;
            coord.Y=10;
            GetConsoleScreenBufferInfo(hout,&csbi);
            coord.X=csbi.dwCursorPosition.X;//得到坐标X的值
            coord.Y=csbi.dwCursorPosition.Y;//得到坐标Y的值
            xz=coord.X;
            yz=coord.Y;
            yz=yz-4;
            gotoxy(xz-2,yz);
            printf("   ");
            gotoxy(xz-2,yz+1);
            printf("      ");
            gotoxy(xz-2,yz+2);
            printf("     ");
            yz=yz-3;
            yz=yz+1;
            gotoxy(xz,yz);
            printf("0\b");
            yz=yz+1;
            gotoxy(xz,yz);
            printf("\b\b-[0]-");
            yz=yz+1;
            gotoxy(xz,yz);
            printf("\b| |  ");
            Sleep(250);
            gotoxy(xz-2,yz-3);
            printf("   ");
            gotoxy(xz-2,yz-2);
            printf("      ");
            gotoxy(xz-2,yz-1);
            printf("     ");
            gotoxy(xz,yz);
            printf("\b    \b\b\b0\b");
            yz=yz+1;
            gotoxy(xz,yz);
            printf("\b\b-[0]-");
            yz=yz+1;
            gotoxy(xz,yz);
            printf("\b| |");
            yz=yz+2;
            gotoxy(xz,yz);
        }
        if (ch=='g')
        {
            CONSOLE_CURSOR_INFO cursor_info = {1,1};
            SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
        }
        if (ch=='h')
        {
            CONSOLE_CURSOR_INFO cursor_info = {1,0};
            SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
        }
        tie=clock();
        tik=clock();
        if(ch==0x1B)
        {
            esc:;
            char get;
            system("cls");
            gotoxy(16,12);
            printf("               游戏已暂停。\n");
            gotoxy(16,15);
            printf("按“ESC”键返回游戏,按“S”键查看游戏日志,\n");
            gotoxy(16,17); 
            printf("按“G”键显示光标,按“H”键隐藏光标(此功能需在游\n");
            gotoxy(16,19);
            printf("戏界面使用)按“C”键重新加载游戏(本功能会清除您\n");
            gotoxy(16,21);
            printf("本次所有游戏数据,请谨慎使用)按“B”退出游戏。\n");
            gotoxy(16,23);
            printf("按“Shift”键加“Q”键进行崩溃测试(此功能请谨慎使用)\n");
            gotoxy(16,25);
            printf("按“B”键打开背包,如果背包异常,请尝试使用崩溃测试。"); 
            gotoxy(6,27);
            printf("按“S”键向下挖,按“W”键向上走,按“D”键向右挖,按“A”键向左挖。"); 
            get=getch();
            if(get==0x1B)
            {
                goto loop;
            }
            if(get=='s')
            {
                tin=clock();
                system("cls");
                printf("Initialization:%d mm\n",ti);
                printf("Handle terrain:%d mm\n",tim);
                printf("Print terrain:%d mm\n",tin);
                printf("Judgment input:%d mm\n",tie);
                printf("Total running time:%d mm\n",tik);
                printf("Initialization:%d mm\n",ti);
                printf("Handle terrain:%d mm\n",tim);
                printf("Print terrain:%d mm\n",tin);
                printf("Judgment input:%d mm\n",tie);
                printf("Total running time:%d mm\n",tik);
                printf("Initialization:%d mm\n",ti);
                printf("Handle terrain:%d mm\n",tim);
                printf("Print terrain:%d mm\n",tin);
                printf("Judgment input:%d mm\n",tie);
                printf("Total running time:%d mm\n",tik);
                printf("Initialization:%d mm\n",ti);
                printf("Handle terrain:%d mm\n",tim);
                printf("Print terrain:%d mm\n",tin);
                printf("Judgment input:%d mm\n",tie);
                printf("Total running time:%d mm\n",tik);
                printf("Total running time:%d mm\n",tik);
                printf("Handle terrain:%d mm\n",tim);
                printf("Handle terrain:%d mm\n",tim);
                printf("Print terrain:%d mm\n",tin);
                printf("Judgment input:%d mm\n",tie);
                printf("Total running time:%d mm\n",tik);
                printf("Initialization:%d mm\n",ti);
                printf("Handle terrain:%d mm\n",tim);
                printf("Print terrain:%d mm\n",tin);
                printf("Judgment input:%d mm\n",tie);
                printf("Total running time:%d mm\n",tik);
                printf("Initialization:%d mm\n",ti);
                printf("Handle terrain:%d mm\n",tim);
                printf("Print terrain:%d mm\n",tin);
                printf("Judgment input:%d mm\n",tie);
                printf("Total running time:%d mm\n",tik);
                printf("Total running time:%d mm\n",tik);
                printf("Handle terrain:%d mm\n",tim);
                printf("按任意键继续...");
                getch();
                goto esc;
            }
            if (get=='c')
            {
                goto loop;
            }
            if (get=='b')
            {
                exit(0);
            } 
        }
        if (ch=='Q')
        {
            system("cls");
            gotoxy(16,15);
            printf("游戏崩溃了!");
            gotoxy(16,17); 
            printf("按“S”键查看游戏日志。");
            int s;
            s=getch();
            if (s=='s')
            {
                tin=clock();
                system("cls");
                printf("Initialization:%d mm\n",ti);
                printf("Handle terrain:%d mm\n",tim);
                printf("Print terrain:%d mm\n",tin);
                printf("Judgment input:%d mm\n",tie);
                printf("Total running time:%d mm\n",tik);
                printf("Initialization:%d mm\n",ti);
                printf("Handle terrain:%d mm\n",tim);
                printf("Print terrain:%d mm\n",tin);
                printf("Judgment input:%d mm\n",tie);
                printf("Total running time:%d mm\n",tik);
                printf("Initialization:%d mm\n",ti);
                printf("Handle terrain:%d mm\n",tim);
                printf("Print terrain:%d mm\n",tin);
                printf("Judgment input:%d mm\n",tie);
                printf("Total running time:%d mm\n",tik);
                printf("Initialization:%d mm\n",ti);
                printf("Handle terrain:%d mm\n",tim);
                printf("Print terrain:%d mm\n",tin);
                printf("Judgment input:%d mm\n",tie);
                printf("Total running time:%d mm\n",tik);
                printf("Total running time:%d mm\n",tik);
                printf("Handle terrain:%d mm\n",tim);
                printf("Handle terrain:%d mm\n",tim);
                printf("Print terrain:%d mm\n",tin);
                printf("Judgment input:%d mm\n",tie);
                printf("Total running time:%d mm\n",tik);
                printf("Initialization:%d mm\n",ti);
                printf("Handle terrain:%d mm\n",tim);
                printf("Print terrain:%d mm\n",tin);
                printf("Judgment input:%d mm\n",tie);
                printf("Total running time:%d mm\n",tik);
                printf("Initialization:%d mm\n",ti);
                printf("Handle terrain:%d mm\n",tim);
                printf("Print terrain:%d mm\n",tin);
                printf("Judgment input:%d mm\n",tie);
                printf("Total running time:%d mm\n",tik);
                printf("Total running time:%d mm\n",tik);
                printf("Handle terrain:%d mm\n",tim);
                printf("按任意键退出游戏...");
                getch();
                exit(0);
            }
            
        }
        if (ch=='s')
        {
            COORD coord;//屏幕上的坐标
            CONSOLE_SCREEN_BUFFER_INFO csbi;//控制台屏幕缓冲区信息
            coord.X=10;
               coord.Y=10;
            GetConsoleScreenBufferInfo(hout,&csbi);
               coord.X=csbi.dwCursorPosition.X;//得到坐标X的值
               coord.Y=csbi.dwCursorPosition.Y;//得到坐标Y的值
            xz=coord.X;
            yz=coord.Y;
            yz=yz+1;
            gotoxy(xz,yz);
            printp();
        } 
        if (ch=='b')
        {
            while(1)
            {
                int cho;
                system("cls"); 
                printf("1、木块*n\n2、泥土*n\n3、石头*n\n4、草块*n\n当前手持:%s\n输入每种方块对应的数字,可以手持你选择的方块",hand);
                scanf("%d",&cho);
                if(cho==1)
                {
                    hand = "木块";
                }
                if(cho==2)
                {
                    hand = "泥土";
                }
                if(cho==3)
                {
                    hand = "石头"; 
                }
                if(cho==4)
                {
                    hand = "草块";
                } 
            }
            goto loop;
        }
    }

 

 
    getchar();
     
}

在第481~493之间,出错的代码在下面
程序代码:
if(cho==1)
                {
                    hand = "木块";
                }
                if(cho==2)
                {
                    hand = "泥土";
                }
                if(cho==3)
                {
                    hand = "石头"; 
                }
                if(cho==4)
                {
                    hand = "草块";
                } 

这里显示“[Warning] assignment makes integer from pointer without a cast”警告信息,忘了怎么弄,求大佬指教一下。
图片附件: 游客没有浏览图片的权限,请 登录注册
搜索更多相关主题的帖子: printf time Print input Total 
2021-08-15 10:41
自由而无用
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:14
帖 子:61
专家分:1456
注 册:2021-8-9
收藏
得分:10 
char *hand;
2021-08-15 11:13
狮子头
Rank: 2
等 级:论坛游民
帖 子:17
专家分:10
注 册:2021-5-22
收藏
得分:0 
回复 2楼 自由而无用
谢谢大佬
2021-08-20 16:16
username1111
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2021-8-20
收藏
得分:0 
回复 4楼 username1111
哈哈哈
2021-09-01 20:53
username1111
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2021-8-20
收藏
得分:0 
回复 3楼 狮子头
厉害
2021-09-01 20:54
username1111
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2021-8-20
收藏
得分:0 
回复 5楼 username1111
你也是
2021-09-01 20:54
快速回复:我在写一个2D的小游戏时遇到了一个BUG不会修,求各位大佬指教
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.078178 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved