15.08.2013 Views

Creating Moving Graphics With the Sprite Routines

Creating Moving Graphics With the Sprite Routines

Creating Moving Graphics With the Sprite Routines

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Creating</strong> <strong>Moving</strong> <strong>Graphics</strong> <strong>With</strong> <strong>the</strong> <strong>Sprite</strong> <strong>Routines</strong><br />

Drawing <strong>Moving</strong> <strong>Graphics</strong> <strong>With</strong> <strong>the</strong> Pic.Draw Command<br />

Type <strong>the</strong> following program into <strong>the</strong> computer to produce moving graphics using <strong>the</strong> Pic.Draw predefined<br />

procedures.<br />

var spaceship, chair : int % Declares variables to store <strong>the</strong> spaceship and <strong>the</strong> chair graphics<br />

spaceship := Pic.FileNew ("spaceship.bmp") % Creates a storage spot for <strong>the</strong> picture of <strong>the</strong> spaceship<br />

chair := Pic.FileNew ("chair.gif") % Creates a storage spot for <strong>the</strong> picture of <strong>the</strong> chair<br />

for i : 1 .. maxx by 50<br />

Pic.Draw (spaceship, i, 100, picCopy)<br />

Pic.Draw (chair, i, 300, picCopy)<br />

delay (50)<br />

end for<br />

What is <strong>the</strong> problem with this program? What is causing this problem? Can you make a simple change to <strong>the</strong><br />

program above to make sure <strong>the</strong> “trail” does not appear?<br />

Drawing <strong>Moving</strong> <strong>Graphics</strong> <strong>With</strong> <strong>Sprite</strong>s<br />

Turing 4.1 introduced sprites which has made working with graphics much easier. A sprite is a small graphic<br />

that can be moved independently around <strong>the</strong> screen, producing animated effects. Turing 4.1 has a set of<br />

predefined procedures and functions which allows <strong>the</strong> programmer to work with sprites. Using sprites<br />

eliminates to need to draw and erase objects on <strong>the</strong> screen with <strong>the</strong> Pic.Draw procedure.<br />

A sprite is created from a picture using <strong>the</strong> <strong>Sprite</strong>.New function. The sprite must <strong>the</strong>n be shown on <strong>the</strong> screen<br />

using <strong>the</strong> <strong>Sprite</strong>.Show procedure. The position of a sprite on <strong>the</strong> monitor can be defined using <strong>the</strong><br />

<strong>Sprite</strong>.SetPosition procedure. A sprite can be moved by changing its position again using <strong>Sprite</strong>.SetPosition.<br />

The sprite at <strong>the</strong> original position will be automatically erased (restoring <strong>the</strong> background) and be redrawn at <strong>the</strong><br />

new location.<br />

Multiple sprites can be created on <strong>the</strong> same window. They are each in different “layers” which allows <strong>the</strong>m to<br />

pass over/under each o<strong>the</strong>r. The layering of sprites is determined by <strong>the</strong> order of creation (<strong>the</strong> most recent ones<br />

are higher) or by <strong>the</strong> <strong>Sprite</strong>.SetHeight procedure.<br />

Learning Exercise #1 - <strong>Sprite</strong>.New, <strong>Sprite</strong>.Show, <strong>Sprite</strong>.SetPosition<br />

Required: 1) Make sure <strong>the</strong> graphic files spaceship.bmp, chair.gif and airplane.bmp are in your <strong>Graphics</strong><br />

folder.<br />

2) Input <strong>the</strong> following program to learn about <strong>the</strong> various <strong>Sprite</strong> predefined procedures and<br />

functions.<br />

% <strong>Moving</strong> sprites!<br />

var chair, chair<strong>Sprite</strong> : int % Declares variables to store <strong>the</strong> chair graphic and <strong>the</strong> chair sprite<br />

chair := Pic.FileNew ("chair.gif") /* Creates a storage spot for <strong>the</strong> picture of <strong>the</strong> chair and stores it in <strong>the</strong> variable chair.*/<br />

chair<strong>Sprite</strong> := <strong>Sprite</strong>.New (chair) /* Creates a sprite from <strong>the</strong> picture of <strong>the</strong> chair and stores it in <strong>the</strong> variable chair<strong>Sprite</strong>.<br />

The parameter for this function is <strong>the</strong> variable storing <strong>the</strong> picture. */


