⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 snake.cpp

📁 用opengl编写的简单贪吃蛇游戏
💻 CPP
字号:
// Snake.cpp: implementation of the Snake class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "SnakesSubsonic.h"
#include "Snake.h"
#include "math.h"


#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

#define rad 0.1f

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
Snake::Snake()
{
	head = tail = new cell;
	tail->link = NULL;
	tail->sx = 0;
	tail->sy = 0;

	cellnum = 5;

	foodkind = 0;

	snk_MD = snk_left;

	first = 1;

	foodcell = new cell;

	for (int i=1; i<cellnum; i++)
	{
		cell *p = new cell;
		p->sx = tail->sx + 2*rad;
		p->sy = tail->sy;
		p->link = NULL;
		tail->link = p;
		tail = p;
	}

}

Snake::~Snake()
{
	cell *seak=head;
	cell *del;
	while(seak != NULL){
		del = seak;
		seak = seak->link;
		delete del;
	}
}

void Snake::DrawCell(cell *sc,float cell_r = rad){
	// 	glClearColor(1.0f, 1.0f, 1.0f, 1.0f);				// 设置刷新背景色
// 	glClear(GL_DEPTH_BUFFER_BIT);
//	glLoadIdentity();
	
	GLfloat light_ambient[] = {0.0, 1.0, 0.0, 1.0};
	GLfloat light_diffuse[] = {1.0, 1.0, 1.0, 1.0};
	GLfloat light_specular[] = {1.0, 1.0, 1.0, 1.0};
	GLfloat light_position[] = {0.0, 0.5, 1.0, 0.0};
	
	glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
	glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
	glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
	glLightfv(GL_LIGHT0, GL_POSITION, light_position);
	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT0);
	
	glEnable(GL_COLOR_MATERIAL);
	glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);

	//===========================================================
	glPushMatrix();		//不加只有一个球
		glTranslatef(sc->sx,sc->sy,-4);
		glColor4f(1.0,0.0,0,1.0);
		glutSolidSphere(cell_r, 80, 30);
	glPopMatrix();
	
	glDisable(GL_LIGHT0);
	glDisable(GL_LIGHTING);

	glFinish();

//	SwapBuffers(wglGetCurrentDC());
}

void Snake::DrawSnake(){
	cell *seak=head->link;
	while(seak != NULL){
		DrawCell(seak);
		seak = seak->link;	
	}
}

void Snake::DrawHead(GLuint headimage){
	glPushMatrix();
	glColor4f(1,1,1,1);		// 设置背景贴图底色为白色
	
	glTranslatef(0,0,-4);	// 背景图片位置
	
	glBindTexture(GL_TEXTURE_2D, headimage);	// This Will Select The BG Maps...

	float width = 0.6;
	float height = 0.6;
	
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glEnable(GL_ALPHA_TEST);
	glAlphaFunc(GL_GREATER, 0);
	
	glEnable(GL_TEXTURE_2D);
		glBegin(GL_QUADS);
			glTexCoord2f(0.0f, 0.0f); glVertex2f( head->sx-width/2, head->sy-height/2);		
			glTexCoord2f(1.0f, 0.0f); glVertex2f( head->sx+width/2, head->sy-height/2);
			glTexCoord2f(1.0f, 1.0f); glVertex2f( head->sx+width/2, head->sy+height/2);
			glTexCoord2f(0.0f, 1.0f); glVertex2f( head->sx-width/2, head->sy+height/2);
		glEnd();
				
	glDisable(GL_TEXTURE_2D);
	glPopMatrix();
}

BOOL Snake::Eat(){
 	double dx = head->sx - foodcell->sx;
	double dy = head->sy - foodcell->sy;
 	double d = sqrt(dx*dx+dy*dy);
 	if(d<rad+0.4)return 1;
	else return 0;
}

void Snake::SnakeRun(double borderx, double bordery)
{
	if (borderx - head->sx > 0 && borderx - head->sx < 1.1 && snk_MD == snk_right)snk_MD = snk_up;
	if (-borderx - head->sx < 0 && -borderx - head->sx > -1.1 && snk_MD == snk_left)snk_MD = snk_down;
	if (bordery - head->sy >0 && bordery - head->sy < 0.9 && snk_MD == snk_up)snk_MD = snk_left;
	if (-bordery - head->sy < 0 && -bordery - head->sy > -0.9 && snk_MD == snk_down)snk_MD = snk_right;

	cell *p = new cell;
	switch(snk_MD){
		case snk_left	:	p->sx = head->sx - 2*rad; p->sy = head->sy;
			break;
		case snk_right	:	p->sx = head->sx + 2*rad; p->sy = head->sy;
			break;
		case snk_up		:	p->sx = head->sx; p->sy = head->sy + 2*rad;
			break;
		case snk_down	:	p->sx = head->sx; p->sy = head->sy - 2*rad;
			break;
	}
	p->link = head;
	head = p;

	if(first){
		NewFood();
		first = 0;
	}
	
	if (Eat())NewFood();
	else CutTail();
}

void Snake::CutTail(){
	cell *p = head;
	while (p->link != tail && p->link != NULL)p = p->link;	
	if(tail != head)delete tail;
	tail = p;
	tail->link = NULL;
}

void Snake::NewFood(){

	int foodx = 8 - rand() / 2000;
	int foody = 8 - rand() / 2000;
	if(foodcell!=NULL)delete foodcell;
	foodcell = new cell;
	foodcell->sx = foodx*2*rad;
	foodcell->sy = foody*2*rad;
	foodcell->link = NULL;
	if (isFoodCrush())
	{
		NewFood();
	}
	foodkind = rand()%7;
}

void Snake::DrawFood(UINT image, float width, float height){
	//DrawCell(foodcell,0.3f);
	
	//	LoadBMP("pic/bg4.bmp",TextureImage[0]);
	
	glPushMatrix();
	glColor4f(1,1,1,1);		// 设置背景贴图底色为白色
	
	glTranslatef(0,0,-5);	// 背景图片位置
	
	glBindTexture(GL_TEXTURE_2D, image);	// This Will Select The BG Maps...
	
 	glEnable(GL_BLEND);
 	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glEnable(GL_ALPHA_TEST);
	glAlphaFunc(GL_GREATER, 0);

	glEnable(GL_TEXTURE_2D);
	//		glPushMatrix();
		glBegin(GL_QUADS);
	//		glNormal3f( 0.0f, 0.0f, 1.0f);	//设置传递参数的当前法线
			glTexCoord2f(0.0f, 0.0f); glVertex2f( foodcell->sx-width/2, foodcell->sy-height/2);		
			glTexCoord2f(1.0f, 0.0f); glVertex2f( foodcell->sx+width/2, foodcell->sy-height/2);
			glTexCoord2f(1.0f, 1.0f); glVertex2f( foodcell->sx+width/2, foodcell->sy+height/2);
			glTexCoord2f(0.0f, 1.0f); glVertex2f( foodcell->sx-width/2, foodcell->sy+height/2);
		glEnd();
				
	glDisable(GL_TEXTURE_2D);
	glPopMatrix();
}

BOOL Snake::isFoodCrush(){
	cell *seak=head;
	double dx,dy,d;
	while(seak != NULL){
		dx = seak->sx - foodcell->sx;
		dy = seak->sy - foodcell->sy;
		d = sqrt(dx*dx+dy*dy);
		if(d<rad+0.3)return 1;
		seak = seak->link;	
	}
	return 0;
}

BOOL Snake::isDead(){
	cell *seak=head->link;
	double dx,dy,d;
	while(seak != NULL){
		dx = seak->sx - head->sx;
		dy = seak->sy - head->sy;
		d = sqrt(dx*dx+dy*dy);
		if(d<2*rad)return 1;
		seak = seak->link;	
	}
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -