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

Username:   Password: 
 RegisterRegister   
 sparkle/explosion thing
Index -> Programming, Turing -> Turing Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
shoobyman




PostPosted: Fri Jan 12, 2007 8:44 pm   Post subject: sparkle/explosion thing

I made this sparkle effect for my space shooter (it was for an explosion effect). I will be posting the full game up in a couple of weeks. Although it is for my shooter, it is still a cool effect on it's own. There is some gravity somewhere in there.

code:
View.Set ("offscreenonly")
colourback (black)
var sparkx, sparky, velx, vely : flexible array 1 .. 0 of real
var explx, exply, duration, ctr : flexible array 1 .. 0 of int
var x, y, button : int
var sparkdelete, expldelete, sparknum : int := 0

proc create_explosion
    new explx, upper (explx) + 1
    new exply, upper (exply) + 1
    new duration, upper (duration) + 1
    new ctr, upper (ctr) + 1
    explx (upper (explx)) := x
    exply (upper (exply)) := y
    duration (upper (duration)) := 10
    ctr (upper (ctr)) := 0
    sparknum := 3
end create_explosion

loop
    mousewhere (x, y, button)
    View.Update
    Time.DelaySinceLast (30)
    cls
    for i : 1 .. upper (explx)
        for pppp : 1 .. sparknum
            new sparkx, upper (sparkx) + 1
            new sparky, upper (sparky) + 1
            new velx, upper (velx) + 1
            new vely, upper (vely) + 1
            sparkx (upper (sparkx)) := explx (i)
            sparky (upper (sparky)) := exply (i)
            velx (upper (velx)) := (Rand.Int (-5, 5) * Rand.Real)
            vely (upper (vely)) := (Rand.Int (-5, 5) * Rand.Real)
        end for
        Text.Colour (0)
        ctr (i) += 1
        if ctr (i) >= duration (i) then
            expldelete := i
        end if
    end for
    %%%% DELETING EXPLOSIONS %%%%
    if expldelete > 0 then
        for i : expldelete + 1 .. upper (explx)
            explx (i - 1) := explx (i)
            exply (i - 1) := exply (i)
            duration (i - 1) := duration (i)
            ctr (i - 1) := ctr (i)
        end for
        new explx, upper (explx) - 1
        new exply, upper (exply) - 1
        new duration, upper (duration) - 1
        new ctr, upper (ctr) - 1
        expldelete := 0
    end if
    if button = 1 then
        create_explosion
    end if
    put upper (sparkx)
    for i : 1 .. upper (sparkx)
        sparkx (i) += velx (i)
        sparky (i) += vely (i)
        drawdot (round (sparkx (i)), round (sparky (i)), 0)
        vely (i) -= 0.607
        if sparkx (i) < 0 or sparkx (i) > maxx or sparky (i) < 0 or sparky (i) > maxy then
            sparkdelete := i
        end if
    end for
    %%%% DELETING SPARKLES %%%%
    if sparkdelete > 0 then
        for i : sparkdelete + 1 .. upper (sparkx)
            sparkx (i - 1) := sparkx (i)
            sparky (i - 1) := sparky (i)
            velx (i - 1) := velx (i)
            vely (i - 1) := vely (i)
        end for
        new sparkx, upper (sparkx) - 1
        new sparky, upper (sparky) - 1
        new velx, upper (velx) - 1
        new vely, upper (vely) - 1
        sparkdelete := 0
    end if
end loop
Sponsor
Sponsor
Sponsor
sponsor
CodeMonkey2000




PostPosted: Fri Jan 12, 2007 10:17 pm   Post subject: RE:sparkle/explosion thing

Pretty cool. Only problem is that the sparkles should delete it self as soon as it gets off screen. Or have a limit on the amount of sparkles that apper on screen, it really lags on my comp.
Saad85




PostPosted: Mon Jan 15, 2007 1:00 am   Post subject: Re: sparkle/explosion thing

the lag factor needs to be cut down. a good way to do it is to make a procedure to kill them as soon as they go off screen. then, make a function to either return the lowest dead element in the array, or, if they are all full, make a new element and then return that.

then, make the new sparkle in the returned value.

also, check to see if the last element in the array is dead. if so, get rid of it.


that's what i did in the game im making, which you can check out here
http://www.compsci.ca/v3/viewtopic.php?t=14869

here's the specific code im referring to

code:

% this function will return the lowest usable value in the array

function shotchecker : int            %it's a function
    for i : 1 .. upper (shot)            %starts at the bottom of the array, works its way up
        if shot (i).alive = false then  %checks to see if it's alive.
            result i                           %if not, it returns the value. the function stops here, the next lines are never carried out
        end if
    end for


    new shot, upper (shot) + 1       % ok, so if it got here, that means the array is full. so it adds another element to the array
    result upper (shot)                   % then results that value
end shotchecker



because it's a function, it returns a value. that means you can then do something like this:

code:

%this is a cutout of a procedure in my game

procedure makeshot (givenangle : int, giventype : int, givenx : real, giveny : real) 

    p := shotchecker %a variable (in this case, p) can be set to the returned value.

    shot (p).x := givenx         %you can then use that variable as subscript, and do whatever you want.
    shot (p).y := giveny         %in your case, you would want to set the sparkle's x, y, velocities, and whatever else.


etc



this way, you can cut down on adding to the array when it's not necessary. should improve the fps greatly
agnivohneb




PostPosted: Mon Jan 22, 2007 10:48 am   Post subject: RE:sparkle/explosion thing

Very lagy. Should find a way to fix it
Barloq




PostPosted: Tue Jan 23, 2007 1:50 pm   Post subject: Re: sparkle/explosion thing

Really laggy, but it is really cool!
Display posts from previous:   
   Index -> Programming, Turing -> Turing Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: