Body.h头文件
#ifndef _BODY_H_
#define _BODY_H_
#include<iostream>
class Body
{
public:
Body* next;
int x;
int y;
Body();
Body(int h,int z);
};
#endif
Body.cpp文件
#include"Body.h"
Body::Body()
{
next=NULL;
}
Body::Body(int h,int z)
{
next=NULL,x=h,y=z;
}
Snake.h文件
#pragma once
#include<iostream>
#include"Body.h"
class Snake
{
private:
Body* head;
Body* tail;
Body* curr;
int length;
int direction;
public:
Snake();
~Snake();
void grow();
bool die();
void move();
void setdir(const int& in);
const int& getdir();
const int& getlengh();
const Body* gethead();
const Body* gettail();
const Body* getcurr();
void addcurr();
void backcurr();
};
Snake.cpp文件
#include"Snake.h"
Snake::Snake()
{
curr=head=new Body(4,7);
curr->next=new Body(4,6);
curr->next->next=new Body(4,5);
tail=curr->next->next;
tail->next=NULL;
length=3;
direction=4;
}
Snake::~Snake()
{
curr=head;
while(curr!=NULL)
{
head=head->next;
delete curr;
curr=head;
}
}
void Snake::grow()
{
curr=head;
Body* temp=new Body;
while(curr->next!=tail)
{
curr=curr->next;
}
if(curr->x==tail->x)
{
if(curr->y>tail->y)
{
temp->x=tail->x;
temp->y=tail->y+1;
}
else
{
temp->x=tail->x;
temp->y=tail->y-1;
}
}
else if(curr->y==tail->y)
{
if(curr->x>tail->x)
{
temp->x=tail->x-1;
temp->y=tail->y;
}
else
{
temp->x=tail->x+1;
temp->y=tail->y;
}
}
tail=temp;
tail->next=NULL;
curr->next->next=tail;
curr=head;
length++;
}
const int& Snake::getdir()
{
return direction;
}
bool Snake::die()
{
if(((head->x==tail->x)&&(head->y=tail->y))||head->x>19||head->x<0||head->y>39||head->y<0)
{
return true;
}
else return false;
}
void Snake::move()
{
curr=head;
Body* temp=new Body;
switch(direction)
{
case 1:
temp->x=head->x-1;
temp->y=head->y;
head=temp;
head->next=curr;
break;
case 2:
temp->x=head->x+1;
temp->y=head->y;
head=temp;
head->next=curr;
break;
case 3:
temp->x=head->x;
temp->y=head->y-1;
head=temp;
head->next=curr;
break;
case 4:
temp->x=head->x;
temp->y=head->y+1;
head=temp;
head->next=curr;
break;
}
while(curr->next!=tail)
{
curr=curr->next;
}
delete tail;
tail=curr;
tail->next=NULL;
curr=head;
}
void Snake::setdir(const int& dir)
{
direction=dir;
}
const int& Snake::getlengh()
{
return length;
}
const Body* Snake::gethead()
{
return head;
}
const Body* Snake::gettail()
{
return tail;
}
const Body* Snake::getcurr()
{
return curr;
}
void Snake::addcurr()
{
if(curr==NULL)return;
curr=curr->next;
}
void Snake::backcurr()
{
curr=head;
}
main.cpp主文件
#include<iostream>
#include<windows.h>
#include<string>
#include<time.h>
#include<conio.h>
#include"Body.h"
#include"Snake.h"
int map[20][40]={0};
enum{up=1,down=2,left=3,right=4};
void print(int map1[20][40],int h,int s)
{
for(int i=0;i<h;i++)
{
for(int j=0;j<s;j++)
{
if(0==map1[i][j])
{
std::cout<<" ";
}
if(1==map1[i][j])
{
std::cout<<"*";
}
if(2==map1[i][j])
{
std::cout<<"o";
}
}
std::cout<<std::endl;
}
}
int main()
{
srand(time(0));
Body food(rand()%20,rand()%40);
bool pause=false;
Snake brick;
while(1)
{
if(kbhit())
{
char ch=getch();
if(ch=='w')
{
if(brick.getdir()!=down)
brick.setdir(up);
}
else if(ch=='s')
{
if(brick.getdir()!=up)
brick.setdir(down);
}
else if(ch=='a')
{
if(brick.getdir()!=right)
brick.setdir(left);
}
else if(ch=='d')
{
if(brick.getdir()!=left)
brick.setdir(right);
}
}
for(int i=0;i<20;i++)
{
for(int j=0;j<40;j++)
{
map[i][j]=0;
}
}
for(int i=0;i<brick.getlengh();i++)
{
map[brick.getcurr()->x][brick.getcurr()->y]=1;
brick.addcurr();
}
map[food.x][food.y]=2;
if(brick.gethead()->x==food.x&&brick.gethead()->y==food.y)
{
srand(time(0));
map[food.x][food.y]=0;
brick.grow();
food.x=rand()%20;
food.y=rand()%40;
brick.backcurr();
}
else
{
brick.backcurr();
print(map,20,40);
brick.move();
Sleep(500);
}
system("cls");
}
system("pause");
return 0;
}