Computer Science Canada

Movement and shooting

Author:  chaos [ Fri Dec 17, 2010 9:12 pm ]
Post subject:  Movement and shooting

I need some help.

I'm creating a game cinsisting of shooting people using text graphics.
But, the problem is that whenever the bullet is shot, the player is frozen, until the bullet stops or leaves the screen.
How can I move the player while the bullet is moving?

Author:  TokenHerbz [ Fri Dec 17, 2010 9:28 pm ]
Post subject:  RE:Movement and shooting

take the bullet out of its own loop and put it inside a fnc or proc to run the same time as the person.

Author:  Insectoid [ Fri Dec 17, 2010 10:00 pm ]
Post subject:  RE:Movement and shooting

This is a common question and as such has many, many solutions (almost all of which are the same) posted around the Turing board.

Author:  chaos [ Fri Dec 17, 2010 11:15 pm ]
Post subject:  Re: Movement and shooting

@tokenherbz
Are you saying that i should put the buullets in a separate loop
Please clarify.

Author:  TokenHerbz [ Sat Dec 18, 2010 12:08 am ]
Post subject:  RE:Movement and shooting

No, I'm saying you need to take it out of its own loop, is what i think the problem is. however, i could be of more assistance with code.

Author:  TheGuardian001 [ Sat Dec 18, 2010 12:52 am ]
Post subject:  Re: Movement and shooting

If you put the bullet in its own loop, nothing outside of that loop will run until the bullet is done. IE,
WRONG:
code:

loop
    if (player moving)
        move player
    end if
    if (bullet shot)
        loop %we don't leave this till the bullet is done. Everything else stops.
            move bullet
            draw bullet
        end loop
    end if
   draw player
end loop

The player moves, then the bullet moves, then the bullet moves, then the bullet moves, etc. The loop prevents the (player moving) statement from being run.

RIGHT:
code:

%Main loop. Contains EVERYTHING that needs to be repeated (NO LOOPS INSIDE THIS ONE!)
loop
    if (player moving)
        move player ONCE
    end if
    if (bullet moving)
        move bullet ONCE
    end if

    draw player
    draw bullet

    update screen
end loop.

The player moves, then the bullet moves, then the player moves, then the bullet moves, etc. They take turns, instead of blocking each other.

Author:  chaos [ Thu Dec 23, 2010 5:28 pm ]
Post subject:  Re: Movement and shooting

So would it be something like this? :

var y1 : int := 10
var x1 : int := 10
var y2 : int := 20
var x2 : int := 20
var count : int := 1
var chars : array char of boolean

loop
Input.KeyDown (chars)

if chars (KEY_UP_ARROW) then
y1 := y1 - 1
if y1 < 1 then
y1 := 1
end if
end if
if chars (KEY_RIGHT_ARROW) then
x1 := x1 + 1
if x1 > 80 then
x1 := 80
end if
end if
if chars (KEY_LEFT_ARROW) then
x1 := x1 - 1
if x1 < 1 then
x1 := 1
end if
end if
if chars (KEY_DOWN_ARROW) then
y1 := y1 + 1
if y1 > 24 then
y1 := 24
end if
end if
if chars (' ') and chars (KEY_RIGHT_ARROW) then
count:=1
for limitx : 1 .. 80-x1
count := count + 1
if x1+count>80 then
count:=1
end if
locate (y1, x1 + count)
put "." ..
delay(50)
cls
end for
end if
locate (y1, x1)
put "A" ..
delay(50)
cls

end loop

Author:  Insectoid [ Thu Dec 23, 2010 5:45 pm ]
Post subject:  RE:Movement and shooting

You have 2 delays in there. That's bad. Also, I don't know why you have a for loop. Try using code tags to format your code so I can be bothered to actually read it instead of just skimming over it.

Author:  chaos [ Thu Dec 23, 2010 7:15 pm ]
Post subject:  Re: Movement and shooting

Hers's the code with tags:

%VARIABLE DECLARATION
var y1 : int := 10
var x1 : int := 10
var y2 : int := 20
var x2 : int := 20
var count : int := 1
var chars : array char of boolean

