Demo entry 6052608

123

   

Submitted by anonymous on Sep 22, 2016 at 04:41
Language: C++. Code size: 3.8 kB.

void drawScore2(char *str)
{
	static int isFirstCall = 1;
	static GLuint lists;

	if (isFirstCall) {
		isFirstCall = 0;
		lists = glGenLists(128);
		wglUseFontBitmaps(wglGetCurrentDC(), 0, 128, lists);
	}

	for (; *str != '\0'; ++str)
		glCallList(lists + *str);
}
//////////////////////////////////////////////////////////////////////////

void CubeGrid::drawS(char *str)
{
	static int isFirstCall = 1;
	static GLuint lists;

	if (isFirstCall) {
		isFirstCall = 0;
		lists = glGenLists(128);
		wglUseFontBitmaps(wglGetCurrentDC(), 0, 128, lists);
	}

	for (; *str != '\0'; ++str)
		glCallList(lists + *str);
}
void CubeGrid::selectFont(int size, int charset, const char* face)
{
	HFONT hFont = CreateFontA(size, 0, 0, 0, FW_MEDIUM, 0, 0, 0,
		charset, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
		DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, face);
	HFONT hOldFont = (HFONT)SelectObject(wglGetCurrentDC(), hFont);
	DeleteObject(hOldFont);
}
void CubeGrid::drawGridQuad(int i, int j)
{
	float x1 = j*dcol, x2 = (j + 1)*dcol;
	float y1 = i*drow, y2 = (i + 1)*drow;
	// draw line
	glColor4f(0, 0, 0, 1);
	glLineWidth(2);
	glBegin(GL_LINES);
	glVertex2f(x1, y1);
	glVertex2f(x1, y2);
	glVertex2f(x1, y1);
	glVertex2f(x2, y1);
	glVertex2f(x1, y2);
	glVertex2f(x2, y2);
	glVertex2f(x2, y1);
	glVertex2f(x2, y2);
	glEnd();
	// draw quad
	glColor4f(0.5, 0.4, 0.2, 0.75);
	glBegin(GL_QUADS);
	glVertex3f(x1, y1, 0.0);
	glVertex3f(x2, y1, 0.0);
	glVertex3f(x2, y2, 0.0);
	glVertex3f(x1, y2, 0.0);
	glEnd();
	//____-------------------------------------------------------------
	//____-------------------------------------------------------------
	//____-------------------------------------------------------------

	char str[40];
	int _value = label[i][j];
	sprintf_s(str, "%d", _value);
	if (_value <100)
		selectFont(30, ANSI_CHARSET, "Comic Sans MS");
	else 
		selectFont(10, ANSI_CHARSET, "Comic Sans MS");
	glColor3f(0.0f, 0.0f, 0.0f);
	
	glRasterPos2f(x1+dcol/3, y1+drow/3);
	drawS(str);
	//____-------------------------------------------------------------
	//____------------------------------------------------------------
}
以上的代码就是我第一个为了解决显示分数和新的方格的问题。因为初次接触OpenGl,一开始用于函数的参数还不是很理解,但是韦老师经常在微信群里为我们答疑,最后花了一天还是搞清OPenGl画图的原理,顺利地debug成功。
接下来,完成了基本的要求,我就开始考虑怎么能让用户在玩这个小游戏的同事,有更加好的游戏体验。
void Game::down()
{
	//------------------------------------------------------------------
	if (flag == 0)
	{
		cout << "The max score on your gird is: " << maxScore() << endl;
		cout << "The value of the current cube is: " << cube->getValue() << endl;
		cout << "The value of the next cube is: "<< next->getValue()<<endl;
	}
	flag++;
	grid->resetMovingLabel(cube);
	if (grid->canDown(cube))
	{
		cube->moveDown();
		grid->setMovingLabel(cube);
	}
	else
	{
		grid->setMovingLabel(cube);
		//------------------------------------------------------------------
		//------------------------------------------------------------------
		checkValueSame();
		flag = 0;
		_score = maxScore();
		const int final = 2048;
		if (_score >= final)
		{
			_end = 1;
		}
		//------------------------------------------------------------------
		//------------------------------------------------------------------
		//____---------------------------___________________
		nextCube();
	}
	
}
通过显示最大的分数能更大地让用户了解到当前的游戏进程。
int main(int argc, char** argv)
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
	glutInitWindowSize(850, 700);	// window size
	glutCreateWindow("MiracleZhang's 2048 Tetris");
	init();
	glutReshapeFunc(reshape);
	glutKeyboardFunc(keyboard);
	glutDisplayFunc(display);
	glutTimerFunc(700, timer, russianCube.isPaused());	// speed 
	glutMainLoop();
	return 0;
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).