Computer Science Canada

Delaying

Author:  pers2981 [ Thu Dec 01, 2011 9:42 am ]
Post subject:  Delaying

What is it you are trying to achieve?
I'm trying to create a wave based "castle defense" game were your job is to survive endless waves of increasing enemies who charge the castle. I managed to spawn the baddies but they are all stacked on top of each other. (Run to see waht I mean)


What is the problem you are having?
The bad guys spawn on either the left side or the right. Their X/Y cords are all the same so they are "stacked"

Describe what you have tried to solve this problem
I have tryed adding a delay in the for loop but that didnt really help. I also tryed to subtract a random int from the X but that just glitched it.


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
Field.bmp can be found here. Posted Image, might have been reduced in size. Click Image to view fullscreen.

Uploaded with ImageShack.us

Turing:

setscreen ("graphics:640;480,nobuttonbar,offscreenonly")
View.Set ("title:Defend Your Castle")
/*
 Draw.FillBox (0, 0, 650, 100, grey)
 Draw.ThickLine (600, 0, 600, 100, 5, darkgrey)
 Draw.Line (100, 350, 550, 350, black)
 Draw.FillOval (240, 220, 5, 5, white)
 */

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
var Field := Pic.FileNew ("Field.bmp")
var TotalE : int
var Round : int := 1
var clocks : int := 0
var i : int := 0
var newround : boolean := true
var randints : int := 0
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
var EnemeyCordsX : array 1 .. 100 of int
var EnemeyCordsY : array 1 .. 100 of int
var ESide : array 1 .. 100 of int

proc EnemyCreate
if newround = true then
    for i : 1 .. TotalE
        ESide (i) := Rand.Int (1, 2)
    end for

    for i : 1 .. TotalE

        if ESide (i) = 1 then
            EnemeyCordsX (i) := 0
            EnemeyCordsY (i) := 99
        end if

        if ESide (i) = 2 then
            EnemeyCordsX (i) := maxx
            EnemeyCordsY (i) := 99
        end if
    end for
end if
end EnemyCreate
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
proc EnemyMove
clocks := clocks + 1
if clocks > 15 then

end if
    for i : 1 .. TotalE
        if EnemeyCordsX (i) < 220 then
            EnemeyCordsX (i) := EnemeyCordsX (i) + 1
        end if

        if EnemeyCordsX (i) > 395 then
            EnemeyCordsX (i) := EnemeyCordsX (i) - 1
        end if
        put i, " ", EnemeyCordsX (i)
    end for
   
end EnemyMove
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
proc Update
    TotalE := Round * 5
end Update
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
proc DrawField
    Pic.Draw (Field, 0, 0, picMerge)
    put "Level: ", Round, "" : 50, " Bad guys left : ", TotalE
    for i : 1 .. TotalE
        Draw.FillBox (EnemeyCordsX (i), EnemeyCordsY (i), EnemeyCordsX (i) + 25, EnemeyCordsY (i) + 50, red)
    end for
    put clocks
end DrawField
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Update
EnemyCreate

loop
    Update
    DrawField
    EnemyMove
    View.Update
    delay (100)
    cls
end loop
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



BooHoo

Please specify what version of Turing you are using
4.1.1[/quote]

Author:  Raknarg [ Thu Dec 01, 2011 3:06 pm ]
Post subject:  RE:Delaying

There are a couple things you should do:

1. Spawn them at different times.

What you are doing here is creating 5 enemies with the exact same coordinates. Instead, you should spawn them at different times using something like a counter:
Turing:

loop
counter += 1
if counter > (arbitrary number) then
     create_enemy
end if
end loop

That way they wont be stacked.

2. Give every enemy a random, but constant velocity.

Instead of adding Rand.Int (3, 5) to the x coordinate, you can give each enemy a vx (aka a velocity for the x coordinate) when you create them. Just make an array, and when you spawn an enemy, have the vx set to a random number.

Turing:

proc spawn_enemy
     x := (some number)
     y := (some number)
     vx := Rand.Int (3, 5)
end spawn_enemy

Author:  pers2981 [ Tue Dec 06, 2011 8:39 am ]
Post subject:  Re: RE:Delaying

Raknarg @ Thu Dec 01, 2011 3:06 pm wrote:
There are a couple things you should do:

1. Spawn them at different times.

What you are doing here is creating 5 enemies with the exact same coordinates. Instead, you should spawn them at different times using something like a counter:
Turing:

loop
counter += 1
if counter > (arbitrary number) then
     create_enemy
end if
end loop

That way they wont be stacked.

Spawning the baddies at differant times would be the best option for my project. I have attempted to add a timer but I am having troubles because of the way my game is coded. If I were to do.
Turing:

if counter > (arbitrary number) then
     create_enemy
end if

Then I would need create_enemy to only create one enemy at a time which would be hard due to the fact that I have it setup to make all of them at once.

Any help?

Author:  programmer1337 [ Tue Dec 06, 2011 10:11 am ]
Post subject:  Re: Delaying

tbh i wouldnt spawn them using delaying because that would make the whole form lag, i would count the frames that go by and then depending on the frame just spawn them ^^

Author:  Dreadnought [ Tue Dec 06, 2011 3:32 pm ]
Post subject:  Re: Delaying

pers2981 wrote:

Then I would need create_enemy to only create one enemy at a time which would be hard due to the fact that I have it setup to make all of them at once.

I think you can't do what you want with a procedure that spawns all the enemies at once. Luckily, you can easily change your procedure to spawn only one enemy at a time.

Then you can count time (or frames like other posts suggest) to decide when to spawn enemies.

Here is an example how one might go about doing this:
Turing:
loop

    % Draw the background and update enemies

    if enemiesToBeSpawned > 0 and Time.Elapsed > spawnTime then % Check if we need to spawn another enemy
   
        % Create an enemy
       
        enemiesToBeSpawned -= 1 % Reduce the number of enemies that we still need to spawn
        spawnTime := Time.Elapsed + Rand.Int (Min, Max) % Set the time at which the next one will spawn
    end if

    View.Update
    Time.Delay (100)
end loop


Also, setscreen and View.Set are identical in their functionality. So you could also write

View.Set ("graphics:640;480,nobuttonbar,offscreenonly,title:Defend Your Castle")

Author:  mirhagk [ Tue Dec 06, 2011 4:05 pm ]
Post subject:  RE:Delaying

Actually you can use your procedure, basically the enemies are at the edges of the screen and move inwards right? So simply have the first one spawn right at the edge, then each one after it spawn just a little beyond. That way it will appear as if they are coming in later.

Author:  Dreadnought [ Tue Dec 06, 2011 4:41 pm ]
Post subject:  Re: Delaying

Ya mirhagk is right you could do that too. My mistake.


: