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

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 5368|回复: 31
收起左侧

[C&C++ 原创] C++贪吃蛇(大神请飘过)

[复制链接]
zcm2005 发表于 2017-8-16 14:47
我用C++编了一个贪吃蛇,供大家参考,大神请无视。
主要代码如下:
Gluttonous-Snake.cpp
[C++] 纯文本查看 复制代码
#include "stdafx.h"
#include "tool.h"

int main()
{
	cout << "                --" << endl;
	cout << "               |贪|" << endl;
	cout << "               |吃|" << endl;
	cout << "               |蛇|" << endl;
	cout << "                --" << endl;
	Sleep(3000);
	//开始界面

	char map[mapa][mapb] = {
		" ------------------------------ ",
		"|                              |",
		"|                              |",
		"|                              |",
		"|                              |",
		"|                              |",
		"|                              |",
		"|                              |",
		"|                              |",
		"|                              |",
		"|                              |",
		"|                              |",
		"|                              |",
		"|                              |",
		"|                              |",
		"|                              |",
		"|                              |",
		"|                              |",
		"|                              |",
		"|                              |",
		"|                              |",
		"|                              |",
		" ------------------------------ "
	};
	queue<snk> snake;
	print(map);
	Sleep(700);
	map[22][16] = ' ';
	print(map);
	Sleep(700);
	map[22][16] = '#';
	print(map);
	Sleep(700);
	map[21][16] = '#';
	map[22][16] = '*';
	print(map);
	Sleep(700);
	map[20][16] = '#';
	map[21][16] = '*';
	map[22][16] = '*';
	print(map);
	Sleep(700);
	map[19][16] = '#';
	map[20][16] = '*';
	map[21][16] = '*';
	map[22][16] = '*';
	print(map);
	Sleep(700);
	map[18][16] = '#';
	map[19][16] = '*';
	map[20][16] = '*';
	map[21][16] = '*';
	map[22][16] = '*';
	print(map);
	Sleep(700);
	map[17][16] = '#';
	map[18][16] = '*';
	map[19][16] = '*';
	map[20][16] = '*';
	map[21][16] = '*';
	map[22][16] = '-';
	print(map);
	Sleep(700);
	system("color 70");
	Sleep(50);
	system("color 07");
	Sleep(50);
	system("color 70");
	Sleep(50);
	system("color 07");
	Sleep(50);
	system("color 70");
	Sleep(50);
	system("color 07");
	Sleep(50);
	system("color 70");
	Sleep(50);
	system("color 07");
	Sleep(50);
	system("color 70");
	Sleep(50);
	system("color 07");
	Sleep(100);
	cout << "按任意键开始游戏......";
	getch();
	//蛇进场动画

	//mstart = clock();//开始时间
	int wait = 1000;

	snk t;
	direction dir = north;
	t.x = 21; t.y = 16;
	snake.push(t);
	t.x = 20;
	snake.push(t);
	t.x = 19;
	snake.push(t);
	t.x = 18;
	snake.push(t);
	t.x = 17;
	snake.push(t);
	//增加蛇

	int rx, ry;
	srand(time(NULL));
	rx = 1 + rand() % (mapa - 3);
	ry = 1 + rand() % (mapb - 3);
	map[rx][ry] = '0';
	//生成食物

	print(map);
	while (true) {
		int start;
		char t;
		bool getin;
		snk snkt;
		cout << "按 a d w s (小写)控制蛇活动......";
		start = clock();
		while (true) {
			if (clock() - start > wait) {
				getin = false;
				break;
			}
			if (kbhit()) {
				t = getch();
				if (t != 'a'&&t != 'd'&&t != 's'&&t != 'w') {
					print(map);
					cout << "错误,请重新输入......";
				}
				else {
					getin = true;
					break;
				}
			}
		}
		if (t == 'w' || (dir == north&&getin == false)) {
			if (snake.back().x - 1 == 0) end(snake.size()-5);
			if (eatself(snake.back().x - 1, snake.back().y, snake))end(snake.size()-5);
			snkt.x = snake.back().x - 1;
			snkt.y = snake.back().y;
			map[snkt.x][snkt.y] = '#';
			map[snake.back().x][snake.back().y] = '*';
			if (snkt.x != rx || snkt.y != ry) {
				map[snake.front().x][snake.front().y] = ' ';
				snake.pop();
			}
			else {
				rx = 1 + rand() % (mapa - 3);
				ry = 1 + rand() % (mapb - 3);
				map[rx][ry] = '0';
				if (wait > 100)wait -= 50;
			}
			snake.push(snkt);
			dir = north;
		}
		else if (t == 'a' || (dir == west&&getin == false)) {
			if (snake.back().y - 1 == 0) end(snake.size()-5);
			if (eatself(snake.back().x, snake.back().y - 1, snake))end(snake.size()-5);
			snkt.x = snake.back().x;
			snkt.y = snake.back().y - 1;
			map[snkt.x][snkt.y] = '#';
			map[snake.back().x][snake.back().y] = '*';
			if (snkt.x != rx || snkt.y != ry) {
				map[snake.front().x][snake.front().y] = ' ';
				snake.pop();
			}
			else {
				rx = 1 + rand() % (mapa - 3);
				ry = 1 + rand() % (mapb - 3);
				map[rx][ry] = '0';
				if (wait > 100)wait -= 50;
			}
			snake.push(snkt);
			dir = west;
		}
		else if (t == 'd' || (dir == east&&getin == false)) {
			if (snake.back().y + 1 == mapb - 2) end(snake.size()-5);
			if (eatself(snake.back().x, snake.back().y + 1, snake))end(snake.size()-5);
			snkt.x = snake.back().x;
			snkt.y = snake.back().y + 1;
			map[snkt.x][snkt.y] = '#';
			map[snake.back().x][snake.back().y] = '*';
			if (snkt.x != rx || snkt.y != ry) {
				map[snake.front().x][snake.front().y] = ' ';
				snake.pop();
			}
			else {
				rx = 1 + rand() % (mapa - 3);
				ry = 1 + rand() % (mapb - 3);
				map[rx][ry] = '0';
				if (wait > 100)wait -= 50;
			}
			snake.push(snkt);
			dir = east;
		}
		else if (t == 's' || (dir == south&&getin == false)) {
			if (snake.back().x + 1 == mapa - 1) end(snake.size()-5);
			if (eatself(snake.back().x + 1, snake.back().y, snake))end(snake.size()-5);
			snkt.x = snake.back().x + 1;
			snkt.y = snake.back().y;
			map[snkt.x][snkt.y] = '#';
			map[snake.back().x][snake.back().y] = '*';
			if (snkt.x != rx || snkt.y != ry) {
				map[snake.front().x][snake.front().y] = ' ';
				snake.pop();
			}
			else {
				rx = 1 + rand() % (mapa - 3);
				ry = 1 + rand() % (mapb - 3);
				map[rx][ry] = '0';
				if (wait > 100)wait -= 50;
			}
			snake.push(snkt);
			dir = south;
		}
		print(map);
	}
	return 0;
}

