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

Username:   Password: 
 RegisterRegister   
 Need solution - need to shoot at the same time i move
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
pttptppt




PostPosted: Sat Feb 25, 2017 4:41 pm   Post subject: Need solution - need to shoot at the same time i move

What is it you are trying to achieve?
The mouse moves an object, as that object moves, clicking is supposed to shoot.


What is the problem you are having?
<Answer Here>
The object has to pause while the shooting happens because the shooting is in a loop. But i dont know how to fix this and keep the program working.

Describe what you have tried to solve this problem
Ive taken out the "Projectile" part of the code and taken it out of a loop but then nothing happened anymore.


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
Problem is at the "Projectile" procedure, specifically to do with the loop

Turing:


import GUI
View.Set ("graphics:1500;750, offscreenonly")

var x, y, button : int
var xtemp, ytemp : int := 0

%%%%%%%%%%%%%%%
%Turret Spinning:
var keys : array char of boolean
var tx, ty : real := 0

%Length of Turret:
var realx, realy : real := 0

realx := 60
realy := 60

%%%%%%%%%%%%%%%

procedure draw (x, y : int)
    %Scope casing
    Draw.FillOval (x, y, 55, 55, black)
    Draw.FillOval (x, y, 50, 50, white)

    %Thick crosshair threads
    Draw.ThickLine (x - 50, y, x - 35, y, 3, black)
    Draw.ThickLine (x + 50, y, x + 35, y, 3, black)
    Draw.ThickLine (x, y - 50, x, y - 35, 3, black)
    Draw.ThickLine (x, y + 50, x, y + 35, 3, black)

    %Thin threads
    Draw.Line (x - 20, y, x + 20, y, red)
    Draw.Line (x, y - 20, x, y + 20, red)

    %Adds slight delay. Optional
    %delay (100)

end draw

proc rotate
    const SHORTEN := 4
    if keys (KEY_UP_ARROW) then
        tx := realx
        ty := realy
        realx := cosd (1) * tx + sind (1) * ty
        realy := cosd (1) * ty - sind (1) * tx
    end if
    if keys (KEY_DOWN_ARROW) then
        tx := realx
        ty := realy
        realx := cosd (1) * tx - sind (1) * ty
        realy := sind (1) * tx + cosd (1) * ty
    end if
    if realx > 0 then
        realx -= SHORTEN
    elsif realx < 0 then
        realx += SHORTEN
    end if
    if realy > 0 then
        realy -= SHORTEN
    elsif realy < 0 then
        realy += SHORTEN
    end if
    Draw.ThickLine (x, y, x + round (realx), y + round (realy), 10, red)
    if realx > 0 then
        realx += SHORTEN
    elsif realx < 0 then
        realx -= SHORTEN
    end if
    if realy > 0 then
        realy += SHORTEN
    elsif realy < 0 then
        realy -= SHORTEN
    end if
    delay (1)

end rotate


var speed : int := 2
var xfinal, yfinal : int
var xv, yv : int
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
var x1, x2, y1, y2 : int
var run, rise : int
var counter : int
var pastvaluex2, pastvaluey2 : int

proc projectile

    x2 := x2 + (run div 10)
    y2 := y2 + (rise div 10)
    drawfilloval (x2, y2, 7, 7, blue)
    delay (50)

    View.Update

    pastvaluex2 := x2
    pastvaluey2 := y2
    drawfilloval (pastvaluex2, pastvaluey2, 7, 7, white)

    counter += 1
end projectile
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
proc shoot
    counter := 0
    var xtemp, ytemp : int

    Mouse.Where (x1, y1, button)
    x2 := x1 + round (realx)
    y2 := y1 + round (realy)

    run := (x2 - x1)
    rise := (y2 - y1)

    var ball : boolean
    ball := false
    var keys : array char of boolean

    if button > 0 then
        ball := true
    end if
    if ball = true then
        loop
            projectile
            exit when counter = 10
        end loop
    end if

end shoot

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

loop
    Draw.Cls
    Mouse.Where (x, y, button)  %button is a variable that checks if mouse is clicked
    draw (x, y)

    delay (1)
    Input.KeyDown (keys)
    rotate

    shoot
    View.Update

    xtemp := x
    ytemp := y
end loop





%TO DO!!!:
%Make turret shoot




Please specify what version of Turing you are using
Latest
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Sat Feb 25, 2017 7:03 pm   Post subject: RE:Need solution - need to shoot at the same time i move

You need the bullet to be in a loop, or else it will travel for one frame and then stop. But when it's in its own loop, everything else has to pause and wait for the bullet to hit something. So what's the solution?

Put it in a different loop!

Everything that needs to move at the same time should be in the same loop. The code will look more or less like this:

code:

loop
    %move player
    if bullet exists then
        %move bullet
    end if
end loop


You'll probably want more than one bullet on screen at a time though, right? So we'll store all our bullets in an array. If a bullet is fired, we add it to the array. If it hits something, we delete it.

code:

loop
    %move player
    for i : 0..length (bullets)
        %move bullets (i)
    end for
   if bullet fired then
        %add bullet to array
   end if
   
    if bullet hits something then
        %delete bullet from array
    end if
end loop
pttptppt




PostPosted: Sat Feb 25, 2017 7:55 pm   Post subject: Re: RE:Need solution - need to shoot at the same time i move

Insectoid @ Sat Feb 25, 2017 7:03 pm wrote:
You need the bullet to be in a loop, or else it will travel for one frame and then stop. But when it's in its own loop, everything else has to pause and wait for the bullet to hit something. So what's the solution?

Put it in a different loop!

Everything that needs to move at the same time should be in the same loop. The code will look more or less like this:

code:

loop
    %move player
    if bullet exists then
        %move bullet
    end if
end loop


You'll probably want more than one bullet on screen at a time though, right? So we'll store all our bullets in an array. If a bullet is fired, we add it to the array. If it hits something, we delete it.

code:

loop
    %move player
    for i : 0..length (bullets)
        %move bullets (i)
    end for
   if bullet fired then
        %add bullet to array
   end if
   
    if bullet hits something then
        %delete bullet from array
    end if
end loop



I get that, kinda. Can you try showing with an example from my code. Try running my code on Turing and you'll see the problem
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 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: