mrsbillinghurst. net a2 computing a2 computing projects game animation in pascal

24
MrsBillinghu rst.net A2 Computing A2 Computing Projects Game Animation in Pascal

Upload: regina-gray

Post on 11-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: MrsBillinghurst. net A2 Computing A2 Computing Projects Game Animation in Pascal

MrsBillinghurst.net

A2 Computing

A2 Computing Projects

Game Animation in Pascal

Page 2: MrsBillinghurst. net A2 Computing A2 Computing Projects Game Animation in Pascal

MrsBillinghurst.net

Creating an Animation - Pascal

• In order to create a simple animation, you will need a number of images which will swap – much like a flip book.

• Here we will be using 8 different images of lemmings walking to achieve the animation.

Simple

Page 3: MrsBillinghurst. net A2 Computing A2 Computing Projects Game Animation in Pascal

MrsBillinghurst.net

Creating an Animation - Pascal

To achieve the effect of a lemming walking across the screen, you will need to add a timer object and eight image objects to the screen. Each image should have one of the eight walking images in order.

When you have added all the objects, move the images on top of the first setting the visible property of each one to ‘false’.

Simple

Page 4: MrsBillinghurst. net A2 Computing A2 Computing Projects Game Animation in Pascal

MrsBillinghurst.net

Creating an Animation - Pascal

The algorithm for making the lemming walk is as follows:

IF lemming x is visibleTHEN set lemming x+1 to visibleAND set all other lemmings to invisible

In code, this could translate to a long IF / ELSE statement, or using procedures, a more elegant looping solution.

This example will show both.

Simple

Page 5: MrsBillinghurst. net A2 Computing A2 Computing Projects Game Animation in Pascal

MrsBillinghurst.net

Creating an Animation - Pascal

For the proof of concept, we will add the animation code to the TimerTick procedure. This is an event which happens at regular intervals set in the interval property of the timer. This should be set at 200.

This code will run until the lemming reaches the right hand side of the screen.

As with many visual programming environments, the screen uses Top and Left co-ordinates to place the objects.

Simple

Page 6: MrsBillinghurst. net A2 Computing A2 Computing Projects Game Animation in Pascal

MrsBillinghurst.net

Creating an Animation - Pascal

In order to get the lemming to actually walk across the screen, we need to tell the program to change the co-ordinates of all 8 images at the same time. In this way, it appears that there is only one object on the screen.

Run this code & you will have a lemming that appears to be wading through treacle across your screen.

Add this code to the end of the IF statement in the TimerTick Procedure

Could you enhance this by using an ImageList……

Simple

Page 7: MrsBillinghurst. net A2 Computing A2 Computing Projects Game Animation in Pascal

MrsBillinghurst.net

Creating an Animation - Pascal

The lemmings game used sections of land on which the lemmings could walk and throughout the course of the game, specific actions could be used to destroy this. The objective was to reach the escape door.

Our first task is to create a visual platform for the lemming to walk to and a door for him to escape through.

By adding these as images to your screen, you can make the lemming appear to be walking on a platform.

Try turning the background of your screen black to make things appear less like an application.

Simple

Page 8: MrsBillinghurst. net A2 Computing A2 Computing Projects Game Animation in Pascal

MrsBillinghurst.net

Creating an Animation - Pascal

Your screen should now look like this:

Run your program to see the differencethat adding images makes.

In order for your lemmings to look like they are walking on the grass, use Shift + PgDown to send things to the back of the screen.

The Z Order will be important to ensure your lemming is at the front of the screen.

Simple

Page 9: MrsBillinghurst. net A2 Computing A2 Computing Projects Game Animation in Pascal

MrsBillinghurst.net

Creating an Animation - Pascal

In the original design, the objective is to exit via a door. Run your program to ensure that your door is placed at the same point that your lemming stops.

When your lemming stops, add codeso that the lemming disappears.

You have already made use of the .Visible property.

To make the lemming disappear entirely, set all images to invisible.

Simple

Page 10: MrsBillinghurst. net A2 Computing A2 Computing Projects Game Animation in Pascal

MrsBillinghurst.net

Creating an Animation - Pascal

Lemmings in the 80s was all about the music. The 8 bit looped songs just about drove everyone who played to distraction, so let’s add our own!

Save your WAV music file into a local folder and create a new procedure called PlaySong. For Windows, you will also need to add the Uses MMSystem library file. This will be called from the CreateForm procedure.

In order to play the song at the same time as your animation, you must ensure that you use ASYNC to run the procedure asynchronously to the timer procedure.

Adequate

Page 11: MrsBillinghurst. net A2 Computing A2 Computing Projects Game Animation in Pascal

MrsBillinghurst.net

Classes & Objects - Pascal

Whilst it’s great to have a single animation, the purpose of a game like this is to have controls which will populate the screen with multiple instances of a class.

Imagine that your code takes on two parts:

The first part of the code describes to the program what a lemming is and what it can do: this is a class with attributes and methods

The second part of the code creates an ‘instance’ of the class in the form of an object. Each object has the same attributes and methods as the class, but this time, it has actual values.

