Code: Select all
screen 12
cls
do
pset(x,y),6
if inkey$=¨w¨ then y=y-1
if inkey$=¨s¨ then y=y+1
if inkey$=¨a¨ then x=x-1
if inkey$=¨d¨ then x=x+1
if inkey$=¨q¨ then end
loop
Moderators:Administrator, Global Moderator
Code: Select all
screen 12
cls
do
pset(x,y),6
if inkey$=¨w¨ then y=y-1
if inkey$=¨s¨ then y=y+1
if inkey$=¨a¨ then x=x-1
if inkey$=¨d¨ then x=x+1
if inkey$=¨q¨ then end
loop
You are definitely correct! Consider the following looppebe wrote:As I recall, you cannot read inkey$ directly as you have done.
No, I saidOtringal wrote:Mac, you said that only when I press ¨a¨ will the program go to instruction nr. 2 .... but how come ???
No matter how fast the computer is, it still does one instruction at a time.Otringal wrote:I mean the whole program is a loop .... doesn´t that repeat 18.2 times per second ??? why is it time consuming ?
Code: Select all
DECLARE SUB ShowBuf ()
DECLARE FUNCTION MyKey$ ()
DIM SHARED kbuf AS STRING
CLS : COLOR 3, 4: LOCATE , 30: PRINT "Study of a loop": PRINT
COLOR 0, 7
LOCATE , 4: PRINT "pset(x,y),6"
LOCATE , 4: PRINT "if inkey$="; CHR$(34); "w"; CHR$(34); " then y=y-1"
LOCATE , 4: PRINT "if inkey$="; CHR$(34); "s"; CHR$(34); " then y=y+1"
LOCATE , 4: PRINT "if inkey$="; CHR$(34); "a"; CHR$(34); " then x=x+1"
LOCATE , 4: PRINT "if inkey$="; CHR$(34); "d"; CHR$(34); " then x=x-1"
LOCATE , 4: PRINT "if inkey$="; CHR$(34); "q"; CHR$(34); " then end"
COLOR 7, 0
iloc = 1: x = 50: y = 50
CONST pbase = 10
DO
ShowBuf
LOCATE pbase + 2: PRINT "x ="; x; SPACE$(10); "y ="; y; SPACE$(10)
LOCATE pbase + 4: PRINT SPACE$(10): tDelta = 2
SELECT CASE iloc
CASE 1: LOCATE pbase + 4, 1: PRINT "Doing PSET": tDelta = 5
CASE 2: IF MyKey$ = "w" THEN y = y - 1
CASE 3: IF MyKey$ = "s" THEN y = y + 1
CASE 4: IF MyKey$ = "a" THEN x = x - 1
CASE 5: IF MyKey$ = "d" THEN x = x + 1
CASE 6:
IF MyKey$ = "q" THEN
LOCATE pbase + 6, 1: PRINT "q was recognized"
END
END IF
END SELECT
LOCATE iloc + 2, 1: PRINT "-->"
t = TIMER + tDelta
DO
w$ = INKEY$
IF LEN(w$) THEN
IF w$ = CHR$(27) THEN LOCATE pbase + 6, 1: END
kbuf = kbuf + w$
ShowBuf
END IF
LOOP WHILE TIMER < t
LOCATE iloc + 2, 1: PRINT " "
iloc = iloc + 1: IF iloc > 6 THEN iloc = 1
LOOP
FUNCTION MyKey$
SELECT CASE LEN(kbuf)
CASE 0: EXIT FUNCTION
CASE 1: MyKey$ = kbuf: kbuf = ""
CASE ELSE: MyKey$ = LEFT$(kbuf, 1): kbuf = MID$(kbuf, 2)
END SELECT
ShowBuf
END FUNCTION
SUB ShowBuf
CONST p = "Content of keyboard buffer: "
LOCATE pbase, 1: PRINT p + SPACE$(LEN(kbuf) + 2)
LOCATE pbase, LEN(p) + 1: PRINT kbuf
END SUB
Don't know where you got 18.2 times per second. Here is a revised demo to show what is happening while a clock is showing time.Otringal wrote:doesn´t that repeat 18.2 times per second
Code: Select all
DECLARE SUB ShowBuf ()
DECLARE FUNCTION MyKey$ ()
DEFSTR A-Z
DIM SHARED kbuf AS STRING, Delta AS SINGLE
DIM Clock AS DOUBLE, InsTime AS DOUBLE
CLS
PRINT "Wait 20 seconds while I see how fast your computer is..."
DIM t AS DOUBLE, c AS LONG
FOR n% = 1 TO 20
t = TIMER + 1
DO WHILE TIMER < t: c = c + 3: LOOP
PRINT n%;
NEXT n%
PRINT : PRINT
PRINT "I found you do"; c; "QBasic instructions in 20 seconds"
PRINT "Which is"; c / 20; "instructions in one second"
InsTime = 1 / (c / 20)
PRINT "Thus on the average one instruction takes ";
PRINT MID$(STR$(1 + InsTime), 3); " seconds."
Clock = 1000
PRINT : PRINT "Let's assume that the pset takes 5 times the average"
PRINT "And at the beginning, the computer clock is"; Clock
PRINT "Then after the pset, the clock will be"; Clock + (5 * InsTime)
PRINT : PRINT "And assume an IF statement is 2 times the average"
PRINT "Then next the clock will be"; Clock + (7 * InsTime)
PRINT : LINE INPUT "Press Enter to begin simulating"; e$
CONST pbase = 12
CLS : COLOR 15, 4: LOCATE , 10
PRINT "Study to show you can't have multiple INKEY$ tests in a loop"
PRINT
COLOR 0, 7
LOCATE , 4: PRINT "pset(x,y),6"
LOCATE , 4: PRINT "if inkey$="; CHR$(34); "w"; CHR$(34); " then y=y-1"
LOCATE , 4: PRINT "if inkey$="; CHR$(34); "s"; CHR$(34); " then y=y+1"
LOCATE , 4: PRINT "if inkey$="; CHR$(34); "a"; CHR$(34); " then x=x-1"
LOCATE , 4: PRINT "if inkey$="; CHR$(34); "d"; CHR$(34); " then x=x+1"
LOCATE , 4: PRINT "if inkey$="; CHR$(34); "q"; CHR$(34); " then end"
COLOR 7, 0
DIM x AS INTEGER, y AS INTEGER
x = 50: y = 50
DIM tDelta AS INTEGER ' Assume PSET takes 5, IF takes 2.
DIM oLoc AS INTEGER, iLoc AS INTEGER ' Instruction being executed
iLoc = 1' Start here
DO
ShowBuf
LOCATE pbase + 2: PRINT "x ="; x; SPACE$(10); "y ="; y; SPACE$(10)
LOCATE pbase + 4: PRINT SPACE$(80): tDelta = 2
LOCATE oLoc + 2, 1: PRINT " "
LOCATE iLoc + 2, 1: PRINT "-->"
LOCATE pbase + 4, 1
SELECT CASE iLoc
CASE 1: PRINT "Doing PSET": tDelta = 5
CASE 2: IF MyKey$ = "w" THEN y = y - 1
CASE 3: IF MyKey$ = "s" THEN y = y + 1
CASE 4: IF MyKey$ = "a" THEN x = x - 1
CASE 5: IF MyKey$ = "d" THEN x = x + 1
CASE 6:
IF MyKey$ = "q" THEN
LOCATE pbase + 6, 1: PRINT "q was recognized"
END
END IF
END SELECT
DIM t1 AS DOUBLE, t2 AS DOUBLE
t1 = TIMER + tDelta
t2 = TIMER + InsTime
DO
IF TIMER > t2 THEN
Clock = Clock + InsTime
LOCATE pbase - 2, 1
PRINT "Computer Clock:"; Clock; SPACE$(20)
t2 = TIMER + InsTime
END IF
w$ = INKEY$
IF LEN(w$) THEN
IF w$ = CHR$(27) THEN LOCATE pbase + 6, 1: END
kbuf = kbuf + w$
ShowBuf
END IF
LOOP WHILE TIMER < t1
oLoc = iLoc
iLoc = iLoc + 1: IF iLoc > 6 THEN iLoc = 1
LOOP
DEFSNG A-Z
FUNCTION MyKey$
SELECT CASE LEN(kbuf)
CASE 0: PRINT "No key to process": EXIT FUNCTION
CASE 1: w$ = kbuf: kbuf = ""
CASE ELSE: w$ = LEFT$(kbuf, 1): kbuf = MID$(kbuf, 2)
END SELECT
klin = CSRLIN
PRINT "INKEY$ now returning [";
COLOR 11, 9: PRINT w$; : COLOR 7, 0: PRINT "] ";
LINE INPUT "Press Enter to continue"; e$
LOCATE klin, 1: PRINT SPACE$(80)
ShowBuf
MyKey$ = w$
END FUNCTION
SUB ShowBuf
CONST p = "Content of keyboard buffer: "
LOCATE pbase, 1: PRINT p + SPACE$(LEN(kbuf) + 5)
LOCATE pbase, LEN(p) + 1
PRINT "["; : COLOR 11, 9: PRINT kbuf; : COLOR 7, 0: PRINT "]"
COLOR 7, 0
END SUB
Not so. The speed of a loop depends on its length (number of instructions) and the processor speed. Are you getting mixed up with the rate at which the OS reads the system clock?Otringal wrote:well a loop repeats every 55 milliseconds and that is equal with 18.2 CPU ticks ... on all processors, no matter how fast they are. I will compile your code soon enough and thanks
Code: Select all
DO
WHILE A$="":A$=INKEY$:A$=UCASE$(A$):WEND
IF A$="W" then...
IF A$="W" or A$="A" ..... then pset(x,y),6
A$=""
LOOP
Good point. The PSET is probably no impact at all on response from the keyboard, but esthetically it is wasted when the point has not changed.Buff wrote:if you only want it to respond to your keyboard input then ...
Code: Select all
SCREEN 12
DIM x AS INTEGER, y AS INTEGER
x = 50: y = 50: PSET (x, y), 6
DO
DO: k$ = INKEY$: LOOP WHILE k$ = ""
SELECT CASE k$
CASE "w": y = y - 1
CASE "s": y = y + 1
CASE "a": x = x - 1
CASE "d": x = x + 1
CASE "q": EXIT DO
CASE ELSE: k$ = ""
END SELECT
IF k$ <> "" THEN PSET (x, y), 6
LOOP
LOCATE 25, 1: LINE INPUT "Piece of Cake - Now press ENTER"; e$
CLS
SYSTEM