Download presentation
Presentation is loading. Please wait.
Published bySimon Bridges Modified over 9 years ago
1
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 8 Fall 2010
2
Today - scripting Until now, our programming has been done largely by using pre-defined structures selected with the mouse. This works fine, and resembles a lot of what happens with recent software engineering templates and APIs. However, it (Mouse based) is slower and less flexible than using text, at least for small programs.
3
Script = program A script is a test version of a program in Game Maker, and is attached to an object, like a sprite. A script has to communicate the same things as does the point-and-click mouse program, so there must be symbols (text) that do this. A script (program) is written in a scripting language (programming language), which has a fixed set of these symbols and has a syntax (structure). Scripts are the more familiar type of computer program.
4
A script is text Count = 0
5
A script is text if (current_sprite) > 2
6
A script is text if (count > 120) { count = 0; current_sprite = 1; if (current_sprite > 2) current_sprite = 0; if (current_sprite==0) sprite_index= spr_electron; else if (current_sprite == 1) sprite_index = spr_proton; else sprite_index = spr_neutron; } count = count + 1;
7
We need to know more than we did! if (count > 120) Comparison needs to be enclosed { This symbol means ‘start block’ count = 0; Statements end with ‘;’ current_sprite = 1; current_sprite is our variable. if (current_sprite > 2) current_sprite = 0; if (current_sprite==0) ‘==‘ means ‘is equal to’ sprite_index= spr_electron; changes the sprite! else if (current_sprite == 1) sprite_index = spr_proton; else sprite_index = spr_neutron; } count = count + 1; Add 1 to count
8
We need to know more than we did! sprite_index= spr_electron; This is the name of a variable that the GameMaker system uses to keep track of the sprite being displayed. This is the name of the sprite for an electron. We gave it this name. New thing: there are a lot of names defined by Game Maker that we need to know in order to accomplish tasks using our program. Sprite_index used to be changed by the ‘change sprite’ command, and we did not need to know what the name of the variable was.
9
Variables and assignments Name = expression; This is an assignment, and gives the named variable a value. The value is expressed in terms of things whose value we already know, or it won’t mean anything. SO: x = 12; y = x+10; y = x*10; y = x/2; y = x-10; distance = x*x + y*y;
10
Variables and assignments List of variables: http://gamemaker.wikicomplete.info/list-of-variables Game Maker also has functions. We have seen some: sine, for instance A function is really a name for something whose value is computed, each time you ask for it, perhaps depending on other variables or numbers. Sine is computed, and it depends on an angle that we give it. y = random (100); Sets the variable y to a random number between 0 and 99.
11
Variables and assignments y = random (100) Variable to get a new value. Function name, in this case the random number generator. Parameter – a value that determines the result of the function. There are many functions available, and you need to know their names to use them. Look them up at http://wiki.yoyogames.com/index.php/Category:Game_Maker_Functions
12
Scripts A regular Gamemaker program is about objects and events. You manipulate those things directly using the mouse. A script is also about those things, but manipulates things by the use of their names (like wizards- if you know a wizard’s name, you can make him do what you want). Some names you make up, some you are provided.
13
Objects & Scripts Each object can have scripts associated with it, and those can be made to execute whenever you choose. When an event happens, is the usual situation. So let’s write a few short ones together. Ideas?
14
Motion Have an object move in a circle. An easy one…
15
Motion - initial.
16
Motion - step.
17
Result
18
Details We could use math to design this. Didn’t. I set the speed to 6 when the electron is created I then placed the electron in the room so that it circled the proton. This took a couple of tries. The faster the electron moves, the greater must be the direction change each step for a given radius.
19
Example 2 Off the top of my head …. How about an aquarium. Bubbles rise from the bottom and pop at the top. Oh, and bubbles get bigger as they rise! We need to find an aquaruium background, a popping sound, a bubble sprite.
20
Example 2 Sprites are easy. Use Google, search for images. Always attribute images in artworks and games, by the way. I edited the aquarium image to be the right size.
21
Example 2 So, new things. 1.Create a background for the room. Right click ‘backgrounds’, ‘new background’.
23
Example 2 So, new things. 1.Create a background for the room. Right click ‘backgrounds’, ‘new background’. Load Background (from an image file) OK Then, under ‘Room properties’/Backgrounds, load the image.
24
Load image as Background
25
New things 2: sounds 2. Load the sound Right click ‘sounds’, ‘new sound’. It’s like a sprite. Load the sound from the file you downloaded. OK
26
Create 3. Arrange the bubble object Create and assign the bubble sprite. Events: step Make it rise Increase size as it rises Sound when it reaches the top. Destroy it and create a new one at the bottom
27
New things 2: sounds Sounds-> new sound Sound file is ‘pop.mp3’ Name is ‘pop’
28
New things 3: script 3. Arrange the bubble object Create and assign the bubble sprite. Load the bubble image (center) into a new sprite. Create the bubble object, assign it the bubble sprite. Place some sprites in the room at the beginning
29
New things 3: script 3. Arrange the bubble object Create and assign the bubble sprite.
30
New things 3: script 3. Arrange the bubble object Step event Bubble moves upwards, gets bigger, and pops at the top of the water.
31
New things 3: script Sprite Basic action on each step is to run a script and then see if the bubble has reached the top. Script makes the Bubble change size.
32
New things 3: script 3. Arrange the bubble object Script Making the bubble get bigger means looking for a variable or function that will scale or resize the image. image_xscale and image_yscale allow you to specify a scale factor. 1.0 means use the basic size, 0.5 means half size, 2.0 means double size. Need to rescale in X and Y directions.
33
New things 3: script Room is 624 wide by 464 high. Aquarium ends at y=144 Aquarium width runs from 32- 592 So: as the bubble moves from 464 to 144, it gets bigger. (0,0) (0,464)
34
New things 3: script So the image is smallest (nearest to scale of 1.0) at the bottom (y=464) IE size is 1.0 of normal there. As bubble rises, y gets smaller so we can’t use Y to make the bubble grow. We *can* use 464-y, which gets bigger as y gets smaller (distance from bottom grows, so does bubble size). Scale = 1.0 + (464-y) ??
35
New things 3: script Scale = 1.0 + (464-y) ?? (at the top this is 1.0 + (464-144) = 321! No, this gets too big. If the bubble is to double in size, then scale = 2.0 at the top, or scale = 2.0 at y=144 scale = 1.0 + s(464-y) So s = (2-1)/320 = 0.003125
36
New things 3: script If you don’t like the arithmetic, note that my first implementation used Scale = 1.0 + 0.01(400-y) I jiggered it to be Scale = 1.0 + 0.005(400-y) This worked pretty well. THEN I did the math. So s = 0.003125
37
New things 3: script Here’s the script. Xscale and yscale change as we said. Last line makes the bubble go Up. (y gets smaller).
38
New things 3: script Here it goes. (screen capture sound does not work)
39
New things 3: script The destroy/recreate places the new object instance at a random position within the bottom part of the aquarium.
40
Processing This is the last lecture on Game Maker. We know what it can teach us about programming. Questions?
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.