Computer Science Canada

New Windows Screensaver :P

Author:  Windsurfer [ Mon Jul 03, 2006 8:54 pm ]
Post subject:  New Windows Screensaver :P

Yeah, this was just something fun i made. Just give it a whirl. It's kind of cool, and could be used in some games. Keep in mind, though, that this is *not* 3D, as the stars do not actually have 3 coordinates, only 2.... though, they do have a "life"... i don't know. I think i'm rambling.

code:

%Created by Adam Bielinski
%Just a simple program that imitates the windows screensaver
%it looks better, and the center is at the mouse, and not the center of the screen



%if you want, you can change these two variables to resize the effective screen
const X_SCREEN := 1024
const Y_SCREEN := 768

View.Set ("noecho")
View.Set ("nobuttonbar")
View.Set ("title:Stars 3D by Adam Bielinski")

setscreen ("graphics:" + intstr (X_SCREEN) + ";" + intstr (Y_SCREEN))
View.Set ("position:centre,middle")
View.Set ("offscreenonly")
drawfillbox (-10, -10, X_SCREEN, Y_SCREEN, black)


%number of stars. if you're getting a slowdown, decrease this
const NUM_STARS := 800

%initial speed ratio of the stars (make 0.95 for a cool effect)
%this is left 1 now because it is not needed
const STAR_SPEED := 1

%how fast the stars grow in size
const STAR_LIFE := 1.018

%how relatively big the stars are
const STAR_SIZE := 6

%how fast the stars fly away fromt the center
const STAR_ACC := 0.1


type point_type :
    record
        x : real
        y : real
        life : real
    end record
var stars : array 1 .. NUM_STARS of point_type

%this is the initialatation stuff. It just puts them around on the screen at random
for lp : 1 .. NUM_STARS
    stars (lp).x := Rand.Int (1, X_SCREEN)
    stars (lp).y := Rand.Int (1, Y_SCREEN)
    stars (lp).life := 0
end for


procedure DrawStar (x, y, life : real)

    %sinxe the shades of gray are nubmers 16 to 31, this finds the color that is needed for the first oval
    const temp_clr := round ((life) mod 15 + 16)
    const temp_size := floor (life / 15)

    %this oval is the oval that makes the change in size smooth
    %it does this by being a shade of gray that makes the other oval appear slightly larger
    Draw.Oval (round (x), round (y),
        temp_size + 1, temp_size + 1, temp_clr)

    if temp_size >= 1 then
        Draw.FillOval (round (x), round (y),
            temp_size, temp_size, white)
    end if

end DrawStar

var mouse_x, mouse_y, button : int := 0
var last_time : real := Time.Elapsed
loop
    Mouse.Where (mouse_x, mouse_y, button)
    for lp : 1 .. NUM_STARS
        %this stuff moves the stars, and increases their "life"
        stars (lp).x := (stars (lp).x - mouse_x) * STAR_SPEED * (stars (lp).life * STAR_ACC + 1) + mouse_x
        stars (lp).y := (stars (lp).y - mouse_y) * STAR_SPEED * (stars (lp).life * STAR_ACC + 1) + mouse_y
        stars (lp).life := (stars (lp).life + 1) * STAR_LIFE - 1

        %this draws the star
        DrawStar (stars (lp).x, stars (lp).y, (stars (lp).life * STAR_SIZE) ** 2)


        %if the star is outside the screen, reinitialize it
        if stars (lp).x < 0 or stars (lp).x > X_SCREEN or stars (lp).y < 0 or stars (lp).y > Y_SCREEN then
            stars (lp).x := Rand.Real * X_SCREEN
            stars (lp).y := Rand.Real * Y_SCREEN
            stars (lp).life := 0
        end if
    end for
    View.Update
    drawfillbox (-10, -10, X_SCREEN, Y_SCREEN, black)
    loop
        exit when Time.Elapsed - last_time > 30
    end loop
    last_time := Time.Elapsed
end loop


For those of us that do not like running things at high res, you can change those two constants at the top to resize the window.

Author:  NikG [ Mon Jul 03, 2006 10:55 pm ]
Post subject: 

Very cool!
And considering you did this in very few lines of code, I can definitely see the possibility of it being used in some type of game.

+25 bits.

Author:  Slaivis [ Mon Jul 03, 2006 11:06 pm ]
Post subject: 

This is very cool. I have been working on a game and the background is similar... It's sort of the same effect... But it's very different, and it's done differently.

Perhaps I will post it in a different thread so that you can see it. Please do check it out.

The thread will be called 'Flying Through Space'.


: