01-07-2023, 06:51 AM
(01-07-2023, 05:12 AM)fistfullofnails Wrote: Ok, SMcneill's example program definitely helped me see what is happening, and Terry's explanation helped to solidify the point. I thank y'all very much. So what I've generally tend to do through Terry's tutorials, is to make up my own little programs using some of the things I've just read about. I was going to post this in a separate place, but I'm thinking it might be appropriate being as I think the problem might lie in the buffer situation being discussed. I attempted to make a model of a traffic light. I realize it's pretty rough and I know I could shorten it using some more variables and loops, but I just wanted to get something working to start with. So don't make too much fun haha.
I expected nothing to happen when pressing any key other than escape. But what actually does happen, is that it seems to jump to the next piece of code after the nearest 'sleep' command. Maybe I should be using '_delay' instead? Even when I press 'ESC', it does the same thing, but eventually does exit now and then. I do not know why the code is duplicated here after the first time. Sorry. Also, I had the '_display' command in that code right near the end. But my traffic light always froze after one time through the loop until I removed that command.Code: (Select All)CONST SCREENWIDTH = 900
CONST SCREENHEIGHT = 900
CONST RED = _RGB32(80, 0, 0)
CONST YELLOW = _RGB32(80, 80, 0)
CONST GREEN = _RGB32(0, 80, 0)
CONST BRIGHTRED = _RGB32(255, 0, 0)
CONST BRIGHTYELLOW = _RGB32(255, 255, 0)
CONST BRIGHTGREEN = _RGB32(0, 255, 0)
CONST DULLYELLOW = _RGB32(40, 40, 0)
DIM x%
DIM Keypress$
x% = SCREENWIDTH / 2
CONST WHITE = _RGB32(255, 255, 255)
SCREEN _NEWIMAGE(SCREENWIDTH, SCREENHEIGHT, 32)
DO
Keypress$ = INKEY$
CLS
_LIMIT 30
LINE (300, 200)-(600, 700), DULLYELLOW, BF
'Red
CIRCLE (x%, 300), 50, WHITE
PAINT (x%, 300), BRIGHTRED, WHITE
CIRCLE (x%, 450), 50, WHITE
PAINT (x%, 450), YELLOW, WHITE
CIRCLE (x%, 600), 50, WHITE
PAINT (x%, 600), GREEN, WHITE
SLEEP 8
'Green
CIRCLE (x%, 300), 50, WHITE
PAINT (x%, 300), RED, WHITE
CIRCLE (x%, 450), 50, WHITE
PAINT (x%, 450), YELLOW, WHITE
CIRCLE (x%, 600), 50, WHITE
PAINT (x%, 600), BRIGHTGREEN, WHITE
SLEEP 8
'Yellow
CIRCLE (x%, 300), 50, WHITE
PAINT (x%, 300), RED, WHITE
CIRCLE (x%, 450), 50, WHITE
PAINT (x%, 450), BRIGHTYELLOW, WHITE
CIRCLE (x%, 600), 50, WHITE
PAINT (x%, 600), GREEN, WHITE
SLEEP 5
LOOP UNTIL Keypress$ = CHR$(27)
SYSTEM
CONST SCREENWIDTH = 900
CONST SCREENHEIGHT = 900
CONST RED = _RGB32(80, 0, 0)
CONST YELLOW = _RGB32(80, 80, 0)
CONST GREEN = _RGB32(0, 80, 0)
CONST BRIGHTRED = _RGB32(255, 0, 0)
CONST BRIGHTYELLOW = _RGB32(255, 255, 0)
CONST BRIGHTGREEN = _RGB32(0, 255, 0)
CONST DULLYELLOW = _RGB32(40, 40, 0)
DIM x%
DIM Keypress$
x% = SCREENWIDTH / 2
CONST WHITE = _RGB32(255, 255, 255)
SCREEN _NEWIMAGE(SCREENWIDTH, SCREENHEIGHT, 32)
DO
Keypress$ = INKEY$
CLS
_LIMIT 30
LINE (300, 200)-(600, 700), DULLYELLOW, BF
'Red
CIRCLE (x%, 300), 50, WHITE
PAINT (x%, 300), BRIGHTRED, WHITE
CIRCLE (x%, 450), 50, WHITE
PAINT (x%, 450), YELLOW, WHITE
CIRCLE (x%, 600), 50, WHITE
PAINT (x%, 600), GREEN, WHITE
SLEEP 8
'Green
CIRCLE (x%, 300), 50, WHITE
PAINT (x%, 300), RED, WHITE
CIRCLE (x%, 450), 50, WHITE
PAINT (x%, 450), YELLOW, WHITE
CIRCLE (x%, 600), 50, WHITE
PAINT (x%, 600), BRIGHTGREEN, WHITE
SLEEP 8
'Yellow
CIRCLE (x%, 300), 50, WHITE
PAINT (x%, 300), RED, WHITE
CIRCLE (x%, 450), 50, WHITE
PAINT (x%, 450), BRIGHTYELLOW, WHITE
CIRCLE (x%, 600), 50, WHITE
PAINT (x%, 600), GREEN, WHITE
SLEEP 5
LOOP UNTIL Keypress$ = CHR$(27)
SYSTEM
Second edit: I have sort of answered my own question about the 'sleep' and '_delay' commands. After reading up on them in the keyword reference guide for QB64, I think it's saying that by pressing any button during the sleep "pause", that it will move on. So I changed all _delays to sleep, and now random button presses seem not to interfere with the timing. I still have the problem of not being able to exit the loop after pressing 'ESC' though.
Give this style solution a run and see if it makes sense for you. One input handler for the code, without any issues with SLEEP and INKEY$ interacting and producing undesirable results.
Code: (Select All)
Const SCREENWIDTH = 900
Const SCREENHEIGHT = 900
Const RED = _RGB32(80, 0, 0)
Const YELLOW = _RGB32(80, 80, 0)
Const GREEN = _RGB32(0, 80, 0)
Const BRIGHTRED = _RGB32(255, 0, 0)
Const BRIGHTYELLOW = _RGB32(255, 255, 0)
Const BRIGHTGREEN = _RGB32(0, 255, 0)
Const DULLYELLOW = _RGB32(40, 40, 0)
Dim x%
Dim Keypress$
x% = SCREENWIDTH / 2
Const WHITE = _RGB32(255, 255, 255)
Screen _NewImage(SCREENWIDTH, SCREENHEIGHT, 32)
Do
Cls
_Limit 30
Line (300, 200)-(600, 700), DULLYELLOW, BF
'Red
Circle (x%, 300), 50, WHITE
Paint (x%, 300), BRIGHTRED, WHITE
Circle (x%, 450), 50, WHITE
Paint (x%, 450), YELLOW, WHITE
Circle (x%, 600), 50, WHITE
Paint (x%, 600), GREEN, WHITE
Pause 8
'Green
Circle (x%, 300), 50, WHITE
Paint (x%, 300), RED, WHITE
Circle (x%, 450), 50, WHITE
Paint (x%, 450), YELLOW, WHITE
Circle (x%, 600), 50, WHITE
Paint (x%, 600), BRIGHTGREEN, WHITE
Pause 8
'Yellow
Circle (x%, 300), 50, WHITE
Paint (x%, 300), RED, WHITE
Circle (x%, 450), 50, WHITE
Paint (x%, 450), BRIGHTYELLOW, WHITE
Circle (x%, 600), 50, WHITE
Paint (x%, 600), GREEN, WHITE
Pause 5
Loop
Sub Pause (seconds#)
t# = Timer + seconds# 'the length of this pause
Do Until Timer > t# Or Timer < 1 'reset at midnight, if necessary. Hope a quick change doesn't cause an accident!
_Delay .1 'a small pause so this timed sleep doesn't use a lot of CPU power
i$ = InKey$ 'check for input from the user
If i$ = Chr$(27) Then System 'if they hit ESC at any point, then exit and shut down the program
If Len(i$) Then Exit Do 'otherwise, if they hit any key, exit the pause early
Loop
End Sub