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

Username:   Password: 
 RegisterRegister   
 Time.Elapsed vs process
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
CyberStorm




PostPosted: Wed May 04, 2011 4:14 pm   Post subject: Time.Elapsed vs process

What is it you are trying to achieve?
i am making a target practice game, and i got stuck on how to make the reload and fire rate delays work without stopping the whole program by using a regular delay statement


What is the problem you are having?
i have read many discouraging things about using processes, and i cant seem to figure out how to use Time.Elapsed statement, i wonder if there is a tutorial on how to use it in different situations... so my question is: how do i make the timing work without using processes? or is using processes not a big problem in this case and then is there a way to fix the simultaneous execution of both processes glitch in the game?


Describe what you have tried to solve this problem
i tried using processes for the fire rate and the reload, but they go weird if they are both running at the same time, which is achieved by holding down fire button while the gun reloads


Post any relevant code

Turing:


import GUI

var x, y, button, ammo, x1, y1, xtar, ytar, tarsp, stage, dmg, frate, rrate, spreadx, spready, targethp, sprd, font1, targets, wave, font2, winID : int
var gun : string
var xshot, yshot : int
var shoot : boolean
var key : array char of boolean

View.Set ("graphics:300;300,position:top;center")

font1 := Font.New ("stencil:15")
font2 := Font.New ("stencil:12")

ammo := 30
targets := 0
wave := 5
shoot := true
stage := 1
tarsp := 1


process reload
    shoot := false
    delay (rrate)
    ammo := 30
    shoot := true
end reload

process fireRate
    shoot := false
    delay (frate)
    shoot := true
end fireRate

proc run
    winID := Window.Open ("position:top;right,graphics:300;300")
    View.Set ("offscreenonly")

    loop
        randint (x1, 10, maxx - 50)
        randint (y1, 10, maxy - 100)
        xtar := maxx + 30
        ytar := y1
        targethp := 100
        loop
            Input.KeyDown (key)
            Mouse.Where (x, y, button)
            Font.Draw (intstr (ammo), 260, 260, font2, blue)
            Font.Draw (gun, 10, 260, font2, black)
            if xtar not= x1 then
                drawfillbox (xtar, ytar, xtar + 30, ytar + 30, green)
                xtar := xtar - tarsp
            else
                drawfillbox (x1, y1, x1 + 30, y1 + 30, green)
            end if
            Draw.Line (x + 5, y, x + 20, y, black)
            Draw.Line (x, y + 5, x, y + 20, black)
            Draw.Line (x - 5, y, x - 20, y, black)
            Draw.Line (x, y - 5, x, y - 20, black)
            Font.Draw (intstr (targets), 10, 240, defFontID, black)
            View.Update
            cls           

            if button = 1 then
                if shoot = true then
                    ammo := ammo - 1
                    randint (spreadx, x - sprd * 10, x + sprd * 10)
                    randint (spready, y - sprd * 10, y + sprd * 10)
                    xshot := spreadx
                    yshot := spready
                    drawdot (xshot, yshot, black)
                    if xshot >= x1 and xshot <= x1 + 30 and yshot >= y1 and yshot <= y1 + 30 then
                        targethp := targethp - dmg
                        if targethp <= 0 then
                            targets := targets + 1                           
                            exit
                        end if
                    end if
                    fork fireRate
                end if
            end if

            if ammo <= 0 then
                fork reload
            end if

            if key ('r') then
                fork reload
            end if


        end loop
        if targets >= wave then
            Font.Draw (intstr (targets), 10, 240, defFontID, white)
            View.Set ("nooffscreenonly")
            Font.Draw ("Stage Cleared", 70, 150, font1, green)
            View.Set ("offscreenonly")
            exit
        end if
    end loop
end run

proc m4
    sprd := 1
    dmg := 35
    frate := 150
    rrate := 3000
    gun := "M4"
    run
end m4

proc uzi
    sprd := 2
    dmg := 25
    frate := 100
    rrate := 1000
    gun := "UZI"
    run
end uzi


var m4Button := GUI.CreateButton (10, 70, 70, "M4", m4)
var uziButton := GUI.CreateButton (10, 10, 70, "UZI", uzi)
var quitButton := GUI.CreateButton (100, 10, 70, "Quit", GUI.Quit)

Font.Draw ("Choose your weapon", 40, 260, font1, red)

loop
    exit when GUI.ProcessEvent
end loop

GUI.Dispose (quitButton)
GUI.Dispose (uziButton)
GUI.Dispose (m4Button)


Sponsor
Sponsor
Sponsor
sponsor
Raknarg




PostPosted: Wed May 04, 2011 4:23 pm   Post subject: RE:Time.Elapsed vs process

Simply use a counter.
Every time the program runs throught the loop, add one to the counter. When the counter reaches a certain point, you give the the ability to shoot. WHen they shoot, reset the counter. So you could have something like this:
Turing:

if counter > 100 then
     shoot
     counter := 0
end if
CyberStorm




PostPosted: Wed May 04, 2011 5:17 pm   Post subject: Re: Time.Elapsed vs process

so now i have this, but the problem is that when ammo goes down to 0, it reloads like it supposed to, but when i press the "R" button to manually reload, it disables your ability to shoot, but does not reset the ammo nor gives you back the ability to shoot after the set period of time

Turing:


import GUI

var x, y, button, ammo, x1, y1, xtar, ytar, tarsp, stage, dmg, frate, rrate, spreadx, spready, targethp, sprd, font1, targets, wave, font2, winID, count : int
var gun : string
var xshot, yshot : int
var shoot, rld : boolean
var key : array char of boolean

View.Set ("graphics:300;300,position:top;center")

font1 := Font.New ("stencil:15")
font2 := Font.New ("stencil:12")

ammo := 30
targets := 0
wave := 5
shoot := true
stage := 1
tarsp := 1
count := 0
rld := false


/*process reload
 shoot := false
 delay (rrate)
 ammo := 30
 shoot := true
 end reload

 process fireRate
 shoot := false
 delay (frate)
 shoot := true
 end fireRate*/


proc run
    winID := Window.Open ("position:top;right,graphics:300;300")
    View.Set ("offscreenonly")

    loop
        randint (x1, 10, maxx - 50)
        randint (y1, 10, maxy - 100)
        xtar := maxx + 30
        ytar := y1
        targethp := 100
        loop
            count := count + 1
            if count >= frate and rld = false then
                shoot := true
                count := 0
            end if
            Input.KeyDown (key)
            Mouse.Where (x, y, button)
            Font.Draw (intstr (ammo), 260, 260, font2, blue)
            Font.Draw (gun, 10, 260, font2, black)
            if xtar not= x1 then
                drawfillbox (xtar, ytar, xtar + 30, ytar + 30, green)
                xtar := xtar - tarsp
            else
                drawfillbox (x1, y1, x1 + 30, y1 + 30, green)
            end if
            Draw.Line (x + 5, y, x + 20, y, black)
            Draw.Line (x, y + 5, x, y + 20, black)
            Draw.Line (x - 5, y, x - 20, y, black)
            Draw.Line (x, y - 5, x, y - 20, black)
            Font.Draw (intstr (targets), 10, 240, defFontID, black)
            View.Update
            cls
              /*Draw.Line (x + 5, y, x + 20, y, white)
             Draw.Line (x, y + 5, x, y + 20, white)
             Draw.Line (x - 5, y, x - 20, y, white)
             Draw.Line (x, y - 5, x, y - 20, white)
             %Font.Draw (intstr (ammo), 260, 260, font2, white)
             drawfillbox (260, 260, 280, 280, white) */


            if button = 1 then
                if shoot = true then
                    ammo := ammo - 1
                    randint (spreadx, x - sprd * 10, x + sprd * 10)
                    randint (spready, y - sprd * 10, y + sprd * 10)
                    xshot := spreadx
                    yshot := spready
                    drawdot (xshot, yshot, black)
                    if xshot >= x1 and xshot <= x1 + 30 and yshot >= y1 and yshot <= y1 + 30 then
                        targethp := targethp - dmg
                        if targethp <= 0 then
                            targets := targets + 1
                            exit
                        end if
                    end if
                    shoot := false
                end if
            end if

            if ammo <= 0 or key ('r') then
            rld := true
                shoot := false
                if count >= rrate then
                    ammo := 30
                    count := 0
                    shoot := true
                rld := false
                end if               
            end if

            /*if key ('r') then
            rld := true
                shoot := false
                if count >= rrate then
                    ammo := 30
                    count := 0
                    shoot := true
                rld := false
                end if               
            end if*/



        end loop
        if targets >= wave then
            Font.Draw (intstr (targets), 10, 240, defFontID, white)
            View.Set ("nooffscreenonly")
            Font.Draw ("Stage Cleared", 70, 150, font1, green)
            View.Set ("offscreenonly")
            exit
        end if
    end loop
end run

proc m4
    sprd := 1
    dmg := 35
    frate := 150
    rrate := 3000
    gun := "M4"
    run
end m4

proc uzi
    sprd := 2
    dmg := 25
    frate := 100
    rrate := 1000
    gun := "UZI"
    run
end uzi

var m4Button := GUI.CreateButton (10, 70, 70, "M4", m4)
var uziButton := GUI.CreateButton (10, 10, 70, "UZI", uzi)
var quitButton := GUI.CreateButton (100, 10, 70, "Quit", GUI.Quit)

Font.Draw ("Choose your weapon", 40, 260, font1, red)

loop
    exit when GUI.ProcessEvent
end loop

GUI.Dispose (quitButton)
GUI.Dispose (uziButton)
GUI.Dispose (m4Button)


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: