Show What You Know Answer Key

On this page, you will find the answer key for the Show What You Know section from each lesson.

Lesson 1.1: Introduction to Small Basic

  1. A programming language.
  2. IntelliSense.
  3. By clicking Run on the Toolbar or by pressing F5 on the keyboard.

Lesson 1.2: Statements, Properties, and Operations

TextWindow.Top = 100
TextWindow.Left = 200
TextWindow.Title = "Small Basic Programming"
TextWindow.CursorTop = 10
TextWindow.CursorLeft = 20
TextWindow.ForegroundColor = "Yellow"
TextWindow.WriteLine("Welcome to the world of Small Basic programming.")

Lesson 1.3: Variables

You can use the following formulas for calculation:

radius = diameter / 2

area = (3.14) * radius * radius

circumference = (3.14) * 2 * radius

Solution:

TextWindow.Write(“What is the diameter of the circle? ")

diameter = TextWindow.ReadNumber()

radius = diameter / 2

area = (3.14) * radius * radius

circumference = (3.14) * 2 * radius

TextWindow.WriteLine("The area of the circle is " + area + ".")

TextWindow.WriteLine("The circumference of the circle is " + circumference + ".")

Lesson 1.4: Conditions and Loops

TextWindow.Write("How many student grades would you like to calculate? ")

number = TextWindow.Read()

n = 1

While n <= number

  TextWindow.Write("What is the student’s percentage? ")

  percentage = TextWindow.Read()

  If  percentage >= 75 Then

    TextWindow.WriteLine("The student’s grade is A.")

  ElseIf  percentage < 75 And percentage >= 60  Then

    TextWindow.WriteLine("The student’s grade is B.")

  ElseIf  percentage < 60 And percentage >= 35 Then

    TextWindow.WriteLine("The student’s grade is C.")

  Else

    TextWindow.WriteLine("The student’s grade is D.")

  EndIf

  n = n + 1

EndWhile

Lesson 1.5: Branching and Subroutines

If i < 10 then

  TextWindow.WriteLine("")

  TextWindow.Write("What is the name of a city? ")

  city = TextWindow.Read()

  TextWindow.Write("How warm is that city (in degrees Celsius)? ")

  temp = TextWindow.Read()

  TextWindow.Write("Is it rainy (Y/N)? ")

  rainy = TextWindow.Read()

  TextWindow.Write("Is it windy (Y/N)? ")

  windy = TextWindow.Read()

  'Calling subroutines

  subtempCount()

  subrainyCount() 

  subwindyCount()

  i = i + 1  

  If i = 10 Then

    subOutput()

  EndIf

EndIf

Goto up

Sub subtempCount

  If temp <= 5 Then

        ColdCount = ColdCount + 1

  ElseIf temp <= 15 Then

        CoolCount = CoolCount + 1     

  ElseIf temp <= 25 Then

        WarmCount = WarmCount + 1    

  Else

        HotCount = HotCount + 1    

  EndIf 

EndSub

Sub subRainyCount

    If Rainy = "y" Or Rainy = "Y" Then

       RainyCount = RainyCount + 1

    EndIf

EndSub

Lesson 2.1: Graphics Window

GraphicsWindow.Show()

GraphicsWindow.Title = "A Graphics Window"

GraphicsWindow.Height = 640

GraphicsWindow.Width = 800

GraphicsWindow.BackgroundColor = "Black"

GraphicsWindow.PenWidth = 10

GraphicsWindow.PenColor = "Gold"

GraphicsWindow.DrawLine(65, 100, 65, 370)

GraphicsWindow.PenColor = "Black"

GraphicsWindow.BrushColor = "Cyan"

GraphicsWindow.DrawEllipse(70, 250, 100, 100)

GraphicsWindow.FillEllipse(70, 250, 100, 100)

For i = 1 To 10

  GraphicsWindow.PenColor = GraphicsWindow.GetRandomColor()

  GraphicsWindow.PenWidth = 2

  GraphicsWindow.Drawrectangle(100, i * 20, 50, 10)

EndFor

image1 = "C:\Small Basic\Winter.jpg"

GraphicsWindow.DrawResizedImage(image1, 200, 100, 500, 500)

GraphicsWindow.ShowMessage("Have a nice day!", "Message")

Lesson 2.2: Turtle Graphics

GraphicsWindow.Height = 400

GraphicsWindow.Width = 400

GraphicsWindow.Title = "Turtle Graphics"

Turtle.Show()

Turtle.Speed = 10

Turtle.X = 100

Turtle.Y = 200

For i = 0 To 200 Step 5 

  GraphicsWindow.PenColor = GraphicsWindow.GetRandomColor()

  Turtle.Move(i) 

  Turtle.Turn(150)

EndFor

Turtle.PenUp()

Turtle.Move(300)

Turtle.Turn(105)

Turtle.Move(150)

Turtle.PenDown()

For i = 0 To 200 Step 5 

  GraphicsWindow.PenColor = GraphicsWindow.GetRandomColor()

  Turtle.Move(i) 

  Turtle.Turn(150)

EndFor

Lesson 2.3: Exploring Shapes

GraphicsWindow.Title = "Exploring Shapes"

GraphicsWindow.Height = 200

GraphicsWindow.Width = 300

GraphicsWindow.PenColor = "Purple"

base = Shapes.AddLine(0, 0, 300, 0)

Shapes.Move(base, 0, 100)

GraphicsWindow.PenColor = "Black"

GraphicsWindow.BrushColor = "Cyan"

circle = Shapes.AddEllipse(50, 50)

Shapes.Move(circle, 0, 50)

Shapes.Animate(circle, 250, 50, 1000)

Lesson 2.4: Sound, Program, and Text Objects

TextWindow.Title = "Let’s take a quiz…"

TextWindow.WriteLine("Name the third planet from the Sun: ")

answer = TextWindow.Read()

If (Text.StartsWith(answer, "Earth") Or Text.StartsWith(answer, "earth")) Then

  Sound.PlayChime()

  TextWindow.WriteLine("You are absolutely right!")

Else

  TextWindow.WriteLine("That is incorrect.")

  Sound.PlayBellRing()

EndIf

Lesson 2.5: Clock, Desktop, and Dictionary Objects

subSetAlarm()

imagePath = program.Directory + "\desk.jpg"

up:

If Clock.Time = alarm Then 

    Desktop.SetWallPaper(imagePath)  

    Sound.PlayBellRing()           

EndIf

Program.Delay(1000)

Goto up

Sub subSetAlarm

  TextWindow.WriteLine("Set Alarm")

  TextWindow.Write("Enter Hour: ")

  hour = TextWindow.Read()

  TextWindow.Write("Enter Minute: ")

  minute = TextWindow.Read()

  TextWindow.Write("AM/PM?: ")

  AmPmStatus = TextWindow.Read()

  alarm = hour + ":" + minute + ":00 " + AmPmStatus

  TextWindow.WriteLine("Alarm Time: " + alarm)

EndSub

Lesson 2.6: Flickr, ImageList, and Network Objects

While picture < 10

  ImagePath = Flickr.GetRandomPicture("Animals")     

  Images = ImageList.LoadImage(ImagePath)

  GraphicsWindow.Height = ImageList.GetHeightOfImage(Images)

  GraphicsWindow.Width = ImageList.GetWidthOfImage(Images)      

  GraphicsWindow.DrawImage(Images, 0, 0)

  Program.Delay(2000) 

  picture = picture + 1

EndWhile

Lesson 3.1: File Input and Output

TextWindow.Write("Enter the name of the new directory: ")

DirectoryName = TextWindow.Read()

File.CreateDirectory(DirectoryName)

filepath = "\\myserver\Share\FileIO.txt"

downloadpath = Network.DownloadFile(filepath)

If File.CopyFile(downloadpath, DirectoryName) = "SUCCESS" Then 

  TextWindow.WriteLine("File has been downloaded from the network and copied to: " + DirectoryName)

  files = File.GetFiles(DirectoryName)       

  TextWindow.WriteLine("This is the content in the file: ")  

  TextWindow.WriteLine(File.ReadContents(files[1]))

  TextWindow.Write("Enter data to be added in the file:")

  AppendedData = TextWindow.Read()

  File.AppendContents(files[1]," " + AppendedData)

  TextWindow.WriteLine("File content after adding data is as follows: ")  

  TextWindow.WriteLine(File.ReadContents(files[1]))

EndIf

Lesson 3.2: Stacks and Arrays

TextWindow.WriteLine("Flight Reservations")

TotalSeats = 10

For i = 1 To TotalSeats

  TextWindow.Write("Enter Passenger Name: ")

  Name[i] = TextWindow.Read() 

  TextWindow.WriteLine("Seat number " + i + " is reserved by " + Name[i])

  GetDetails()

EndFor

Sub GetDetails

  If Array.GetItemCount(Name) = TotalSeatsThen

    TextWindow.WriteLine("No more seats are available!")

  Else

    Array.GetItemCount(Name)

    AvailableSeats = TotalSeats - Array.GetItemCount(Name)   

    TextWindow.WriteLine("Number of available seats is: " + AvailableSeats)

    TextWindow.WriteLine("")   

  EndIf

EndSub

Lesson 3.3: The Math Object

Solution 1:

Rectangle = Shapes.AddRectangle(100, 100)

Shapes.Move(Rectangle, 250, 150)

For i = 0 To Math.GetRandomNumber(30)

  Program.Delay(500)

  Shapes.Rotate(Rectangle, i * 25) 

EndFor

Solution 2:

start:

GraphicsWindow.Width = 600

GraphicsWindow.Height = 450

TextWindow.Write("Enter a radius for the circle: ")

Radius = TextWindow.Read()

Area = Math.Pi * Radius * Radius

Height = Area / 2

Width = Area / 2

x = Math.GetRandomNumber(GraphicsWindow.Width / 2)

y = Math.GetRandomNumber(GraphicsWindow.Height / 2)

GraphicsWindow.DrawEllipse(x, y, Height, Width)

Gotostart

Lesson 3.4: Events and Interactivity

GraphicsWindow.Hide()

w = 620

h = 450

GraphicsWindow.CanResize = "False"

GraphicsWindow.Width = w

GraphicsWindow.Height = h

GraphicsWindow.Top = (Desktop.Height-h) / 2

GraphicsWindow.Left = (Desktop.Width-w) / 2

GraphicsWindow.Show()

GraphicsWindow.Title = "Events and interactivity"

GUI()

Controls.ButtonClicked = MouseAction

Sub GUI 

  GraphicsWindow.DrawRectangle(10, 10, 600, 320)

  GraphicsWindow.DrawRectangle(10, 340, 200, 100)

  GraphicsWindow.DrawRectangle(10, 340, 600, 100)

  GraphicsWindow.DrawRectangle(370, 340, 150, 100)  

  Triangle = Controls.AddButton("Triangle", 40, 345)

  Controls.SetSize(Triangle, 120, 30)

  Rectangle = Controls.AddButton("Rectangle",40,375)

  Controls.SetSize(Rectangle, 120, 30)

  Circle = Controls.AddButton("Circle", 40, 405)

  Controls.SetSize(Circle, 120, 30)

  Rotate = Controls.AddButton("Rotate", 230, 360)

  Controls.SetSize(Rotate, 60, 60)

  Zoom = Controls.AddButton("Zoom", 290, 360)

  Controls.SetSize(Zoom, 60, 60)

  FreeHand = Controls.AddButton("Draw", 390, 360)

  Controls.SetSize(FreeHand, 60, 60)  

  Clear = Controls.AddButton("Clear", 450, 360)

  Controls.SetSize(Clear, 60, 60)

  Exit = Controls.AddButton("Exit", 530, 360)

  Controls.SetSize(Exit, 60, 60)    

EndSub

Sub MouseAction

  x = GraphicsWindow.MouseX

  y = GraphicsWindow.MouseY  

  GraphicsWindow.PenWidth = 1 

  If x > 40 And x < 160 Then    

    GraphicsWindow.BrushColor = GraphicsWindow.GetRandomColor() 

    If y > 345 And y < 375 Then       

      draw = 0

      j = 0

      z = 1

      tri = Shapes.AddTriangle(20, 20, 100, 100, 150, 20)

      Shapes.Move(tri, 80, 100)

    EndIf  

    If y > 375 And y < 405 Then

      draw = 0     

      j = 0 

      z = 2  

      rect = Shapes.AddRectangle(100, 100)

      Shapes.Move(rect, 250, 150)

    EndIf    

    If y > 405 And y < 435 Then

      draw = 0

      j = 0      

      z = 3      

      circ = Shapes.AddEllipse(100, 100)

      Shapes.Move(circ, 400, 150)

    EndIf      

  EndIf    

  If y > 360 And y < 420 Then       

    If x > 230 And x < 290 Then          

      draw = 0   

      If z = 1 Then        

        Shapes.Rotate(tri, 30 + m)

      Else

        If z = 2 Then     

          Shapes.Rotate(rect,30 + m)  

        Else

          If z = 3 Then     

            Shapes.Rotate(circ, 30 + m)    

          Endif           

        Endif 

      Endif

      m = m + 30             

    EndIf      

    If x > 290 And x < 390 Then

      draw = 0         

      i = 0.1 + j

      If i < 0.4 Then  

        If z = 1 Then        

          Shapes.Zoom(tri, 1 + i, 1 + i)

        Else

          If z = 2 Then     

            Shapes.Zoom(rect, 1 + i, 1 + i)

          Else

            If z = 3 Then     

              Shapes.Zoom(circ, 1 + i, 1 + i)      

            EndIf    

          EndIf 

        EndIf     

        j = j + 0.1

      EndIf

    EndIf         

    If x > 390 And x < 450 Then

      draw = 1

      Paint()

    EndIf     

    If x > 450 And x < 510 Then

      draw = 0

      j = 0

      GraphicsWindow.Clear()   

      GraphicsWindow.BrushColor = "Blue"

      GUI()

      draw = 0

    EndIf     

    If x > 530 And x < 590 Then

      draw = 0

      Program.End()

    EndIf     

  EndIf     

EndSub

Sub Paint

  If draw = 1 Then    

    GraphicsWindow.MouseMove = MouseDrag         

  Else

    If Mouse.IsLeftButtonDownThen    

      MouseAction()   

    EndIf

  EndIf    

EndSub

Sub MouseDrag 

  If draw = 1 then 

    x = GraphicsWindow.MouseX

    y = GraphicsWindow.MouseY   

    If x > 10 And x < 600 And y > 10 And y < 320 Then   

      If Mouse.IsLeftButtonDownThen

        GraphicsWindow.DrawLine(OrgX, OrgY, x, y)

      EndIf    

    EndIf 

    OrgX = x

    OrgY = y

  EndIf

EndSub 

Lesson 3.5: The Controls Object

GraphicsWindow.Show()

GraphicsWindow.Width = 300

GraphicsWindow.Height = 250

GraphicsWindow.Top = (Desktop.Height - 450) / 2

GraphicsWindow.left = (Desktop.Width - 600) / 2

GraphicsWindow.Title = "Application Form:"

x = 30

y = 37

'Enter Name

GraphicsWindow.DrawText(x, y, "Name:")

NameTxtBox = Controls.AddTextBox(120, 35)

Controls.SetSize(NameTxtBox, 150, 20)

'Address

GraphicsWindow.DrawText(x, y + 40, "Address:")

AddressTxtBox = Controls.AddMultiLineTextBox(120, 70)

Controls.SetSize(AddressTxtBox, 150, 40)

'Telephone details

GraphicsWindow.DrawText(x, y + 90, "Telephone:")

TelphoneTxtBox = Controls.AddTextBox(120, 125)

Controls.SetSize(TelphoneTxtBox, 150, 20)

'Email -id

GraphicsWindow.DrawText(x, y + 125, "E-mail ID:")

EmailTxtBox = Controls.AddTextBox(120, 160)

Controls.SetSize(EmailTxtBox, 150, 20)

'Submit

SubmitButton = Controls.AddButton("Submit", x + 30, y + 160)

Controls.SetSize(SubmitButton, 70, 30)

'Cancel

CancelButton = Controls.AddButton("Cancel", x + 130, y + 160)

Controls.SetSize(CancelButton, 70, 30)

Controls.ButtonClicked = SubmitForm

Sub SubmitForm

  Name = controls.GetTextBoxText(NameTxtBox)

  Address = controls.GetTextBoxText(AddressTxtBox)

  Telphone = controls.GetTextBoxText(TelphoneTxtBox)

  Email = controls.GetTextBoxText(EmailTxtBox)

  If Controls.GetButtonCaption(Controls.LastClickedButton) = "Submit" Then

    If Name = "" or Address = "" or Telphone = "" or Email = "" Then     

       GraphicsWindow.ShowMessage("Please fill in all the details.", "Error Message")

    Else

       GraphicsWindow.ShowMessage("Your details have been submitted successfully!", "Congratulations!")

    EndIf   

  ElseIf Controls.GetButtonCaption(Controls.LastClickedButton) = "Cancel" Then

     Program.End()   

  EndIf

EndSub

Lesson 3.6: Debugging Aids

Please Note: Ensure that a folder containing the images exists at the path specified in the program.

ImagePath = program.Directory + "\img"    

    i = 1

    GraphicsWindow.Width = 600

    GraphicsWindow.Height = 450

    GraphicsWindow.Top = (Desktop.Height-450) / 2

    GraphicsWindow.left = (Desktop.Width-600) / 2

    GUI()

    SmallBasicImage = File.GetFiles(ImagePath)   

    GraphicsWindow.DrawResizedImage(SmallBasicImage[i], 50, 50, 500, 300)    

    Controls.ButtonClicked = SlideShow   

    Sub GUI      

      GraphicsWindow.BackgroundColor="LightSlateGray"   

      GraphicsWindow.FontSize=25      

      nxtBtn = Controls.AddButton("<",50,370) 

      bckBtn = Controls.AddButton(">",500,370)

      Controls.SetSize(nxtBtn, 50, 50)

      Controls.SetSize(bckBtn, 50, 50)                

    EndSub   

Sub SlideShow          

      If Controls.GetButtonCaption(Controls.LastClickedButton) = ">" Then 

      If i < Array.GetItemCount(SmallBasicImage) Then

             i = i + 1                        

             images[i] = ImageList.LoadImage(SmallBasicImage[i])                    

             GraphicsWindow.DrawResizedImage(images[i], 50, 50, 500, 300)

             TextWindow.WriteLine("Image Name:" + SmallBasicImage[i])             

      EndIf       

      EndIf       

      If Controls.GetButtonCaption(Controls.LastClickedButton) = "<" Then       

      If i > 1 Then

          i = i - 1                

          images[i] = ImageList.LoadImage(SmallBasicImage[i])       

          GraphicsWindow.DrawResizedImage(images[i], 50, 50, 500, 300)           

          TextWindow.WriteLine("Image Name:" + SmallBasicImage[i])             

      EndIf        

      EndIf     

    EndSub

Lesson 4.1: Playing with Shapes

Solution:

' Copyright (c) Microsoft Corporation.  All rights reserved.

GraphicsWindow.Hide()

gw = 620

gh = 450

endtime = Clock.ElapsedMilliseconds

starttime = Clock.ElapsedMilliseconds

blinktime = Clock.ElapsedMilliseconds

gamestarttime = Clock.ElapsedMilliseconds

GraphicsWindow.CanResize = "False"

GraphicsWindow.Width = gw

GraphicsWindow.Height = gh

GraphicsWindow.Top = ( Desktop.Height - gh ) / 2

GraphicsWindow.Left = ( Desktop.Width - gw ) / 2

GraphicsWindow.Title = "Flower"

GraphicsWindow.BrushColor ="Pink"

GraphicsWindow.Show()

CreateUI()

GraphicsWindow.MouseDown = MouseAction

Controls.ButtonClicked = OnclickButton

starttimer()

Sub MouseAction     

  x = GraphicsWindow.MouseX

  y = GraphicsWindow.MouseY

  GraphicsWindow.MouseMove = MouseMove

  If x > px And x < px + 180 And y > py And y < py + 400 Then    

    GraphicsWindow.MouseUp = onMouseUpEvent

    If x < px + 90 and y < py + 100 Then          

      sh = leaf1   

      bsh = leafs[1]

      GetShapeXY()

    ElseIf x < px + 200 And y < py + 100 Then   

      sh = petal1

      bsh = petals[1]

      GetShapeXY()                                                            

    ElseIf x < px + 90 and y < py + 200 Then

      sh = stik

      bsh = stick

      GetShapeXY()       

    ElseIf x < px + 200 And y < py + 200 Then 

      sh = circle 

      bsh = circ

      GetShapeXY()               

    ElseIf x < px + 90 And y < py + 300 Then

      sh = petal2

      bsh = petals[2]

      GetShapeXY()               

    ElseIf x < px + 200 And y < py + 300 then

      sh = petal3 

      bsh = petals[3]

      GetShapeXY()             

    ElseIf x < px+90 And y < py + 400 Then

      sh = petal4 

      bsh = petals[4]

      GetShapeXY()       

    ElseIf x < px + 200 And y < py + 400 Then

      sh = leaf2

      bsh = leafs[2]

      GetShapeXY()       

    EndIf 

  EndIf

EndSub

Sub GetShapeXY  

  shx = shapes.GetLeft(sh)

  shy = shapes.GetTop(sh)

EndSub

Sub CreateUI    

  GraphicsWindow.DrawRectangle(10, 10, 380, 420)

  GraphicsWindow.DrawRectangle(410, 10, 200, 420)       

  GraphicsWindow.BrushColor = "Brown"   

  stick = Shapes.AddRectangle(5, 180) 

  Shapes.Move(stick, 174, 238)     

  GraphicsWindow.BrushColor = "Pink"

  petals[1] = Shapes.AddEllipse(70, 55) 

  Shapes.Move( petals[1], 93, 115)

  Shapes.Rotate( petals[1], 15)

  petals[2] = Shapes.AddEllipse(70, 55) 

  Shapes.Move(petals[2], 148, 81)

  Shapes.Rotate(petals[2], 105) 

  petals[3] = Shapes.AddEllipse(75, 55)

  Shapes.Move( petals[3], 185, 135 )

  Shapes.Rotate( petals[3], 200 )

  petals[4] = Shapes.AddEllipse(75, 55) 

  Shapes.Move( petals[4], 135, 175)

  Shapes.Rotate( petals[4], 90) 

  GraphicsWindow.BrushColor = "Yellow"

  circ = Shapes.AddEllipse(25, 25) 

  Shapes.Move(circ, 162, 140)

  GraphicsWindow.BrushColor = "Green"

  For i = 1 To 2

    leafs[i] = Shapes.AddEllipse(50, 25) 

    Shapes.Move(leafs[i], 223 -(i * 48), 258)

    Shapes.Rotate(leafs[i], 180 * i) 

  EndFor

  Hide() 

  px = 420

  py = 20

  pw = 90

  ph = 100

  GraphicsWindow.DrawRectangle(px, py, 180, 200)

  GraphicsWindow.DrawRectangle(px, 220, 180, 200)

  GraphicsWindow.DrawRectangle(px, py, pw, ph)

  GraphicsWindow.DrawRectangle(px + 90, py, pw, ph)

  GraphicsWindow.DrawRectangle(px, py + 100, pw, ph) 

  GraphicsWindow.DrawRectangle(px + 90, py + 100, pw, ph)

  GraphicsWindow.DrawRectangle(px, py + 200, pw, ph)

  GraphicsWindow.DrawRectangle(px + 90, py + 200, pw, ph)

  GraphicsWindow.DrawRectangle(px, py + 300, pw, ph)

  GraphicsWindow.DrawRectangle(px + 90, py + 300, pw, ph)

  GraphicsWindow.BrushColor = "Green"

  leaf1 = Shapes.AddEllipse(50, 25) 

  Shapes.Move(leaf1, px + 20, py + 40)

  leaf2 = Shapes.AddEllipse(50, 25) 

  Shapes.Move(leaf2, px + 115, py + 340) 

  GraphicsWindow.BrushColor = "Pink"

  petal1 = Shapes.AddEllipse(65, 50) 

  Shapes.Move(petal1, px + 105, py + 30)

  Shapes.Rotate(petal1, 15)

  petal2 = Shapes.AddEllipse(65, 50) 

  Shapes.Move(petal2, px + 20, py + 230)

  Shapes.Rotate(petal2, 105)

  petal3=Shapes.AddEllipse(65, 50) 

  Shapes.Move(petal3, px + 105, py + 230)

  Shapes.Rotate(petal3, 200) 

  petal4 = Shapes.AddEllipse(65, 50) 

  Shapes.Move(petal4, px + 20, py + 330)

  Shapes.Rotate(petal4, 90)

  GraphicsWindow.BrushColor = "Brown"

  stik = Shapes.AddRectangle(5, 50) 

  Shapes.Move(stik, px + 40, py + 130)

  GraphicsWindow.BrushColor = "Yellow"

  circle = Shapes.AddEllipse(25, 25) 

  Shapes.Move(circle, px + 120, py + 130)

EndSub

Sub Hide

  For i = 1 To 8

    Shapes.SetOpacity(petals[i], 10)  

  EndFor

  For i = 1 To 2

    Shapes.SetOpacity(leafs[i], 10)  

  EndFor

  Shapes.SetOpacity(circ, 10)

  Shapes.SetOpacity(stick, 10)

EndSub

Sub MouseMove 

  x = GraphicsWindow.MouseX

  y = GraphicsWindow.MouseY

  If Mouse.IsLeftButtonDownThen

    Shapes.Move(sh, x, y)     

  EndIf 

EndSub 

Sub onMouseUpEvent

  If bsh = stick Then

    If (Shapes.GetLeft(sh) + 10) >= Shapes.GetLeft(bsh) And  (Shapes.GetLeft(sh) - 10) <= Shapes.GetLeft(bsh) Then

      Shapes.SetOpacity(bsh, 100)

      Shapes.Remove(sh)    

      dropped = dropped + 1            

    Else

      Shapes.Move(sh, shx, shy)       

    EndIf

  EndIf   

  If (Shapes.GetLeft(sh) + 10) >= Shapes.GetLeft(bsh) And  (Shapes.GetLeft(sh) - 10) <= Shapes.GetLeft(bsh) And (Shapes.GetTop(sh) + 10) >= Shapes.GetTop(bsh) And  (Shapes.GetTop(sh) - 10) <= Shapes.GetTop(bsh) Then

    Shapes.SetOpacity(bsh, 100)

    Shapes.Remove(sh)       

    dropped=dropped + 1         

  Else

    Shapes.Move(sh, shx, shy)   

  EndIf

EndSub

Sub starttimer

  GraphicsWindow.BrushColor = "Black"

  Submitbtn = Controls.AddButton("Submit", 300, 390)

  Controls.SetSize(Submitbtn, 70, 35)

  end = Clock.ElapsedMilliseconds

  start = Clock.ElapsedMilliseconds

  blink = Clock.ElapsedMilliseconds

  gamestart = Clock.ElapsedMilliseconds

  init = 0

  While init < 1

    game = Clock.ElapsedMilliseconds - start

    GraphicsWindow.BrushColor = "#3975e5"        

    GraphicsWindow.FontSize = 15

    GraphicsWindow.FillRectangle(250, 20, 120, 25)

    GraphicsWindow.BrushColor = "Black"

    tsecamt = Math.Round(game / 1000) 

    tsec    = Math.Remainder(tsecamt, 60)

    tming   = Math.Floor(tsecamt / 60)

    tmin    = Math.Remainder(tming, 60)

    thour   = Math.Floor(tming / 60)

    If tsec < 10 Then

      strSec = Text.Append(":0", tsec)

    Else

      strSec = Text.Append( ":", tsec)  

    EndIf 

    If tmin < 10 Then

      strMin = Text.Append( "0", tmin)

    Else

      strMin = Text.Append( "", tmin)

    EndIf  

    sec = Text.Append(strMin, strSec)

    GraphicsWindow.DrawText(250, 22, " Time:  " + thour + ":" + sec )         

    GraphicsWindow.FontSize = 10

    end = Clock.ElapsedMilliseconds 

    fps = 0                                               

    Program.Delay(1000)

  EndWhile         

EndSub

Sub OnclickButton

  If Controls.GetButtonCaption(Controls.LastClickedButton) = "Submit" Then        

    IF dropped = 8 Then

      init = 2     

      GraphicsWindow.ShowMessage("Congratulations! You took " + thour +":"+ tsec + " seconds to complete the flower.","Result") 

      Program.End()   

    EndIf

  EndIf

EndSub

Graphics Window

Lesson 4.2: Responding to Events

' Copyright (c) Microsoft Corporation.  All rights reserved.

GraphicsWindow.Hide()

gw = 500

gh = 350

scoreBoxLeft = 200

passedQuestion = 0

score = 0

Clicked = ""

i = 1

Q[1] = "Nile is the longest river in the world."

Q[2] = "The highest mountain in the world is Mount Everest."

Q[3] = "Zambia is also known as the 'Country of Copper'."

Q[4] = "The coldest place on the earth is in Siberia."

Q[5] = "Sydney is the capital of the USA."

Q[6] = "The river Jordan flows out into the Dead sea."

Q[7] = "Mumbai is the capital of India."

Q[8] = "Africa is the largest coffee growing continent in the world."

Q[9] = "The largest desert in the world is Sahara Desert."

Q[10] = "London is the capital of the UK."

A[1] = "True"

A[2] = "True"

A[3] = "True"

A[4] = "True"

A[5] = "False"

A[6] = "True"

A[7] = "False"

A[8] = "False"

A[9] = "True"

A[10] = "True"

ArrayRandom[1] = "5234162"

ArrayRandom[2] = "1896523"

ArrayRandom[3] = "5974216"

ArrayRandom[4] = "2756194"

randomNumber = Math.GetRandomNumber(Array.GetItemCount(ArrayRandom))

GraphicsWindow.CanResize = "False"

GraphicsWindow.Width = gw

GraphicsWindow.Height = gh

GraphicsWindow.Top = (Desktop.Height - gh) / 2

GraphicsWindow.Left = (Desktop.Width - gw) / 2

GraphicsWindow.Title = "True or False"

GraphicsWindow.Show()

Controls.ButtonClicked = OnButtonClicked

CreateUI()

Sub CreateUI

  GraphicsWindow.BrushColor = "Purple"

  GraphicsWindow.FontName = "Verdana"

  GraphicsWindow.FontSize = 14

  GraphicsWindow.DrawRectangle(10, 10, 480, 330)

  trueButton  = Controls.AddButton("True", 30, 210)

  falseButton = Controls.AddButton("False", 320, 210)

  Controls.SetSize(trueButton, 150, 100)

  Controls.SetSize(falseButton, 150, 100)

  GraphicsWindow.DrawText(220, 300, "Result")

  resultTextBox = Shapes.AddText("")

  Shapes.Move(resultTextBox, 220, 250)

  scoreTextBox = Shapes.AddText("Score: 0")

  Shapes.Move(scoreTextBox, 404, 15)

  StartGame()

EndSub

Sub StartGame

  passedQuestion = Text.GetSubText(ArrayRandom[randomNumber], i, 1)

  If i <= 7 Then

    qx = 60

    qy = 90 

    GraphicsWindow.BrushColor = "LightBlue"

    GraphicsWindow.FillRectangle(200, 220, 100, 80)    

    GraphicsWindow.FillRectangle(20, 50, 460, 100)

    GraphicsWindow.BrushColor = "Blue"

    GraphicsWindow.DrawBoundText(qx - 20, qy, 420 "" + Q[passedQuestion])

    currentAnswer = A[passedQuestion]

  Else

    Program.Delay(1000)

    GraphicsWindow.ShowMessage("Your Score is " + score, "Game Over")

    Program.End()

  EndIf

  i = i + 1

EndSub

Sub OnButtonClicked

  clickedButtonCaption = Controls.GetButtonCaption(Controls.LastClickedButton)

  If currentAnswer = clickedButtonCaptionThen 

    lastAnswer = "Correct"

    Shapes.Move(resultTextBox, 220, 250)

    score = score + 1

  Else

    lastAnswer = "Incorrect"

    Shapes.Move(resultTextBox, 212, 250)

  EndIf

  Shapes.SetText(resultTextBox, lastAnswer)

  Shapes.SetText(scoreTextBox, "Score: " + score)

  StartGame()

EndSub

Lesson 4.3: Collision Detection

 Copyright (c) Microsoft Corporation.  All rights reserved.

GraphicsWindow.Hide()

gw = 620

gh = 450

GraphicsWindow.CanResize = "False"

GraphicsWindow.Width  = gw

GraphicsWindow.Height = gh

GraphicsWindow.Top = (Desktop.Height - gh) / 2

GraphicsWindow.Left = (Desktop.Width - gw) / 2

GraphicsWindow.Title = "Catch the Apples!"

GraphicsWindow.Show()

applesLeft = 30

gameScore = 0

catcherImage = Program.Directory + "\catcher.jpg"

appleimage = Program.Directory + "\apple.jpg"

GraphicsWindow.MouseMove = OnMouseMove

CreateUI()

While applesLeft> 0

  DropApple()

  applesLeft = applesLeft - 1 

  Shapes.SetText(applesLeftTextBox, "Apples Left: " + applesLeft)

EndWhile

GraphicsWindow.ShowMessage("Your Score is: " + gameScore, "GameOver")

Program.End()

Sub CreateUI

  GraphicsWindow.BackgroundColor = "LightBlue"

  catcher = Shapes.AddImage(catcherImage)

  apple = Shapes.AddImage(appleimage)

  GraphicsWindow.BrushColor = "Gray"

  GraphicsWindow.FillRectangle(1, 1, gw, 40)

  GraphicsWindow.FontName = "Verdana"

  GraphicsWindow.FontSize = 18

  GraphicsWindow.BrushColor = "White"

  GraphicsWindow.DrawText(220, 5, "Catch the Apples!")

  scoreTextBox = Shapes.AddText("Score: 0")

  Shapes.Move(scoreTextBox, 480, 5)

  applesLeftTextBox = Shapes.AddText("Apples Left: " + applesLeft)

  Shapes.Move(applesLeftTextBox, 5, 5)

  GraphicsWindow.PenColor = "Black"

  GraphicsWindow.DrawLine(0, 50, 620, 50)

  Shapes.Move(catcher, 0, gh - 40) 

  Mouse.HideCursor()

EndSub

Sub OnMouseMove

  catcherX = Math.Min(GraphicsWindow.MouseX, 570)

  Shapes.Move(catcher, catcherX, gh - 40)

EndSub

Sub DropApple 

  appleX = Math.GetRandomNumber(600)

  appleY = 50

  While (appleY < gh + 20)

    Shapes.Move(apple, appleX, appleY)

    Program.Delay(15)

    DetectCatch()

    appleY = appleY + 5

  EndWhile

EndSub

Sub DetectCatch

  If appleY >= gh - 40 And appleX< catcherX + 50 And appleX>= catcherXThen

    gameScore = gameScore + 1

    Shapes.SetText(scoreTextBox, "Score: " + gameScore)     

    appleY = gh + 20

  EndIf

EndSub

Lesson 4.4: Advanced Games

' Copyright (c) Microsoft Corporation.  All rights reserved.

GraphicsWindow.Hide()

GraphicsWindow.Height = 450

GraphicsWindow.Width  = 600

GraphicsWindow.Top = ( Desktop.Height - 450 ) / 2

GraphicsWindow.Left = ( Desktop.Width - 600 ) / 2

GraphicsWindow.CanResize = "False"

GraphicsWindow.Show()

paddleImage = Program.Directory + "\rectangle.png"

ballImage =   Program.Directory + "\circle.png"

Left  = 42

bStartY = 35

CreatUI()

hitCount = 0

GraphicsWindow.MouseMove = MouseAction

For index = 0 To 15

  Array.SetValue("PinkBricks", Index, 1)

  Array.SetValue("VioletBricks", Index, 1)

  Array.SetValue("AquaBricks", Index, 1)

Endfor

InitBricks()

score = 0

ShowScore()

gw = GraphicsWindow.Width

gh = GraphicsWindow.Height

y = gh - 28

Shapes.Move(ball, x, y)

dX= 1

dY =-2

Loop:

x = x + dX

y = y + dY

If x >= gw - 16 Or x <= 0 Then

  dX= -dX

EndIf

If y <= 0 Then

  dY = -dY

EndIf 

padX = Shapes.GetLeft(paddle)

If y >= gh - 28 + 2 And x >= padX And x <= padX + 70 Then

  y = gh - 28 + 2   

  hitCount = hitCount + 1

  If Math.Remainder(hitCount, 3) = 0 Then

    For Index = 0 To 15

      HidePinkBrick()

      HideVioletBrick()

      HideAquaBrick()

    Endfor

    bStartY = bStartY + 20

    InitBricks()

  EndIf

  TestAqua:

  For Index = 0 To 15

    If Array.GetValue("AquaBricks", Index) = 1 Then

      If bStartY > gh - 160 Then

        Goto EndGame

      EndIf

    EndIf

  EndFor

  TestViolet:

  For Index = 0 To 15

    If Array.GetValue("VioletBricks", Index) = 1 Then

      If bStartY > gh - 100 Then

        Goto EndGame

      EndIf

    EndIf

  EndFor

  TestPink:

  For Index = 0 To 15

    If Array.GetValue("PinkBricks", Index) = 1 Then

      If bStartY > gh - 40 Then

        Goto EndGame

      EndIf

    EndIf

  EndFor

  EndTest:

  dX= dX- 2 + (x - padX) / 30

  If score = oldScoreThen

    If score <> 0 Then

      score = score - 1

    EndIf

  EndIf

  oldScore = score

  ShowScore()

  dY = -dY

EndIf

Shapes.Move(ball, x, y)

Program.Delay(5)

If y > bStartY - 16 And y < bStartY + 20 Then

  Index = (x+8) / 40

  Index = Math.Floor(Index)

  If Array.GetValue("PinkBricks", Index) = 1 Then        

    If Index=8 Then

    Else

      Array.SetValue("PinkBricks", Index, 0)

      HidePinkBrick()

      Left = Left - 1

      score = score + 15

      ShowScore()

    EndIf           

    dY = -dY 

    gameFinish()     

  EndIf

EndIf

If y > bStartY + 44 And y < bStartY + 80 Then

  Index = (x + 8) / 40

  Index = Math.Floor(Index)

  If Array.GetValue("VioletBricks", Index) = 1 Then

    If Index=4 Or Index=11 Then

    Else 

      Array.SetValue("VioletBricks", Index, 0)

      HideVioletBrick()

      Left = Left - 1

      score = score + 10

      ShowScore()

    EndIf    

    dY = -dY

    gameFinish()     

  EndIf

EndIf

If y > bStartY + 104 And y < bStartY + 140 Then

  Index = (x + 8) / 40 

  Index = Math.Floor(Index)

  If Array.GetValue("AquaBricks", Index) = 1 Then

    If Index = 2 Or Index = 7 Or Index = 13 Then

    Else 

      Array.SetValue("AquaBricks", Index, 0)

      HideAquaBrick()

      score = score + 5

      ShowScore()

      Left = Left - 1

    EndIf 

    dY = -dY 

    gameFinish()

  EndIf

EndIf

If y < ghThen

  Goto Loop

EndIf 

EndGame:

GraphicsWindow.ShowMessage("Your score is: " + score, "Game Over")

Program.End()

Sub CreatUI

  GraphicsWindow.Title = "Paddle Game"

  GraphicsWindow.FontSize = 14

  paddle = Shapes.AddImage(paddleimage)

  ball = Shapes.AddImage(ballimage)

EndSub

Sub MouseAction

  paddleX = GraphicsWindow.MouseX 

  Shapes.Move(paddle, paddleX - 10, GraphicsWindow.Height - 14)

EndSub

Sub ShowScore

  GraphicsWindow.BrushColor = "White"

  GraphicsWindow.FillRectangle(520, 10, 200, 20)

  GraphicsWindow.BrushColor = "Black"

  GraphicsWindow.DrawText(500, 10, "Score: " + score)

EndSub

Sub InitBricks 

  For Index = 0 To 15    

    If Index = 8 Then

      GraphicsWindow.PenColor = "Black"

      GraphicsWindow.BrushColor = "Gray"

    Else

      If Array.GetValue("PinkBricks", Index) = 1 Then

        GraphicsWindow.PenColor = "Black"

        GraphicsWindow.BrushColor = "Pink"

      Else

        GraphicsWindow.PenColor = "White"

        GraphicsWindow.BrushColor = "White"

      EndIf

    EndIf

    GraphicsWindow.FillRectangle(Index * 40, bStartY, 40, 20)

    GraphicsWindow.DrawRectangle(Index * 40, bStartY, 40, 20)

    GraphicsWindow.BrushColor = "Violet"

    If Index = 4 Or Index=11 Then

      GraphicsWindow.PenColor = "Black"

      GraphicsWindow.BrushColor = "Gray"

    Else

      If Array.GetValue("VioletBricks", Index) = 1 Then

        GraphicsWindow.PenColor = "Black"

        GraphicsWindow.BrushColor = "Violet"

      Else

        GraphicsWindow.PenColor = "White"

        GraphicsWindow.BrushColor = "White"

      EndIf

    EndIf

    GraphicsWindow.FillRectangle(Index * 40, bStartY + 60, 40, 20)

    GraphicsWindow.DrawRectangle(Index * 40, bStartY + 60, 40, 20)

    GraphicsWindow.BrushColor = "Aqua"

    If Index = 2 Or Index = 7 Or Index = 13 Then

      GraphicsWindow.PenColor = "Black"

      GraphicsWindow.BrushColor = "Gray"

    Else

      If Array.GetValue("AquaBricks", Index) = 1 Then

        GraphicsWindow.PenColor = "Black"

        GraphicsWindow.BrushColor = "Aqua"

      Else

        GraphicsWindow.PenColor = "White"

        GraphicsWindow.BrushColor = "White"

      EndIf

    EndIf

    GraphicsWindow.FillRectangle(Index * 40, bStartY + 120, 40, 20)

    GraphicsWindow.DrawRectangle(Index * 40, bStartY + 120, 40, 20)

  EndFor

EndSub

Sub HidePinkBrick

  GraphicsWindow.PenColor = "White"

  GraphicsWindow.BrushColor = "White"

  GraphicsWindow.FillRectangle(Index * 40, bStartY, 40, 20)

  GraphicsWindow.DrawRectangle(Index * 40, bStartY, 40, 20)

EndSub

Sub HideVioletBrick

  GraphicsWindow.PenColor = "White"

  GraphicsWindow.BrushColor = "White"

  GraphicsWindow.FillRectangle(Index * 40, bStartY + 60, 40, 20)

  GraphicsWindow.DrawRectangle(Index * 40, bStartY + 60, 40, 20)

EndSub

Sub HideAquaBrick

  GraphicsWindow.PenColor = "White"

  GraphicsWindow.BrushColor = "White"

  GraphicsWindow.FillRectangle(Index * 40, bStartY + 120, 40, 20)

  GraphicsWindow.DrawRectangle(Index * 40, bStartY + 120, 40, 20)

EndSub

Sub gameFinish

  If Left = 0 Then

    GraphicsWindow.ShowMessage("Well Done! Your score is: " + score, "Game Over")  

    Program.End()  

  EndIf

EndSub

Lesson 5.1: Sharing Code

  1. By clicking Publish on the toolbar.

  2. By clicking Import and specifying the unique program ID.