loop
Input.KeyDown (chars)

%CONTROLS FOR MOVEMENT
if chars (KEY_UP_ARROW) then %MOVING UP
y1 := y1 - 1
if y1 < 1 then %BORDER PARAMETERS FOR MOVING UP
y1 := 1
end if
end if

if chars (KEY_RIGHT_ARROW) then %MOVING RIGHT
x1 := x1 + 1
if x1 > 80 then %BORDER PARAMETERS FOR MOVING RIGHT
x1 := 80
end if
end if

if chars (KEY_LEFT_ARROW) then %MOVING LEFT
x1 := x1 - 1
if x1 < 1 then %BORDER PARAMETERS FOR MOVING LEFT
x1 := 1
end if
end if

if chars (KEY_DOWN_ARROW) then %MOVING DOWN
y1 := y1 + 1
if y1 > 24 then %BORDER PARAMETERS FOR MOVING DOWN
y1 := 24
end if
end if

if chars (' ') and chars (KEY_RIGHT_ARROW) then %PLAYER SHOOTING
count:=1
for limitx : 1 .. 80-x1 %FOR LOOP IS USED TO MOVE BULLET ENTIRELY WHEN CONTROL IS PRESSED ONCE
count := count + 1
if x1+count>80 then %BORDER PARAMETERS FOR BULLET
count:=1
end if
locate (y1, x1 + count) %LOCATE BULLET
put "." .. %OUTPUT BULLET
delay(50)
cls
end for
end if
locate (y1, x1) %LOCATE PLAYER
put "A" .. %OUTPUT PLAYER
delay(50)
cls

end loop

Author:  Insectoid [ Thu Dec 23, 2010 7:58 pm ]
Post subject:  RE:Movement and shooting

I think you're confusing comments with tags. Syntax tags highlight your code and preserve formatting. example:

code:
[syntax="turing"]
*code here*
[/syntax]


If you do this, you get code like this
Turing:

put "This is highlighted!"
loop
    put "This is indented! OMG!"
end loop

Author:  ProgrammingFun [ Thu Dec 23, 2010 8:18 pm ]
Post subject:  RE:Movement and shooting

You could always do it for the person:
Turing:

%VARIABLE DECLARATION
var y1 : int := 10
var x1 : int := 10
var y2 : int := 20
var x2 : int := 20
var count : int := 1
var chars : array char of boolean

loop
    Input.KeyDown (chars)

    %CONTROLS FOR MOVEMENT
    if chars (KEY_UP_ARROW) then %MOVING UP
        y1 := y1 - 1
        if y1 < 1 then %BORDER PARAMETERS FOR MOVING UP
            y1 := 1
        end if
    end if

    if chars (KEY_RIGHT_ARROW) then %MOVING RIGHT
        x1 := x1 + 1
        if x1 > 80 then %BORDER PARAMETERS FOR MOVING RIGHT
            x1 := 80
        end if
    end if

    if chars (KEY_LEFT_ARROW) then %MOVING LEFT
        x1 := x1 - 1
        if x1 < 1 then %BORDER PARAMETERS FOR MOVING LEFT
            x1 := 1
        end if
    end if

    if chars (KEY_DOWN_ARROW) then %MOVING DOWN
        y1 := y1 + 1
        if y1 > 24 then %BORDER PARAMETERS FOR MOVING DOWN
            y1 := 24
        end if
    end if

    if chars (' ') and chars (KEY_RIGHT_ARROW) then %PLAYER SHOOTING
        count := 1
        for limitx : 1 .. 80 - x1 %FOR LOOP IS USED TO MOVE BULLET ENTIRELY WHEN CONTROL IS PRESSED ONCE
            count := count + 1
            if x1 + count > 80 then %BORDER PARAMETERS FOR BULLET
                count := 1
            end if
            locate (y1, x1 + count) %LOCATE BULLET
            put "." .. %OUTPUT BULLET
            delay (50)
            cls
        end for
    end if
    locate (y1, x1) %LOCATE PLAYER
    put "A" .. %OUTPUT PLAYER
    delay (50)
    cls

