Art 315 Lecture 5

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

description

Art 315 Lecture 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?. - PowerPoint PPT Presentation

Transcript of Art 315 Lecture 5

Page 1: Art 315 Lecture 5

Art 315Lecture 5

Dr. J. Parker

AB 606

Page 2: Art 315 Lecture 5

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.

Page 3: Art 315 Lecture 5

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.

Page 4: Art 315 Lecture 5

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.

Page 5: Art 315 Lecture 5

GameMakerAdd event -> Key Press -> Right

Page 6: Art 315 Lecture 5

GameMakerAdd event -> Key Press -> Up

Page 7: Art 315 Lecture 5

… and DownAdd event -> Key Press -> Down

Page 8: Art 315 Lecture 5

Run the program

Page 9: Art 315 Lecture 5

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.

Page 10: Art 315 Lecture 5

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.

Page 11: Art 315 Lecture 5

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?

Page 12: Art 315 Lecture 5

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?

Page 13: Art 315 Lecture 5

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)

Page 14: Art 315 Lecture 5

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

Page 15: Art 315 Lecture 5

The Result

Page 16: Art 315 Lecture 5

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.

Page 17: Art 315 Lecture 5

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.

Page 18: Art 315 Lecture 5

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)

Page 19: Art 315 Lecture 5
Page 20: Art 315 Lecture 5

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?

Page 21: Art 315 Lecture 5

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)

Page 22: Art 315 Lecture 5

The Room Looks Like This:

Page 23: Art 315 Lecture 5

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.

Page 24: Art 315 Lecture 5

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)

Page 25: Art 315 Lecture 5

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:

Page 26: Art 315 Lecture 5
Page 27: Art 315 Lecture 5

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.

Page 28: Art 315 Lecture 5

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 …

Page 29: Art 315 Lecture 5

Bouncing

Page 30: Art 315 Lecture 5

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)

Page 31: Art 315 Lecture 5

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?

Page 32: Art 315 Lecture 5

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.

Page 33: Art 315 Lecture 5

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

Page 34: Art 315 Lecture 5

Make a Target

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

Ideas??

Page 35: Art 315 Lecture 5

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.

Page 36: Art 315 Lecture 5

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.

Page 37: Art 315 Lecture 5

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

Page 38: Art 315 Lecture 5

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.

Page 39: Art 315 Lecture 5

Score

Page 40: Art 315 Lecture 5

Variable

Score is an example of a variable.

This will be the subject of the next class.