C++命令行贪吃蛇
作者:互联网
闲来无事,用c++写了个命令行贪吃蛇玩玩,发现刷新太快,光标跑来跑去的很难受,不过勉强能“冲”。下面放上代码。DALAO勿喷。
代码
#include<iostream>
#include<string>
#include<stdio.h>
#include<windows.h>
#include<conio.h>
#include<ctime>
using namespace std;
const int top = 0;
const int feet = 20;
typedef struct {
int x;int y;
}BodyPos;
typedef struct {
int dx;int dy;
}Direction;
class Snake
{
private:
int length;
Direction dir;
public:
Snake(int Length, int Dx, int Dy){
this->length = Length;this->dir.dx = Dx;this->dir.dy = Dy;
}
~Snake() { cout << "你蛇没了" <<endl; }
void Get_food() { this->length++; }
int Get_length() { return length; }
void Put_deirection(int Dx, int Dy) { this->dir = { Dx,Dy }; }
Direction Get_direction() { return dir; }
};
class Map {
private:
char map[21][21] = { ' ' };
char food = '*';
char body = 'o';
char TransverseBondary = '-';
char VerticalBondary = '|';
BodyPos bodypos[300] = { 0,0 };
BodyPos foodpos;
public:
Map(Snake& s) {
for (int i = top;i <= feet;i++) {
this->map[top][i] = this->TransverseBondary;this->map[feet][i] = this->TransverseBondary;
//this->map[i][top] = this->VerticalBondary;this->map[i][feet] = this->VerticalBondary;
}
for (int i = top + 1;i < feet;i++) {
this->map[i][top] = this->VerticalBondary;this->map[i][feet] = this->VerticalBondary;
}
int j = 5;
for (int i = 2;i >= 0;i--) {
bodypos[i] = { 7,j };
j++;
}
this->GetFoodPos();
SetSnake(s);
}
void SetSnake(Snake& s) {
int length = s.Get_length();
this->DeleteMap();
for (int i = 0;i < length;i++) {
map[bodypos[i].x][bodypos[i].y] = body;
}
this->map[foodpos.x][foodpos.y] = '*';
}
void DeleteMap() {
for (int i = 0;i <= feet;i++) {
for (int j = 0;j <= feet;j++) {
map[i][j] = ' ';
}
}
for (int i = top;i <= feet;i++) {
this->map[top][i] = this->TransverseBondary;this->map[feet][i] = this->TransverseBondary;
//this->map[i][top] = this->VerticalBondary;this->map[i][feet] = this->VerticalBondary;
}
for (int i = top + 1;i < feet;i++) {
this->map[i][top] = this->VerticalBondary;this->map[i][feet] = this->VerticalBondary;
}
}
void PrintMap() {
for (int i = top;i <= feet;i++) {
for (int j = top;j <= feet;j++)
printf("%c ", map[i][j]);
printf("\n");
}
}
/*void GetFood(Snake &s) {
s.Get_food();
this->bodypos[s.Get_length()];
}*/
void GetFoodPos() {
srand(time(0));
this->foodpos.x = rand() % 19 + 1;
this->foodpos.y = rand() % 19 + 1;
}
void Move(Snake& s) {
int dx = s.Get_direction().dx;
int dy = s.Get_direction().dy;
int length = s.Get_length();
if (this->map[bodypos[0].x][bodypos[0].y] == '*') { s.Get_food();this->GetFoodPos(); }
for (int i = length-1;i > 0;i--) {
bodypos[i].x = bodypos[i - 1].x;
bodypos[i].y = bodypos[i - 1].y;
}
if (this->bodypos[0].x + dx > feet) { this->bodypos[0].x = top;this->SetSnake(s); return; }
if (this->bodypos[0].x + dx < top) { this->bodypos[0].x = feet;this->SetSnake(s);return; }
if (this->bodypos[0].y + dy > feet) { this->bodypos[0].y = top;this->SetSnake(s); return; }
if (this->bodypos[0].y + dy < top) { this->bodypos[0].y = feet;this->SetSnake(s);return; }
this->bodypos[0].x += dx, this->bodypos[0].y += dy;
this->SetSnake(s);
}
bool HoldJudge(int Dx,int Dy) {
if (this->bodypos[0].x + Dx == this->bodypos[1].x && this->bodypos[0].y + Dy == this->bodypos[1].y)return false;
if (this->bodypos[0].x + Dx > feet) { return false; }
if (this->bodypos[0].x + Dx < top) { return false; }
if (this->bodypos[0].y + Dy > feet) { return false; }
if (this->bodypos[0].y + Dy < top) { return false; }
else return true;
}
bool TKJudge(Snake &s) {
int length = s.Get_length();
if (map[this->bodypos[0].x+s.Get_direction().dx][this->bodypos[0].y+s.Get_direction().dy]=='o')return false;
else return true;
}
};
int main() {
Snake s(3,0,1);
Map m(s);
int ch;
while (1) {
cout << "score:" << s.Get_length() << endl;
m.PrintMap();
if (m.TKJudge(s) == false) { cout << "No!";break; }
if (_kbhit()) { //如果有按键按下,则_kbhit()函数返回真
ch = _getch(); //使用_getch()函数获取按下的键值
if (ch == 75) { if(m.HoldJudge(0,-1))s.Put_deirection(0,-1); }
if (ch == 72) { if(m.HoldJudge(-1,0))s.Put_deirection(-1,0); }
if (ch == 77) { if(m.HoldJudge(0,1))s.Put_deirection(0,1); }
if (ch == 80) { if(m.HoldJudge(1,0))s.Put_deirection(1,0); }
}
m.Move(s);
Sleep(50);
system("cls");
}
system("pause");
return 0;
}
运行截图
标签:map,return,bodypos,int,top,C++,feet,贪吃蛇,命令行 来源: https://blog.csdn.net/GX233/article/details/109923115