Jump to content
  • Advertisement
Sign in to follow this  
phil67rpg

simple routine

This topic is 2570 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 routine that counts the ball variable from 400 to 780.I want to count down to 0 then back up to 780 again.I basically want the ball variable to oscillate from 0 to 780 and then back to 0 again.
here is the code I am working on.
[font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]#include[/font][/font][/font][font="Consolas"][font="Consolas"] [/font][/font][font="Consolas"][color="#a31515"][font="Consolas"][color="#a31515"][font="Consolas"][color="#a31515"]<iostream>

[/font][/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]using[/font][/font][/font][font="Consolas"][font="Consolas"] [/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]namespace[/font][/font][/font][font="Consolas"][font="Consolas"] std;

[/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]int[/font][/font][/font][font="Consolas"][font="Consolas"] ball,k=0;

[/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]int[/font][/font][/font][font="Consolas"][font="Consolas"] test=0;

[/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]int[/font][/font][/font][font="Consolas"][font="Consolas"] main()

{

[/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]while[/font][/font][/font][font="Consolas"][font="Consolas"](ball<=779)

{

ball=400+k;

cout << ball;

[/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++;

}

[/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>=0)

{

k--;

}

}

cin >> test;

[/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]return[/font][/font][/font][font="Consolas"][font="Consolas"] 0;

}




[/font][/font]

Share this post


Link to post
Share on other sites
Advertisement

I basically want the ball variable to oscillate from 0 to 780 and then back to 0 again.


So what is the 400 part for, then? Because if it's not needed in the solution, you can do this with a handful of lines.

Share this post


Link to post
Share on other sites
int ball = 400;
int i = 1;

for(;;)
{
if(ball == 0) i = 1;
else if(ball == 780) i = -1;
ball += i;
}

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!