<strong>Sprite</strong>.Show (chair<strong>Sprite</strong> ) /* Makes <strong>the</strong> sprite appear in <strong>the</strong> window. The parameter for this procedure is <strong>the</strong><br />

variable storing <strong>the</strong> sprite. */<br />

delay (1000)<br />

<strong>Sprite</strong>.SetPosition (chair<strong>Sprite</strong> , 0,0, false) /* Changes <strong>the</strong> position of <strong>the</strong> sprite. The parameter for this procedure is <strong>the</strong><br />

variable storing <strong>the</strong> sprite, <strong>the</strong> x and y position and a boolean value (true or<br />

false) . If this value is true, <strong>the</strong>n <strong>the</strong> sprite is centered on <strong>the</strong> x, y position.<br />

O<strong>the</strong>rwise, <strong>the</strong> x, y position specifies <strong>the</strong> lower-left corner of <strong>the</strong> sprite.*/<br />

delay (1000)<br />

<strong>Sprite</strong>.SetPosition (chair<strong>Sprite</strong> , 200,0, false) % Re-sets <strong>the</strong> position of <strong>the</strong> sprite<br />

delay (1000)<br />

<strong>Sprite</strong>.SetPosition (chair<strong>Sprite</strong> , 0,200, false)<br />

delay (1000)<br />

<strong>Sprite</strong>.SetPosition (chair<strong>Sprite</strong> , 200,200, false)<br />

Learning Exercise #2 - <strong>Sprite</strong>.Height<br />

1. Add <strong>the</strong> following code to your current program to create and show a sprite of a spaceship. The code will be<br />

placed immediately after <strong>the</strong> corresponding commands to create, show and move <strong>the</strong> chair sprite.<br />

var spaceship, spaceship<strong>Sprite</strong> :int % Declares variables to store <strong>the</strong> spaceship graphic and <strong>the</strong> spaceship sprite<br />

spaceship := Pic.FileNew ("spaceship.bmp") /* Creates a storage spot for <strong>the</strong> picture of <strong>the</strong> chair and stores it in <strong>the</strong><br />

variable spaceship.*/<br />

spaceship<strong>Sprite</strong> := <strong>Sprite</strong>.New (spaceship) /* Creates a sprite from <strong>the</strong> picture of <strong>the</strong> spaceship and stores it in <strong>the</strong><br />

variable spaceship<strong>Sprite</strong>. The parameter for this function is <strong>the</strong> variable<br />

storing <strong>the</strong> picture. */<br />

<strong>Sprite</strong>.Show (spaceship<strong>Sprite</strong>) /* Makes <strong>the</strong> sprite appear in <strong>the</strong> window. The parameter for this procedure is <strong>the</strong><br />

variable storing <strong>the</strong> sprite*/<br />

<strong>Sprite</strong>.SetPosition (spaceship<strong>Sprite</strong> , 0,0, false) /* Changes <strong>the</strong> position of <strong>the</strong> sprite. The parameter for this procedure<br />

is <strong>the</strong> variable storing <strong>the</strong> sprite, <strong>the</strong> x and y position and a boolean<br />

value (true or false) . If this value is true, <strong>the</strong>n <strong>the</strong> sprite is centered on<br />

<strong>the</strong> x, y position. O<strong>the</strong>rwise, <strong>the</strong> x, y position specifies <strong>the</strong> lower-left<br />

corner of <strong>the</strong> sprite.*/<br />

<strong>Sprite</strong>.SetPosition (spaceship<strong>Sprite</strong> , 200,0, false) % Re-sets <strong>the</strong> position of <strong>the</strong> sprite<br />

<strong>Sprite</strong>.SetPosition (spaceship<strong>Sprite</strong> , 0,200, false)<br />

<strong>Sprite</strong>.SetPosition (spaceship<strong>Sprite</strong> , 200,200, false)<br />

Note that <strong>the</strong> spaceship always ends up on top. Why?<br />

2. After <strong>the</strong> code <strong>Sprite</strong>.SetPosition (spaceship<strong>Sprite</strong> , 0,200, false), add <strong>the</strong> following lines of code:<br />

<strong>Sprite</strong>.SetHeight (chair<strong>Sprite</strong>, 2) % Changes <strong>the</strong> order of <strong>the</strong> sprites<br />

<strong>Sprite</strong>.SetHeight (spaceship<strong>Sprite</strong>, 1)<br />

What happened? Why?<br />

3. Change <strong>the</strong> order of <strong>the</strong> sprites back to <strong>the</strong> original order when <strong>the</strong>y are drawn at position 200,200.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!