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:
// These needs...// using static SplashKitSDK.SplashKit;
// Event looppublic static void ProcessEvents();public static bool QuitRequested();
// Key input// This also requiers... to get access to the KeyCode details.// using SplashKitSDK;public static bool KeyDown(KeyCode key);
// Random numberspublic static float Rnd(); // 0 to 1public static int Rnd(int ubound); // 0 to ubound - 1public static int Rnd(int min, int max); // min to max
// Time trackingpublic static Timer CreateTimer(string name);public static void StartTimer(string name);public static uint TimerTicks(string name);
// Collision detectionpublic static bool CirclesIntersect(double c1X, double c1Y, double c1Radius, double c2X, double c2Y, double c2Radius);public static bool TriangleRectangleIntersect(Triangle tri, Rectangle rect);public static Rectangle RectangleFrom(double x, double y, double width, double height);public static Triangle TriangleFrom(double x1, double y1, double x2, double y2, double x3, double y3);
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:
// These methods need// using static SplashKitSDK.SplashKit;
// Was a given button on the mouse clicked?public static bool MouseClicked(MouseButton button);// Is a given button in the mouse current held down?public static bool MouseDown(MouseButton button);
// Where is the mouse - relative to the left side of the window?public static float MouseX();// Where is the mouse - relative to the top of the window?public static float MouseY();
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.
using static SplashKitSDK.SplashKit;using SplashKitSDK;
const int MOVE_SPEED = 3;
OpenWindow("Movement", 800, 600);
// Event loopdouble x = 400;Color clr = ColorBlack();
while( !QuitRequested() ){ ProcessEvents();
if (KeyDown(KeyCode.RightKey)) { x += MOVE_SPEED; }
if (KeyDown(KeyCode.LeftKey)) { x -= MOVE_SPEED; }
if (KeyTyped(KeyCode.SpaceKey)) { clr = RandomColor(); }
ClearScreen(ColorWhite()); FillCircle(clr, x, 300, 50); RefreshScreen(60);}
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.
using static SplashKitSDK.SplashKit;using SplashKitSDK;
const int BALL_RADIUS = 5;const int PLAYER_RADIUS = 20;
// Used to calculate distance from player - mouse for// Ball speed. speed = distance / DIST_TO_SPEED_RATIO.const double DIST_TO_SPEED_RATIO = 30;// as above for the line drawn to show direction of shotconst double DIST_LINE_RATIO = 10;
OpenWindow("Ball Throw", 800, 600);
bool ballFired = false;
double playerX = 400, playerY = 300;double ballX = playerX;double ballY = playerY;double ballXSpeed = 0;double ballYSpeed = 0;
while (!QuitRequested()){ ProcessEvents();
if (MouseClicked(MouseButton.LeftButton) && !ballFired) { ballFired = true;
// calculate velocity for the BALL ballXSpeed = (MouseX() - playerX) / DIST_TO_SPEED_RATIO; ballYSpeed = (MouseY() - playerY) / DIST_TO_SPEED_RATIO; }
if (ballFired) { // Move the projective by its x and y speed ballX += ballXSpeed; ballY += ballYSpeed;
// check if off screen if (ballX + BALL_RADIUS < 0 || // off left ballX - BALL_RADIUS > ScreenWidth() || // off right ballY + BALL_RADIUS < 0 || // off top ballY - BALL_RADIUS > ScreenHeight() ) // off bottom { ballFired = false; ballX = playerX; ballY = playerY; } }
ClearScreen(ColorWhite()); FillCircle(ColorLightGreen(), playerX, playerY, PLAYER_RADIUS); FillCircle(ColorBlack(), ballX, ballY, BALL_RADIUS);
if ( ! ballFired ) { // Show direction of travel DrawLine(ColorBlack(), playerX, playerY, playerX + (MouseX() - playerX) / DIST_LINE_RATIO, playerY + (MouseY() - playerY) / DIST_LINE_RATIO ); }
RefreshScreen(60);}