请教大佬做一下游戏的墙壁系统,谢谢!
最近在写游戏,但游戏的墙壁既不能被穿过,又要添加可破坏的属性,请教大佬怎么写?(总而言之就是出了人物,其他字符不允许被覆盖)代码:
程序代码:
#include <stdio.h> #include <stdlib.h> #include <conio.h> #include <windows.h> #include <time.h> #include <string.h> HANDLE hout; int hand = 1; int ti,tim,tin,tie,tik; int yz,xz,xx,yx; 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() { gotoxy(12,0); printf("┌─────┬─────┬─────┬─────┐\n"); gotoxy(12,1); printf("│ 1:木块 │ 2:泥土 │ 3:石头 │ 4:草块 │\n"); gotoxy(12,2); printf("└─────┴─────┴─────┴─────┘\n"); printf("\n\n\n----------------------------------------------------------------------------"); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN); gotoxy(0,24); 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'||ch=='j'||ch=='l'||ch=='k'||ch=='i'||ch=='b') { GetConsoleScreenBufferInfo(hout,&csbi); coord.X=csbi.dwCursorPosition.X;//得到坐标X的值 coord.Y=csbi.dwCursorPosition.Y;//得到坐标Y的值 ch=getch(); xz=coord.X; yz=coord.Y; if(ch=='b') { xz=coord.X; yz=coord.Y; gotoxy(0,4); CONSOLE_CURSOR_INFO cursor_info = {1,1}; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info); int chse; printf("请输入物品对应的数字以手持这个物品:"); scanf("%d",&chse); if(chse==1||chse==2) { hand=1; gotoxy(0,4); printf(" "); gotoxy(xz,yz); CONSOLE_CURSOR_INFO cursor_info = {1,0}; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info); } if(chse==3) { hand=2; gotoxy(0,4); printf(" "); gotoxy(xz,yz); CONSOLE_CURSOR_INFO cursor_info = {1,0}; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info); } if(chse==4) { hand=3; gotoxy(0,4); printf(" "); gotoxy(xz,yz); CONSOLE_CURSOR_INFO cursor_info = {1,0}; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info); } } if(yz==41) { char die; system("cls"); gotoxy(24,16); printf(" 你死了!"); gotoxy(20,20); printf("死亡原因:你不应该到这层来的,你掉出了这个游戏。"); gotoxy(22,22); printf("按空格键重生,按“ESC”键退出游戏。"); die=getch(); if(die==' ') { ShellExecute(NULL,"open","2D创造游戏.exe",NULL,NULL,SW_SHOW); exit(0); } if(die==0x1B) { exit(0); } } if(ch=='j') { if(hand==1) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN|FOREGROUND_RED); } if(hand==2) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN|FOREGROUND_RED|FOREGROUND_BLUE); } if(hand==3) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN); } xz=coord.X; yz=coord.Y; xx=xz; yx=yz; yx=yx-2; xx=xx-3; gotoxy(xx,yx); printf("#"); yx=yx-1; gotoxy(xx,yx); printf("#"); yx=yx-1; gotoxy(xx,yx); printf("#"); xz=xz+1; gotoxy(xz,yz); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN|FOREGROUND_RED|FOREGROUND_BLUE); } if(ch=='l') { if(hand==1) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN|FOREGROUND_RED); } if(hand==2) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN|FOREGROUND_RED|FOREGROUND_BLUE); } if(hand==3) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN); } xz=coord.X; yz=coord.Y; xx=xz; yx=yz; yx=yx-2; xx=xx+3; gotoxy(xx,yx); printf("#"); yx=yx-1; gotoxy(xx,yx); printf("#"); yx=yx-1; gotoxy(xx,yx); printf("#"); xz=xz+1; gotoxy(xz,yz); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN|FOREGROUND_RED|FOREGROUND_BLUE); } if(ch=='k') { if(hand==1) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN|FOREGROUND_RED); } if(hand==2) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN|FOREGROUND_RED|FOREGROUND_BLUE); } if(hand==3) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN); } yz=coord.Y; xz=coord.X; xx=xz; yx=yz; xx=xx-2; yx=yx-1; gotoxy(xx,yx); printf("#####"); gotoxy(xz,yz); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN|FOREGROUND_RED|FOREGROUND_BLUE); } if(ch=='i') { if(hand==1) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN|FOREGROUND_RED); } if(hand==2) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN|FOREGROUND_RED|FOREGROUND_BLUE); } if(hand==3) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN); } yz=coord.Y; xz=coord.X; xx=xz; yx=yz; xx=xx-2; yx=yx-5; gotoxy(xx,yx); printf("#####"); gotoxy(xz,yz); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN|FOREGROUND_RED|FOREGROUND_BLUE); } 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); 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 -[0]-"); yz=yz+1; gotoxy(xz,yz); printf("\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); while(1) { if(hand==1) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN|FOREGROUND_RED); } if(hand==2) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN|FOREGROUND_RED|FOREGROUND_BLUE); } if(hand==3) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN); } break; } printf("\b\b # "); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN|FOREGROUND_RED|FOREGROUND_BLUE); coord.Y--; exit1:; } if(ch=='s') { xz=coord.X; yz=coord.Y; gotoxy(0,0); printf("挖掘中..."); Sleep(850); 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(" "); yz=yz-3; 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++; } //左 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) { xz=coord.X; yz=coord.Y; yz=yz-4; gotoxy(xz,yz); printf(" 0\b"); yz=yz+1; gotoxy(xz,yz); printf("\b\b -[0]-"); yz=yz+1; gotoxy(xz,yz); printf("\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(); } die:; return ch; } void 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"); int 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-1,yz); printf(" "); gotoxy(xz-2,yz+1); printf(" "); gotoxy(xz-1,yz+2); printf(" "); yz=yz-3; yz=yz+1; gotoxy(xz-2,yz); printf(" 0 \b\b\b"); yz=yz+1; gotoxy(xz,yz); printf("\b\b-[0]-"); yz=yz+1; gotoxy(xz,yz); printf("\b\b | | "); Sleep(250); gotoxy(xz-2,yz-2); printf(" "); gotoxy(xz-2,yz-1); printf(" "); gotoxy(xz-2,yz); 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,10); printf(" 游戏已暂停。\n"); gotoxy(16,13); printf("按“ESC”键返回游戏,按“S”键查看游戏日志,\n"); gotoxy(16,15); printf("按“G”键显示光标,按“H”键隐藏光标(此功能需在游\n"); gotoxy(16,17); printf("戏界面使用)按“C”键重新加载游戏(本功能会清除您\n"); gotoxy(16,19); printf("本次所有游戏数据,请谨慎使用,本游戏没有存档功能。)\n"); gotoxy(16,21); printf("按“Shift”键加“Q”键进行崩溃测试(此功能请谨慎使用)\n"); gotoxy(16,23); printf("按“B”键选取物品,如果背包异常,请尝试使用崩溃测试。\n"); gotoxy(6,25); printf("按“S”键向下挖,按“W”键向上走,按“D”键向右挖,按“A”键向左挖。\n"); gotoxy(16,27); printf("按“J”键在左边放置一排方块,“L”键在右边放置一排方块,\n"); gotoxy(16,29); printf("“K”键在下方放置一排方块,“I”键在上方放置一排方块。\n"); 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(); } } getchar(); }
最后的原理我自己理解就行,各位大佬把单独的代码段附上就行,感谢各位大佬!!!
[此贴子已经被作者于2021-8-22 13:53编辑过]