你可以试试哦18

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <climits>
#include <cstdlib>
#include <sstream>
#include <windows.h>
#include <stack>
#include <conio.h>
#include <string>
#include <ctime>
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;
}

贪吃蛇代码!!!

如果你喜欢的话,记得一键三连哦!求求了,这对我很重要!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值