#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# calling a procedure once in a loop

Member Posts: 44
I have a simple game with two tanks, one moves around and you controll the other. I'm trying to make the human controlled tank fire but there's one thing i can't figure out. In order for the missile to start from the human tank co-ordinates I have two integers which are in the main loop which are set equal to the tanks x,y, co-ords. This is the problem, when I call the fire routine I call it using a boolean from the main loop and the idea is that everytime the main loop is refreshed, so is everything else (including the missile when it is called) however, this means that i can't controll the co-ords of the missile because everytime the loop is run, they are refreshed and set back to the co-ords of the human tank.

Here is most of the relevant code (it's a little sloppy):

[code]
repeat
tiles; {this builds the level}

if right then {this moves the comp player}
begin
inc(right_num);
if right_num = 160 then
begin
upp:=true;
downn:=false;
right:=false;
left:=false;
right_num:=0;
end;
end;

if left then {this moves the comp player}
begin
inc(left_num);
if left_num = 160 then
begin
upp:=false;
right:=false;
left:=false;
downn:=true;
left_num:=0;
end;
end;

if upp then {this moves the comp player}
begin
inc(up_num);
if up_num = 160 then
begin
left:=true;
right:=false;
upp:=false;
downn:=false;
up_num:=0;
end;
end;

if downn then {this moves the comp player}
begin
inc(down_num);
if down_num = 160 then
begin
right:=true;
downn:=false;
left:=false;
upp:=false;
down_num:=0;
end;
end;

way_points; {this calls the way points for the comp}

if keydown[leftscan]=true then player_left; {These are the controls}
if keydown[rightscan]=true then player_right;
if keydown[upscan]=true then up;
if keydown[downscan]=true then down;
if keydown[escscan]=true then end_game:=true;

if keydown[f1scan]=true then fire:=true; {this sets the fire boolean}
{to true }

if fire then {this procedure takes the co-ords of the human tank and}
begin {passes them to the missile, it also increments a }
inc(fire_num); {number which controls how long the missile fires for }

bullet_x:=x+5; {this is the problem, these two integers are the }
bullet_y:=y+5; {starting point for the missile but are updated even }
{when the missile is "trying" to move }

if fire_num >= 200 then
begin
fire:=false;
fire_num:=0;
end;
end;

bullet; {this procedure checks if fire is true then if it is moves the}
{missile (dosn't work because of above problem) }

waitretrace;
rotatepal;

until end_game;

setoldkeyint;
main_screen;
end;
[/code]

hope you can help.

Beardtrix

• Member Posts: 6,349
: I have a simple game with two tanks, one moves around and you controll the other. I'm trying to make the human controlled tank fire but there's one thing i can't figure out. In order for the missile to start from the human tank co-ordinates I have two integers which are in the main loop which are set equal to the tanks x,y, co-ords. This is the problem, when I call the fire routine I call it using a boolean from the main loop and the idea is that everytime the main loop is refreshed, so is everything else (including the missile when it is called) however, this means that i can't controll the co-ords of the missile because everytime the loop is run, they are refreshed and set back to the co-ords of the human tank.
:
: Here is most of the relevant code (it's a little sloppy):
:
: [code]
: repeat
: tiles; {this builds the level}
:
: if right then {this moves the comp player}
: begin
: inc(right_num);
: if right_num = 160 then
: begin
: upp:=true;
: downn:=false;
: right:=false;
: left:=false;
: right_num:=0;
: end;
: end;
:
: if left then {this moves the comp player}
: begin
: inc(left_num);
: if left_num = 160 then
: begin
: upp:=false;
: right:=false;
: left:=false;
: downn:=true;
: left_num:=0;
: end;
: end;
:
: if upp then {this moves the comp player}
: begin
: inc(up_num);
: if up_num = 160 then
: begin
: left:=true;
: right:=false;
: upp:=false;
: downn:=false;
: up_num:=0;
: end;
: end;
:
: if downn then {this moves the comp player}
: begin
: inc(down_num);
: if down_num = 160 then
: begin
: right:=true;
: downn:=false;
: left:=false;
: upp:=false;
: down_num:=0;
: end;
: end;
:
: way_points; {this calls the way points for the comp}
:
: if keydown[leftscan]=true then player_left; {These are the controls}
: if keydown[rightscan]=true then player_right;
: if keydown[upscan]=true then up;
: if keydown[downscan]=true then down;
: if keydown[escscan]=true then end_game:=true;
:
: if keydown[f1scan]=true then fire:=true; {this sets the fire boolean}
: {to true }
:
: if fire then {this procedure takes the co-ords of the human tank and}
: begin {passes them to the missile, it also increments a }
: inc(fire_num); {number which controls how long the missile fires for }
:
: bullet_x:=x+5; {this is the problem, these two integers are the }
: bullet_y:=y+5; {starting point for the missile but are updated even }
: {when the missile is "trying" to move }
:
: if fire_num >= 200 then
: begin
: fire:=false;
: fire_num:=0;
: end;
: end;
:
: bullet; {this procedure checks if fire is true then if it is moves the}
: {missile (dosn't work because of above problem) }
:
:
: waitretrace;
: rotatepal;
:
: until end_game;
:
: setoldkeyint;
: main_screen;
: end;
: [/code]
:
: hope you can help.
:
: Beardtrix
:
You need to check if the fire_num is 1 before setting the start point. Something like this:
[code]
: if fire then
: begin
: inc(fire_num);
[red] if fire_num = 1 then begin [/red]
:
: bullet_x:=x+5;
: bullet_y:=y+5;
[red]end;[/red]
[/code]
Now the bullet_x and bullet_y are only set when the bullet appear for the first time. Otherwise nothing happens.
• USAMember Posts: 4,624 ✭✭

_______ ( http://forcoder.org ) free ebooks and video tutorials about \ C, Ruby, Visual Basic, Objective-C, PL/SQL, Assembly, Python, PHP, Delphi, Scratch, C#, R, JavaScript, Go, Perl, Java, C++, Swift, Visual Basic .NET, MATLAB Logo, Clojure, Rust, Lisp, Prolog, Awk, COBOL, D, Kotlin, LabVIEW, Fortran, Julia, Bash, ML, Crystal, Hack, SAS, Apex, Scala, Dart, Transact-SQL, ABAP, Alice, Scheme, Lua, FoxPro, Erlang, Ada, F#, VBScript \ ________