Advertisement

maze game

Started by October 22, 2020 08:34 PM
13 comments, last by pbivens67 4 years ago

I am attempting to move a sprite through a maze. here is a screen shot https://imgur.com/46vUtw1​ here is the code I am working on. I am able to get the sprite to move up but not the left. I just need a hint on how to get the sprite to move through the maze. I am trying not to troll for answers.

void handleSpecialKeypress(int key, int, int y)
{
	switch (key)
	{
	case GLUT_KEY_LEFT:
		move_horizontal--;
		if (move_horizontal <= 0.0f)
		{
			move_horizontal = 0.0f;
		}
		direction_x = -1;
		break;
	case GLUT_KEY_RIGHT:
		move_horizontal++;
		if (move_horizontal >= 0.0f)
		{
			move_horizontal = 0.0f;
		}
		direction_x = 1;
		break;
	case GLUT_KEY_UP:
		move_vertical++;
		if (move_vertical >= 20.0f)
		{
			move_vertical = 20.0f;
		}
		break;
	case GLUT_KEY_DOWN:
		move_vertical--;
		if (move_vertical <= 0.0f)
		{
			move_vertical = 0.0f;
		}
		break;
	}
	glutPostRedisplay();
}

First thing I see in the code, is the function parameters look funny to me. “(int key, int, int y)” That middle one looks like it should be “int x”?

Second thing I noticed is that you have a vertical range of 0-20, but a horizontal range of 0-0.

I think that might be causing some unexpected behavior.

But I don't see anything in this code that looks like it would make moving left not work.

Advertisement

@pbivens67 so what is your question? what problem did you find?

Look at the code for GLUT_KEY_RIGHT and GLUT_KEY_UP and compare them. Aside from that one is vertical and one is horizontal, what's the difference? GLUT_KEY_UP goes up to 20, but GLUT_KEY_RIGHT can only go up to 0. So unless move_horizontal starts out as negative, the GLUT_KEY_RIGHT handler does nothing.

@DavinCreed I corrected the int x statement

Advertisement

well I have worked on my code and have got my sprite to move up and down but not to move to the left when it moved up.

void drawSprite()
{
	glEnable(GL_TEXTURE_2D);
	glPushMatrix();
	glBindTexture(GL_TEXTURE_2D, texture[0]);
	if (direction_x == 1)
	{
		glBindTexture(GL_TEXTURE_2D, texture[6]);
	}
	if (direction_x == -1)
	{
		glBindTexture(GL_TEXTURE_2D, texture[0]);
		glTranslatef(-1.0f, 0.0f, 0.0f);
	}
	cout << move_horizontal << " " << move_vertical << endl;
	if (move_horizontal >= 0.0f)
	{
		move_horizontal = 0.0f;
	}
	if (move_vertical <= 0.0f)
	{
		move_vertical = 0.0f;
	}
	if (move_horizontal <= 0.0f)
	{
		move_horizontal = 0.0f;
	}
	if (move_vertical >= 20.0f)
	{
		move_vertical = 20.0f;
	}
	if (move_horizontal <= 0.0f && move_vertical >=20.0f)
	{
		move_horizontal--;
		move_vertical = 20.0f;
	}
	glBegin(GL_POLYGON);
	glTexCoord3f(0.0f, 0.0f+screen, 0.0f);

	glVertex3f(110.0f + move_horizontal, -80.0f + move_vertical, 0.0f);
	glTexCoord3f(1.0f, 0.0f + screen, 0.0f);

	glVertex3f(110.0f + move_horizontal, -60.0f + move_vertical, 0.0f);
	glTexCoord3f(1.0f, 0.125f + screen, 0.0f);

	glVertex3f(90.0f + move_horizontal, -60.0f + move_vertical, 0.0f);
	glTexCoord3f(0.0f, 0.125f + screen, 0.0f);

	glVertex3f(90.0f + move_horizontal, -80.0f + move_vertical, 0.0f);
	glEnd();
	glPopMatrix();
	glDisable(GL_TEXTURE_2D);
}

You're going to need to express your question better, Phil. The problem is not clear.

-- Tom Sloper -- sloperama.com

well I have drawn a sprite in the lower right hand corner, I then move it upwards to the upper brick I then want it to move to the left. My sprite only moves up and down.

Didn't a light breeze's answer above already answer this?

-- Tom Sloper -- sloperama.com

This topic is closed to new replies.

Advertisement