• 关于一款c++贪吃蛇小游戏


    好久不资瓷了。

    首先声明,这个东西为转载(窝不会写这个。)

    原作者:洛谷dalaoWZK20080124

    代码如下:

    #include <iostream>
    #include <Windows.h>
    #include <conio.h>
    #include <fstream>
    using namespace std;
    
    char ch='w',o;
    
    int i=10,j=10,a[100][100],b[100000],c[100000],u=0,gh=0,i1=0,j1=0,h=1,s=0,m=0,su=8;
    
    int main() {
    
    ifstream data("D:\max.data");
    int dd,d;
    while (data >> dd) {
        d=dd;
    }
    data.close();
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN);
    cout << "-";
    for(int k=0; k<=20; k++)
        cout << "--";
    cout << "
    ";
    for(int k=1; k<=20; k++) {
        cout << "| ";
        for(int u=1; u<=20; u++)
            cout << "  ";
        cout << "|
    ";
    }
    cout << "-";
    for(int k=0; k<=20; k++)
        cout << "--";
    cout << "
    当前得分:0      当前速度:18
    历史最高分:"<< d;
    while(true) {
        Sleep((11-su)*50);
        gh++;
        b[gh]=i;
        c[gh]=j;
        a[i][j]=1;
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), {j*2,i});
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN );
        printf("");
        if(kbhit()) {
            o = getch();
            if(o=='+'&&su+1<=10) {
                su++;
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN);
                SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), {25,22});
                cout << su << " ";
            }
            if(o=='-'&&su-1>=1) {
                su--;
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN);
                SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), {25,22});
                cout << su << " ";
            }
            if(o=='w'&&a[i-1][j]!=1)
                ch=o;
            if(o=='s'&&a[i+1][j]!=1)
                ch=o;
            if(o=='a'&&a[i][j-1]!=1)
                ch=o;
            if(o=='d'&&a[i][j+1]!=1)
                ch=o;
        }
        switch (ch) {
            case 'w':
                i--;
                break;
            case 's':
                i++;
                break;
            case 'a':
                j--;
                break;
            case 'd':
                j++;
                break;
            default:
                break;
        }
        if(i>20)
            i=1;
        if(i==0)
            i=20;
        if(j>20)
            j=1;
        if(j==0)
            j=20;
        if(a[i][j]==1) {
            SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), {30,11});
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN);
            cout << "游戏结束 按1退出";
            char uuu;
            while(uuu!='1')
            uuu=getch();
            system("cls");
            if(s>d) {
                cout << "破记录了";
                std::ofstream outFile;
                outFile.open("D:\max.data");
                outFile << s;
                outFile.close();
            }
            return 0;
        }
        if(i==i1&&j==j1) {
            h=1;
            m=0;
            s=s+su;
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN);
            SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), {9,22});
            cout << s;
        }
        a[i][j]=2;
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), {j*2,i});
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE );
        printf("");
        while(m==0) {
            i1=(rand()%20)+1;
            j1=(rand()%20)+1;
            if(a[i1][j1]==0) {
                m=1;
                SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), {j1*2,i1});
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN|FOREGROUND_RED );
                printf("");
            }
        }
        int k=0;
        if(h==0) {
            while(k<gh) {
                k++;
                if(b[k]!=0) {
                    a[b[k]][c[k]]=0;
                    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), {c[k]*2,b[k]});
                    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);
                    printf("  ");
                    b[k]=0;
                    break;
                }
            }
        }
        h=0;
    }
    }

    有空的话可以颓一下(不是)

  • 相关阅读:
    部分源码:Visual Studio 2019 C运行时库(CRT)部分源码
    反思:中国歌手和欧美歌手的比较
    lol:lol 10分钟前上路到线小兵107只
    OS:RPM包的通用命名规则(转载)
    OS: kali linux的安装和配置
    OS_linux:Systemd 文档
    visual studio 2019 插件安装(插件扩展名vsix)
    解决git合并冲突问题
    nodejs封装的MongoDB的增删改查方法
    vue项目中购物车的全选功能的实现
  • 原文地址:https://www.cnblogs.com/lbssxz/p/11486460.html
Copyright © 2020-2023  润新知