#include<bits/stdc++.h>
#include<cstring>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <cmath>
#include <windows.h>
using namespace std;
HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE);
COORD coord;
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);
}
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 << "-";
}
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));
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==fo
C++小程序系统
这是一个C++实现的贪吃蛇小游戏,用户可以输入地图大小和难度,通过方向键控制蛇移动,吃到食物会增长,碰到边界或自身会游戏结束。程序还包括简单的登录验证和多个功能选项,如计算器、抽奖等。

最低0.47元/天 解锁文章

207

被折叠的 条评论
为什么被折叠?