end loop

But Insectoid does have a point...from now on, please try to use the syntax tags so that your code is easier to comprehend for those who might want to help you out...

Author:  chaos [ Thu Dec 23, 2010 9:05 pm ]
Post subject:  Re: Movement and shooting

so based on the code i gave, how would i go about moving the player and the bullet at the same time?

Author:  TokenHerbz [ Thu Dec 23, 2010 9:49 pm ]
Post subject:  RE:Movement and shooting

your for loop which moves the bullet is wrong.

what you are doing is, "DO ALL OF THIS LOOP NOW BEFORE WE GO BACK TO MAIN LOOP"

what you want to do, is move the BULLET WHILE going back to the main loop.

you want to use the for loop to just move it by a vector at a time then if statement to check to see if its passed boundries and time to remove it from the program.

Author:  chaos [ Thu Dec 23, 2010 10:36 pm ]
Post subject:  Re: Movement and shooting

could u show me what u mean using code please?

Author:  SS1389 [ Thu Dec 23, 2010 10:51 pm ]
Post subject:  Re: Movement and shooting

BooHoo

Author:  chaos [ Fri Dec 24, 2010 8:13 am ]
Post subject:  Re: Movement and shooting

???????

Author:  TokenHerbz [ Fri Dec 24, 2010 12:31 pm ]
Post subject:  Re: Movement and shooting

@SS1389: please don't spam.

@chaos: Sure I'll make you a quick example to show you how it works.

Turing:

var box_x, box_y : int := 100
var box_s : int := 5
type Bullet :   %%if you don't understand these 6 lines of code, check out F10 in turing.
    record       %%essentially we make a type (bullet) with its own variables.
        x, y : int  %so when we make the array at the bottom, we can use ALL THESE VARS
        s : int   %instead of making seperate arrays of each variable.
    end record          %%use like //  bullet(array slot).type VARIBLE
var bullet : flexible array 1 .. 0 of Bullet
var keys : array char of boolean
%For this test, its to show you how to move both (user) and (bullet) at the same time

View.Set ("Gpraphics:400;400,offscreenonly") %offscreenonly lets smoother draw
%by storing all outputs in buffer, and drawing all at once with View.Update

loop %this is the mail loop your program with run inside.
    Input.KeyDown (keys)
    cls

    %so if the key is up, we move box up (Y axis up @ box speed)
    %and same with going down, these share an IF since only 1 at a time.
    %for this demo, we will only allow up and down movements and shoot RIGHT.
    if keys (KEY_DOWN_ARROW) then
        box_y -= box_s
    elsif keys (KEY_UP_ARROW) then
        box_y += box_s
    end if
    %here we shoot the bullet RIGHT. since i don't want you to get all free answers
    %F10 flexible arrays and types.  Figure how to "REMOVE" a bullet array when its done
    %instead of the array keep growing (which is how i set it (on purpose mind you)).
    if keys (KEY_RIGHT_ARROW) then
        new bullet, upper (bullet) + 1 %%we make a new array element, new bullet(array), @ place upper bullet (top most array) + 1
        bullet (upper (bullet)).x := box_x %we assign our bullet the same variables as our BOX it shoots from (created it)
        bullet (upper (bullet)).y := box_y
        bullet (upper (bullet)).s := 5
    end if

    %Now even tho the user needs INPUT TO MOVE, the bullet DOES NOT. it moves if its CREATED, until destination when its REMOVED.
    for check_bullet : 1 .. upper (bullet) %we make a flexible for loop, that counts from 1 to the MOST NUMBER OF ARRAY BULLETS. (can be even ZERO)
        bullet (check_bullet).x += bullet (check_bullet).s %like i said we move it just right for this example, You can easily make bullet shoot
    end for                                                 %any direction, use vectors, etc.  just add a bit more logic for the code to use.

    %now since we have our basics, (player box moving around) and bullet creating and moving also.. Lets draw these to see them.
    Draw.FillBox (box_x, box_y, box_x + 5, box_y + 5, 7) %should use box sizes vars instaed of a hardcoding "5" imo but for this demo.. lol
    for i : 1 .. upper (bullet) %yes, using a simple i instead of bullet_check i be lazy now :P
        Draw.FillBox (bullet (i).x, bullet (i).y, bullet (i).x + 1, bullet (i).y + 1, red)
    end for

    %%Now try to understand the concept of this.  We draw the bullets, we move the bullets, we move the box, etc all in ONE LOOP.
    %%We don't draw and move the bullets in its own loop inside the main loop.   Heres something else to figure out.
    %%Get it do when the bullets pass the right side of the screen, its removed from the flexible array.
    %after you figure that out, Your ready to start practicing with this type/ flex array codeing. very fun stuff.
    put "Flexible Array Amount: ", upper (bullet)

    View.Update  %takes all our drawings in the buffer and draws it at the same time.
    delay (50)
end loop

%%Please ask for more help if you need it! Hope this helps you out a bit. I hope i did a good job expalining things.

Author:  SS1389 [ Fri Dec 24, 2010 2:14 pm ]
Post subject:  Re: Movement and shooting

Sorry. Won't happen again.

Author:  chaos [ Mon Dec 27, 2010 4:33 pm ]
Post subject:  Re: Movement and shooting

@Tokenherbz: I am amazed by the results, but i don not understand the "type" and "record" syntax. I checked F10 in turing but they don't help, could u explain what this means? Or, is there a way to do this with text graphics?

Author:  Tony [ Mon Dec 27, 2010 4:46 pm ]
Post subject:  RE:Movement and shooting

Tutorial on records from The Turing Walkthrough

Author:  chaos [ Tue Dec 28, 2010 12:07 am ]
Post subject:  Re: Movement and shooting

@tokenherbz: could u show me this bullet movement using text graphics using the code i already gave before in this topic ?

Author:  TokenHerbz [ Tue Dec 28, 2010 9:49 am ]
Post subject:  RE:Movement and shooting

There is more then enough info inside this thread to make your program work.

Your drawing your bullet in it's own loop so that the main loop (which moves/does everything else like your "A" player) isn't executed. To get everything to work, you need to take that bullet loop out.

You said it yourself inside your own program with comments: "FOR LOOP IS USED TO MOVE BULLET ENTIRELY WHEN CONTROL IS PRESSED ONCE"

Now, then, First lets start with the shooting.
Why not have to shoot with just space.
Turing:

if chars (' ') then  %space to shoot, not space+right arrow key


and like we said many many times above, what your program does is this

code:

loop
    player input
    if input is shoot bullet then
        loop   ----> THIS IS YOUR FOR LOOP!
            draw bullet
            move bullet
            if bullet hits destination then
                leave this loop to main program
            end if
            %%%%%%%%AS YOU CAN SEE WE ARE STUCK IN THIS LOOP UNTIL BULLET IS DONE ITS PATH = BAD
        end loop
    end if
    draw player
end loop


Now thats how your program works, this is how it SHOULD WORK, so re-read these posts here, and check out the link in turing walk thru, IF YOU POST NEW CODE (that shows effort to what we suggested) then we'll assist more.
code:

loop
    get player input
    if input is shoot
        then create the bullet
    end if
    if bullet excists then
        move bullet
        if bullet reaches destination then
            remove bullet
        end if
    end if
    draw bullet
    draw player
end loop

Author:  chaos [ Tue Dec 28, 2010 2:33 pm ]
Post subject:  Re: Movement and shooting

Here is my code. It is an attachment.

Author:  ProgrammingFun [ Tue Dec 28, 2010 3:50 pm ]
Post subject:  RE:Movement and shooting

I tried your code...there are some things I noticed (I do not know if they have been previously mentioned):
    1. Your program is very flickery (try to use View.set ("offscreenonly") )

    2. Your program crashes if the letter goes off the screen. Try to fix that by either looping the letter around the other side or restricting it from going off the screen.

    3. I did not see anything to shoot but I guess you are working on that (or have not released it in this build) Razz


