art 315 lecture 5 dr. j. parker ab 606. last time … we wrote our first program. we used a tool...

40
Art 315 Lecture 5 Dr. J. Parker AB 606

Upload: gloria-paul

Post on 31-Dec-2015

216 views

Category:

Documents


0 download

TRANSCRIPT

Art 315Lecture 5

Dr. J. Parker

AB 606

Last time …

We wrote our first program.

We used a tool called GameMaker.

The program we wrote causes a ball to move left when we type aleft arrow key.

That’s all.

This is a program?

The equivalent C code for a PC would be between 500-700 lines long.

Let’s continue with the program.

There are three other directions to move the ball, let’s do that first.

GamemakerDouble click on the file L0401.gmk from last time and it will open in GameMaker.

Now we can edit things, add new ones, and run the program.

GameMakerAdd event -> Key Press -> Right

GameMakerAdd event -> Key Press -> Up

… and DownAdd event -> Key Press -> Down

Run the program

The Program

This program is now bigger and better than a typical ‘Hello, World’, but still has very little in the way of function.

Most programs in intro programming classes are, in fact, pure demonstrations, and have no other real value. They are pedagogical tools.

Ours will ultimately become something else.

The Relative BoxThis determines whether the motion/direction specified is absolute or relative.

If Relative is unchecked,then motion is absolute.EG Speed = 1 (unit/turn)

If checked, the motion isrelative to the currentmotion.That is, speed=1 meansadd 1 to the current speed.

RelativeIf the motions we specified are made relative,

Then each time an arrow is pressed the speed will change. We can make the ball go faster and slower.

Well, faster, anyhow.

Why can’t we make it goslower?

Slower?We could make it go slower, we just

asked incorrectly.

What we asked for was a relative change in speed of +1 and specified a new direction.

So it will go faster with each key press, but in a new direction, just as we asked.

How can we get it to move slower?

Slower?The combination of speed and direction change

is called velocity.

We can change speed by adding to the speed value.

Making the speed value negative makes the ball go ‘backwards’, or in the opposite direction. (going DOWN with negative speed means going UP)

Slower?So we could specify the DOWN motion as

a negative change in the UP speed.We could specify RIGHT motion as a

negative change in LEFT speed.

Right is –ve left Down is –ve up

The Result

GravityYou may recall that in the Move menu

there is an item for gravity.

It does what you probablythink – it pulls the objectin a specified direction.

GravityProblem: just turning gravity on the ball will make

it fall, past the bottom of the room, and forever.

Let’s do it anyhow.

When???

We can turn gravity on when the ball is created, in this case it will be when the program starts to run.

Gravity

Creation is an event.

Make a new event for Create and as the action, select the gravity item. Set gravity down, and with intensity 1.

When doesthe creationevent occur?

In this case,when the program startsrunning (greenarrow pushed)

Falling

The ball falls. Good.

It falls quickly, and passes off of the visible part of the screen. I guess this is to be expected (but did you?)

Programs are often guilty of this kind of unexpected behaviour; that which could have been predicted, but which did not seem obvious.

How can we keep the ball on the screen?

Build a Floor

We could build a floor, an object that the ball could collide with.

This works as before: create a sprite (get a texture, set centre), create an object using that sprite. Then place those objects in a row along the bottom of the room.

Important: make the object solid by checking the box. (ball too)

The Room Looks Like This:

Are we Done?

Nope.

If you run this program, the ball falls as before, passing right through the floor.

Why?

All interactions have to be specifically allowed and accounted for in a program. We did not do this.

We Program A Collision With the Floor

Double click on ‘obj_ball’ and select Add Event->CollisionSelect the obj_wall as the collision target.

This will allow us to create some code to deal with a collision between the ball and the wall (floor)

Collision Code

What do we want to happen?

The ball should stop falling, I guess.

I set the vertical speed to 0.

There are other ways to do this.

Tell me what they are:

Gravity, Balls, and suchIn the ‘real’ world, a ball usually bounces.Can we write a program to do that?

Of course.

When the ball collides with the floor, is should change Direction instead of stopping.

There is a Motion item that does that. Making it seem realistic is a bit trickier.

BouncingYou should play with this problem too.

My solution: - Set vertical speed to -3 relative - Reverse vertical direction

Why -3 relative? Because I tried -1 and -2 and it Did not seem realistic.

Why reverse vertical direction? Because that’s what a bounce is.

The gravity setting automatically pulls the ball down again, and so it bounces again …

Bouncing

What Have We Learned?(about programming, that is)

We have to understand how to do it before we can write a program to do it.

We can experiment with a program if we don’t know exactly what to do.

Programs can do things we may not have anticipated (usually this is our fault)

Programming can be fun. *(RH)

Now Lets Have Some Fun

It’s called ‘GameMaker’, so let’s make a game.

We can make a ball move around, and ‘fall’ in simulated gravity. Sounds a bit like basketball. But perhaps we can do better. Ideas?

An Opponent or Target

Conflict is a key aspect of game design. I suggest a target, as an opponent is harder.

Each time the ball, controlled by the player, collides with a new object, a target, then a point will be scored.

Make it challenging – reverse the control directions left for right.

GameReverse control directions:Pressing left key goes right, right key goes left.

Make a Target

Target could be another ball-type object that moves. Or a ‘goal’-like thing.

Ideas??

Goal

You will make your own game in the lab.Mine will have a goal, and a wall protecting it. Behindthe goal is a target that moves. Quite tricky.

Scores

In a game, there must be some sort of goal or objective to be achieved. Often this is recognized by an increase in a value called the score.

In a shooting game, for instance, the score increases whenever an opponent is shot or destroyed.

ScoresBecause GameMaker was designed to make games, there is a built-in mechanism for keeping track of the score.

ScoreSet Score Test score Draw score

Score is a name that represents a value, the number of ‘points’ earned by the player.

It can be incremented,decremented, or displayed.

You get to decide how points are added/subtracted.

Score

Variable

Score is an example of a variable.

This will be the subject of the next class.