Thursday, January 3, 2008

How to Host Silverlight Videos



Silverlight Streaming: new iframe-based invocation mechanism

With a recent update to the Silverlight Streaming service, it's now even easier to instantiate a hosted app on a web page: all you need is a single iframe element whose source attribute refers to a server-side invocation URL.

Wednesday, January 2, 2008

Vector Graphics

 

Get ready to build your space ship. Before performing any task in space, N.A.S.A. spends most of its time running simulations to ensure that the mission will be a success. On the ground engineers perform many exercises and learn what to expect during their mission. It’s much easier to try and fail on the ground than in space.

We are going to continue to build our space simulator where we will practice landing the space ship on the moon’s surface. In this chapter, we will build the outer frame for your space ship.

We are going to build our spaceship out of a single polygon. You have seen polygons before, but do you know what a polygon is? A polygon is a shape with at least three points. So a triangle is a polygon and so is a square. Let’s start by writing drawing a simple triangle.

We need to define a new polygon object called SpaceShip. In previous chapters, we defined variables as Integers. Now we are going to define an object called SapceShip as a Polygon.

An object defines properties and methods that allow us to interact with the object. Properties tell us about the object. Methods perform tasks on the object.

For example, a cell phone is an object. The cell phone is made up of many different parts that together make the phone. This collection of parts is known as an object. The cell phone has many different properties about it. On the phones screen you can see the phone’s volume, the amount of battery remaining, signal strength and the time. You can also perform actions with the phone. For example, you can choose to call someone or answer an incoming call.

Properties

Methods

Volume

AnswerCall()

Battery strength

Dial()

Signal strength

AddContact()

Time

Let’s look at the code needed to define a polygon object and create a triangle.

Program SpaceShip

Method Main()

Define SpaceShip As Polygon

SpaceShip.AddPoint(50,0)

SpaceShip.AddPoint(100,100)

SpaceShip.AddPoint(0,100)

SpaceShip.Color = Colors.Red

SpaceShip.IsFloating = True

SpaceShip.Visible = True

End Method

End Program

A polygon requires at least three points and we use the .AddPoint() method to add each point. The .AddPoint() method requires two arguments X and Y. X and Y provide the coordinates for the polygon you are creating. Remember, X is the number across and Y is the number down. An easy way to remember this is the X looks like a cross and the Y points down.

To define the objects color, we use the objects color property. The computer defines a set of colors which you can choose from. Try changing colors. For example, Colors.Black.

Finally, the SpaceShip.IsFloating property and SpaceShip.Visible property must be set to true for the triangle to be displayed.

clip_image002

Sprites

A sprite is another type of object. You have learned how to use other types of objects like polygon and joystick. A sprite object allows you to display and positions graphics easily on the console window. With poloygons, you have to programmatically draw the graphics for your game on the screen. With sprites, you design the graphics using a drawing program and then programmatically load the sprite to display on the console window. Sprite objects provide several methods that allow you to control the location, size and position of the graphics on the screen.

An animated gif file is a single file composed of several images. When the individual images are show in sequence, the image appears to animate. The following is an example of a dancing hula girl. Notice that each image is slightly different. The 8 images shown one after the other make the hula girl appear to dance.

clip_image004

Sprites allow the developer of the game to focus on the programming logistics of the game and the graphics designer to focus on the graphics for the game. This allows each person to focus on their area of expertise. Most complex games developed today and written by dozens people each with different roles such as developers, testers and graphic artists.

To use a sprite, you must first define a new variable and load the sprite into memory.

Ship.Load ("AnimatedShip.gif" )

Ship.MoveTo (100,150)

Ship.Show()

To move a sprite on the screen you can use one of several different methods.

MoveTo(x, y) allows you to move a sprite to a known set of coordinates, X and Y. This is useful if you know exactly where you want to place the sprite.

The following move methods are useful if you don’t need to specify the exact location. For example, the user presses the LEFT arrow on the keyboard and you simply want to move the ship to the left.

MoveByAmount (X,Y) moves the sprite based on the current location. For example, adding Ship.MoveByAmount (10,15) would move the sprite Ship over left 10 and down 15.

MoveLeft (Pixels) moves the spite to the left by the number of pixels specified.

