Advertisement

if problem

Started by May 09, 2023 12:18 AM
17 comments, last by scott8 1 year, 6 months ago

here is my code

#include <freeglut.h>
#include <iostream>
#include <SOIL.h>
//#include "glui.h"

using namespace std;

GLuint texture[8];

float move_x = 0.0f, move_y = 0.0f;
int turn_tank = 0;

void Land_Outline()
{
	glColor3f(1.0f, 1.0f, 1.0f);
	glLineWidth(2.0f);
	glBegin(GL_LINE_STRIP);
	glVertex3f(135.0f, -100.0f, 0.0f);
	glVertex3f(115.0f, -80.0f, 0.0f);
	glVertex3f(90.0f, -80.0f, 0.0f);
	glVertex3f(80.0f, -60.0f, 0.0f);
	glVertex3f(50.0f, -60.0f, 0.0f);
	glVertex3f(30.0f, -30.0f, 0.0f);
	glVertex3f(10.0f, -30.0f, 0.0f);
	glVertex3f(-20.0f, 0.0f, 0.0f);
	glVertex3f(-50.0f, 0.0f, 0.0f);
	glVertex3f(-70.0f, -20.0f, 0.0f);
	glVertex3f(-100.0f, -20.0f, 0.0f);
	glVertex3f(-120.0f, -40.0f, 0.0f);
	glVertex3f(-133.0f, -100.0f, 0.0f);
	glEnd();
}

void Player_Tank()
{
	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, texture[0]);

	glBegin(GL_POLYGON);
	glTexCoord3f(0.0f, 0.0f, 0.0f);
	glVertex3f(115.0f + move_x, -80.0f + move_y, 0.0f);

	glTexCoord3f(1.0f, 0.0f, 0.0f);
	glVertex3f(135.0f + move_x, -100.0f + move_y, 0.0f);

	glTexCoord3f(1.0f, 1.0f, 0.0f);
	glVertex3f(145.0f + move_x, -90.0f + move_y, 0.0f);

	glTexCoord3f(0.0f, 1.0f, 0.0f);
	glVertex3f(125.0f + move_x, -70.0f + move_y, 0.0f);
	glEnd();

	glDisable(GL_TEXTURE_2D);
}

void Player_Tank_two()
{
	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, texture[0]);

	glBegin(GL_POLYGON);
	glTexCoord3f(0.0f, 0.0f, 0.0f);
	glVertex3f(110.0f + move_x, -90.0f + move_y, 0.0f);

	glTexCoord3f(1.0f, 0.0f, 0.0f);
	glVertex3f(130.0f + move_x, -90.0f + move_y, 0.0f);

	glTexCoord3f(1.0f, 1.0f, 0.0f);
	glVertex3f(130.0f + move_x, -80.0f + move_y, 0.0f);

	glTexCoord3f(0.0f, 1.0f, 0.0f);
	glVertex3f(110.0f + move_x, -80.0f + move_y, 0.0f);
	glEnd();

	glDisable(GL_TEXTURE_2D);
}

GLuint loadTex(const char *texname)
{
	/* load an image file directly as a new OpenGL texture */
	GLuint texture = SOIL_load_OGL_texture
	(
		texname,
		SOIL_LOAD_AUTO,
		SOIL_CREATE_NEW_ID,
		SOIL_FLAG_POWER_OF_TWO
	);
	return texture;
}

void init()
{
	texture[0] = loadTex("C:\\Users\\Owner\\Desktop\\tanksprite.png");
}

void renderScene()
{
	glClear(GL_COLOR_BUFFER_BIT);
	if (turn_tank == 0)
	{
	Player_Tank();
	}
	if (turn_tank == 1)
	{
	Player_Tank_two();
	}
	Land_Outline();
	glFinish();
}

void ChangeSize(GLsizei w, GLsizei h)
{
	GLfloat aspectRatio;
	if (h == 0)
		h = 1;
	glViewport(0, 0, w, h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	aspectRatio = (GLfloat)w / (GLfloat)h;
	if (w <= h)
		glOrtho(-100.0, 100.0, -100.0 / aspectRatio, 100.0 / aspectRatio, 1.0, -1.0);
	else
		glOrtho(-100.0*aspectRatio, 100.0*aspectRatio, -100.0, 100.0, 1.0, -1.0);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

void handleKeypress(unsigned char key, int x, int y)
{
	switch (key)
	{
	case 27:
		exit(0);
		break;
	case 32:
		break;
	}
	glutPostRedisplay();
}

void handleSpecialKeypress(int key, int x, int y)
{
	switch (key)
	{
	case GLUT_KEY_LEFT:
		if (move_y <= -24)
		{
			move_y++;
			turn_tank = 0;
			move_x = -24;
		}
		else if (move_x <= -11 && move_y >= 11)
		{
			turn_tank = 1;
			move_y = 11;
			move_x--;
		}
		cout << move_x << " " << move_y << endl;
		move_x--;
		move_y++;
		break;
	case GLUT_KEY_RIGHT:
		move_x++;
		move_y--;
		if (move_x >= 0 && move_y <= 0)
		{
			move_x = 0;
			move_y = 0;
		}
		break;
	case GLUT_KEY_UP:
		break;
	case GLUT_KEY_DOWN:
		break;
	}
	glutPostRedisplay();
}

int main(int argc, char **argv)
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
	glutInitWindowPosition(600, 300);
	glutInitWindowSize(800, 600);
	glutCreateWindow("ScorchedEarth");
	glutKeyboardFunc(handleKeypress);
	glutSpecialFunc(handleSpecialKeypress);
	glutDisplayFunc(renderScene);
	glutReshapeFunc(ChangeSize);
	init();
	glutMainLoop();
	return 0;
}

What does it print? Is what it prints consistent with what you see? Is it what you wanted the program to do? If not, can you find a particular setting of the variables where it's not doing what you want? Can you rework your logic with that example in mind?

Advertisement

here is my updated code

	case GLUT_KEY_LEFT:
		if (move_x <= -11 && move_y >= 11)
		{
			turn_tank = 1;
			move_y = 11;
		}
		cout << move_x << " " << move_y << endl;
		move_x--;
		move_y++;
		break;

The likely much simpler path is to only change x with your left and right key, and compute y from the x value, something like

int compute_y(int x) {
    if (x < 10)
        return 0; // Flat land at the bottom
    else if (x >= 10 && x <= 30)
        return x - 10; // y increases from 0 to 20 for x from 10 to 30.
    else
        return 20; // 20 high for x > 30
}
Advertisement

well I stubbed out Alberth's code but I am unsure how to incorporate it into my code.

#include <iostream>

using namespace std;

int compute_y(int x) {
	if (x < 10)
		return 0; // Flat land at the bottom
	else if (x >= 10 && x <= 30)
		return x - 10; // y increases from 0 to 20 for x from 10 to 30.
	else
		return 20; // 20 high for x > 30
}

int main()
{
	int x = 0;

	cout << "Enter x: ";
	cin >> x;
	cout << endl;

	cout << compute_y(x) << endl;

	return 0;
}

It computes the y value from the x value, so the simplest form is when the x variable changes, call it and update your y variable with the result as well.

@pbivens67 If it's moving straight, it's executing the second IF statement: MoveX ≤ -11 and Move_Y >11

Also the code in the 2nd IF statement gets executed, then Move_X is decreased again, so it's moving twice as fast in the x-direction!

One thing you should get in the habit of doing is not using ‘magic numbers’ like -24,-11, +11. I guess these are screen positions where the hills are?

Try to do something like:

If (NearAMountain(XPosition, YPosition) == true)

YPosition --;

The NearAMountain function would compare your tank position with a list (array , vector) of mountains. You may want to add more mountains in the future or give them different screen positions.

This topic is closed to new replies.

Advertisement