4.2 responding to events

11
Microsoft® Small Basic Responding to Events Estimated time to complete this lesson: 1 hour

description

 

Transcript of 4.2 responding to events

Page 1: 4.2   responding to events

Microsoft® Small Basic

Responding to Events

Estimated time to complete this lesson: 1 hour

Page 2: 4.2   responding to events

In this lesson, you will learn about:

Creating interactive games in Small Basic that respond to events.

Responding to Events

Page 3: 4.2   responding to events

Fun with Shapes

You have also learned to use controls and keyboard and mouse events to include interactivity in your Small Basic program.

So far, you have learned to insert and animate various shapes in your Small Basic programs.

And you also know that you can use these shapes, controls, and events collectively to create interactive games in Small Basic. Let’s see how…

Page 4: 4.2   responding to events

Make the Turtle Draw – The Game

In this simple game, you tell the Turtle to draw unique shapes in the graphics window by defining the number of sides.

Notice how you use the Turtle object to draw shapes and how you use mouse and keyboard events to choose the color and the number of sides.

The game demonstrates how the user can make the Turtle draw colorful, unique shapes by specifying a color and the number of sides.

Page 5: 4.2   responding to events

Make the Turtle Draw – How to Play

Steps to play the game:

First, you need to select a color from the color palette.

Next, you specify the number of sides, so you can define what the Turtle should draw.

When you click Submit, the Turtle starts drawing.

So how do you play this game?

Page 6: 4.2   responding to events

Make the Turtle Draw - Code

Now let’s understand the code for the game in detail…

To create this game, you use the GraphicsWindow to create a user interface. You use the Controls object to add a button and a text box and set the size for the control buttons. You use the Shapes object to add different shapes. Then, you use the Shapes object to show, move, and hide shapes. You also use the Turtle object and set its angle, speed, and position. You use different conditions for different actions.

Page 7: 4.2   responding to events

Bounce the Ball – The Game

Let’s move to a complex game now. In this game, you keep a ball above the ground by using the mouse to click the ball to keep it afloat.

Notice how we use mouse events to keep the ball from touching the ground. The ball responds to mouse clicks and stays afloat.

The objective of the game is to keep the ball afloat as long as possible.

Page 8: 4.2   responding to events

Bounce the Ball – How to Play

Steps to play the game:

On the screen, a ball is dropped on the ground.

You click the ball to make it bounce up in the air.

You must keep the ball from touching the ground. To do so, keep clicking the ball.

The timer keeps track of how many seconds you keep the ball from touching the ground.

So how do you play this game?

Page 9: 4.2   responding to events

Bounce the Ball – The Code

Now let’s understand the code for the game in detail…

To develop this game, you use the GraphicsWindow object to create the user interface. You use the Controls object to define the mouse event that will be used to balance the ball. You use the Shapes object to add the image of the ball. You also use conditional statements to define the action to be executed when a particular mouse event occurs.

Page 10: 4.2   responding to events

Let’s Summarize…

Congratulations! Now you know how to:

Create interactive games in Small Basic that respond to events.

Page 11: 4.2   responding to events

It’s Time to Apply Your Learning…

Write a program to display a graphics window, and perform the following steps:

Create two buttons: Label one button True, and label the other button False.

Create a series of statements to be displayed on the screen.

For any statement, the user will click True or False.

The user scores points by guessing correctly.