Thursday, 22 November 2012

Animated background

Here a very simple example of an animated background (moving stars) and a spaceship (a simple triangle).
Please notice that you are free to move the transparent triangle on the screen using keyboard arrows or A S D W letters.

Here the source:
Rebol [Title: "spaceship"]
;our spaceship
triangle: [translate 150x300   polygon 10x80 50x10 90x80]
s: 10 ;spaceship speed

;function that move the triangle on the screen
move-s: func [b] [
    if b = #"a" [ triangle/2/x:     triangle/2/x - s ]
    if b = #"d" [triangle/2/x:   triangle/2/x + s ]  
    if b = #"s" [triangle/2/y:   triangle/2/y + s ]
    if b = #"w" [triangle/2/y:   triangle/2/y - s ]      
    repeat i 2 [
        if triangle/2/:i < 0 [triangle/2/:i: 0 ]
        if triangle/2/:i > 300 [triangle/2/:i: 300 ]
    show a
;our background
out: copy []
;this update the background
sfondo: func [/local a][
        ;move all one line (10px) down      
        insert out [translate 0x10]
        ;insert a star (a box 2x2)
        a: random 400x0
        insert out reduce ['box   a   (a + 2) ]      
        ; short out series a only 205 elements
        clear skip out 205
;main window    
view layout [
    at 0x0
    box black 400x400   rate 30   feel [engage: func [f a e][f/effect: reduce [ 'draw sfondo] show f ]]
    at 0x0
    a: box 400x400 effect [draw triangle ]
    key keycode [#"a" left] [move-s #"a"]  
    key keycode [#"s" down] [move-s #"s"]      
    key keycode [#"d" right ] [move-s #"d"]
    key keycode [#"w" up ] [move-s #"w"]            

No comments:

Post a Comment