Jump to content
  • Advertisement
Sign in to follow this  
phil67rpg

simple routine

This topic is 2546 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am working on a simple routine that uses opengl and c++.I want to count up to a certain number and then back down and up again in an loop but I dont want to use the while comand. I am using this routine to move a paddle back and forth across the screen in a breakout game. here is some of the code I am working on.

[font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]void[/font][/font][/font][font="Consolas"][font="Consolas"] loop()

{



[/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]if[/font][/font][/font][font="Consolas"][font="Consolas"](j>-380 && j<380)

{

glRectf(375.0+j,0.0,525.0+j,40.0);

j=j+10;

}



[/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]else[/font][/font][/font][font="Consolas"][font="Consolas"] [/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]if[/font][/font][/font][font="Consolas"][font="Consolas"](k<380 && k>-380)

{

glRectf(375.0+k,0.0,525.0+k,40.0);

k=k-10;

}

[/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]else[/font][/font][/font][font="Consolas"][font="Consolas"] [/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]if[/font][/font][/font][font="Consolas"][font="Consolas"](l>-380 && l<380)

{

glRectf(375.0+l,0.0,525.0+l,40.0);

l=l+10;

}

}

[/font][/font][font="Consolas"][font="Consolas"]I have been able to move the paddle back and forth across the screen three times only .[/font][/font]

Share this post


Link to post
Share on other sites
Advertisement
Does that function define 3 variables (one for each move across the screen?)

You can get this done in about 2 variables: an int and bool.

Set the integer to initial position and bool to represent in which direction the ball is moving (lets say false is to the left and true is to the right).

If we start at initial value of -380 and go all the way to 380 by increments of 10, we can do this:

void loop()
{

direction ? k += 10 : k -= 10;
if(k<(-380))
{
k = -380;
direction = true;
}
else if (k>380)
{
k = 380
direction = false;
}
glRectf(375.0+k, 0.0, 520.0+k, 0.0);

}

making the loop much simpler and it will continue forever (unless I've made a careless mistake). Tada!

EDIT: Oh, I hear that the ternary operator (the ? and : combo) is supposedly "bad habit," so you may choose to express it with if statements rather than the way I wrote it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!