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 / how would I implement a timer into this game?

Author
Message
Coding Beast
13
Years of Service
User Offline
Joined: 13th Dec 2012
Location:
Posted: 20th Mar 2013 15:58
i need a timer for this question. Any ideas?







set text size 20
cls rgb (56,22,112)
`question
center text 330,0, "Question no.1"
center text 330,15,"hit the key of your answer choice"
set text size 30
starttime = timer()
do
cls
text 0,0,"My Time: "+str$((timer()-starttime)/1000)
loop
center text 330,40, "SCORE:+0"
set text size 25
center text 330,100, "Which is the greatest decimal value?"
center text 330,150, " 0.45, 0.50, 0.35, 0.75"
center text 200,250, "A) 0.35"
center text 200,300, "B) 0.50"
center text 400,250, "C) 0.75"
center text 400,300, "D) 0.45"
wait key
`answer choices
if inkey$()="a"
cls
cls rgb (255,0,0)
play sound 2
set text size 50
center text 330,50, "Game Over"
center text 330,150, "Please reload the game"
wait key
end
endif
if inkey$()="b"
cls
cls rgb (255,0,0)
play sound 2
set text size 50
center text 330,50, "Game Over"
center text 330,150, "Please reload the game"
wait key
end
endif
if inkey$()="d"
cls
cls rgb (255,0,0)
play sound 2
set text size 50
center text 330,50, "Game Over"
center text 330,150, "Please reload the game"
wait key
end
endif
if inkey$()="c"
cls
cls rgb (0,255,0)
play sound 3
set text size 65
center text 330,150, "Correct"
set text size 36
center text 330,225, "(Please hit any key to continue)"
wait key
cls
endif
cls

if i fail i try again
ShellfishGames
13
Years of Service
User Offline
Joined: 6th Feb 2013
Location:
Posted: 20th Mar 2013 22:40
Maybe you should be a little more specific. Apart from that Sergey K already hinted at the timer()-function in your other thread on the same topic.

Considering that your game already works in real time, adding a timer is really simple. You just need to put "starttime = timer()" before your main loop and the following inside it:



And I guess that's it. Given that this is the kind of timer you are asking for.

Coding Beast
13
Years of Service
User Offline
Joined: 13th Dec 2012
Location:
Posted: 21st Mar 2013 16:11
How do i get it starting?
also how do u make a score counter to put the score at the end of the game

if i fail i try again
ShellfishGames
13
Years of Service
User Offline
Joined: 6th Feb 2013
Location:
Posted: 21st Mar 2013 17:54
You should seriously inform yourself about arrays (and user defined types) and functions. They would save you a lot of work (and make your code approximately 90% shorter). I mean, seriously, do that as soon as possible, the concepts are quite simple and it's totally worth it.
You might also consider code indentation.

Although this probably doesn't have a great learning effect, I wrote an example code for such a program including a timer, the score and a question-system utilizing the things mentioned above. Makes things a lot easier, and applying changes to your code requires a lot less time.





Libervurto
20
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 21st Mar 2013 21:18
Coding Beast
13
Years of Service
User Offline
Joined: 13th Dec 2012
Location:
Posted: 22nd Mar 2013 16:20
can u tell me in detail what you did please @shellfishgames

if i fail i try again
Coding Beast
13
Years of Service
User Offline
Joined: 13th Dec 2012
Location:
Posted: 22nd Mar 2013 16:21
can u tell me in detail what you did please @shellfishgames

if i fail i try again
ShellfishGames
13
Years of Service
User Offline
Joined: 6th Feb 2013
Location:
Posted: 22nd Mar 2013 18:14 Edited at: 22nd Mar 2013 18:50
Sure.

Lets start with the type:



You can define your own types - basically the same thing as integer, string, float etc., just potentially more complex. This data type is designed in a way to contain questions for your quiz as well as their respective solutions. So you could create a single question like this:



So you have one variable myQuestion that contains all the data. Using the "variable.attribute"-notation you can access the individual values defined in the type.

However, this isn't much of an improvement in comparison to your code. So the first step would be to use an array instead of individual variables. Consequently all questions will be stored in a set of variables that can be accessed via indices.



Arrays are basically just lists. This array is created with 0 elements (actually 1, but let's ignore that for now), so it starts empty. The following code would add a single question to the list of questions stored in that array:



But since you want to add more than just one question, you'd end up with a lot of redundant code. That's why writing a function makes sense.

This is the above code utilizing a function:



This way you can add arbitrarily many functions without writing any redundant code.

This whole construct yields the advantage that everything is way easier to handle. Instead of working with a specific question, you can always refer to some question that is defined by an index.
For instance you could write a function 'printQuestion(ID)':



And you can use this function for any of your questions instead of writing the code for each question individually.

Consider that you want to change the position to which the question is printed. In your code you'd need to change the position value at many places, whereas in the above implementation you could just change the printQuestion()-function and save a lot of time.


Well, I hope that made things clearer. Otherwise, feel free to ask, but I highly recommend reading a few tutorials on arrays, types, functions etc. (the DBPro help contains some paragraphs as well afaik).

Login to post a reply

Server time is: 2026-07-07 02:39:48
Your offset time is: 2026-07-07 02:39:48