Tuesday, March 30, 2010

Introduction to Programming: Starting Out With Alice; Chapter 6: Events

Starting Out with Alice: A Visual Introduction to Programming

1. Responding to Events

An event is an action that takes place while a program is running. It could be a mouse click, some even turning true or false, a key stroke, or one of many other items. For an example, in the events editor you will see when the world starts do xxxxx method. This starting of a world is an event.

The process of responding to an event is called handling an event. You have to have an event title in the event editor in order for some event to use it. This is done with the create new event button. This will require some other additional arguments. These methods are known as event handlers.

Specialized Events

Some events will not appear till you create a general event and then right click and select change to on the menu to change to a more specialized event.

2. Handling Key Press and Mouse Events

Key Press Events

A key press event is created when a key is pressed and released on the keyboard. These are created in the event editor with the create new event button. We then choose which key or let it be any key. Lastly we choose what will be done.

Handling the While a key is pressed Event

This is different from the previous in that this event happens when the event is held down, not when it is pressed and released. This event has 4 place holders. One is for the key and the others are for begin, during and end. Each of these refers to the actions that will be taken at each point of the key press. Not all of them will have to be filled.

Mouse Events

When mouse is clicked on anything is another even to use. Created like the others we will also fill in the parameters as needed.

Handling the While the mouse is clicked on something Event

This differs from the mouse click like the key press does to the while a key is pressed. As long as the mouse is held down, the event takes place. Like the while a key is pressed event, we have the same four parameters to fill out.

3. Using Events in Simulations and Games

It is possible to use the mouse and key presses to create games.

4. Tips for Games and Simulations

Random Numbers

Alice environment has a random number generator that can be used any time random events are needed. It is part of the world object and is called random. It generates a number between 0 an 1 which can be manipulated by its properties.

Playing Audio

Some objects like the lion have sound associated with them. You can see this by looking at the class information for that object. To call it, use the object’s primitive play sound method.

Importing and Recording Sound

If you have a wav or mp3 file, these can be used as sounds for objects. Use the import sound button or record sound button on the objects properties tabs to get a sound to use.

The Character Builders

Alice has tools for creating characters on your own. They are called hebuilder and shebuilder. They are located in the local gallery’s people collection. Click the thumbnail and:

  • Design the body be selecting the head, torso and leg types
  • Design the face by selecting a skin color, eyes and mouth
  • Design the hair by selecting a hair style and color
  • Design the clothing by selecting shirt, pants and shoes

These characters have several custom class-level methods.

Debugging with the print Statement

For debugging purposes, the print statement can be used to display in a console below the viewing screen what is going on in the program.

 

 

Visual Basic for Beginners: A Project Approach to Event-Driven Programming



No comments:

Post a Comment