Download presentation
Presentation is loading. Please wait.
Published byEdmund Cain Modified over 9 years ago
1
CRE Programming Club - Class 7 Robert Eckstein and Robert Heard
2
Understanding Events Certain objects in Small Basic have events that you can “subscribe” to. In order to subscribe to an event, create a subroutine, then assign the name of the subroutine to the event in the object. Whenever the event happens, the subroutine is called to “handle” the event. This can be almost never, or several times a second.
3
Events Events are something in computers that we call interrupts. With an interrupt, everything is going smoothly in the main program, until something outside the program needs to get the program’s attention. At that point, execution in the main program is brought to a halt, and the subroutine that handles the interrupt is run.
4
Import VSX414 GraphicsWindow.KeyDown = handleKeyPress GraphicsWindow.Show() looping = "True" While (looping) TextWindow.WriteLine("Happily looping...") Program.Delay(500) EndWhile Sub handleKeyPress TextWindow.WriteLine("Key Down is " + GraphicsWindow.LastKey) EndSub
5
Adding... Import PDG320 GraphicsWindow.KeyDown = handleKeyPress GraphicsWindow.KeyUp = handleKeyUp GraphicsWindow.MouseMove = handleMouseMove GraphicsWindow.MouseDown = handleMouseClick GraphicsWindow.Show() looping = "True" While (looping) TextWindow.WriteLine("Happily looping...") Program.Delay(1000) EndWhile
6
Import PDG320 Sub handleKeyPress TextWindow.WriteLine("Key Down is " + GraphicsWindow.LastKey) EndSub Sub handleKeyUp TextWindow.WriteLine("Key Up is " + GraphicsWindow.LastKey) EndSub Sub handleMouseMove TextWindow.WriteLine("Mouse is at " + GraphicsWindow.MouseX + "," + GraphicsWindow.MouseY) EndSub Sub handleMouseClick TextWindow.WriteLine("Mouse is clicked") EndSub
7
These Are the Key Lines... GraphicsWindow.KeyDown = handleKeyPress GraphicsWindow.KeyUp = handleKeyUp GraphicsWindow.MouseMove = handleMouseMove GraphicsWindow.MouseDown = handleMouseClick They assign subroutines to these events....
9
The Program Object Program.Delay(time) - This will cause the program to wait for a certain amount of time, specified as a number in milliseconds. Program.End() - This will cause the program to immediately terminate.
10
Timer There is also a Timer object if you want Small Basic to let you know when a certain amount of time has passed. You tell the object how often the time interval is, and give it the name of a subroutine. Timer.Interval = 1000 Timer.Tick = OnTimerTick Sub OnTimerTick TextWindow.WriteLine(“Timer has ticked”) EndSub
11
The Timer Object Why Would You Use This? Perhaps you have a clock in your program that needs to count down or up every second... You can also use it to help show an obstacle or enemy in your game. (A new one comes on screen every 3 seconds.)
12
The Sound Object Look at the reference documentation for the Sound Object Small Basic gives you the ability to play some elementary or complex sounds. A bell ringing, a chime, and a click are three of the sounds that you get for “free” with Small Basic.
13
Import QRL274 looping = "True" While (looping) Program.Delay(3000) Sound.PlayBellRing() TextWindow.WriteLine("Playing bell ring") Program.Delay(3000) Sound.PlayChime() TextWindow.WriteLine("Playing chime") Program.Delay(3000) Sound.PlayBellRingAndWait() TextWindow.WriteLine("Played bell ring") EndWhile
14
What’s the Difference Between Play and PlayAndWait? The Play() operations will play the sound and immediately continue running the program (even if the sound hasn’t finished yet). The PlayAndWait() operations will play the sound and stop the program until the sound has completed playing. Why would you want to use either one?
15
Import XMV444 filepath = "http://creprogramming.com/sounds/a.wav"http://creprogramming.com/sounds/a.wav Sound.PlayAndWait(filepath) Sound.Play(filepath) Program.Delay(1000) Sound.Pause(filepath) Program.Delay(1000) Sound.Play(filepath) Program.Delay(500) Sound.Pause(filepath) Program.Delay(500) Sound.Play(filepath) Program.Delay(500) Sound.Stop(filepath)
16
We Can Play a Sound, Pause It, Continue Playing It, and Stop It The Sound.Play() operation will start to play the sound, and immediately continues running the program. The Sound.Pause() operation pauses the sound. The Sound.Stop() operation stops and resets.
17
The Text Object Look at the reference documentation for the Text object The Text object allows you to manipulate Strings. We can get the length of a string, append to it, check for text inside of it, etc.
18
Text - Import LKW972 TextWindow.Write("Enter a valid e-mail address: ") EmailID = TextWindow.Read() m = Text.IsSubText(EmailID, ".") n = Text.IsSubText(EmailID, "@") If (m = "True") And (n = "True") Then TextWindow.WriteLine("Valid e-mail address.") Else TextWindow.WriteLine("Invalid e-mail address.") EndIf
19
What is SubText? Text.isSubText(text, subText) - Gets whether or not a given subText is a subset of the larger text. Text.GetSubText(text, start, length) - Gets a sub-text from the given text. Text.GetSubTextToEnd(text, start) - Gets a sub-text from the given text from a specified position to the end.
20
More Text Operations Text.GetIndexOf(text, subText) - Finds the position where a sub-text appears in the specified text. Text.ConvertToLowerCase(text) - Converts the given text to lower case. Text.ConvertToUpperCase(text) - Converts the given text to upper case.
21
Names - Import JFG395 TextWindow.Write("Enter your full name: ") Name = TextWindow.Read() c = Text.GetIndexOf(Name, " ") firstName = Text.GetSubText(Name, 1, c-1) lastName = Text.GetSubTextToEnd(Name, c+1) TextWindow.WriteLine("First name: " + firstName) TextWindow.WriteLine("Last name: " + lastName)
22
How Did We Separate The Names? We asked the Text object to locate the first instance of a “space” in the string. We then took the substring from position 1 up to the position before the space (c-1) and made that the first name. We took the substring from position c+1 to the end and made that the last name.
23
Assignment Create a Timer that calls a subroutine every 5 seconds. In that subroutine, play the bells sound...unless it’s been 30 seconds, at which point, you should play the chimes instead. Or… create a shape on top of a graphics window that will move up, down, left, and right if you press the arrow keys. Create a “wrap around” if the shape goes off the edge of the screen.
24
Next Time... We’re going to learn about stacks and the main game loop.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.