Complex

Page 12: MrsBillinghurst. net A2 Computing A2 Computing Projects Game Animation in Pascal

MrsBillinghurst.net

Classes & Objects - Pascal

To create a lemming class, we need to know what the lemming will do.

Above the Tform class declaration, but still inside the type heading, create a new class called TLemming of type Tobject.

In here, you will have Private variables (these are local to just the objects created from the class) and Public methods (such as create)

Our Lemming class will have a single image and booleans to let us know what state the lemming is in.

Complex

Page 13: MrsBillinghurst. net A2 Computing A2 Computing Projects Game Animation in Pascal

MrsBillinghurst.net

Classes & Objects - Pascal

The program variables will also need to be adjusted.

LemmingList is an array of the Tlemmings that we created above. This allows you to create more than just one instance of the class which will act in the same way as each other.

The integers allow you to count which image is displayed, how many lemmings you want to create and how many lemmings have been created so far.

Complex

Page 14: MrsBillinghurst. net A2 Computing A2 Computing Projects Game Animation in Pascal

MrsBillinghurst.net

Classes & Objects - Pascal

The next stage is to write the constructor method for the TLemming class

Self is used here to indicate that each attribute is being applied to the newly created object.

Before the object can appear on screen, you will need to state its parent (in this case Form1).

Complex

Page 15: MrsBillinghurst. net A2 Computing A2 Computing Projects Game Animation in Pascal

MrsBillinghurst.net

Classes & Objects - Pascal

At this point, you can set up the initial values for the variables (although we’re not quite there yet!)

LemmingImage will commence the animation at the first image.

NumLemmings could be adjusted to be a user input based on how many lemmings they wanted to use.

SetLength() is a function which changes the length of the array to the number of lemmings required.

Complex

Page 16: MrsBillinghurst. net A2 Computing A2 Computing Projects Game Animation in Pascal

MrsBillinghurst.net

Classes & Objects - Pascal

The next step is to add the Timers and a door image to the form. Set the TimerWalk & TimerDrop enabled properties to false. You will start these inside the code.

The Timers will each control a section of the lemmings movements.

Name these: TimerCreateLem (interval: 1500) TimerWalk (interval: 100) TimerDrop (interval: 100)

Complex

Page 17: MrsBillinghurst. net A2 Computing A2 Computing Projects Game Animation in Pascal

MrsBillinghurst.net

Classes & Objects - Pascal

Double click on your TimerCreateLem Timer object to access its tick procedure. In here, set up the code that will create a new instance of a lemming and set its initial boolean values (landed and safe) to false.

For each ‘tick’ of the timer, a new lemming will be created and added to the array until the maximum is reached.

Complex

Page 18: MrsBillinghurst. net A2 Computing A2 Computing Projects Game Animation in Pascal

MrsBillinghurst.net

Classes & Objects - Pascal

Double click on your TimerDrop Timer object to access its tick procedure. In here, set up the code that will allow any lemming that has been created, but hasn’t yet landed on the ground to fall down.

Once, the lemming has landed, set the attribute to true.

Then start the TimerWalk.

Complex

Page 19: MrsBillinghurst. net A2 Computing A2 Computing Projects Game Animation in Pascal

MrsBillinghurst.net

Classes & Objects - Pascal

Double click on your TimerWalk Timer object to access its tick procedure. In here, you will be creating a number of sections of code.

Firstly, if your lemming has landed, move them forward:

Complex

Page 20: MrsBillinghurst. net A2 Computing A2 Computing Projects Game Animation in Pascal

MrsBillinghurst.net

Classes & Objects - Pascal

Next, if the lemming has landed, iterate through the image files to simulate movement (this CASE statement should go up to image 8 where it sets the image back to image 1):

Complex

Page 21: MrsBillinghurst. net A2 Computing A2 Computing Projects Game Animation in Pascal

MrsBillinghurst.net

Classes & Objects - Pascal

After the CASE statement, an IF, ELSE statement is used to update the Lemming Image number, which will loop back around to 1 if image 8 has been reached.

Complex

Page 22: MrsBillinghurst. net A2 Computing A2 Computing Projects Game Animation in Pascal

MrsBillinghurst.net

Classes & Objects - Pascal

Finally, if the lemming has reached the door & it has not already been recorded as going through the door, play the ‘YIPPEE’ wav file and make the Lemming dissapear.

By setting the safe attribute to true, this statement will only run once for each lemming.

Complex

Page 23: MrsBillinghurst. net A2 Computing A2 Computing Projects Game Animation in Pascal

MrsBillinghurst.net

Classes & Objects - Pascal

Extend your code by creating a random song to be played for the level.

Complex

Page 24: MrsBillinghurst. net A2 Computing A2 Computing Projects Game Animation in Pascal

MrsBillinghurst.net

Classes & Objects - Pascal

Test your code by adjusting the number of lemmings created via the NumLemmings variable. Extend this by allowing the user to increase or decrease the number of lemmings to be used.

Complex