Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Need suggestions on timer

Author
Message
MC Man
13
Years of Service
User Offline
Joined: 14th Feb 2013
Location:
Posted: 14th Feb 2013 17:31
I was just wondering if anyone will look at my code and see if there is anything wrong with the timer I have. I'm trying to make the timer count down while I answer the questions. Any suggestions?

If so, please reply. Thanks!
Libervurto
20
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 14th Feb 2013 19:06 Edited at: 14th Feb 2013 20:58
This is a nice game and a good example to learn from.
The issue you're having is because you've split up the program into tiny pieces with all these waits and loops. When you use a loop like this:
the program will be stuck in the loop until the exit condition is met (TimeLeft=0), everything below this will not be executed until the time has run out. (Kudos for using a variable for the time limit btw. )

What we need to do is print the question and check for input all while the timer is counting down, so everything has to be within the same loop.

When we look at the rest of the program we see a pattern: we have a rigid question structure and check for inputs in the same way. With practice you will learn to recognise the patterns in code that hint that loops can be used to reduce the amount of code we need to write (and read!). Make the computer do the work, it can work faster than you and never gets bored.

Let's compare two question and answer pairs:


Now let's cut out everything that's unique and replace it with variables to leave a standard format that we can use for all questions:

See that I've used "X" in all the new variables? That's just a reminder that these strings are specific to question X (where X is any number). This reminds us that we can't just throw one string into each variable: we're going to need a different set of strings for each question. Luckily we can use an array to do just that!

An array is a group of similar variables (when part of an array these are called "fields" or "entities"), where each variable can be called by its number (just like players on a sports team). Arrays are very useful for situations where you have lots of similar data that you want to manipulate in the same way. We declare and assign values to arrays like so:

Note that I make use of field zero, you don't have to start with zero and sometimes it's useful to leave it empty but never forget zero.
When initialising an array with data it's much better to read it in rather than do the above, here's an example of reading from data statements:

Much less typing and much easier to edit. (Note that there's a limit to how many characters you can put on one line, if the compiler complains simply start a new line with a new data statement: multiple data lines act just the same as a single line.)

Now we can do the same with the answer options, but instead of this:
we can combine the options into the same array by making it two-dimensional (sounds complex but I will explain):
The elements of an array that are separated by a comma are its dimensions: we have two dimensions here so we can think of this array like a square of length "2" and width "maxQ", this gives us three times as many variables as we had for the questions since we have three rows of "maxQ" variables. (You didn't forget zero did you?) Now when we reference this array we give two numbers, one for each dimension, just like referencing a grid-square on a map. So when we write
we're storing "17/10" as option zero for question zero, "17/100" as option one for question zero, and so on...

We can put lots of data in the same array as long as it is of the same type (integer, float, string...), indeed we make room for the questions themselves in this array but I'd strongly recommend against that as it will be confusing since questions and answers are not the same thing. It takes no more memory to declare two arrays with ten fields than to declare one array with twenty. When deciding what data to group together, think about how you will use the data: if you're going to perform the same action on a group of variables then it will probably help to keep them together.

Here's what I have after applying what we've just gone over:
notice how I've grouped the questions and answers together so it's easier to understand which answers apply to which questions.

Now let's add in the main loop that will actually display the questions and take input:
There's no timer or delayed printing of the options but we can add those things back in later. It's important to get things working before adding new features.

Still here?...

I added in Phaelax's timer and that means we have to get rid of the input command because it will halt our program and stop the clock counting down until the player enters an answer; that's no good so we will replace it with a keystate() check.

I had an issue at this point because I used zero as the first question/answer, can you see why this might cause a problem?:

There's nothing to distinguish between no answer and the answer zero, since they both have a value of zero! We could give the "ans" variable a value of -1 to start or something like that but I think it's better if we just change it to start at one instead.

Only a few changes were required to fix this problem: that's the beauty of using arrays, imagine if we still had a block of code for every question and suddenly noticed something was wrong! Here's the full code


I'll stop there since I've covered quite a lot. There are still a few things that could be worked on:
* User can accidentally answer the next question if they press (1 to 4) when it says "press any key to continue"
* Text is unformatted (look into the TEXT command, you will not get formatting besides colour using PRINT)
* Could add animations to the text, sound etc.

Congratulations, you've reached the end. Your penguin rating has increased to:

^ That's what she said.
Phaelax
DBPro Master
23
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 14th Feb 2013 20:12
I didn't look at your code, and I didn't read the novel Obese just wrote, but....

Simple count down timer:


"You're all wrong. You're all idiots." ~Fluffy Rabbit
Libervurto
20
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 16th Feb 2013 23:10
I think I scared him off!

Login to post a reply

Server time is: 2026-07-07 05:16:04
Your offset time is: 2026-07-07 05:16:04