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.

Newcomers DBPro Corner / decreasing variables in loops

Author
Message
Paul08
16
Years of Service
User Offline
Joined: 20th Feb 2008
Location: Oxford, UK
Posted: 28th Feb 2008 22:42
health=100
do

if downkey()=1 then dec health,1

loop

-obviously because my laptop is a super being it runs this at a stupid speed and so just touching the "down key" takes a lot off this variable

solution? i need some kind of a trap so it only takes this key press on release or something.?

thanks

Paul
Dabbler
17
Years of Service
User Offline
Joined: 3rd Mar 2007
Location: Minnesota
Posted: 28th Feb 2008 22:55 Edited at: 28th Feb 2008 22:55
How about this:




Whatever...
Sixty Squares
18
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 29th Feb 2008 00:17 Edited at: 29th Feb 2008 00:18
Or this:



TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 29th Feb 2008 00:41
Can I have a go?...



TDK_Man

Dabbler
17
Years of Service
User Offline
Joined: 3rd Mar 2007
Location: Minnesota
Posted: 29th Feb 2008 03:35
First: I have never had a problem with that little debounce thingy, but if a user leans on a button it will kinda suspend your main loop.

Second: Sixty Squares' code doesn't have the suspend effect.

Third: I like the formula look of TDK's code.

So:




Whatever...
procyon
16
Years of Service
User Offline
Joined: 29th Feb 2008
Location:
Posted: 29th Feb 2008 08:22 Edited at: 29th Feb 2008 16:45
I haven't programmed in DarkBasic before but would this work.

Obviously, this would not work within a game loop as the program is waiting for you to release the downkey.
Paul08
16
Years of Service
User Offline
Joined: 20th Feb 2008
Location: Oxford, UK
Posted: 29th Feb 2008 08:37
cant run a cls as it would look dodgy.
a nice selection there cheers for everyones input - i swear some of you have swallowed the DB manual

thnx
Paul

Paul
Ed222
17
Years of Service
User Offline
Joined: 3rd Nov 2007
Location: Calgary
Posted: 7th Mar 2008 23:12
@procyon
That wouldn't work because it would sill dec the health not to metion you forgot to put until in so maybe this would be better?


Tip Of The Day!: a new rouge anti-virus has risen in 2008 called xpantivirus 2008 don't download this. Please help spread the word.
Dabbler
17
Years of Service
User Offline
Joined: 3rd Mar 2007
Location: Minnesota
Posted: 8th Mar 2008 18:01 Edited at: 8th Mar 2008 18:04
Quote: "All others will completely stop the main loop while the key is being held down."


No. My second snippet does not stop the main loop:




Whatever...
Ed222
17
Years of Service
User Offline
Joined: 3rd Nov 2007
Location: Calgary
Posted: 8th Mar 2008 18:10 Edited at: 8th Mar 2008 18:12
@WindowsKiller
I know it'll stop the program but I was just making procyon
code work

heres my version dabbler although it's close to yours


don't worry this won't pause your program either

Tip Of The Day!: a new rouge anti-virus has risen in 2008 called xpantivirus 2008 don't download this. Please help spread the word.
TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 8th Mar 2008 18:17 Edited at: 8th Mar 2008 18:19
Quote: "Sixty Squares' snippet is the only good one"


You can't say that!!

That means you are saying that all the other suggestions are bad - which is not the case.

In fact, all the snippets did what Paul08 asked for - despite the fact that Sixty Squares code snippet is most definitely the better way to do it in some situations.

So, none of the solutions were bad because they all did what was asked for. It wasn't known at the time if the program freezing mattered but as Paul08 included the phrase:

Quote: "takes this key press on release"


..it suggested that it didn't matter. For example, on a screen where you set up a game character's attributes it probably wouldn't matter.

Obviously there are many cases where pressing a key and the program not stopping could be problematic.

TDK_Man

IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 8th Mar 2008 19:21
They are from this programmers point of view.

They all fulfil the original requirement. They all work. Ok, they may not fit future requirements, but until we know those requirements, we can't possibly say.

I've coded variations of the same methods posted above myself. Sixty Squares code is good for a program that needs to continually run the main loop whether the key is pressed or not, and the others are good for a program that doesn't.

Dabbler
17
Years of Service
User Offline
Joined: 3rd Mar 2007
Location: Minnesota
Posted: 8th Mar 2008 19:35
Quote: " Sixty Squares code is good for a program that needs to continually run the main loop whether the key is pressed or not, and the others are good for a program that doesn't.
"


I hate to have to repeat myself, but my second snippet does not suspend the main loop.


Whatever...
Ed222
17
Years of Service
User Offline
Joined: 3rd Nov 2007
Location: Calgary
Posted: 8th Mar 2008 19:56
My second snippet also doesn't suspend the main loop

Tip Of The Day!: a new rouge anti-virus has risen in 2008 called xpantivirus 2008 don't download this. Please help spread the word.
Paul08
16
Years of Service
User Offline
Joined: 20th Feb 2008
Location: Oxford, UK
Posted: 9th Mar 2008 13:53
Oh crap have I started a war?

Well much thanks to everyone, i've robbed all your code and used all of them in different ways - ok well some of them

Over and out

Paul
procyon
16
Years of Service
User Offline
Joined: 29th Feb 2008
Location:
Posted: 9th Mar 2008 22:22
@Ed222
I don't think i did too bad considering i don't own DarkBasic Pro and have never programmed with it but i will be buying it in the future. Check the code again, i did put the Until command in just not on a new line. Here is my version of a timed key delay. The quarter second delay can be changed to whatever you want and the program does not stop.

Login to post a reply

Server time is: 2024-11-25 08:00:46
Your offset time is: 2024-11-25 08:00:46