Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Movement and shooting
Index -> Programming, Turing -> Turing Help
Goto page Previous  1, 2, 3  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
chaos




PostPosted: Fri Dec 24, 2010 8:13 am   Post subject: Re: Movement and shooting

???????
Sponsor
Sponsor
Sponsor
sponsor
TokenHerbz




PostPosted: 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.
SS1389




PostPosted: Fri Dec 24, 2010 2:14 pm   Post subject: Re: Movement and shooting

Sorry. Won't happen again.
chaos




PostPosted: 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?
Tony




PostPosted: Mon Dec 27, 2010 4:46 pm   Post subject: RE:Movement and shooting

Tutorial on records from The Turing Walkthrough
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
chaos




PostPosted: 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 ?
TokenHerbz




PostPosted: 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
chaos




PostPosted: Tue Dec 28, 2010 2:33 pm   Post subject: Re: Movement and shooting

Here is my code. It is an attachment.


Tests.t
 Description:

Download
 Filename:  Tests.t
 Filesize:  1.43 KB
 Downloaded:  182 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
ProgrammingFun




PostPosted: 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
chaos




PostPosted: Tue Dec 28, 2010 9:51 pm   Post subject: Re: Movement and shooting

@ProgrammingFun: To shoot , you hit the spacebar.
ProgrammingFun




PostPosted: 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)...
chaos




PostPosted: 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?
TheGuardian001




PostPosted: 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".
chaos




PostPosted: 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.


Tests.t
 Description:

Download
 Filename:  Tests.t
 Filesize:  1.46 KB
 Downloaded:  165 Time(s)

TokenHerbz




PostPosted: 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]
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 2 of 3  [ 33 Posts ]
Goto page Previous  1, 2, 3  Next
Jump to:   


Style:  
Search: