Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Art 315 Lecture 6 Dr. J. Parker

2 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.

3 The Abstract Explanation A 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”.

4 The Engineering Explanation The variable is another name for the address in memory where the value is kept. The programming language takes the name that it sees and replaces it with a memory address. So score = 12 becomes memory[10020] = 12 Where 10020 is the address of score.

5 The Common Sense Explanation A variable is a place to put something; it’s the name of that place.

6 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)?

7 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)?

8 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.

9 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

10 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?)

11 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.

12 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.

13 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.

14 Draw Variable Problem: 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.

15 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.

16 Draw Variable Draw

17 Draw Variable Draw

18 Draw Variable

19 Note that the value is drawn at a position relative 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)

20 Variables The variable ‘fuel’ is a user defined variable. They tend 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.

21 Variables The horizontal position of the ball is obj_ball.x The 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, …

22 Variables Variables 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

23 Variables Ball moves UP on each bounce.

24 Variables At 30 steps per second, a speed of 1 moves 30 pixels/second. Angles: specified in degrees. 0 degrees is right; 180 is left 90 degrees is up, 270 is down 0 90 180 270

25 Random Numbers A 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

26 Random Numbers Random 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.

27 Random Numbers Example: 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.

28 Random Numbers

29 Example: 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.

30 Random Numbers

31 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.

32 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)

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


Download ppt "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."

Similar presentations


Ads by Google