Skip to content

Sprites in Motion

Alban edited this page Aug 15, 2020 · 32 revisions

Sprites in Motion

Lets start with a static scene; the hero at the bottom of the screen.

The invaders are in a line at the top.

(clear-image 0.0 0.0 0.0 1.0)

(load-sprites "images/bg1.jpg" 0)	
(load-sprites "images/h1.png" 1)	
(load-sprites "images/D-17.png" 20)
 
(draw-sprite 0 0.0 10.0)
(draw-sprite 1 400.0 460.0)

(for x from 120.0 to 700.0 step 88.0
	(draw-scaled-rotated-sprite
		20 x 20.0 0.0 0.65) )
 
(show 1)	

That is essentially a picture; there is nothing animated.

We certainly need the hero ship to move from left to right when the player presses a key.

Arranging things into functions helps.

(define load-all-sprites 
 (lambda ()
   (load-sprites "images/bg1.jpg" 0)	
   (load-sprites "images/h1.png" 1)	
   (load-sprites "images/D-17.png" 20)))

(define clear-scene 
  (lambda ()
    (clear-image 0.0 0.0 0.0 1.0)
    (draw-sprite 0 0.0 10.0)))

(define draw-invaders 
 (lambda (x y)
   (for x-pos from x to 700.0 step 88.0
	(draw-scaled-rotated-sprite
		20 x-pos y 0.0 0.65))))

(define draw-hero 
  (lambda (x) 
    (draw-sprite 1 x 460.0)))


The same scene is now drawn using the more meaningful functions.

(load-all-sprites)
(clear-scene)
(draw-invaders 120.0 20.0)
(draw-hero  400.0)
(show 0)

We can see that drawing the scene will require the hero and invaders positions at least.

(define draw-scene 
 (lambda (hx ix iy )
   (clear-scene)
   (draw-invaders ix iy)
   (draw-hero hx)))

Now it is easy to try the scene out with different positions.

(draw-scene 140.0 168.0 20.0)
(show 0)

It is easy now to test the aliens sweeping straight down the screen

(for y from 20.0 to 400.0 step 0.5 
 (draw-scene 400 100 y ) (show 0))
Clone this wiki locally