Home > Database > Mysql Tutorial > 以opengl制作2D贪吃蛇(一)

以opengl制作2D贪吃蛇(一)

WBOY
Release: 2016-06-07 15:07:58
Original
1589 people have browsed it

1) 搭建好基本的窗口环境 要确定窗口大小, 通过确定子的大小(TILE_WIDTH, TILE_HEIGHT), 子的数量(TILE_COLUMN, TILE_ROW) 2) 画子 两个循环 /* * 1) 搭建好基本的窗口环境 * 要确定窗口大小, 通过确定格子的大小(TILE_WIDTH, TILE_HEIGHT), * 格子的数量(TIL

1) 搭建好基本的窗口环境
    要确定窗口大小, 通过确定格子的大小(TILE_WIDTH, TILE_HEIGHT),

                            格子的数量(TILE_COLUMN, TILE_ROW)

2) 画格子
     两个循环

/*
 * 1) 搭建好基本的窗口环境
 *    要确定窗口大小, 通过确定格子的大小(TILE_WIDTH, TILE_HEIGHT),
 *                            格子的数量(TILE_COLUMN, TILE_ROW)
 */
 //2) 画格子
 //*    两个循环
//3) 画方块来表示蛇的身体
// *    glPointSize(..)
#include <stdio.h>
#include <gl>
#include <gl>

int TILE_WIDTH = 32;
int TILE_HEIGHT = 32;
const int TILE_COLUMN = 20;
const int TILE_ROW = 11;

const int dt = 33;  /* 毫秒数 */

#define DIR_UP      0
#define DIR_DOWN    1
#define DIR_LEFT    2
#define DIR_RIGHT   3
#define GL_PI 3.1415f
int   dir = DIR_RIGHT;

float snake_x = 0;
float snake_y = 0;
float snake_v = 3;

float food_x = 10;
float food_y = 5;


 void ChangeSize(int w,int h)
 {
 	GLfloat nRange = 100.0f;
 	// Prevent a divide by zero
	if(h == 0)
		h = 1;

	// Set Viewport to window dimensions
    glViewport(0, 0, w, h);

	// Reset coordinate system
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	// Establish clipping volume (left, right, bottom, top, near, far)
    
		glOrtho (0.0f,TILE_COLUMN*TILE_WIDTH,TILE_ROW*TILE_HEIGHT,0,-1, 1);
   

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
 }

 void SetupRC()//在循环之前,为程序做一些一次性的设置
	{
		//  background color
		glClearColor(0.0f, 0.0f, 0.0f, 1.0f );
		glColor3f(0.0f,1.0f,0.0f);
    
}

void RenderScene(void)
	{GLfloat x,y,z,angle;
	// Clear the window with current clearing color
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
	glPointSize(TILE_HEIGHT);
	glBegin(GL_POINTS);
	glVertex3f(snake_x+TILE_WIDTH/2,snake_y+TILE_HEIGHT/2,0.0f);
	glEnd();
   	for(float line_y=0;line_y


<p>编译得到</p>
<p>演示结果:</p>
<p><img  src="/inc/test.jsp?url=http%3A%2F%2Fimg.blog.csdn.net%2F20131119171758109%3Fwatermark%2F2%2Ftext%2FaHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3R1NzQx%2Ffont%2F5a6L5L2T%2Ffontsize%2F400%2Ffill%2FI0JBQkFCMA%3D%3D%2Fdissolve%2F70%2Fgravity%2FSouthEast&refer=http%3A%2F%2Fblog.csdn.net%2Fstu741%2Farticle%2Fdetails%2F16826925" alt="以opengl制作2D贪吃蛇(一)" ><br>
</p>
<br>


</gl></gl></stdio.h>
Copy after login
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template