Sign in to follow this  
phil67rpg

simple routine

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"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]void[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] loop()

{



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

{

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

j=j+10;

}



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

{

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

k=k-10;

}

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

{

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

l=l+10;

}

}

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

Share this post


Link to post
Share on other sites
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()
{[indent]
direction ? k += 10 : k -= 10;
if(k<(-380))
{
[/indent][indent][indent] k = -380;[/indent][indent] direction = true;[/indent][/indent][indent]}
else if (k>380)
{
[/indent][indent][indent] k = 380[/indent][indent] direction = false;[/indent][/indent][indent]}
glRectf(375.0+k, 0.0, 520.0+k, 0.0);[/indent]
}

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this