DaVince scripting tutorial tests
From Spheriki
This page is intended for the tests that go with DaVince's scripting tutorial. It contains tests so that you will be able to use the information you learned in practice.
Do not forget you can use the resource pack for the simple resources (simple images, spritesets, windowstyles, everything you need).
NOTE: filenames are Dutch. I hope you don't mind that too much. If it does I'll create an English version of the resource pack.
Contents |
Test 1
It's best to try this one after you finished chapter 3.2.
Since you now know how to draw things on-screen, let's try to make an intro screen that contains the following:
- Place a background image that takes up the entire game screen (game resolution is 320x240 by default).
- Place a window in the top, which contains the text "Welcome to my first script intro!" You can define size and coordinates (as long as things stay on the top of the screen) yourself.
- Add some background music that plays during the intro screen.
- Place a small piece of text in the bottom that states that this game was made by you. ;)
The intro screen should stay as long as you don't press a key. But when you press a key, the following must happen:
- The previous music must be stopped and a different music file has to be played.
- A map and sprite/person are loaded into memory and the map engine will be started. Note that you have to be able to move around with the person.
Results will have to look somewhat like the following:
This image is with Dutch text, but it at least shows what it kinda should look like.
Well, that's it. Have fun!
Test 2
It's best to try this one after you finished the chapter about conditions and loops.
You now have enough knowledge about Sphere and Javascript to make your own game. For this test, you will have to try to make a game that contains the following:
- An animated intro. You can do this in a loop, moving around things or switching between images to show. You can decide whether to wait for a keypress or close the intro after some time yourself.
- Start the MapEngine() after having shown the intro.
- Your game has to have a textbox function which can show different text (using a parameter).
- Try animating the textbox a bit when it's opening up. You can make the textbox stretch from a height of 0 to a height of around 100 pixels.
- When pressing escape, you'll have to see a textbox containing some text that's read from an array. After this the game has to exit.
- Bonus points: use a loop to quickly display all items in the array in a single textbox.
- Add a person to the map. Make it so you can talk to him, and add 1 to a story variable when you talk to him.
- If the story variable equals 5, make the person say something else, using TextBox().
- If the story variable is higher than 10, make the person say something about him being annoyed. Also make him say how many times you talked to him.
This is quite a lot to do, but with the knowledge you should have now it's all doable.
The reason you're given so much freedom with what you can do with the animation and content is so you can entertain yourself a bit and so you don't have to follow some fixed, exact instructions. :)
Look up these functions
You can look up the following functions in the Wiki or doc_functions. They'll come in useful or even HAVE to be used for this game.
- BindKey(key, "pressed", "released")
- Exit()
- IsAnyKeyPressed()
- IsKeyPressed(key)
Hints
Hints if you don't know how to do something, but don't want to look at the answers.
Hints test 1
- Commands (most of the code) are being run in order. That means that you have to watch the order you're using and not mess them up. For example, font.drawText() must go AFTER WindowStyle.drawWindow(), because you will first draw the window and then the text over it that way, instead of drawing the window over the text (which is bad).
- About those orders: keep in mind a GetKey() will come AFTER a FlipScreen() because else, Sphere would not be able to do a FlipScreen() until a key was pressed...
- Important for the functions CreatePerson(), AttachCamera() and AttachInput() is that they go BEFORE a MapEngine(), same deal with the order: Sphere won't be able to read these commands until the map engine would be closed...
Hints test 2
- You can make text, images and stuff move around if you use a loop. To move around stuff, make some variables just before the loop, then change these variables inside of the loop, and make the image/text drawing functions use these variables.
- You can use IsKeyPressed(key) in a loop to make the engine wait for that key.
- Use Sphere/docs/keys.txt for reference on the keys.
- Variables that are important and that have to keep their values must be declared globally, which means NOT inside any function. The top of your script is a nice place to put these variables. The story variable is one of those important variables.
Answers
These are the answers to the test, or at least how I made it. Of course some things might look different in your scripts, because you could be scripting things a bit differently, or use different filenames for external files (if you're not using the resource pack), or different variable names...
There are also lots of comments in the answers, so you can check what you might have done wrong somewhere.
Answers test 1
This is taken directly from the downloadable project above, so you can get that one too and view this script directly in Sphere.
/*
DAVINCE SCRIPTING TUTORIAL
Test 1 - answers.
Only check this out once you've tried to do the test and you're either stuck or finished!
*/
function game()
{
/* The following variable names are very likely different from whatever you used. It doesn't matter,
as long as you know to identify them properly.
*/
var image = LoadImage("background.png");
var window = LoadWindowStyle("window.rws");
var font = LoadFont("font.rfn");
var song_a = LoadSound("song1.mid");
var song_b = LoadSound("song2.mid");
//====
//Drawing things starts from here, since we've loaded all resources we need.
//====
//Draw our image at the coordinates (0,0), which is the most top-left pixel on the screen.
image.blit(0, 0);
//Draw the windowstyle (using window.rws) at coordinates (0,0) to (320,100).
//The window takes up approximately half of the screen with these dimensions.
window.drawWindow(10, 10, 300, 100);
/* Write "Welcome to my first script intro!" at screen coordinates (10,10),
* which is close to the top-left of the screen. The 10 pixels are so the text is within
* the windowstyle's border. */
font.drawText(10, 10, "Welcome to my first script intro!");
//Play song 1 (which is stored in var song_a) and loop it ("true" is for looping here).
song_a.play(true);
//Write "made by <your name>" close to the bottom of the screen.
//(The game's resolution is 320x240, so y position 220 is close to the bottom.)
font.drawText(0, 220, "Made by DaVince");
//We have drawn all the above stuff into a special video buffer.
//Now let's display this video buffer onto the screen using FlipScreen()!
FlipScreen();
//====
//Now that we've drawn our stuff, let's focus on the game's logic. In our case, we want the
//above screen to keep displaying until a key is pressed, and after that, do some more stuff.
//====
//Wait for a key (ANY key) to be pressed.
GetKey();
//Switch the music after the keypress. That is, stop the old song and then play the new one.
song_a.stop();
song_b.play(true);
/* In preparation to open the map engine, we first create our main character:
* A person who will never disappear, even when you switch maps
* (that's what the "false" in CreatePerson() is for here).
* We also attach the camera and input to this person. */
CreatePerson("stickman", "person.rss", false);
AttachCamera ("stickman");
AttachInput ("stickman");
//Finally, open the map at 60 frames per second . Our person "stickman" will appear at the
//map's entry point (which is indicated with the letters ST in the map editor).
MapEngine("map.rmp", 60);
} //Close the function game()'s code with a final closing brace (}).
If your script doesn't work and you think it's good, watch for typos. Also check if you didn't forget any ( )'s or { }'s. If it still doesn't work, check what error Sphere gives and try checking out that specific piece of code.
Answers test 2
In the script file
/*
DAVINCE SCRIPTING TUTORIAL
Test 2 - answers.
Only check this out once you've tried to do the test and you're either stuck or finished!
*/
//First, let's make our global variables.
var font = LoadFont("font.rfn");
var window = LoadWindowStyle("window.rws");
var song1 = LoadSound("song1.mid");
var song2 = LoadSound("song2.mid");
var image = LoadImage("background.png");
//Why are the resources global? Because they're used more often, this saves both time and
//memory because the resources have to be loaded only once!
//story variable. Also global, because we need it in several places.
var story = 0;
//The main function.
function game()
{
//I've put my intro animation in another function, so I'll call it from here.
IntroAnimation();
//We're binding the escape key to a function so that that function will run once you press escape.
BindKey(KEY_ESCAPE, "EscapeEvent()", "");
//Now let's make the main person and start the map engine.
CreatePerson("main character", "person.rss", false);
AttachCamera("main character");
AttachInput("main character");
MapEngine("map.rmp",60);
}
//Textbox function, with input argument.
function TextBox(input)
{
/* Let's draw the windowstyle and the text. These are in a loop
* so the textbox will really stretch out from height 0 to 100 over time.
* We use a "for" loop which has the variable 'n', which we can use
* for our stretching quite neatly. */
for (var n = 0; n < 100; n++)
{
//BONUS POINTS: update the map engine before we render it.
//If you have animating tiles or moving people, they will keep animating and moving this way.
UpdateMapEngine();
//Render the map on the background first.
RenderMap();
/* The windowstyle will change height accordingly with the value of var n.
* Since the value of n becomes bigger each time the loop is run, the window-
* style will also be drawn a little bit bigger each time.
* The 10+ is there to give it a basic starting height of 10. */
window.drawWindow(10, 10, 300, 10+n);
//Same for the text, it appears vertically slowly and according to n.
font.drawTextBox(10, 10, 300, 10+n, 0, input);
//Now let's put it all on screen! Note that this is done just once at the end of your loop.
FlipScreen();
}
/* Okay, we're done with the loop. Now we wait until the space key
was pressed. And while it isn't: wait until it is! */
while (IsKeyPressed(KEY_SPACE) == false)
{ }
//Done with text box function.
}
//This is our intro animation.
function IntroAnimation()
{
var anim = 0;
//Keep repeating this code until any key was pressed.
while (IsAnyKeyPressed() == false)
{
anim++;
//Some background images that change size and position as the loop restarts.
image.zoomBlit(anim/200, anim/350, anim/5000);
image.zoomBlit(anim/50, anim/100, 0.5);
image.zoomBlit(320-(anim/40), 240-(anim/80), 0.7);
//Some text that moves around. Like how I did it, they start on the same position
//and move all in opposite directions.
font.drawText(110+(anim/20), 100+(anim/100), "Domme animatie");
font.drawText(110+(anim/-20), 100+(anim/-100), "Domme animatie");
font.drawText(110+(anim/100), 100+(anim/20), "Domme animatie");
font.drawText(110+(anim/-100), 100+(anim/-20), "Domme animatie");
//Draw the bunch on screen.
FlipScreen();
}
}
//Escape key event.
function EscapeEvent()
{
//We're making the array.
var array = [];
array[0] = "Milk";
array[1] = "Sugar";
array[2] = "Bread";
array[3] = "Eggs";
array[4] = "Pizza!";
array[5] = "Candy";
array[6] = "Moorrreee!";
//Let's make a textbox that shows it all. \n means new line.
TextBox(array[0] + "\n" +
array[1] + "\n" +
array[2] + "\n" +
array[3] + "\n" +
array[4] + "\n" +
array[5] + "\n" +
array[6]);
/* BONUS POINTS (and a useful tip):
* Instead of doing the textbox like above, you made a variable containing an empty string,
* then filled up that string with the array's contents using a loop. This is how you do it:
var str = "";
for (var i = 0; i < array.length; i++)
str += array[i] + "\n";
TextBox(str);
*/
//And then exit the game.
Exit();
}
Person: On Activate (Talk) script (check the person's properties)
TextBox("Hello. I say something.");
story++;
if (story == 5) TextBox("You talk a lot, don't you?");
if (story >= 10) TextBox("Oh, stop it! You've talked to me " + story + " times now!");
If your script doesn't work and you think it's good, watch for typos. Also check if you didn't forget any ( )'s or { }'s. If it still doesn't work, check what error Sphere gives and try checking out that specific piece of code.



