C++贪吃蛇小游戏 vector 版本 food和snake没有独立

/* ---------------------------------------------------------vector--------------------------------------------------------------*/

//Snake.h begin--------------------------------------------------------------------------------
#ifndef _SNAKE_H_
#define _SNAKE_H_

#include<vector>
using namespace std;

//贪吃蛇每一节身体的X和Y坐标
class point
{
public:
int x;
int y;
point();
point(int px, int py);
~point();
int Get_Pointx();
int Get_Pointy();
void Set_Pointx(int px);
void Set_Pointy(int py);

};

//贪吃蛇身体
class Snake
{
private:
vector<point>Body; //蛇身
int len; //蛇长
int direct; //移动方向
public:
Snake();
Snake(point head);
~Snake();
point Get_Head();
int Get_len();
int Get_director();
void Set_director(int Director);
void Move(point &);
void Print(point food);
void Eat(point );
point Food(point &);
};

#endif // !1

//Snake.h  end--------------------------------------------------------------------------------

//Snake.cpp begin--------------------------------------------------------------------------------

#include<iostream>
#include<graphics.h>
#include<conio.h>
#include<time.h>
#include"Snake.h"
#include<vector>
#include <windows.h>
//#define UP 0;

constexpr int UP = 0;
constexpr int DOWN= 1;
constexpr int LEFT = 2;
constexpr int RIGHT = 3;

using namespace std;

//贪吃蛇每一节身体的X和Y坐标

//默认构造
point::point():x(0),y(0)
{

}
//带参数构造
point::point(int px, int py) : x(px), y(py)
{

}
point::~point()
{

}
//设置横坐标
void point::Set_Pointx(int px)
{
this->x = px;
}
//设置纵坐标
void point::Set_Pointy(int py)
{
this->y = py;
}

//获取横坐标
int point::Get_Pointx()
{
return this->x;
}
//获取纵坐标
int point::Get_Pointy()
{
return this->y;
}


//贪吃蛇

//默认初始化
Snake::Snake():len(1), direct(DOWN)
{
point head;
Body.push_back(head);
}

//初始化蛇头
Snake::Snake(point head):len(1),direct(DOWN)
{
Body.push_back(head);
}
Snake::~Snake()
{

}

//获取蛇头
point Snake::Get_Head()
{
return Body[0];
}

//获取蛇长
int Snake::Get_len()
{
return this->len;
}
//改变方向
int Snake::Get_director()
{
return this->direct;
}
void Snake::Set_director(int Director)
{
this->direct = Director;
}

//蛇在移动
void Snake::Move(point &food)
{
point temp;
if (food.x == Body[0].x &&food.y == Body[0].y)
{
Eat(food);
food = point(-1, -1);
}

//保存蛇头
temp.x = Body[0].x;
temp.y = Body[0].y;
if (len > 1)
{
for (int i = len-1;i >0;--i)
{
if (i != 0)
{
Body[i].x = Body[i-1].x;
Body[i].y = Body[i-1].y;
}

}
Body[0].x = Body[len - 1].x;
Body[0].y = Body[len - 1].y;

}
int px = 0;
int py = 0;
switch (direct)
{
case UP:
py -= 1;
break;
case DOWN:
py += 1;
break;
case LEFT:
px -= 1;
break;
case RIGHT:
px += 1;
break;
default:
break;
}
Body[0].x = temp.x + px;
Body[0].y= temp.y + py;
if (Body[0].x < 0)
Body[0].x = 79;
if (Body[0].x >79)
Body[0].x = 0;
if (Body[0].y < 0)
Body[0].y = 59;
if (Body[0].y >59)
Body[0].y = 0;

int i = 0; // 跳过头部
for (auto sk : Body)
{
if (i == 0)
{
i = 1;
continue;
}
if (sk.x == Body[0].x && sk.y == Body[0].y)
{
MessageBox(NULL, "对不起", "一不小心!", MB_YESNO);
exit(0);
}
}

}

// 蛇的食物
point Snake::Food(point &food)
{
bool flag = true;
while (flag)
{
//生成食物
food.x = rand() % 80;
food.y = rand() % 60;
for (auto temp : Body)
{
if (temp.x == food.x && temp.y==food.y)
{
break;
}
flag = false;
}
}
return food;
}

//打印蛇身

void Snake::Print(point food)
{
PIMAGE snake_img = newimage(800,600);
settarget(snake_img);

setfillcolor(WHITE);
for (int i = 0;i < len;++i)
{
bar(Body[i].x * 10, Body[i].y * 10, Body[i].x * 10 + 10, Body[i].y * 10 + 10);
}
setfillcolor(GREEN);
bar(food.x*10, food.y*10, food.x *10+ 10, food.y *10+ 10);
settarget(NULL);
putimage(0, 0, snake_img);
delimage(snake_img);
}

void Snake::Eat(point food)
{
Body.push_back(food);
++len;
}

//Snake.cpp end -----------------------------------------------------------------------------------

//main.cpp begin--------------------------------------------------------------------------------

#include<iostream>
#include<graphics.h>
#include<conio.h>
#include<time.h>
#include"Snake.h"
#include<vector>
#include <windows.h>
using namespace std;

#define SCREEN_HEIGHT 600
#define SCREEN_WIDTH 800
#define MAXLENGTH 4800
char Direction;
// 蛇的位置
point position(40, 30);
Snake snake(position);

DWORD WINAPI ThreadProc(LPVOID IParam)
{
while (true)
{
if (snake.Get_len() == MAXLENGTH)
{
MessageBox(NULL, "太厉害了!", "天才专属!", MB_YESNO);
exit(0);
}
Direction = getch();
switch (Direction)
{
case'W':
case'w':
if (snake.Get_director() == 1)
break;
snake.Set_director(0);
break;
case'S':
case's':
if (snake.Get_director() == 0)
break;
snake.Set_director(1);
break;
case'A':
case'a':
if (snake.Get_director() == 3)
break;
snake.Set_director(2);
break;
case'D':
case'd':
if (snake.Get_director() == 2)
break;
snake.Set_director(3);
break;
default:
break;
}
}
return 0;
}

int main()
{
srand((unsigned int)time(0));
initgraph(SCREEN_WIDTH, SCREEN_HEIGHT);

//改变窗口标题
HWND hwnd = getHWnd();
HDC hdc = GetDC(hwnd);

SetWindowText(hwnd, "贪吃蛇");
point food(rand()%80,rand()%60);
point temp;
CreateThread(0, 0, ThreadProc, 0, 0, 0);
while (true)
{
//食物的位置
if (food.x == -1 && food.y == -1)
{
food = snake.Food(food);
}
snake.Move(food);
snake.Print(food);
Sleep(100);
}
getch();
closegraph();
return 0;
}

//main.cpp end--------------------------------------------------------------------------------

猜你喜欢

转载自www.cnblogs.com/yangshengjing/p/11636797.html