Page 1 of 1

Lightning effect using canvas?

PostPosted: Sat May 02, 2015 4:18 pm
by DeltaLeeds
Hello everyone! Glad to see some interesting posts lately including Bat78's newspaper posts which perks me up to what's going on concerning gE's development and new rising game makers like Koala... So onto the topic!

I really want to know if it is possible to make a random lightning effect for gE using canvas... This sounds ridiculous (Especially with my knowledge of programming and stuff.) but is it possible? And if yes, how do I do it? (Tbh, I don't really know how to use canvas except for making health bars and other meters. xD)

Re: Lightning effect using canvas?

PostPosted: Sat May 02, 2015 6:21 pm
by koala
jonathang wrote:Hello everyone! Glad to see some interesting posts lately including Bat78's newspaper posts which perks me up to what's going on concerning gE's development and new rising game makers like Koala... So onto the topic!

I really want to know if it is possible to make a random lightning effect for gE using canvas... This sounds ridiculous (Especially with my knowledge of programming and stuff.) but is it possible? And if yes, how do I do it? (Tbh, I don't really know how to use canvas except for making health bars and other meters. xD)
Hi, jonathang! :D

Just draw a canvas and write this code to it:
Code: Select all
int i, j, k, t; // iterators
int left, right; // number of left and right branches
int leftSection, rightSection; // one section for every branch
int leftI, rightI; // starting position of a branch
int length; // length of a branch


erase(0, 0, 0, 0);

left = 4;
right = 3;
length = 100;
 
leftSection = height / (left + 1);
rightSection = height / (right + 1);


leftI = (int) rand(leftSection);
rightI = (int) rand(rightSection);


// drawing lightning
j = (int) rand(width - width / 5) + width / 10; // x coordinate
for (i = 0; i < height; i++) { // y coordinate
    // main lightning
    setpen(255, 255, 255, 0, 3);
    j = j + rand(3) - 1;
    putpixel(j, i);
 
    // one left branch
    if (i == leftI && left > 0) {
        setpen(255, 255, 255, 0, 2);
        for (k = j, t = leftI; t < leftI + length; t++) {
            k = k + (int) rand(4) - 2;
            putpixel(k, t);
        }
 
        leftI += leftSection;
        left--;
    }
 
    // one right branch
    if (i == rightI && right > 0) {
        setpen(255, 255, 255, 0, 2);
        for (k = j, t = rightI; t < rightI + length; t++) {
            k = k + (int) rand(4) - 1;
            putpixel(k, t);
        }
 
        rightI += rightSection;
        right--;
    }
}
Windows executable
ged and data files

Re: Lightning effect using canvas?

PostPosted: Wed May 06, 2015 12:51 pm
by DeltaLeeds
Sorry for the really late reply! :oops:
Woah! That's amazing! Thanks for the help and even a demo too (+1ed), but can we make it so that the lightning strikes a specific actor inside the canvas?

Re: Lightning effect using canvas?

PostPosted: Wed May 06, 2015 2:46 pm
by koala
jonathang wrote:Sorry for the really late reply! :oops:
Woah! That's amazing! Thanks for the help and even a demo too (+1ed), but can we make it so that the lightning strikes a specific actor inside the canvas?
I am glad you like it! :D Here's what you've asked.
Windows executable
ged and data files
Now you are drawing main lightning from bottom, from player's xscreen coordinate. Side lightnings are still drawn from top to bottom.
Canvas has timer now, which erases lightning and sets normal animation to player.

Re: Lightning effect using canvas?

PostPosted: Thu May 07, 2015 9:06 am
by DeltaLeeds
koala wrote:
jonathang wrote:Sorry for the really late reply! :oops:
Woah! That's amazing! Thanks for the help and even a demo too (+1ed), but can we make it so that the lightning strikes a specific actor inside the canvas?
I am glad you like it! :D Here's what you've asked.
Windows executable
ged and data files
Now you are drawing main lightning from bottom, from player's xscreen coordinate. Side lightnings are still drawn from top to bottom.
Canvas has timer now, which erases lightning and sets normal animation to player.

Ah! So you have to draw the lightning from the player's XSCREEN coordinate.. Lol, I thought it was just the x coordinate, and I've been wondering what I was doing wrong all along!
A really clear explanation and demo! Thanks a million Koala! :D

Re: Lightning effect using canvas?

PostPosted: Thu May 07, 2015 11:21 am
by koala
jonathang wrote:
koala wrote:
jonathang wrote:Sorry for the really late reply! :oops:
Woah! That's amazing! Thanks for the help and even a demo too (+1ed), but can we make it so that the lightning strikes a specific actor inside the canvas?
I am glad you like it! :D Here's what you've asked.
Windows executable
ged and data files
Now you are drawing main lightning from bottom, from player's xscreen coordinate. Side lightnings are still drawn from top to bottom.
Canvas has timer now, which erases lightning and sets normal animation to player.

Ah! So you have to draw the lightning from the player's XSCREEN coordinate.. Lol, I thought it was just the x coordinate, and I've been wondering what I was doing wrong all along!
A really clear explanation and demo! Thanks a million Koala! :D
No problem, Jonathang! :D