93
Visual Basic C++ Diane Zak

Visual BasicC++ Diane Zak. Microsoft © Small (But Powerful) Basic Presented by Diane Zak

Embed Size (px)

Citation preview

Visual Basic C++

Diane Zak

Microsoft© Small (But Powerful) Basic

Presented by Diane Zak

1GB Silicone Wrist Band Flash Drive

You are welcome to take them home!

OverviewWalk-throughs Code snippets ActivitiesWrap-up

OVERVIEW

What is it? 1. Language2. IDE3. Libraries

Created by: Vijaye Raji at Microsofthttp://smallbasic.com/

Language Variables Assignment statements Concatenation, arithmetic , relational, logical operators Mathematical functions Graphics (shapes and controls) If statements, For and While loops Arrays Sequential access file processing Text (string) manipulation methods Sub procedures and events

IDE

Intellisense

Help

LibrariesArray MouseClock NetworkControls ProgramDesktop ShapesDictionary SoundFile StackFlickr TextGraphicsWindow TextWindowImageList TimerMath Turtle

1. Language2. IDE3. Libraries

Requirements? Small Basic

Windows XP, Vista, 7

.NET Framework 3.5

Why would I want to learn it?

Fun and easy way to introduce programming

Intro to Computers

Intro to Programming

College Freshmen Day

High School Recruitment

How can I teach it?

Turtle ObjectTextWindowsequence onlyor combined with selection

TextWindowsequence, selection, repetition, sound, and random numbers

GraphicsWindowand Turtlesequence onlyor combined with selection and repetition

GraphicsWindowand Turtlesequence only

GraphicsWindowsequence and repetition

GraphicsWindowsequence, selection, repetition, random numbers, and counters

GraphicsWindowsequence, selection, arrays, controls, and procedures

GraphicsWindowsequence, selection, arrays, controls, and procedures

Walk-throughs

TextWindow

On your own:

1.Enter a TextWindow.Write statement that displays the message “Gallons: ”

2.Enter a TextWindow.ReadNumber statement that assigns the user’s input to the gallons variable

Arithmetic operations

+ addition (also can be used for concatenation)

– subtraction* multiplication/ division

Math.Power exponentiationMath.Floor integer division

mpg = miles / gallons

TextWindow.WriteLine(mpg)

Math object’s Round method

mpg = Math.Round(mpg)

String concatenation operator

+"Miles per gallon: " + mpg

Publish

Publish

Import

If Statement

If condition Thenstatements

[ElseIf condition Thenstatements]

[Elsestatements]

EndIf

If mpg > 30 Then TextWindow.WriteLine("Great")Else TextWindow.WriteLine("Not great")EndIf

Save using Mpg and then run. Then publish again.

Graduate to Visual Basic

Turtle

1. Close the mpg program.2. Open a new document.3. Save the new document as

MyFirstTurtle.

1. Create a 10 line program using the following commands. Keep in mind that the default height and width of the GraphicsWindow is 442 and 624 pixels, respectively.

2. Save and then run.

Turtle.Speed = number from 1 through 10Turtle.Move(number of pixels)Turtle.MoveTo(x, y) Turtle.TurnLeft()Turtle.TurnRight()

1. Close the MyFirstTurtle program.2. Open the Hi.sb file contained in the

Class folder.3. Run the program.

1. ' Lines 2 through 8 will center the GraphicsWindow2. GraphicsWindow.Hide()3. gwH = 4004. gwW = 2255. GraphicsWindow.Left = (Desktop.Width - gwW) / 26. GraphicsWindow.Top = (Desktop.Height - gwH) / 27. GraphicsWindow.Height = gwH8. GraphicsWindow.Width = gwW

10. ' Line 11 displays text on the title bar11. GraphicsWindow.Title = "Hi"

13. ' Line 14 shows the GraphicsWindow14. GraphicsWindow.Show()

16. ' Line 17 sets the Turtle's speed17. Turtle.Speed= 9

19. ' position turtle and draw left vertical line in H20. Turtle.X = 5021. Turtle.Y = 35022. Turtle.Move(250)

24. ' reposition turtle and draw right vertical line in H

25. Turtle.PenUp()26. Turtle.X = 10027. Turtle.Angle = 18028. Turtle.PenDown()29. Turtle.Move(250)

31. ' reposition turtle and draw horizontal line in H

32. Turtle.PenUp()33. Turtle.Y = 20034. Turtle.TurnRight()35. Turtle.PenDown()36. Turtle.Move(50)

38. ' reposition turtle and draw vertical line in i

39. <lift the pen>40. <set the turtle’s X property to 150>41. <set the turtle’s Y property to 350>42. <turn the turtle to the right>43. <put the pen down>44. <move the turtle appropriately>

38. ' reposition turtle and draw vertical line in i

39. Turtle.PenUp()40. Turtle.X = 15041. Turtle.Y = 35042. Turtle.TurnRight()43. Turtle.PenDown()44. Turtle.Move(200)

Answer

Drawing Shapes

46. ' draw circle above vertical line in i47. <use the GraphicsWindow’s DrawEllipse method (Hint: use 145 and 100 for X and Y, and use 10 for the width and height>

Drawing Shapes

46. ' draw circle above vertical line in i47. GraphicsWindow.DrawEllipse(145, 100, 10, 10)

Answer

1. Complete Lines 49 through 65.2. Save and then run.

Images and Sound

1. Close the Hi program.2. Open the PicViewer.sb file

contained in the Class folder.3. Run the program to see the effect

of the existing code.

13. ' Lines 14 through 16 load the images and assign names to each14. imgBirds = ImageList.LoadImage(Program.Directory + "\birds.jpg")15. imgCat = ImageList.LoadImage(Program.Directory + "\cat.jpg")16. imgTheEnd = ImageList.LoadImage(Program.Directory + "\TheEnd.jpg")

18. ' Lines 20 through 22 make each loaded image a Shape object that can19. ' be moved, animated, shown, hidden, or rotated20. birds = Shapes.AddImage(imgBirds)21. cat = Shapes.AddImage(imgCat)22. theEnd = Shapes.AddImage(imgTheEnd)

24. ' Lines 25 through 27 position each Shape object at the right edge of the GraphicsWindow25. Shapes.Move(birds, 650, 0)26. Shapes.Move(cat, 650, 0)27. Shapes.Move(theEnd,650, 0)

29. ' Lines 31 through 33 hide each Shape object (this is necessary because the30. ' AddImage method automatically displays them on the GraphicsWindow31. Shapes.HideShape(birds)32. Shapes.HideShape(cat)33. Shapes.HideShape(theEnd)

35. ' Line 36 shows the GraphicsWindow36. GraphicsWindow.Show()

38. ' enter the code to play a wav file named PicViewer.wav

39. <Use the Sound object’s Play method, the Program object’s Directory method, and the concatenation operator to play the "\PicViewer.wav" file.>

38. ' enter the code to play a wav file named PicViewer.wav39. Sound.Play(Program.Directory + "\PicViewer.wav")

Answer

41. ' Lines 42 through 44 show the birds Shape object, animate it, and then delay the program

42. Shapes.ShowShape(birds)43. Shapes.Animate(birds, -500, 0, 6000)44. Program.Delay(4000)

1. Complete Lines 46 through 54.2. Save and then run.3. For an additional challenge,

complete the DIFFERENT VERSION section.

Events and

Sub Procedures

1. Close the PicViewer program.2. Open a new document.3. Save the new document as Hello.

1. Enter the following two lines of code:txtName = Controls.AddTextBox(100, 50)btnHello = Controls.AddButton("Hello", 100, 100)

2. Save and then run.

1. Enter Controls followed by a period.2. Press the down arrow until the ButtonClicked

event is selected.

Finish the statement as follows:

Controls.ButtonClicked = OnButtonClick

Now we need to create the OnButtonClick event procedure. Enter the following lines of code:

5. Sub OnButtonClick6. myName = Controls.GetTextBoxText(txtName)7. GraphicsWindow.ShowMessage(myName, "Name Entry")8. EndSub

1. Save and then run.2. Click the text box and then type your

name.3. Click the Hello button.

ActivitiesNow it’s time for you to have some

fun!

Take your pick:

1.Create the Space Invaders game using the handout provided to you.2.Look in the EXE folder on your flash drive for a program you might like to create. Then, locate the partially completed .sb file on your flash drive. The FolderContents.doc file on your flash drive provides some additional information about the program.3.Create your own Small Basic program.4.Check out the Coin Toss.sb program in the Coin Toss folder.5.Import one or more of the following programs/games:

XHX455 (Click Shapes) KXM316-0 (Calculator)CBX615 (Flower) LWL206-3 (Music Notes)MDJ923 (Paddle Game) PMT149 (Bouncing Balls)

Thank you for coming!

I hope you enjoyed the presentation

Have a great day!