Other than that, it's a good program. Smile

Author:  chaos [ Tue Dec 28, 2010 9:51 pm ]
Post subject:  Re: Movement and shooting

@ProgrammingFun: To shoot , you hit the spacebar.

Author:  ProgrammingFun [ Tue Dec 28, 2010 10:01 pm ]
Post subject:  Re: Movement and shooting

chaos @ Tue Dec 28, 2010 9:51 pm wrote:
@ProgrammingFun: To shoot , you hit the spacebar.

Perhaps you misunderstood me....I meant to say that there is nothing which should be shot (you know...as in enemies etc)...

Author:  chaos [ Tue Dec 28, 2010 10:05 pm ]
Post subject:  Re: Movement and shooting

I have listed my code above. I am just wondering if it is as Tokenherbz described??

Could someone help me please??

@Tokenherbz: When you made the post about how my program should be, within it you stated that "if the bullet exists". I do not understand what this means, could u explain it to me or give me the code to it please?

Author:  TheGuardian001 [ Tue Dec 28, 2010 11:14 pm ]
Post subject:  Re: Movement and shooting

by exists, he means as a part of the world.

the bullet always "exists" as a variable, but in this case "exists" means that it has been fired but has not yet hit a target (meaning you still need to do stuff with it.)

You can represent this (if you want) in the form of a boolean variable. If the variable is true, the bullet "exists" and we need to move it, draw it, and check for collisions. If it's false, we ignore that bullet. If the bullet hits something, we set that variable to false, and the bullet no longer "exists".

Author:  chaos [ Wed Dec 29, 2010 1:40 pm ]
Post subject:  Re: Movement and shooting

Can someone help me. I have given my code below in an attachment. The problem is that the program stops at random times. I can't seem to find the problem.

Author:  TokenHerbz [ Wed Dec 29, 2010 2:54 pm ]
Post subject:  Re: Movement and shooting

Turing:

%%in cycle of bullet array
if bullet (i).x >= 50 then
%if bullet (cycles) X position is GREATER then or EQUIL to 50 then
    for removal : bullet (i).x .. upper (bullet) - 1
    %for removal: bullet (that bullet).x (which is 50) .. max bullet array - 1
        bullet (i).x := bullet (i + 1).x
        %bullets.x (50) := bullet (i-cycled bullet) + 1
        new bullet, upper (bullet) - 1
        %%change array to remove the max bullet array slot...
    end for
    %%%elsif the bullets X pos is less then 50
else
    locate (bullet (i).y, bullet (i).x)
    %we locate its pos (fyi i hate the use of locate) and draw "-"
    put "-"
end if
%end cycle of bullet array



%%AS you can see, Your code here makes no sense.   So lets write down the
%Theory code, to get an understanding basics to start programming from...

cycle threw the array of bullets
    if bullet hits destination
        remove bullet from its array
        and if needed move other bullets in array
    OTHERWISE
        we move bullet by its speed
        we draw this bullet
    end if
end cycle

%%Also please look at this little example (to help you with your code also)
var test_array : array 1 .. 1 of int
test_array (1) := 50
put test_array (1)

for r : test_array (1) .. upper (test_array) - 1
    %Same as for r: 50 .. 0
    put r
end for
%%if you wanted to have a count down for loop structure is this.
for decreasing r : 5 .. 1
    put r ..
end for
[/syntax]

Author:  chaos [ Wed Dec 29, 2010 3:03 pm ]
Post subject:  Re: Movement and shooting

@Tokenherbz: Is the way of removal in my code okay. If not, could u help me out?

Author:  TokenHerbz [ Wed Dec 29, 2010 5:30 pm ]
Post subject:  RE:Movement and shooting

its not okay, in the first part if you read my "comments" it tells you why your program doesn't work.

post me some code to the logic iv'e also put on that last post and ill assist you more...

Author:  chaos [ Wed Dec 29, 2010 11:19 pm ]
Post subject:  Re: Movement and shooting

Here is the code.


: