c++贪吃蛇

普通贪吃蛇,不解释,
↑↓←→控制移动。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include 
using namespace std;

#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)

int p, q, score;
bool ppppp;
int nandu;

struct block{
    bool wall;
    bool snake;
    int fangxiang;
}a[50][50];

int main();

void put(char *p, int t);

void retur();

void run(){
    srand(unsigned(time(0)));
    int w1 = 9, w2 = 10;
    int x = 10, y = 10, f = 8;
    score = 0;
    p = rand() % 17 + 2;
    q = rand() % 17 + 2;
    memset(a, 0, sizeof a);
    for(int i = 1; i <= 20; i ++) {
        a[i][1].wall = true;
        a[i][20].wall = true;
        a[1][i].wall = true;
        a[20][i].wall = true;   
        a[i][1].snake = false;
        a[i][20].snake = false;
        a[1][i].snake = false;
        a[20][i].snake = false;
    }
    for(int i = 2; i <= 19; i ++) {
        for(int j = 2; j <= 19; j ++) {
            a[i][j].snake = false;
            a[i][j].wall = false;
            a[i][j].fangxiang = 0;
        }
    }
    system("cls");
    printf("3");
    Sleep(1000);
    printf("%c2", char(8));
    Sleep(1000);
    printf("%c1", char(8));
    system("cls");
    while(a[x][y].wall == false) {
        for(int i = 1; i <= 20; i ++) {
            for(int j = 1; j <= 20; j ++) {
                if(i == x && j == y) printf("□");
                else if(i == p && j == q) printf("☆");
                else if(a[i][j].snake == true) printf("◇");
                else if(a[i][j].wall == true) printf("■");
                else printf(" ");
            }
            puts("");
        }
        printf("分数:%d", score);
        if(kbhit()) {
            char c = getch();
            if(c == -32) {
                c = getch();
                if(c == 72 && a[x + 1][y].fangxiang != 2) {
                    f = 8;
                }
                if(c == 75 && a[x][y + 1].fangxiang != 6) {
                    f = 4;
                }
                if(c == 77 && a[x][y - 1].fangxiang != 4) {
                    f = 6;
                }
                if(c == 80 && a[x - 1][y].fangxiang != 8) {
                    f = 2;
                }
            }
        }
        a[x][y].fangxiang = f;
        if(f == 8) {
            x --;
        }
        else if(f == 2) {
            x ++;
        }
        else if(f == 4) {
            y --;
        }
        else if(f == 6) {
            y ++;
        }
        if(a[x][y].snake == true || a[x][y].wall == true) retur();
        a[x][y].snake = true;
        a[w1][w2].snake = false;
        if(x == p && y == q) {
            score += rand() % 10;
            p = rand() % 17 + 2;
            q = rand() % 17 + 2;
            system("cls");
            continue;
        }
        if(a[w1][w2].fangxiang == 8) {
            -- w1;
        }
        else if(a[w1][w2].fangxiang == 2) {
            ++ w1;
        }
        else if(a[w1][w2].fangxiang == 4) {
            -- w2;
        }
        else if(a[w1][w2].fangxiang == 6) {
            ++ w2;
        }
        Sleep(nandu * 50);
        system("cls");
    }
}

void put(char *p) {
    while(1) {
        if(*p != 0)
            printf("%c", *p ++);
        else
            break;
        Sleep(80);
    }
}

void retur() {
    put("\n您的分数是");
    printf("%d", score);
    put("分\n");
    printf("┏━━━━━┓ ┏━━━━━┓\n");
    printf("┃ a.重生   ┃ ┃ b.退出   ┃\n");
    printf("┗━━━━━┛ ┗━━━━━┛\n");
    POINT a;
    char temp;
    Sleep(200);
    while(true) {
//        printf("%d", KEY_DOWN(VK_RBUTTON));
        GetCursorPos(&a);
        if(kbhit()) {
        	temp = getch();
        	if(temp == 'a') {
        		run();
			}
			else {
				while(true) system("pause");
			}
		}
    }
}

void jieshao() {
    put("c++小游戏:贪吃蛇\n");
    put("对于闪屏和回头蛇,我已无能为力\n");
    Sleep(1000);
    system("cls");
    put("游戏开始...");
    Sleep(100);
    system("cls");
}

int main() {
    if(ppppp == false) {
        jieshao();
        ppppp = true;
    }
    put("请输入难度1~5");
    scanf("%d", &nandu);
    run();
    return 0;
}

你可能感兴趣的:(小游戏,游戏)