Computer Science Canada

star field

Author:  omni [ Wed Feb 25, 2004 7:01 pm ]
Post subject:  star field

I dont know if anyone previously did this, but this is a
program that makes stars move on the randomly and you can change the direction of the stars movement. You can also change the speed of the star movement with ALT and CTRL.

Author:  Paul [ Wed Feb 25, 2004 7:06 pm ]
Post subject: 

Pretty good, its gets flashy though, if you slow it down.

Author:  omni [ Wed Feb 25, 2004 7:11 pm ]
Post subject: 

ya i don't know how to get it a smooth animation and the ability to handle more stars. (it gets a lot slower with more stars)

Author:  Paul [ Wed Feb 25, 2004 7:15 pm ]
Post subject: 

Here, use View.Update, its ezness:
try this code, looks much better, with only 2 added lines
code:

%Jeffery Liu
setscreen ("graphics:787;541,nobuttonbar")
setscreen ("offscreenonly")
var x, y, c : int
var jeff : string (1)
var key : array char of boolean
var xchange, ychange : int
xchange := 1
ychange := 0
put "Move stars with arrow keys, "
put "Speed up with ALT "
put "Slow down with CTRL "
put "Press the any key .."
getch (jeff)
var xstars, ystars : array 1 .. 15 of int
for a : 1 .. upper (xstars)
    randint (x, 0, maxx)
    randint (y, 0, maxy)
    %randint (c, 0, 255)
    xstars (a) := x
    ystars (a) := y %initializing the arrays
end for
drawfillbox (0, 0, maxx, maxy, black) %the screen is now black
var speed : int := 2
loop
    Input.KeyDown (key)
    if key (KEY_LEFT_ARROW) then
        xchange := -1
        ychange := 0
    elsif key (KEY_RIGHT_ARROW) then
        xchange := 1
        ychange := 0
    elsif key (KEY_UP_ARROW) then
        xchange := 0
        ychange := 1
    elsif key (KEY_DOWN_ARROW) then
        xchange := 0
        ychange := -1
    elsif key (KEY_ALT) then
        if speed > 0 then
            speed := speed - 1
        end if
    elsif key (KEY_CTRL) then
        if speed < 10 then
            speed := speed + 1
        end if
    end if
    exit when key (KEY_ESC)
    delay (speed)
    for count : 1 .. upper (xstars) %drawing all the stars
        drawstar (xstars (count), ystars (count), xstars (count) + 10, ystars (count) + 10, white)
    end for
       View.Update
    for count : 1 .. upper (xstars) %erasing the stars
        drawstar (xstars (count), ystars (count), xstars (count) + 10, ystars (count) + 10, black)
    end for
    for count : 1 .. upper (xstars) %increasing the coordinates of the stars
        ystars (count) := ystars (count) + ychange
        xstars (count) := xstars (count) + xchange
    end for
    for count : 1 .. upper (xstars)
        if xchange = 1 then
            if xstars (count) > maxx then %checking if stars hit right boundary
                ystars (count) := Rand.Int (0, maxy)
                xstars (count) := 0
            end if
        elsif xchange = -1 then
            if xstars (count) < 0 then %left side
                ystars (count) := Rand.Int (0, maxy)
                xstars (count) := maxx
            end if
        elsif ychange = 1 then
            if ystars (count) > maxy then %top
                xstars (count) := Rand.Int (0, maxx)
                ystars (count) := 0
            end if
        elsif ychange = -1 then
            if ystars (count) < 0 then %bottom
                xstars (count) := Rand.Int (0, maxx)
                ystars (count) := maxy
            end if
        end if
    end for
 
end loop

Author:  omni [ Wed Feb 25, 2004 7:19 pm ]
Post subject: 

thanks

Author:  jonos [ Wed Feb 25, 2004 8:13 pm ]
Post subject: 

nice looking program

Author:  shorthair [ Wed Feb 25, 2004 8:21 pm ]
Post subject: 

I like it , and its got some nice clean code , keep up your good work , and look into smoother animation for best results

Author:  apomb [ Wed Feb 25, 2004 9:19 pm ]
Post subject: 

I also like it ... using cls instead of redrawing the stars in black, and seting the background color to black, it is also less choppy ...quite neat code as well! nicely done Claping

Author:  omni [ Thu Feb 26, 2004 12:45 pm ]
Post subject: 

for some reason when I add the view.update, it moves slower. Is this normal, or is it my computer?

Author:  recneps [ Thu Feb 26, 2004 3:45 pm ]
Post subject: 

Thats normal, just lower delays

Author:  Cervantes [ Thu Feb 26, 2004 6:25 pm ]
Post subject: 

if you do what compwizz suggests and not redraw the background with a huge rectangle then you need to do the following
code:

colourback (color#)
%then whereever you want...
cls

Author:  Jodo Yodo [ Sat Mar 06, 2004 10:57 pm ]
Post subject: 

Purdy Stars

Author:  jonos [ Sat Mar 06, 2004 10:59 pm ]
Post subject: 

this topic is old, you didn't need to bring it up. but then again i brought back up two of the oldest, but my actions were justified because they were the oldest posts.

Author:  MacDaddyM [ Mon May 17, 2004 5:17 pm ]
Post subject: 

nice

Author:  omni [ Tue Jun 15, 2004 3:56 pm ]
Post subject: 

Made the animation smoother, by putting in View.Update.
Also added 3 layers. Ex: there is a background layer of stars that dont move that fast(dark colour), middle layer of stars that move and a top layer of stars that move fast(bright colour). I practiced the use of records in this program. Controls are still same.

Help on more efficient way of checking if the stars hit boundaries ?

Author:  Paul [ Tue Jun 15, 2004 4:01 pm ]
Post subject: 

The stars get stuck on the left boundary, thats the only problem I see Eh

Author:  omni [ Tue Jun 15, 2004 4:07 pm ]
Post subject: 

fixed it up.


: