吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 3155|回复: 15
收起左侧

[C&C++ 转载] 贪吃蛇

  [复制链接]
山野村夫-陈墨 发表于 2020-3-6 18:24
这是一个简单的贪吃蛇 ,有界面,有颜色,但没有图片,因为它是控制台下的。
函数都是先声明,后实现,所以比较方便看。
先看看程序运行图片吧



[C++] 纯文本查看 复制代码
#include  <iostream>
#include  <windows.h>
#include  <time.h>
#include  <conio.h>
#include  <stdlib.h>
#include  <stdio.h>
#include  <string.h>

int  sudu = 300;
struct snake {
	int x;
	int y;
	snake* next;
	snake* fron;
};

int  score = 0;

struct food {
	int x;
	int y;
};
food f;

struct people {
	char name[20];
	int  score;
};
people p;

int direction = 3;
//不是看不起你的专业 ,而是看不懂,嗯,是这样!
//"看不懂",划重点,
//考试要考!
void   gotoxy(int x, int y);//光标移动
void   color(int c );       //输出字体颜色
void   frame();            //框架
void   create(snake* &head);//连接蛇节点
void   pri(snake* head,int ok);      //打印蛇
snake* init();             //  初始化蛇
int    move();             //   上下左右 空格 退出 数字化 
int    food_judge(int x,int y,snake *head);//判断位置食物是否为空
bool   start_meau();     //开始菜单
void   location(snake* head); //产生食物位置 
void   write();   //记录最高分
void   read();    //读取最高分
void   priscore();//打印分数,记录分数
bool   snake_judge(snake* head);//是否吃到食物
bool   judge(snake* head);     //是否被撞
void   pri_food();
void   over(snake* head);
// x = 111; y= 31; 
void   run(snake* &head,snake* &End,int &pause, int &exit);
void  speed();
int main() {
	read();
	system("color block");
	while (1) {
		score = 0;
		sudu = 300;
		system("mode con cols=82 lines=32");
		start_meau();
		int  pause = 0;
		int exit = 0;
		int  flag = move();
		Sleep(300);
		if (flag == 0) {
			snake* head = init();
			snake* End = init();
			head->next = End;
			End->fron = head ;
			End->x = head->x;
			End->y = head->y + 1;
			system("mode con cols=112 lines=32");
			location(head);
			while (!exit) {

				while (!pause && !exit) {
					speed();
					Sleep(sudu);
					priscore();
					run(head, End, pause, exit);
				}
				if (pause == 1) {
					if (move() == 0)
						pause = 0;
				}
				if (exit == 1) {
					write();
					getchar();
				}
			}
			
		}
		else if (flag == -1)
			break;

	}
	
	return 0;
}

void  gotoxy(int x,int y) {
	COORD c;
	c.X = x;
	c.Y = y;
	SetConsoleCursorPosition(GetStdHandle( STD_OUTPUT_HANDLE),c);
}


/**
1=蓝色   2=绿色   3=湖蓝色
4=红色   7=白色   9=淡蓝色
*/
void color(int c) {
	SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE), c);
}

/**x\y
    0  * * *  *  *   81
	*                *
	*                *
	31 *  * *  * * ***
*/

void  frame() {
	for (int i = 0; i < 32; i++) {
		color(1);
		gotoxy(0,i);
		printf("#");

		gotoxy(81, i);
		printf("#");
	}
	for (int i = 0; i < 82; i++) {
		color(1);
		gotoxy(i,0);
		printf("#");

		gotoxy(i,31);
		printf("#");
	}
}

/**
   1  2   3   4  0     -1  5
  上  下  左  右 空格  esc  其他
  38、40、37和39
*/
int  move() {

	if (GetAsyncKeyState(VK_UP))
		return 1;
	else if (GetAsyncKeyState(VK_DOWN))
		return 2;
	else if (GetAsyncKeyState(VK_LEFT))
		return 3;
	else if (GetAsyncKeyState(VK_RIGHT))
		return 4;
	else if (GetAsyncKeyState(VK_SPACE))
		return 0;
	else if (GetAsyncKeyState(VK_ESCAPE))
		return -1;
	else
		return 5;
	
}

bool start_meau(){
	frame();
	color(4);
	gotoxy(30,4);
	printf("   贪       吃       蛇 ");
	color(2);
	gotoxy(28,3);
	printf("=============================");
    gotoxy(28,5);
	printf("=============================");
	gotoxy(28,4);
	printf("||");
    gotoxy(55,4);
	printf("||");

	color(9);
	gotoxy(35,10);
	printf(" 开     始 ");

	gotoxy(30,14); 
	printf("   最         高         分");
	gotoxy(33,16);

	color(7);
	printf("玩家:%s ",p.name);
	gotoxy(33, 17);
	printf("分数: %d",p.score);

	color(3);
	for (int i = 10; i < 70; i++) {
		gotoxy(i,20);
		printf("*");
		for (int i = 20; i < 31; i++) {
			gotoxy(10, i);
			printf("*");
			gotoxy(70,i);
			printf("*");
		}
		
	}
	color(9);
	gotoxy(16,22);
	printf("提示: 空格开始或者暂停; ↑↓← → 进行移动; ESC 退出");

	color(7);
	gotoxy(25,24);
	printf("游戏快乐大家,");
	gotoxy(40,25);
	printf("玩家嘻嘻哈哈.");
	gotoxy(25,26);
	printf("你若乐得自在,");
	gotoxy(40,27);
	printf("小哥打个赏吧!");

	gotoxy(60,28);
	printf("---陈墨");
	color(0);
	for (; ; ) {
		int tmp = move();
		if ( tmp == 0)
			return true;
		if (tmp == -1)
			return false;
	}
	color(0);
}


