Multimedia methods to use
With the more interactive programs using SplashKit there are now a whole range of methods that we can use to check for user inputs, and to help test different aspect of our game (such as time passing, and detecting collisions).
The methods we will need in this chapter include:
-
Event loop methods:
- Process Events - listens for actions that the user has performed such as keystrokes, mouse clicks, quitting, etc.
- Quit Requested - returns true when the user has asked to close the program.
-
Key input methods:
- Key Down - allows you to test if a key is held down.
-
Random number methods:
- Rnd - Generate a random number.
-
Time management methods:
- Create Timer - create a timer to keep track of time.
- Start Timer - start a timer ticking. You can then query it for how long has passed since it was started.
- Timer Ticks - tells you how long has passed since the timer was started.
-
Collision detection methods:
- Circles Intersect - test if two circles intersect.
- Triangle Rectangle Intersect - test if a triangle and rectangle intersect.
- Rectangle From - create a rectangle from a given point, width, and height.
- Triangle From - create a triangle from a given set of points.
These methods have the following declarations:
Key Codes
For the KeyDown
method, you need to tell it which key you are wanting to check. SplashKit provides a list of key codes in a KeyCode
type, which provides a list of values you can choose from. You should be able to see these in VS Code using its IntelliSense, just make sure you have using SplashKitSDK;
so that you can see the KeyCode
.
The main options include:
BackspaceKey | TabKey | ReturnKey | EscapeKey | SpaceKey |
Num0Key | Num1Key | Num2Key | Num3Key | Num4Key |
Num5Key | Num6Key | Num7Key | Num8Key | Num9Key |
ColonKey | SemiColonKey | AKey | BKey | CKey |
DKey | EKey | FKey | GKey | HKey |
IKey | JKey | KKey | LKey | MKey |
NKey | OKey | PKey | QKey | RKey |
SKey | TKey | UKey | VKey | WKey |
XKey | YKey | ZKey | DeleteKey | Keypad0 |
Keypad1 | Keypad2 | Keypad3 | Keypad4 | Keypad5 |
Keypad6 | Keypad7 | Keypad8 | Keypad9 | KeypadPeriod |
KeypadDivide | KeypadMultiply | KeypadMinus | KeypadPlus | KeypadEnter |
KeypadEquals | UpKey | DownKey | RightKey | LeftKey |
InsertKey | HomeKey | EndKey | PageUpKey | PageDownKey |
F1Key | F2Key | F3Key | F4Key | F5Key |
F6Key | F7Key | F8Key | F9Key | F10Key |
F11Key | F12Key | F13Key | F14Key | F15Key |
RightShiftKey | LeftShiftKey | RightCtrlKey | LeftCtrlKey | RightAltKey |
LeftAltKey | LeftSuperKey | RightSuperKey |
You access these using KeyCode.
and then the specific key value. For example, KeyCode.AKey
for the “a” key or KeyCode.LeftKey
for the left arrow. See the example code below for how to use this.
Mouse Buttons and Methods
SplashKit also includes a number of methods for working with the mouse. These include the following:
For these you will need to use the MouseButton
data. As with the KeyCode
data, make sure you have using SplashKitSDK;
so that you can see the MouseButton
type.
There are the following mouse button values:
LeftButton
for the left mouse buttonMiddleButton
for the middle mouse buttonRightButton
for the right mouse buttonMouseX1Button
for the x1 mouse buttonMouseX2Button
for the x2 mouse button
As with the KeyCode
data, you access this using MouseButton.
and the button name, MouseButton.LeftButton
for example. See the example code below to see how this may work.
Example Multimedia Code
Here is some example multimedia code that creates a program where the user is able to move a shape around on the screen.
The following code shows an example of using the mouse, and its position as part of a program. This uses the distance from the player to the mouse to set the direction of a ball which is thrown when the mouse is clicked.