Art 315 Lecture 6 Dr. J. Parker. Variables Variables are one of a few key concepts in programming...

33
Art 315 Lecture 6 Dr. J. Parker

Transcript of Art 315 Lecture 6 Dr. J. Parker. Variables Variables are one of a few key concepts in programming...

Page 1: Art 315 Lecture 6 Dr. J. Parker. Variables Variables are one of a few key concepts in programming that must be understood. Many engineering/cs students.

Art 315 Lecture 6Dr. J. Parker

Page 2: Art 315 Lecture 6 Dr. J. Parker. Variables Variables are one of a few key concepts in programming that must be understood. Many engineering/cs students.

Variables

• Variables are one of a few key concepts in programming that must be understood.

• Many engineering/cs students do not have a good understanding of them until later in their studies.

An example of a variable is score, as we discussed last time.

Page 3: Art 315 Lecture 6 Dr. J. Parker. Variables Variables are one of a few key concepts in programming that must be understood. Many engineering/cs students.

The Abstract ExplanationA variable is a name (IE a symbol) that represents some value, usually a numeric value.

The name is often indicative of the nature of the value – so the variable score represents a value that is also called a “score” in English. This is a coincidence; the game score could just as easily be kept in a variable called “fred”.

Page 4: Art 315 Lecture 6 Dr. J. Parker. Variables Variables are one of a few key concepts in programming that must be understood. Many engineering/cs students.

The Engineering Explanation

The variable is another name for the address in memory where the value is kept.

The programming language takes the namethat it sees and replaces it with a memory address.

So score = 12 becomes memory[10020] = 12Where 10020 is the address of score.

Page 5: Art 315 Lecture 6 Dr. J. Parker. Variables Variables are one of a few key concepts in programming that must be understood. Many engineering/cs students.

The Common Sense Explanation

A variable is a place to put something; it’s the name of that place.

Page 6: Art 315 Lecture 6 Dr. J. Parker. Variables Variables are one of a few key concepts in programming that must be understood. Many engineering/cs students.

What can we do with variables?

We can save things for later.

We can add to them, accumulate values. EG - How many enemies have we shot? - How much fuel do we have left?- Where is my ‘ball’ object?

We can test it against sentinal/target values.- Am I out of fuel (is fuel = 0)?- Am I near to a water hole (distance < 20)?

Page 7: Art 315 Lecture 6 Dr. J. Parker. Variables Variables are one of a few key concepts in programming that must be understood. Many engineering/cs students.

Variables in GameMaker

We can save things for later.

We can add to them, accumulate values. EG - How many enemies have we shot? - How much fuel do we have left?- Where is my ‘ball’ object?

We can test it against sentinel/target values.- Am I out of fuel (is fuel = 0)?- Am I near to a water hole (distance < 20)?

Page 8: Art 315 Lecture 6 Dr. J. Parker. Variables Variables are one of a few key concepts in programming that must be understood. Many engineering/cs students.

Variable Fuel

Let’s make a variable named Fuel.

The ball starts out with some fuel, let’s say 1000 units of it.

Every turn it uses 1 unit.

The ball bounces off of walls.

We should display the value of fuel.

Page 9: Art 315 Lecture 6 Dr. J. Parker. Variables Variables are one of a few key concepts in programming that must be understood. Many engineering/cs students.

Variable Fuel

Creating the variable: When an obj_ball is created, set the value of a variable names Fuel to 1000.

Event: create ---- Action: Set variable Fuel to 1000

Page 10: Art 315 Lecture 6 Dr. J. Parker. Variables Variables are one of a few key concepts in programming that must be understood. Many engineering/cs students.

Variable Fuel

This activity does three important things for the variable:

Declares it: Establishes the name as having a meaning – it is a variable, and holds a value.

Allocates it: Associates the variable with space; gives it some real estate.

Initializes it: Gives it a value to begin with (else what is it?)

Page 11: Art 315 Lecture 6 Dr. J. Parker. Variables Variables are one of a few key concepts in programming that must be understood. Many engineering/cs students.

Variable Fuel

Now subtract one for each turn that takes place.

Turns are called steps in Gamemaker, and there is an event for a step.

Page 12: Art 315 Lecture 6 Dr. J. Parker. Variables Variables are one of a few key concepts in programming that must be understood. Many engineering/cs students.

Turns

A step is one time increment. I t is 1/30 of a second, all steps are the same length, and all motions are broken up into how far an object moves in one step.

This is because real time is continuous, but computers have to simulate the passage of time in small increments, thus allowing computation to be done in each increment.

Page 13: Art 315 Lecture 6 Dr. J. Parker. Variables Variables are one of a few key concepts in programming that must be understood. Many engineering/cs students.

Back to Variables …

Whether the variable is changing or not is a matter of faith – unless it can be used in some real way.

The most obvious way to show that a variable is doing what it should is to display it.

Also called print, show, draw.

In Game Maker it is called drawing because it uses game graphics.

Page 14: Art 315 Lecture 6 Dr. J. Parker. Variables Variables are one of a few key concepts in programming that must be understood. Many engineering/cs students.

Draw VariableProblem: when a variable is drawn, it must be done in

a draw event.

Normally the sprite for an object is drawn each step, in the correct position. The Draw event means ‘when drawing takes place’, and using it allows you to do things each time the object is drawn.

Problem: If you use the Draw event, then automatic drawing does not happen – your sprite will not appear automatically.

Page 15: Art 315 Lecture 6 Dr. J. Parker. Variables Variables are one of a few key concepts in programming that must be understood. Many engineering/cs students.

Draw Variable

You have a choice between doing your own drawing and allowing Game Maker to do it.

Method 1: use a Draw event, and draw the variable AND the sprite.