stdafx.h
[C++] 纯文本查看 复制代码
#pragma once
const int mapa = 23, mapb = 33;
enum direction { north, west, east, south };
#include "targetver.h"

#include <stdio.h>
#include <tchar.h>

#include <iostream>
#include <cstdlib>
#include <conio.h>
#include <queue>
#include <time.h>
#include <Windows.h>
using namespace std;

tool.h
[C++] 纯文本查看 复制代码
#pragma once
#include "stdafx.h"
void print(char a[mapa][mapb]) {
	system("cls");
	int i, j;
	for (i = 0; i < mapa; i++) {
		for (j = 0; j < mapb; j++)
			cout.put(a[i][j]);
		cout << endl;
	}
}
enum snkt { head, tail, body };
struct snk {
	int x, y;
};
void end(int n) {
	system("cls");
	if (n > 0)cout << "你输了!" << endl << "你成功吃掉了" << n << "个食物!" << endl;
	else cout << "你还能输得在彻底一点吗?" << endl;
	cout << "按任意键退出......";
	getch();
	exit(0);
}
bool eatself(int x, int y, queue <snk> snake) {
	while (snake.size() > 0) {
		if (snake.front().x == x&&snake.front().y == y)return true;
		snake.pop();
	}
	return false;
}

Gluttonous-Snake.7z

8.7 KB, 下载次数: 53, 下载积分: 吾爱币 -1 CB

编译后的文件

Gluttonous-Snake源码.7z

1.69 KB, 下载次数: 17, 下载积分: 吾爱币 -1 CB

免费评分

参与人数 7吾爱币 +6 热心值 +7 收起 理由
樱落丶未央 + 1 + 1 热心回复!
Azure_atk + 1 + 1 谢谢@Thanks!
pholy + 1 非常不错,学习下了!~
geek_007 + 1 + 1 用心讨论,共获提升!
aipanpann + 1 + 1 热心回复!
sau + 1 + 1 热心回复!
巭孬嫑勥烎 + 1 + 1 用心讨论,共获提升!

查看全部评分

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

明月相照 发表于 2017-8-17 20:00

广度搜索?没有试过,应是一种可行方向,当时并不知道这个方法。
只记得当时试过的不成功的方法是:通过不断出现的食物或者说点位置来修正边界值的方法。
 楼主| zcm2005 发表于 2017-8-17 21:03
明月相照 发表于 2017-8-17 20:00
广度搜索?没有试过,应是一种可行方向,当时并不知道这个方法。
只记得当时试过的不成功的方法是:通过 ...

嗯……能帮到你就好
S1MPLYOUNG 发表于 2017-8-16 15:01
 楼主| zcm2005 发表于 2017-8-16 15:13
S1MPLYOUNG 发表于 2017-8-16 15:01
感谢分享,学习一下

感谢顶贴,祝你成功
czg945 发表于 2017-8-16 15:18
可惜看不懂啊
AppSec 发表于 2017-8-16 15:23
{:1_917:}{:1_917:}{:1_917:}这个怎么看也是C,不是C++
陕宝辉 发表于 2017-8-16 15:36 来自手机
学习了,谢谢
fq645122 发表于 2017-8-16 15:37
这是什么鬼东西?
大萌黑 发表于 2017-8-16 15:41
厉害了,这样实现还真没想过
巭孬嫑勥烎 发表于 2017-8-16 15:45
一开始就来个error 1。。。。。。
gunxsword 发表于 2017-8-16 16:10
控制台实现,历害啊,应该上传个运行时的图
您需要登录后才可以回帖 登录 | 注册[Register]

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

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

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

GMT+8, 2024-4-17 05:03

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

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