MoveRight (Pixels) moves the sprite to the right by the number of pixels specified.

To spin or rotate a sprite set the property Rotation to a value between 0 and 360. 0 is North, 180 is South. For example, to spin the ship once you can add the following code to the example above.

For x = 1 To 360

Delay(10)

Ship.Rotation = x

Next

The Delay() method pauses the computer program a number of milliseconds specified. Decrease this number to reduce the delay and increase the speed of the ship spinning.

Scaling

You can determine the size of a sprite by setting the ScaleX and ScaleY properties of a sprite. ScaleX = 200 and ScaleY=200 will double the size of the sprite. ScaleX = 50 and ScaleY= 50 will reduce the size of the sprite in half.

LAB

Write a program that displays the ship in the center of the screen.

Hint use the function ScreenWidth() to get the width of the consolescreen and ScreenHeight() to get the height of the screen. The center of the screen is half the height and half the width.

Create a Loop that rotates the ship left or right depending on if the user presses the Left or right arrow keys.

Hint, is the Function IsKeyDown( "Right" ) and IsKeyDown( "Left" ) to check if the user is pressing the left or right arrow keys.

Bonus, exit the program when the user presses “Q” on the keyboard.

Console Input

 

In all of our examples so far, we have had to modify the program each time to change the values of a variable. Many programs though require input from the user. This allows us to ask the user of our programs questions and then have our computer program do something based on the input of the user.

In this section, we will program a guessing game where the computer this of a number between 1 and 100 and the user has to try and guess the number. Each time the user guesses, the computer will tell the user higher or lower. The game stops when the user guesses the number correctly. At the end, the computer will tell the user how many times they guessed before getting the answer write.

To read input from the user, we need to use one of the following two methods

ConsoleReadInt()

ConsoleReadLine()

ConsoleReadInt() is used to input integers from the user. ConsoleReadLine() is used to read text from the user. In this example, you are writing guessing game. Because the input are integers, we will use ConsoleReadInt() to get input from the user.

When you ask someone a question, you get an answer. Where do you store the answer? In your memory. The same is true when you use ConsoleReadInt(). The computer asks the user a question and the users answer is stored in the computers memory which we define as a variable.

Before we can use the variable it must be defined. What type of variable should we define? Integer, right?

Let’s take a quick look at an example:

Program MyNewProgram

Method Main()

Define X As Integer

X = ConsoleReadInt("Enter a number: " , False)

ConsoleWriteLine ("You entered: " + X)

End Method

End Program

In this example, we can see that ConsoleReadInt() takes two paramters.

The first Parameter Prompt determines what message is displayed to the user. The second parameter EchoToConsole determines if the value the user enters is displayed in the console window. Typically the value of EchoToConsole should be set to false.

clip_image002[4]

Let’s review the ConsoleReadInt() Method

clip_image004

This program asks the user to input a variable and then write the value of the variable on the screen. Pretty simple.

How do we determine what number the computer is thinking? Since we want the number to be different each time the user plays the game we can have the computer generate a random number using the Random() method. Random() asks the computer to pick a number between two numbers. This is just like when someone asks you to pick a number between 1 and 10.

To tell the computer to pick a number between 1 and 10 we use Random(1,10).

clip_image006[4]

Again, just like with ConsoleReadInt() we need to store the value returned by the method Random(). What type of variable should we use? Let’s define a variable called Answer as an Interger and return a number between 1 and 100.

Define Answer As Integer

Answer = Random (1,100)

Now let’s build program that puts all of the pieces together. We want to

1. Have the computer pick a number between 1 and 100 and store it in the variable answer

2. Have the user guess a number between 1 and 100

3. Repeat this process until the users guess is equal to the anwer

In the next section, we will add logic to the code to tell the computer if the number is higher or lower.

First we need to define our variable. This program will start with 2 variables. One for the Answer and one for Guess.

Define Answer As Integer

Define Guess As Integer

Next, we need the computer to select a number between 1 and 100

Answer = Random (1,100)

Now we need to setup a Loop. Since we want the program to repeat until the user gets the answer correct, we will use a While Loop. The computer will keep asking the user while their guess is NOT equal to their answer.

While Answer <> Guess

Then we will prompt the user to enter a number and display the number the user entered on the console window.

Guess = ConsoleReadInt("Enter a number between 1 and 100: " , False)

ConsoleWriteLine ("You entered: " + Guess)

The program will continue until the user guess the right answer and then we can display a message telling the user they got the answer right.

End While

ConsoleWriteLine ("You guessed right.")

Next, let’s prompt the user to enter a number between 1 and 100.

X = ConsoleReadInt("Enter a number between 1 and 100: " , False)

Here is what our program should look like.

Program GuessANumber

Method Main()

Define Answer As Integer

Define Guess As Integer

Answer = Random (1,100)

While Answer <> Guess

Guess = ConsoleReadInt("Enter a number 1 and 100: " , False)

ConsoleWriteLine ("You entered: " + Guess)

End While

ConsoleWriteLine ("You guessed right.")

End Method

End Program

Now, have some fun and see if you can guess the number the computer is thinking.

Right now, the user has to keep guess until they get the number right. Typically when you play this game, the person you are playing with will say Higher or Lower based on the number you say. Let’s look at how we can modify the computer program to do the same. Here is what we want to do.

If the guess the user enters is higher than the answer then display Lower

If the guess the user enters is lower than the answer then displayer Higher

This can be performed by using an If / Then statement. Basically you are saying, if something is true then do this. Let’s take a look at what this looks like.

If Guess > Answer Then

ConsoleWriteLine ("The number is LOWER")

End If

Here we are telling the computer if the value of the variable Guess is greater than the value of the variable Answer then display a message “The number if Lower”. Now we need to do the same and tell the user if the number is higher.

If Guess < Answer Then

ConsoleWriteLine ("The number is HIGHER")

End If

Now our complete program looks like this.

Program GuessANumber

Method Main()

Define Answer As Integer

Define Guess As Integer

Answer = Random (1,100)

While Answer <> Guess

Guess = ConsoleReadInt("Enter a number between 1 and 100: " , False)

ConsoleWriteLine ("You entered: " + Guess)

If Guess > Answer Then

ConsoleWriteLine ("The number is LOWER")

End If

If Guess < Answer Then

ConsoleWriteLine ("The number is HIGHER")

End If

End While

ConsoleWriteLine ("You guessed right.")

End Method

End Program

In this section we are going to create a program to write a math quiz for students. The program will ask the user 10 questions and allow the user to enter the answer for each question. At the end of the quiz the program will display the number of questions right and the number of questions wrong.

Joystick Support

 

Most computer games use either the keyboard or joystick to control the game. In this section, we are going to learn how to accept input from a joystick and output the results on the screen. Adding support for a joystick is very simple using Phrogram and is just as easy as input from a keyboard. Because some users may not have a joystick, it is good practice to support both the keyboard and the joystick.

To begin using the joystick, you must first declare a new variable defined as an array of Joysticks. Your computer may have more than one joystick attached to the computer at a single time, but for this example, we are going to assume the user is using the first joystick connected to the computer.

Define myJoysticks As Joystick[1]

If you wanted your game to accept input from multiple joysticks connected to the computer, enter the maxium number of joysticks to search for. For example, the following declares a new array of 5 joysticks.

Define myJoysticks As Integer = 5

Define myJoysticks As Joystick[MaximumJoysticks]

For now, we are going to keep things simple and check only one joystick. Now that we have defined a new joystick, we can display the properties of the joystick and add decision logic using If/Then/else to direct the computer program.

In this next example, we will write a computer program that displays the name of the joystick and list the values of the thumb stick if connected. This will continue until the user presses Q on the keyboard.

Method Main()

Define myJoysticks As Joystick[1]

If myJoysticks[1].IsConnected Then

Alert ("Joystick 1 is a " + _

myJoysticks[1].MakeAndModel , "Joystick Name")

While IsKeyDown("Q") = False

If myJoysticks[1].IsConnected Then

ConsoleWriteLine("Thumbstick X=" + _

myJoysticks[1].Stick1.X + _

" Y=" + myJoysticks[1].Stick1.Y)

End If

End While

Else

ConsoleWriteLine ("The joystick is not connected.")

End If

End Method

First, we check to see if the joystick is connected. This is done by checking the Joysticks’s IsConnected property. If the value of this property is True, then the joystick is connected. If the joystick is not connected, this will return false.

In this example, we want to display an alert with the name of the joystick of the joystick is connected. First we use the IF/THEN statement to test if the joystick is connected.

If myJoysticks[1].IsConnected Then

If myJoysticks[1].IsConnected Returns TRUE, then the computer executes the next line of code. In this case, the next line is the alert method. If myJoysticks[1].IsConnected = False, then the line after ELSE executed. In this example, that would be ConsoleWriteLine ("The joystick is not connected.")

 

Joystick Video

Learn To Phrogram

Learn to Program

Variables

A variable is used to store a value in the computer’s memory. Before you can use a variable, you must first Define the variable. This allows the computer to know the name of the variable and what type of data the variable will store. Let’s define our first variable.

Define guess As Integer

In this example, the name of the variable you are defining is guess and the data type is Integer. Now, you must be asking yourself, what is an integer? An integer is a whole number. For example, 1,2,3 etc. You could not assign the value 2.5 to the variable guess because 2.5 is not a whole number.

By default, when you define a variable as an integer, the value of the integer is zero. Try the following program.

Method Main()

Define guess As Integer

Print (guess)

End Method

Before you run the program, try and think what the console window will display. This is how you start to think like a computer and is an important part of becoming a good computer programmer.

clip_image002

If you said 0, then you are correct. Guess is equal to zero because it hasn’t been assigned a value. Let’s move to the next step and assign a value to the variable we defined.

To assign a variable a value, you need to set the variable equal to something. To assign a variable, we use the equal sign. The equal sign on the computer is =.

To assign the variable guess the number 5, we use the following command.

guess = 5

Now try the following program.

Method Main()

Define guess As Integer

guess = 5

Print (guess)

End Method

What will the output be now?

clip_image004

Simple, right?

Now, let’s practice some basic computer math. Try to answer the following problems. Remember, think like a computer. If you need help, write a small computer program to help you answer the questions.

1.

x = 5

y = 10

x + y = _____________

2.

x = 5

y = 15

y – x = ____________

3.

Y = 10

Y = Y + 1

Y = ____________

4. X = 15

Y = 10

Z = _____________

5. X = 10

Y = 5

Z = X + Y

Z = ______________

A variable is used to store a value in the computer’s memory. Once you have defined a variable and assigned it a value, you can use the variable. In the first chapter, you already started to use variables by adding and subtracting them. Let’s now take a look at how we can comparison operations. When you compare something, what are you doing? You are looking to see what is the same and what is different between two items. If you were to compare two apples, you may compare their size, shape, type and color.

A computer program can be used to compare variables as well. Now, it is time to start thinking like a computer. Answer the following questions

Define X As Integer

Define Y As Integer

X = 5

Y = 10

Is X > 5? _________

Is Y < 10? _________

Is X = Y? _________

Let’s review the three comparison operators you used. These should be familiar to you already from math lessons.

>

Greater than

<

Less than

=

Equal too

Enter the following program to see the results for yourself in the console window.

Method Main()

Define X As Integer

Define Y As Integer

X = 5

Y = 10

ConsoleWriteLine ("Answer: " + (Y > 5) )

ConsoleWriteLine ("Answer: " + (Y < 10) )

ConsoleWriteLine ("Answer: " + (X=y))

End Method

When you run the program, you should see the following results.

clip_image006

Hey, why is there a + sign in the method ConsoleWriteLine? Good question. First, we are using a new Method to tell the computer to write information to the console window. The Method ConsoleWriteLine() tells the computer to output information to the console window and start a new line. Try changing your program to use the ConsoleWrite() method instead of the ConsoleWriteLine(). What do you see now?

Method Main()

Define X As Integer

Define Y As Integer

X = 5

Y = 10

ConsoleWrite ("Answer: " + (Y > 5) )

ConsoleWrite ("Answer: " + (Y < 10) )

ConsoleWrite ("Answer: " + (X=y))

End Method

clip_image008

Using ConsoleWriteLine() looks much better, doesn’t it.


Methods

Methods are computer commands. A computers programming language has hundreds of built in methods it knows how to execute. You as a computer programmer have the ability to define your own new methods. Most methods take a parameter. A parameter simply provides the method additional information. For example, let’s take a look at the method ConsoleWriteLine().

When you start typing ConsoleWriteLine ( the following tooltip will be displayed.

clip_image010

The tool tip tells you what type of parameter is required and provides a brief description. In this example, the method ConsoleWriteLine() requires a String which is the message to display on the console window.

Now, lets go back to our last example. Look at the following line.

ConsoleWriteLine ("Answer: " + (Y > 5) )

In this example, we are calling the method ConsoleWriteLine() and passing the String value of

“Answer: “ + (Y>5)

The + sign is used here to join two strings together. The + sign can be used to add numbers together or to join two strings together. Take a look at the following examples.

If the plus sign come between two numbers, the + sign will add the numbers

For example:

Define X As Integer

X = 5 + 10

ConsoleWriteLine (X)

If the plus sign comes between two Strings, the + sign will join the two values together.

Define X As String
X = " Answer: " + " True"
ConsoleWriteLine (X)

clip_image012

A few more points about using string. When you define strings they MUST be between quotation marks. Try the following

Define X As String

X = What is your name?

ConsoleWriteLine (X)

The program looks ok, but you will get a syntax error. What do you need to do to correct the syntax error? Add quotation marks, right?

Define X As String
X = "What is your name?"
ConsoleWriteLine (X)

Review Time

1.

Define X As String

X = "What is your name?"

ConsoleWriteLine (X)

What type of variable is X? ____________

2.

Define X As Integer

Define Y As Boolean

Y = X > 5

What is the value of Y? ______________

3.

Define X As String

X = What is your name?

ConsoleWriteLine (X)

What is wrong with the program above above?

Rinse and repeat

Computers are very good at repeating the same tasks over and over. They never get tired of doing the same tasks again and again. In this section we are going to learn how to repeat a set of tasks multiple times.

Suppose you want to display your name on the screen 5 times. What’s the best way to do this? You could choose to use the ConsoleWriteLine() method 5 times. While this would work ok for repeating a task 5 times, what if you wanted to change the program and display your name 100 times? This would be very tedious. The language Phrogram provides several commands to accomplish this.

Method Main()

ConsoleWriteLine("Jon Smith")

ConsoleWriteLine("Jon Smith")

ConsoleWriteLine("Jon Smith")

ConsoleWriteLine("Jon Smith")

ConsoleWriteLine("Jon Smith")

End Method

LOOP LOOP

The first way is to use the command LOOP. LOOP takes one argument, the number of time to repeat. Place LOOP before the section of code you want to repeat and END LOOP at the end of the section. Let’s look at how to repeat Jon Smith using the Loop command.

Method Main()

Loop 5

ConsoleWriteLine("Jon Smith")

End Loop

End Method

Much simpler. The code is easier to read and easier to update. Change the program above to output the name Jon Smith 25 times. Only one line of code to change.

FOR LOOP

Another method to repeat a section of code multiple times is to use the FOR NEXT command.

Method Main()

Define X As Integer

For x = 1 To 5

ConsoleWriteLine("Jon Smith")

Next

End Method

First, you must define a variable as an Integer. In this example we define X. Next we call For and specify the start and end values. In this case, we instruct to start at the value of 1 and end at 5. Each time the computer executes the Next command, the value of the integer X is incremented. To see this better, lets modify the program to display the value of X each time the method ConsoleWriteLine() is called.

Method Main()

Define X As Integer

For x = 1 To 5

ConsoleWriteLine("Jon Smith:" + X)

Next

End Method

When you run the program, you will see the following.

clip_image014

Let’s review.

This instructs the computer to start at the value 1 and continue until X = 5. Every time Next is called, the value of X increase by 1.

In this example, using both Loop and For / Next produce the same results. Loop is somewhat easier to use because you do not need to define a variable first.

While LOOP

A third method is to use the While command. Using the While command allows you to control when code is executed. While a given condition is true, the computer code will be executed. If the condition is not true, the computer code will not be executed. Let’s look at two examples. The first example will again display the name Jon Smith on the screen 5 times.

Method Main()

Define X As Integer

While X < 5

X = X + 1

ConsoleWriteLine("Jon Smith: " + X)

End While

End Method

In the next example, we will continue to display the name Jon Smith on the screen until you press Q on the keyboard to quit. If you don’t hit Q, the program will continue to execute the method ConsoleWriteLine() forever. Remember, computers never get tired.

clip_image016

Let’s review the program line by line.

Line

Description

2

Name of the program

4

Defines the variable X as an Integer

5

Checks to see if the value of IsKeyDown(“A”) is equal to “A”. IsKeyDown(“A”) checks to see if the user is pressing the “A” key on the keyboard. If FALSE then the code on lines 7 and 8 is executed. If TRUE then the execution of the code resumes at line 9.

6

Prints Jon Smith and the value of the integer X on the console

7

Increments the value of X

8

End of the conditional loop

9

Prints Done on the console window

Lab questions

1. Write a program that counts down starting at number 10 and ending at 1. When done counting, display the message All Done. The output should look like the following. There are a couple of ways this can be done.

clip_image018

One way is to use the While statement and subtract 1 from the variable X each time. By starting with the variable X equal to 11, the program counts backward from 11 to 1 and stops when X is less than 1.

Program MyNewProgram

Method Main()

Define X As Integer

X = 11

While X > 1

X = X - 1

ConsoleWriteLine("The Number is: "+ X)

End While

ConsolewriteLine("All Done")

End Method

End Program

Another method is to use the For / Next statement and use an argument to tell the For / Next statement to count by negative 1. Notice in this example we do not need to set X = 11 before the loop we are defining.

Program MyNewProgram

Method Main()

Define X As Integer

For X = 10 To 1 Step -1

ConsoleWriteLine("The Number is: "+ X)

Next

ConsolewriteLine("All Done")

End Method

End Program

Lab Work

1. What is the value of Y after the following program is run?

Define X As Integer

Define Y As Integer

For X = 1 To 5

Y = Y + 1

Next

ConsoleWriteLine (y)

Answer: 5

2. What is the value of X after the following program is run?

Define X As Integer

Define Y As Integer

For X = 1 To 5 Step 2

Y = Y + 1

Next

ConsoleWriteLine (y)

Answer:3

3. What is the value of X after the following program is run?

Define X As Integer

Define Y As Integer

For X = 10 To 1 Step -2

Y = Y + 1

Next

ConsoleWriteLine (y)

Answer 5

4. What is the value of X after the following program is run?

Define X As Integer

Define Y As Integer

For X = 10 To 1

Y = Y + 1

Next

ConsoleWriteLine (y)

Answer 0

Decisions, Decisions, Decision

Everyone makes decisions and computers can to using the IF/THEN statement. An IF/THEN statement allows your computer program to execute code when an expression is TRUE. The code between the IF and END IF statement is only executed if the expression is TRUE. IF the expression is FALSE, the statement after the END IF statement is executed. Let’s look at the following program.

Define X As Integer

X = 10

If X > 1 Then

ConsoleWriteLine ("A")

End If

ConsoleWriteLine ("B")

End Method

X > 1 is the expression being evaluated. If X > 1 = TRUE then the code between IF and END IF is executed. In this case, ConsoleWriteLine (“A”) . If the expression is FALSE, then the code after END IF is executed.

What will be displayed when this program is executed?

clip_image020

Let’s modify the program and change the expression.

Define X As Integer

X = 10

If X < 1 Then

ConsoleWriteLine ("A")

End If

ConsoleWriteLine ("B")

End Method

clip_image022

In addition to IF we can also add ELSE. This executes code if the statement is NOT true. This is useful if you need the computer to choose between two choice and you know if the first one is not true, then the second one must be true. For example, if a person is not a girl, they must be a boy. Of if the light is not on, it must be off.

Method Main()

Define X As Integer

X = 1

If X = 1 Then

ConsoleWriteLine ("Girl")

Else

ConsoleWriteLine ("Boy")

End If

End Method

clip_image024

The expression used to test if the statement is true or false can be a combination of multiple expressions. For example X = 1 And Age > 17. Let’s look at another example.

X = 1

Age = 18

If X = 1 And Age > 17 Then

ConsoleWriteLine ("Women")

Else If X = 1 And Age < 17 Then

ConsoleWriteLine ("Girl")

Else If X = And Age > 17 Then

COnsoleWriteLine ("Man")

Else

ConsoleWriteLine ("Boy")

End If

clip_image026