🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

simple if question

Started by
32 comments, last by pbivens67 10 months, 4 weeks ago

I still don't know what the point of this code is, so it's hard to recommend a way to get there. But here's a much simpler program that doesn't have any state variables, doesn't use storage, has a simple function which can be evaluated at non-integer values if you want, and produces the same output:

#include <iostream>
#include <cmath>

float f(float j) {
  float x = std::min(j - 22.5f, 0.5f);
  return 50.f - 0.1f * x * x;
}

int main() {
  for (int j = 0; j < 46; j++)
    std::cout << f(j) << '\n';
}

Advertisement

well thanks to alberth I have solved my problem, onto my next problem.

pbivens67 said:
well thanks to alberth I have solved my problem, onto my next problem.

Shall I presume you'll start a new thread with your next problem? So this one can be closed?

Here's hoping your next project goes well and you don't have to post a problem at all.

-- Tom Sloper -- sloperama.com

thanks for all the help

well I am still working on my little 2d platformer. I am able to get my dog sprite to jump up onto the first tier. My question is how I get my dog sprite to move to the right instead of staying in the same place when I try to move to the right. I have tried using move_sprite=x line of code to solve my problem.

void count()
{
	for (float x = 22.5f; x >= -22.5f; x--)
	{
		y = (-0.1f * (x * x)) + 50;
		parabola.push_back(y);
	}
}

void jump()
{
	
	if (state == 0)
	{
		y = parabola[i];
		i++;
	}
	if (y >= tier_one)
	{
		state = 1;
	}
	if (state == 1)
	{
		y = tier_one;
	}

	if (state == 0 && direction_x == -1)
	{
		x--;
	}
	if (state == 0 && direction_x == 1)
	{
		x++;
		move_sprite = x;
	}
	glutPostRedisplay();
}


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

void handleSpecialKeypress(int key, int x, int y)
{
	switch (key)
	{
	case GLUT_KEY_LEFT:
		move_sprite--;
		if (move_sprite <= -255.0f)
		{
			move_sprite = -255.0f;
		}
		direction_x = -1;
		break;
	case GLUT_KEY_RIGHT:
		cout << move_sprite << endl;
		if (move_sprite >= 0.0f)
		{
			move_sprite = 0.0f;
		}
		move_sprite++;
		direction_x = 1;
		break;
	case GLUT_KEY_UP:

		break;
	case GLUT_KEY_DOWN:

		break;
	}
	glutPostRedisplay();
}

Is move_sprite intialized to a value of 0? If so that's why it won't move past 1 in case GLUT_KEY_RIGHT:

misc tips:

  1. Instead of using ascii key codes like 27 for escape and 32 for space, use variables like you did for left / right /down etc
  2. For keys like space you can use ‘ ’ in your switch statement (theres a space inbetween the ‘’ )
  3. Use variables instead of numbers. I guess -255 and 0 are the bounds of your dog's area? Use LeftBound = -255 and RightBound = 0. If you decide to change the size of your playable area, you just need to change two variables.
  4. Every time you press space (case 32:) you call count(), which pushes a bunch of numbers into parabola. Press space a few times, and parabola will contain hundreds of values.
  5. To someone unfamiliar with your game, it's not immediately clear what state = 0 or 1 means. You could try using an enumeration like this

enum State {Jumping, Standing, Sitting}

State MyState = State::Standing

If (MyState == State::Jumping)

{ jumping code }

scott8 said:
Is move_sprite intialized to a value of 0? If so that's why it won't move past 1 in case GLUT_KEY_RIGHT:

yes move_sprite initialized to a value of 0

well, scott8 I made some changes to my code.

void count()
{
	for (float x = 22.5f; x >= -22.5f; x--)
	{
		y = (-0.1f * (x * x)) + 50;
		parabola.push_back(y);
	}
}

void jump()
{
	
	if (MyState == State::Jumping)
	{
		y = parabola[i];
		i++;
	}
	if (y >= tier_one)
	{
		MyState = State::Standing;
	}
	if (MyState == State::Standing)
	{
		y = tier_one;
	}

	if (MyState == State::Jumping && direction_x == -1)
	{
		x--;
	}
	if (MyState == State::Jumping && direction_x == 1)
	{
		x++;
		move_sprite = x;
	}
	glutPostRedisplay();
}

void handleKeypress(unsigned char key, int x, int y)
{
	switch (key)
	{
	case escape:
		exit(0);
		break;
	case ' ':
		count();
		jump();
		break;
	}
	glutPostRedisplay();
}

void handleSpecialKeypress(int key, int x, int y)
{
	switch (key)
	{
	case GLUT_KEY_LEFT:
		move_sprite--;
		if (move_sprite <= left_bound)
		{
			move_sprite = left_bound;
		}
		direction_x = -1;
		break;
	case GLUT_KEY_RIGHT:
		cout << move_sprite << endl;
		if (move_sprite >= right_bound)
		{
			move_sprite = right_bound;
		}
		move_sprite++;
		direction_x = 1;
		break;
	case GLUT_KEY_UP:

		break;
	case GLUT_KEY_DOWN:

		break;
	}
	glutPostRedisplay();
}

Phil, does your code work now?

You're just telling us you changed your code, but you haven't said whether or not it works, and you have not asked a question.

So naturally, we can only assume that it works now and you're just sharing your success, and your simple If question has now been answered.

-- Tom Sloper -- sloperama.com

@Tom Sloper Without a ringmaster like you, the circus wouldn't be nearly as sad and depressing.

🙂🙂🙂🙂🙂<←The tone posse, ready for action.

This topic is closed to new replies.

Advertisement