赞
踩
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <conio.h>//检测键盘输入
#include <cmath>
#include <windows.h>//控制光标位置
#define _CRT_SECURE_NO_WARNINGS0
#include<stdlib.h>
#pragma comment(lib,"winmm.lib")
#include<mmsystem.h>
using namespace std;
/*** 光标定位 ***/
HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);//HANDLE是一个系统定制的结构体,GetStdHandle( STD_OUTPUT_HANDLE ) 这里就是一个固定的函数格式,获得标准输出函数的句柄
COORD coord;//COORD是一个C语言内部做好的结构体,记录坐标xy
void locate(int x, int y)
{
coord.X = y;//设置光标的列位置
coord.Y = x;//设置光标的行位置
SetConsoleCursorPosition(hout, coord);//设置光标位置,把标准输出的句柄和坐标传给函数,就可以把光标定位在对应位置
};
/*** 隐藏光标 ***/
void hide()
{
CONSOLE_CURSOR_INFO cursor_info = { 1,0 };//定义光标信息结构体变量
SetConsoleCursorInfo(hout, &cursor_info);//调用设置控制台光标信息函数
}
/*** 生成随机数 ***/
double random(double start, double end)
{
return start + (end - start) * rand() / (RAND_MAX + 1.0);//产生start到end之间的随机数,0 <= rand() <= RAND_MAX
}
/*** 定义地图的长宽,蛇的坐标,长度,方向,食物的位置 ***/
int m, n;
struct node//定义结构体,蛇和食物
{
int x, y;
}snake[1000];
int snake_length, dir;//蛇的长度和方向
node food;
int direct[4][2] = { {-1,0},{1,0},{0,-1},{0,1} };//方向数组,分别代表上、下、左、右
/*** 输出墙 ***/
void print_wall()
{
cout << " ";
for (int i = 1; i <= n; i++)
cout << "-";
cout << endl;
for (int j = 0; j <= m - 1; j++)
{
cout << "|";
for (int i = 1; i <= n; i++) cout << " ";
cout << "|" << endl;
}
cout << " ";
for (int i = 1; i <= n; i++)
cout << "-";
}
/*** 首次输出蛇,其中snake[0]代表头 ***/
void print_snake()
{
locate(snake[0].x, snake[0].y);
cout << "@";
for (int i = 1; i <= snake_length - 1; i++)
{
locate(snake[i].x, snake[i].y);
cout << "*";
}
}
/*** 判断是否撞墙或者自撞 ***/
bool is_correct()
{
if (snake[0].x == 0 || snake[0].y == 0 || snake[0].x == m + 1 || snake[0].y == n + 1) return false;
for (int i = 1; i <= snake_length - 1; i++)
{
if (snake[0].x == snake[i].x && snake[0].y == snake[i].y) return false;
}
return true;
}
/*** 随机生成并输出食物位置 ***/
bool print_food()
{
srand((unsigned)time(0));//使用当前时间作为随机数生成器的种子,time函数用于获取当前时间,通过将当前时间作为种子 ,使得每次程序运行时生成的随机数序列都不同
bool e;
while (1)
{
e = true;
int i = (int)random(0, m) + 1, j = (int)random(0, n) + 1;//随机生成食物位置的横纵坐标
food.x = i; food.y = j;
for (int k = 0; k <= snake_length - 1; k++)//判断食物是否位于蛇的一部分
{
if (snake[k].x == food.x && snake[k].y == food.y)
{
e = false; break;
}
}
if (e) break;
}
locate(food.x, food.y);
cout << "$";
return true;
}
/*** 蛇的前进 ***/
bool go_ahead()
{
node temp;
bool e = false;
temp = snake[snake_length - 1];
for (int i = snake_length - 1; i >= 1; i--)
snake[i] = snake[i - 1];
snake[0].x += direct[dir][0];
snake[0].y += direct[dir][1];
locate(snake[1].x, snake[1].y);
cout << "*";
/*** 吃到了食物 ***/
if (snake[0].x == food.x && snake[0].y == food.y)
{
snake_length++;
e = true;
snake[snake_length - 1] = temp;
}
/*** 输出此时蛇状态 ***/
if (!e)
{
locate(temp.x, temp.y);
cout << " ";
}
else
print_food();
locate(snake[0].x, snake[0].y);
cout << "@";
/*** 如果自撞 ***/
if (!is_correct())
{
system("cls");
cout << "回去练习两年半再来吧!" << endl << "蛇长: " << snake_length << endl;
return false;
}
return true;
}
/*** 输出规则以及坤坤 ***/
void kun()
{
cout << "--------------------贪吃蛇---------------------" << endl;
cout << "请先选择难度.在1-10中输入1个数,1最简单,10最难" << endl;
cout << "然后进入游戏画面,以方向键控制方向." << endl;
cout << "-----------------------------------------------" << endl;
cout << " |利|" << '\n';
cout << " 拔唐唐拔| " << '\n';
cout << " 伏唐庸庸唐庸 " << '\n';
cout << " 未赢赢唐庸庸 " << '\n';
cout << " 干谨 谨谨谨庸未奏 " << '\n';
cout << " 未谨谨谨谨 谨谨谨奏" << '\n';
cout << " 十谨谨谨谨谨唐谨谨谨谨" << '\n';
cout << " 十谨谨谨赢赢谨赢十谨未 |" << '\n';
cout << " 谨赢魔魔赢谨谨谨 十未赢谨十" << '\n';
cout << " 赢魔魔 赢赢赢谨赢伏唐赢赢赢" << '\n';
cout << " 赢魔奏 魔赢谨赢赢 赢赢赢赢" << '\n';
cout << " 拔赢魔 赢赢赢赢赢来赢赢赢魔唐" << '\n';
cout << " 庸赢赢谨谨赢赢利赢赢赢赢魔利" << '\n';
cout << " 拔赢赢赢赢赢 魔魔魔赢魔" << '\n';
cout << " 下拔赢赢魔伏赢赢赢赢魔" << '\n';
cout << " 未伏利利奏拔唐赢赢赢奏" << '\n';
cout << " 拔伏伏利拔拔拔拔奏唐" << '\n';
cout << " 伏伏利利利拔拔拔拔庸" << '\n';
cout << " 未伏伏拔拔拔拔拔拔拔拔未" << '\n';
cout << " 未伏利利拔拔奏奏奏奏奏拔伏" << '\n';
cout << " 未伏利利拔奏奏奏奏奏奏奏拔拔未" << '\n';
cout << " 伏利拔拔奏奏 未奏奏拔拔利" << '\n';
cout << " 未利拔拔奏 未奏奏拔拔" << '\n';
cout << " 伏唐奏奏 拔奏奏奏利" << '\n';
cout << " 十未奏唐 利唐奏拔" << '\n';
cout << " 十未利木 唐唐伏未" << '\n';
cout << " 十未利木 拔伏伏" << '\n';
cout << " 未伏拔十 未利未十" << '\n';
cout << " 伏伏拔 利伏伏" << '\n';
cout << " 伏伏伏 利利伏" << '\n';
cout << " 未伏未 木伏伏" << '\n';
cout << " 未利 伏未" << '\n';
cout << " 庸庸 伏谨 " << '\n';
cout << " 谨唐 庸拔" << '\n';
cout << " 干谨唐奏 唐谨" << '\n';
cout << " 利谨谨庸庸 奏谨奏" << '\n';
}
/*** 主函数 ***/
int main()
{
mciSendString("play F://gedan2//ji.mp3 repeat", 0, 0, 0);
kun();
m = 25;
n = 40;
// if (m<10 || n<10 || m>25 || n>40)
// {
// cout << "ERROR" << endl;
// system("pause");
// return 0;
// }
int hard;
cin >> hard;
if (hard <= 0 || hard > 100)
{
cout << "ERROR" << endl;
system("pause");
return 0;
}
/*** 数据全部初始化,包括蛇长,位置,方向 ***/
snake_length = 5;
clock_t a, b;
char ch;
double hard_len;
for (int i = 0; i <= 4; i++)
{
snake[i].x = 1;
snake[i].y = 5 - i;
}
dir = 3;
/*** 输出初始地图,蛇与食物 ***/
system("cls");//清空屏幕
hide();
print_wall();
print_food();
print_snake();
locate(m + 2, 0);
cout << "蛇长: ";
/*** 开始游戏 ***/
while (1)//进入游戏循环,不断更新游戏状态
{
/*** 难度随长度增加而提高 ***/
hard_len = (double)snake_length / (double)(m * n);
/*** 调节时间,单位是ms ***/
a = clock();//记录当前时间
while (1)
{
b = clock();// 记录当前时间
if (b - a >= (int)(400 - 30 * hard) * (1 - sqrt(hard_len))) break;
}
/*** 接受键盘输入的上下左右,并以此改变方向 ***/
if (kbhit())//检查控制台窗口的按键是否被按下
{
ch = getch();//从控制台中获取输入的字符
if (ch == -32)//判断输入的字符是否为特殊键
{
ch = getch();//获取特殊键的键值
switch (ch)
{
case 72:
if (dir == 2 || dir == 3)//当前方向为左或右时才可以改变为上
dir = 0;//0表示上
break;
case 80:
if (dir == 2 || dir == 3)
dir = 1;//下
break;
case 75:
if (dir == 0 || dir == 1)
dir = 2;//左
break;
case 77:
if (dir == 0 || dir == 1)
dir = 3;//右
break;
}
}
}
/*** 前进 ***/
if (!go_ahead()) break;
/*** 在最后输出此时长度 ***/
locate(m + 2, 12);
cout << snake_length;
}
system("pause");//暂停程序,按任意键继续
return 0;
}
说明:需要在visual stdio上运行,F:\\gedan2\\ji.mp3 这个路径要放同名mp3(把只因你太美改名成ji),运行前要在项目属性中进行以下三步操作(4条消息) mciSendString函数简介(播放音乐以及录音相关操作)_灰勒塔德的博客-CSDN博客
参考文献:(4条消息) 6个C++游戏代码,Dev-C++都可以运行,可复制。_游戏代码大全可复制_精通Python的博客-CSDN博客
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。