270 likes | 400 Views
CRE Programming Club Class 6. Robert Eckstein and Robert Heard. Introducing Arrays. An array can store more than one value at a time. For example, if you wanted to store the names of five users, you could create five variables, or you can create just one array to store all five names.
E N D
CRE Programming ClubClass 6 • Robert Eckstein and Robert Heard
Introducing Arrays • An array can store more than one value at a time. For example, if you wanted to store the names of five users, you could create five variables, or you can create just one array to store all five names. • An array has a name, and between braces is an index. You can create an array called name as: name[1], name[2], name[3], name[4], and name[5]. Here, 1, 2, 3, 4, and 5 are the indices for the name array.
Indices Can Be Strings! • You can also use strings as indices into arrays! • For example, you can create elements in the name arrays: name["a"], name["b"], name["c "], name["dumbledore"], and name["epsilon"]. • Why do we have to have the quotes?
Here is a Simple Array • Subjects[1] = "English" • Subjects[2] = "History" • Subjects[3] = "Computers" • Subjects[4] = "Science" • Subjects[6] = "Math"
There is Also an Array Object. It Helps You! • You can determine whether the specified variable is an array by using the IsArray() operation. • You can determine whether an array contains the specified index if you use the ContainsIndex() operation. This operation is helpful if you want to determine whether a specific value initializes the array’s index.
Import QWF562 • Subjects[1] = "English" • Subjects[2] = "History" • Subjects[3] = "Computers" • Subjects[4] = "Science" • Subjects[6] = "Math“ • TextWindow.WriteLine("Subjects is an array: " + Array.IsArray(Subjects)) • TextWindow.WriteLine("Subjects[4] has value: " +Array.ContainsIndex(Subjects, 4)) • TextWindow.WriteLine("Math is a value: " + Array.ContainsValue(Subjects, "Math")) • TextWindow.WriteLine("Number of items: " + Array.GetItemCount(Subjects))
Import QWF562 • list = Array.GetAllIndices(Subjects) • For i = 1 To Array.GetItemCount(Subjects) • TextWindow.WriteLine(list[i] + ":" + Subjects[list[i]]) • EndFor
GetAllIndices? • You can use the GetAllIndices operation to get all the indices for the array--in the form of another array! This operation is especially useful when you don’t know the indices of an array (maybe they’re strings!) • This operation creates another array. The indices are numbers starting with 1, while the values are the indices of the original array.
ImageList • ImageList is a simple class. It’s primary purpose is to load an image, either from across the Internet, or from the local disk. • When you use the LoadImage operation, you get back a unique name for the image as a String, which is auto-generated. • You can also find out the height and the width of any image that you loaded.
Import BKW869 • imageName = ImageList.LoadImage("http://creprogramming.com/mm_travel_photo.jpg") • TextWindow.WriteLine("Image name is " + imageName) • width = ImageList.GetWidthOfImage(imageName) • height = ImageList.GetHeightOfImage(imageName) • TextWindow.WriteLine("Image width is " + width) • TextWindow.WriteLine("Image height is " + height)
Import ZMR044 • imageName = ImageList.LoadImage("http://creprogramming.com/mm_travel_photo.jpg") • TextWindow.WriteLine("Image name is " + imageName) • width = ImageList.GetWidthOfImage(imageName) • height = ImageList.GetHeightOfImage(imageName) • TextWindow.WriteLine("Image width is " + width) • TextWindow.WriteLine("Image height is " + height) • GraphicsWindow.DrawImage(imageName, 30, 40) ‘ What does this do?
DrawImage? • Did you notice that the GraphicsWindow object has an operation called drawImage()? • Its purpose is to draw an image on the graphics windows. • This is very useful if you want to create a background picture for your game! Let’s start doing that!
Shapes • Last time, we drew rectangles and circles and ellipses directly to the graphics window. • Now, we’re going to use Shapes. Shapes allow us to create something that can be moved around, rotated, expanded, hidden, and shown on the GraphicsWindow. • Many of the Shape operations are similar to the GraphicsWindow operations. But Shapes “float above” the GraphicsWindow.
Shapes: Reference Documentation • Look at the Reference Documentation for the Shape object! • We can create shapes out of rectangles, ellipses, triangles, lines, text, and most importantly, images! • Once we use one of the add...() operations, the operation returns a String, which is the shape name.
Shapes: Reference Documentation • Once you get the shape name, the shape is registered, and you can do a number of things with that shape: • Instantaneously move the Shape to X,Y • Animate a move of the Shape to X,Y • Rotate the Shape • Zoom the Shape • Show/Hide the Shape
Zoom: Import VKF231 • hamster = ImageList.LoadImage("http://www.creprogramming.com/hamster.png") • hamsterShape = Shapes.AddImage(hamster) • currentZoom = .5 • currentZoomDelta = 0 • GraphicsWindow.BackgroundColor = "Black" • GraphicsWindow.Width = 650 • GraphicsWindow.Height = 650 • GraphicsWindow.KeyDown = handleKeyDown • GraphicsWindow.KeyUp = handleKeyUp • Shapes.Move(hamsterShape, 75, 50) • GraphicsWindow.Show()
Import VKF231 • continue = "True" • While (continue) • currentZoom = currentZoom + currentZoomDelta • If (currentZoom < .1) Then • currentZoom = .1 • ElseIf (currentZoom > 10) Then • currentZoom = 10 • EndIf • Shapes.Zoom(hamsterShape, currentZoom, currentZoom) • Program.Delay(10) • EndWhile
Import VKF231 • Sub handleKeyDown • If (GraphicsWindow.LastKey = "Up") Then • currentZoomDelta = -.01 • ElseIf (GraphicsWindow.LastKey = "Down") Then • currentZoomDelta = .01 • EndIf • EndSub • Sub handleKeyUp • currentZoomDelta = 0 • EndSub
Rotate: Import FPQ487 • spaceship = ImageList.LoadImage("http://www.creprogramming.com/spaceship.png") • spaceshipShape = Shapes.AddImage(spaceship) • currentHeading = 180 • currentHeadingDelta = 0 • GraphicsWindow.BackgroundColor = "Black" • GraphicsWindow.Width = 650 • GraphicsWindow.Height = 650 • GraphicsWindow.KeyDown = handleKeyDown • GraphicsWindow.KeyUp = handleKeyUp • GraphicsWindow.Show()
Import FPQ487 • continue = "True" • While (continue) • currentHeading = currentHeading + currentHeadingDelta • Shapes.Rotate(spaceshipShape, currentHeading) • Shapes.Move(spaceshipShape, 300, 300) • Program.Delay(10) • EndWhile
Import FPQ487 • Sub handleKeyDown • If (GraphicsWindow.LastKey = "Left") Then • currentHeadingDelta = -2 • ElseIf (GraphicsWindow.LastKey = "Right") Then • currentHeadingDelta = 2 • EndIf • EndSub • Sub handleKeyUp • currentHeadingDelta = 0 • EndSub
Now Import fpq487-0 • What has changed in this program? • How has the rotate and move commands been used in conjunction with the up, down, left, and right keys? • Don’t worry about the subroutine we used to determine displacement based on speed and heading. But feel free to use it for your own spaceship (or lasers, or enemies, etc.)!
Use These Programs... • ...as a springboard! Take this source code and expand on it! Think of some fun and original ideas! • Do you need graphics? Look around on the web to see what you can find. The best way to do this is to Google a topic and then press the “Images” link. • It’s best to store images at PNG or JPG.
Import FVJ492-1 • This program looks like one of the first video games that came out in 1979. It was called Space Invaders. • Something is not right with the space invaders. Watch for a little bit. What’s wrong with this program?
Your Assignments • Keep working on your clock assignment. It is due next Tuesday. • See if you can fix the space invader program so that the invaders move down and shift direction when they reach the right side. • BONUS: If any space invader reaches the bottom of the screen, end the program!