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 chapter 5.
You now have enough knowledge about Sphere and Javascript to make your own game. You will have 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.
- 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.
- Answers
- Hints
- Download my result (it's English!)
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
function game()
{
//The following variables might be named differently from your code,
//as well as the filenames (except when you used the resource pack).
var image = LoadImage("plaatje.png");
var window = LoadWindowStyle("Window.rws");
var font = LoadFont("font.rfn");
var sound1 = LoadSound("muziek.mid");
var sound2 = LoadSound("muziek2.mid");
//Let's draw!
image.blit(0, 0);
//Draw platje.png (that's stored in var image) on coordinates (0,0).
window.drawWindow(0, 0, 320, 100);
//Draw the windowstyle window.rws on coordinates (0,0) to (320,100).
//That's almost the whole top half of the screen, horizontally.
font.drawText(5,5,"Welcome to my first script intro!!");
//Draws the text "Welcome to my first script intro!", starting at coordinates (5,5).
//(zodat het keurig in de window komt).
sound1.play(true);
//Plays geluid1.mid and loops it (that's what the second 'true' parameter is for).
font.drawText(0, 230, "Made by me");
//Draw the text "Made by me" on coordinated (0, 230), which is at the bottom-left of the screen.
FlipScreen();
//Put everything we've just drawn on screen.
GetKey();
//And then the engine must wait until a key was pressed!
//The following code will only work AFTER this keypress:
sound1.stop(); //Stop sound 1.
sound2.play(true);
//We will now create a new person called "Person", who uses the spriteset
//"persoon.rss" and who will not be destroyed when the game switches maps (or exits the map engine).
//After that, we'll attach the camera view and the keyboard input to this person.
CreatePerson("Person", "persoon.rss", false);
AttachCamera ("Persoon");
AttachInput ("Persoon");
//And now we start... The map engine! It opens up "map.rmp" and renders it
//with a framerate of 60 frames per second (meaning the map refreshes once every 1/60th
//second).
MapEngine("map.rmp", 60);
} //A brace to close our function game().
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.
If you really don't get why it won't work, paste your code in the | Pastebin and link to it in this Article's discussion page (with your nickname). Or PM me on the Spherical forums.
Answers test 2
In the script file
//First, let's make our global variables.
var font = LoadFont("font.rfn");
var WindowStyle = LoadWindowStyle("Window.rws");
var music1 = LoadSound("muziek1.mid");
var music2 = LoadSound("muziek2.mid");
var image = LoadImage("plaatje.png");
//Why are the resources global? Because they're used more often, and this saves time and memory because the resources only have to be loaded once!
//Story variable. Global.
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("hoofdpersoon", "persoon.rss", false);
AttachCamera("hoofdpersoon");
AttachInput("hoofdpersoon");
MapEngine("map.rmp",60);
}
//Textbox function, with input parameter.
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.
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++)
{
//Render the map on the background first.
RenderMap();
/*The windowstyle will change height accordingly to the n variable.
Since the value n becomes bigger each time the loop is run, the window-
style will also become bigger. /*
WindowStyle.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!
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]);
//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("Wow, we're at 5!");
if (Story >= 10) TextBox("Oh, stop it! You've talked to me " + Story + " times already!");
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.
If you really don't get why it won't work, paste your code in the | Pastebin and link to it in this Article's discussion page (with your nickname). Or PM me on the Spherical forums.