Page 16: Art 315 Lecture 6 Dr. J. Parker. Variables Variables are one of a few key concepts in programming that must be understood. Many engineering/cs students.

Draw Variable

Draw

Page 17: Art 315 Lecture 6 Dr. J. Parker. Variables Variables are one of a few key concepts in programming that must be understood. Many engineering/cs students.

Draw Variable

Draw

Page 18: Art 315 Lecture 6 Dr. J. Parker. Variables Variables are one of a few key concepts in programming that must be understood. Many engineering/cs students.

Draw Variable

Page 19: Art 315 Lecture 6 Dr. J. Parker. Variables Variables are one of a few key concepts in programming that must be understood. Many engineering/cs students.

Draw Variable

Note that the value is drawn at a positionrelative to that of the ball (what we asked for),and so moves as the ball does.

We can easily draw the number at a fixed position.(Do it)

Page 20: Art 315 Lecture 6 Dr. J. Parker. Variables Variables are one of a few key concepts in programming that must be understood. Many engineering/cs students.

Variables

The variable ‘fuel’ is a user defined variable. Theytend to be associated with objects.

Each object also has pre-defined (by the system) variables that we can use.

Example: each object has a position, expressed as an x (horizontal) and a y (vertical) position.

Page 21: Art 315 Lecture 6 Dr. J. Parker. Variables Variables are one of a few key concepts in programming that must be understood. Many engineering/cs students.

Variables

The horizontal position of the ball is obj_ball.xThe vertical position of the ball is obj_ball.y

And a similar scheme can be used for all objects.

Other variables include: xprevious, xstart, vspeed,fps, direction, friction, gravity, hspeed, room_width, room_height, …

Page 22: Art 315 Lecture 6 Dr. J. Parker. Variables Variables are one of a few key concepts in programming that must be understood. Many engineering/cs students.

VariablesVariables can be altered.

For example, whenever the ball hits the wall, subtract 10 from the Y value (which should make the ball move UP).

That is, obj_ball.y = obj_ball.y - 10

Page 23: Art 315 Lecture 6 Dr. J. Parker. Variables Variables are one of a few key concepts in programming that must be understood. Many engineering/cs students.

VariablesBall moves UP on each bounce.

Page 24: Art 315 Lecture 6 Dr. J. Parker. Variables Variables are one of a few key concepts in programming that must be understood. Many engineering/cs students.

VariablesAt 30 steps per second, a speed of 1 moves 30 pixels/second.

Angles: specified in degrees. 0 degrees is right; 180 is left90 degrees is up, 270 is down

0

90

180

270

Page 25: Art 315 Lecture 6 Dr. J. Parker. Variables Variables are one of a few key concepts in programming that must be understood. Many engineering/cs students.

Random NumbersA random number is one that has an unpredictable value. In GameMaker they are created by using a function (more on those later) called random.

The value of random(100) is a number between 0 and 99 (there are 100 values), possibly different each time, and where the exact value won’t be known in advance.

To create the roll of a die, we would use random(6)+1. random(6) is 0..5, so random(6)+1 is 1..6

Page 26: Art 315 Lecture 6 Dr. J. Parker. Variables Variables are one of a few key concepts in programming that must be understood. Many engineering/cs students.

Random NumbersRandom numbers are crucial in a game and in simulations generally.

They are how a computer program simulates intelligence, in many cases.

A real person does not do the same thing all of the time – they are unpredictable. Since we don’t know the basis that humans use for making decisions, we use random choices, which have the appearance of intelligence. Or of natural activities that we don’t understand.

Page 27: Art 315 Lecture 6 Dr. J. Parker. Variables Variables are one of a few key concepts in programming that must be understood. Many engineering/cs students.

Random NumbersExample: Multiple balls bouncing about in a

box.

This would seem to be a complex situation, and hard to keep track of everything. But all we need to do is make some basic simple rules, use some random behaviour, and create more than one ball.

1.Ball hits wall: bounce in a random direction.2.Ball hits ball: ditto.3.Ball is created with a random direction.

Page 28: Art 315 Lecture 6 Dr. J. Parker. Variables Variables are one of a few key concepts in programming that must be understood. Many engineering/cs students.

Random Numbers

Page 29: Art 315 Lecture 6 Dr. J. Parker. Variables Variables are one of a few key concepts in programming that must be understood. Many engineering/cs students.

Random NumbersExample: Atom.

Electrons don’t actually orbit the nucleus, they occur in all positions about it with certain probabilities..

1.Nucleus is in the centre of the room.2.Electrons appear in random positions each

step.

Page 30: Art 315 Lecture 6 Dr. J. Parker. Variables Variables are one of a few key concepts in programming that must be understood. Many engineering/cs students.

Random Numbers

Page 31: Art 315 Lecture 6 Dr. J. Parker. Variables Variables are one of a few key concepts in programming that must be understood. Many engineering/cs students.

Random Numbers

This does not really look very realistic, but it could …

We need to avoid the nucleus, first of all.

So we need to test to see if the random position we’ve selected is near the nucleus, and pick another if so.

This is the second main new aspect of programming: flow of control.

Page 32: Art 315 Lecture 6 Dr. J. Parker. Variables Variables are one of a few key concepts in programming that must be understood. Many engineering/cs students.

Control

What we need to do is to be able to say:

If the value of variable fuel is 0 then stop all motion.

Or

If the random (x,y) position is less than 100 units (distance) from the nucleus then pick a new (x,y)

Page 33: Art 315 Lecture 6 Dr. J. Parker. Variables Variables are one of a few key concepts in programming that must be understood. Many engineering/cs students.

Control

This is the subject of the next class, and is a key subject in programming.