/**
  在空闲处输出食物
  */
void location(snake* head) {
	for (; ; ) {
		f.x = rand() % 80 + 1;
		f.y = rand() % 30 + 1;
		if (food_judge(f.x, f.y,head))
			break;
	}
	
}

int food_judge(int x, int y, snake *head) {
	while (head) {
		if (head->x == x && head->y == y)
			return 0;
		head = head->next;
	}
	return 1;
}

snake* init() {
	snake* s = (snake*)malloc( sizeof( snake));
	s->next = NULL;
	s->fron = NULL;
	s->x = rand() % 80 + 1;
	s->y = rand() % 30 + 1;

	return s;
}

void pri(snake* head, int ok) {
	system("cls");
	priscore();
	color(2);
	frame();
	pri_food();
	color(7);
	while (head->fron) {
		head->x = (head->fron)->x;
		head->y = (head->fron)->y;
		head = head->fron;
		gotoxy(head->x, head->y);
		printf("+");
	}
	
	if(ok != 5)
	direction = ok ;
	if (ok == 1)
		head->y -= 1;
	else if (ok == 2)
		head->y += 1;
	else if (ok == 3)
		head->x -= 1;
	else if (ok == 4)
		head->x += 1;
	else {
         ok = direction;
		 if (ok == 1)
			head->y -= 1;
	     else if (ok == 2)
			head->y += 1;
	   	 else if (ok == 3)
			head->x -= 1;
	     else if (ok == 4)
			head->x += 1;
	}
		   
		    gotoxy(head->x, head->y);
		    printf("+");	
		 
}

void create(snake* &End) {
	snake* tmp = init();
	tmp->x = f.x;
	tmp->y = f.y;
	tmp->fron = End;
	End->next = tmp;
	End = tmp;
}

bool judge(snake* head) {
	if (head->x == 0 || head->x == 81 || head->y == 0 || head->y == 31) 
		return false;
	int x = head->x;
	int y = head->y;
	
	if (!head->next)
		return true;
	head = head->next;
	while (head) {
		if (head->x == x && head->y == y)
			return false;
		head = head->next;
	}
	return true;
}

void write() {
	system("cls");
	frame();

	color(7);
	if (score > p.score) {
		gotoxy(20, 15);
		printf("最高分:      %d ",score);
		gotoxy(20,17);
		printf("请输入昵称:");
		gotoxy(32,17);
		scanf("%s", p.name);

		p.score = score;

		FILE* pf;
		pf = fopen("s.txt", "w");
		fwrite(&p, sizeof(people),1,pf );
		fclose(pf);
	}
	else{
		gotoxy(20,15);
		printf("游   戏   结   束  !");
		gotoxy(20,17);
		printf("最终得分 :  %d",score);
	}
	
}

void read() {
	
	FILE* pf;
	if ((pf = fopen("s.txt", "a+")) == NULL) {
		
		strcpy(p.name, "no one");
		std::cout << p.name;
		p.score = 0;
		write();
	}
	else {
		fread(&p, sizeof(people), 1, pf);
		fclose(pf);
	}
}

void priscore() {
	color(7);
	gotoxy(90, 4);
	printf("贪吃蛇大作战");
	gotoxy(90,8);
	printf("分数:  %d",score);

	gotoxy(90,12);
	printf("最高分: %d",p.score);
	gotoxy(90,14);
	printf("获得者: %s",p.name);

	gotoxy(90, 20);
	printf("游戏是好玩,");
	gotoxy(90,22);
	printf("维修要花钱。");
	gotoxy(90,24);
	printf("大哥若富裕,");
	gotoxy(90,26);
	printf("谢谢你助力!");
	gotoxy(94, 28);
	printf("---(^-^)你懂得");
	
	color(1);
	for (int i = 81; i < 112; i++) {
		gotoxy(i,0);
		printf("#");
		gotoxy(i, 31);
		printf("#");
	}
	for (int i = 0; i < 32; i++) {
		gotoxy(111, i);
		printf("#");
	}
}

bool snake_judge(snake* head) {
	if (head->x == f.x && head->y == f.y)
		return true;
	return false;
}

void pri_food() {
	gotoxy(f.x, f.y);
	color(2);
	printf("*");
}
void over(snake* head) {
	snake* tmp = init();
		while(head) {
			tmp = head->next;
			free(head);
			head = tmp;
		}
		free(tmp);
}

