1 / 27

CRE Programming Club Class 6

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.

Télécharger la présentation

CRE Programming Club Class 6

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CRE Programming ClubClass 6 • Robert Eckstein and Robert Heard

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

  3. 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?

  4. Here is a Simple Array • Subjects[1] = "English" • Subjects[2] = "History" • Subjects[3] = "Computers" • Subjects[4] = "Science" • Subjects[6] = "Math"

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

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

  7. Import QWF562 • list = Array.GetAllIndices(Subjects) • For i = 1 To Array.GetItemCount(Subjects) • TextWindow.WriteLine(list[i] + ":" + Subjects[list[i]]) • EndFor

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

  9. What if the Subjects Array was This?

  10. This is the array that GetAllIndices returns...

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

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

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

  14. 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!

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

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

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

  18. 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()

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

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

  21. 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()

  22. Import FPQ487 • continue = "True" • While (continue) • currentHeading = currentHeading + currentHeadingDelta • Shapes.Rotate(spaceshipShape, currentHeading) • Shapes.Move(spaceshipShape, 300, 300) • Program.Delay(10) • EndWhile

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

  24. 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.)!

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

  26. 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?

  27. 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!

More Related