贪吃蛇

#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; }

__EOF__

本文作者niuzeyu
本文链接https://www.cnblogs.com/niuzeyu1/p/17612500.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   niuzeyu1  阅读(18)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示