void run(snake* &head, snake* &End,int &pause,int &exit) {
	int  flag = move();
	switch (flag) {
	case 1:
	case 2:
	case 3:
	case 4:
	case 5:pri(End,flag); break;
	case 0:pause = 1; break;
	case -1:exit = 1; break;

	}
	if (judge(head)) {
		if (snake_judge(head)) {
			snake* tmp = init();
			tmp->x = End->x + 1;
			tmp->y = End->y;
			tmp->fron = End;
			End->next = tmp;
			End = tmp;
			location(head);
			score += 10;
		}
	}
	else
		exit = 1;

}

void speed() {
	
	switch (score) {
	case 100: sudu = 250; break;
	case 200:sudu = 200; break;
	case 250:sudu = 150; break;
	case 300:sudu = 100; break;
	case 350:sudu = 50; break;
	}
}
~M1V5EWZ9LZD$@YK7P]Z9RU.png
IEW8)FXQOJOCQ(8L@PO[`3I.png
0Y5%`UVG)IW[X~`ZK_7V9MC.png

免费评分

参与人数 4吾爱币 +5 热心值 +4 收起 理由
粽子饺子 + 1 + 1 热心回复!
瞎蹦跶 + 1 + 1 用心讨论,共获提升!
冰炎梦幻 + 2 + 1 很棒!
守心人 + 1 + 1 很有创意

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

不懂破解 发表于 2020-3-10 18:56
山野村夫-陈墨 发表于 2020-3-9 21:16
终于赶上你,  还好我没有放弃

记得是 2005 年写的,当时只会用 VB6 编程语言,虽然是窗口程序,但却绘制一个个的点表示蛇,而并没有使用图片,不过也有些创新的地方。具体来说就是:初始的整个蛇由五个黑色的点是构成,但吃的一个个点(食物)是有颜色的,当吃了三个同样的点之后,即可消除此时才会得分,另外被蛇吃了食物后,不同颜色构成的后面那段蛇尾部分,是可以使用两个特定的按键进行切换的,类似于马里奥医生药丸的切换。之后不断改进版本,终于加入了最多四人本机对战功能,其中两人可以用键盘控制,另外两人是电脑控制,算是人工智能不过是变态难度版本……当然一对一人机对战也是可以的。。。又过了几个月我的第二款游戏诞生,还是 VB6 语言所写,不过这次让游戏可以加载 jpg 的图片功能,显示效果更进一层,具体玩法类似于马里奥医生三消游戏,名为《食物大战》—— 时间推到至今疫情过年前,我终于成为了一名资深的分拣快递的大侠 !!!



直到看了你这个关于贪吃蛇的帖子,已往对小游戏开发而通宵写代码的画面,纷纷浮现在脑海里心情久久不能平静,开发游戏的热情被再次激发。虽然时代变迁,正在从事的行业也与游戏一点不沾边,但平时也在默默关注游戏开发方面的新闻,所以这次就直接使用游戏引擎开发游戏吧,虚幻?Unity3D?不不不,这些太高级了大材小用,开发小游戏太浪费啦,经过一阵搜索找到了一款轻量化的开源游戏引擎 ——【Godot Engine】



PS:现在才想起来那款用 VB6 写的贪吃蛇源代码,当年好像备份到了某云盘上不知道还能找回不?
不懂破解 发表于 2020-3-11 15:52
网上高人真多,找到个开源的游戏项目:元气骑士 + 贪吃蛇 = 合体的游戏,里面有些很有意思的设计,来源于小众软件发现频道(https://www.appinn.com/dungeonrush/

加长版介绍: 大一 C 语言课设老师给了一个策划,要求实现一个游戏设定比较多的贪吃蛇。我觉得那份设定不够有趣,想搞点花的。
那一阵子又和室友打元气骑士打得比较多,于是就想,能不能让 英雄 作为贪吃蛇的节,然后做一个 Roughlike 地牢闯关的游戏呢?
于是就有了 DungeonRush 。 元 气 贪 吃 蛇(划掉)


元气贪吃蛇.gif
哈利路亚9527 发表于 2020-3-6 18:30
守心人 发表于 2020-3-6 18:34
666,帮顶。
 楼主| 山野村夫-陈墨 发表于 2020-3-6 18:38
哈利路亚9527 发表于 2020-3-6 18:30
帮你顶一下。我是新来的

你居然顶我???
zz0809001 发表于 2020-3-6 18:39
不愧是你
Seven1314pp 发表于 2020-3-6 18:45
C++  好怀念啊~~~
风後葬 发表于 2020-3-6 18:50
顶一下,好东西,正好在学习
墨芜 发表于 2020-3-6 19:51
好东西,帮忙顶一下
lbc___ 发表于 2020-3-6 20:31
牛啊,纯c写的游戏
不懂破解 发表于 2020-3-8 20:21
哈哈,我以前也写过这个游戏哦
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-6 12:28

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表