首页 > 其他分享 >贪吃蛇

贪吃蛇

时间:2023-08-07 19:22:48浏览次数:25  
标签:10 rand int void 贪吃蛇 bool include

#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,rand,int,void,贪吃蛇,bool,include
From: https://www.cnblogs.com/niuzeyu1/p/17612500.html

相关文章

  • c++_ 贪吃蛇_蛇尾坐标记录问题
    c++_贪吃蛇_蛇尾坐标记录问题思路:利用双指针,把蛇尾的状态数组向后移动1位。intprevX=tailX[0];intprevY=tailY[0];//之前的蛇头坐标给prevX,prevYintprev2X,prev2Y;tailX[0]=x;tailY[0]=y;//更新蛇头坐标for(inti=1;i<nTail;i++)......
  • 贪吃蛇小游戏
    <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>Title</title></head><body><h1>画布canvas</h1><canvasid="gameCanvas"width="600"......
  • 贪吃蛇游戏制作(附源码)
    CSS:部分*{margin:0;padding:0;}.wrap{width:600px;margin:0auto;position:relative;}p{position:absolute;left:73%;top:10%;}h1{text-align:center;margin-bottom:20px;}#score{text-a......
  • 简单的贪吃蛇小游戏
    <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>Title</title></head><body><h1>画布canvas</h1><canvasid="gameCanvas"width="600"......
  • 小游戏——贪吃蛇案例
    一、案例目的 1、实现贪吃蛇游戏基本功能,屏幕上随机出现一个“食物”,称为豆子,上下左右控制“蛇”的移动,吃到“豆子”以后“蛇”的身体加长一点。2、“蛇”碰到边界或蛇头与蛇身相撞,蛇死亡,游戏结束。3、为游戏设计友好的交互界面;例如欢迎界面,游戏界面,游戏结束界面。要有开始键......
  • 贪吃蛇制作js
    <!DOCTYPEhtml><html><head><title>贪吃蛇游戏</title><style>#game-board{width:400px;height:400px;border:1pxsolid#000;position:relative;}.snake{width:20px;height:20......
  • 数据结构和算法系列课程(02) --- 线性表和贪吃蛇
    线性结构是一种具有以下特点的结构:存在唯一一个被称为“第一个”的数据元素存在唯一一个被称为“最后一个”的数据元素除第一个元素之外,集合中的每个元素均有且仅有一个前驱除最后一个元素之外,集合中的每个元素均有且仅有一个后继那么,线性表、栈、队列、数组、字符串都可以......
  • 贪吃蛇小程序
    <!DOCTYPEhtml><htmllang="en"><head>  <metacharset="UTF-8">  <title>Title</title></head><body><h1>画布canvas</h1><canvasid="gameCanvas"width="6......
  • JavaScript原生实现《贪吃蛇》
    概述JavaScript原生实现《贪吃蛇》,每吃掉一个食物,蛇的身体会变长,食物会重新换位置。详细贪吃蛇大家都不陌生吧~简单做一个。一、思路1.让我们的小蛇动起来2.随机生成食物3.每吃掉一个食物,蛇的身体会变长,食物会重新换位置html界面<divclass="face"><!--小蛇移......
  • 贪吃蛇游戏代码
    #include<stdio.h>#include<conio.h>#include<stdlib.h>#include<time.h>constintmaxn=100;constintn=20;structnode{ intx,y;};intmap[maxn][maxn];//0表示空格,1表示蛇身,2表示食物,3表示撞死的位置,4表示蛇头.nodefood;nodesquence[m......