News:

Building a 3D Ray Tracer  By stevmjon

Main Menu

Input Gathering

Started by kevin, April 06, 2011, 12:20:36 AM

Previous topic - Next topic

kevin

 Input Gathering

  This example collections the users input over a 500 millisecond period.  The user controls the character with the Arrow keys,  the character only responds at 1/2 second intervals.  The program listens to the user input between these intervals then decides the movement direction.   So if the direction that's been down the longest is the direction it'll move in.  


PlayBASIC Code: [Select]
   Flushkeys

Xpos=400
Ypos=300

Constant Up=1
Constant Down=2
Constant Left=3
Constant Right=4

Dim Direction(4)

NextFrame=Timer()+500
Do

Cls

CurrentTime=Timer()

if LeftKey() Then Direction(Left)++
if RightKey() Then Direction(Right)++
if upKey() Then Direction(up)++
if downKey() Then Direction(down)++

if CurrentTime>=NextFrame

// Work out the move direction
MoveDirection=-1
MoveHighest=-1
For lp=1 to 4
if Direction(lp)>Highest
MoveHighest=Direction(lp)
MoveDirection=lp
endif
Direction(lp)=0
next

Select MoveDirection
case up
Ypos-=5
case down
Ypos+=5
case Left
Xpos-=5
case right
Xpos+=5
Endselect


NextFrame=CurrentTime+500
endif

circle Xpos,ypos,20,true

print NextFrame
For lp=1 to 4
print Direction(lp)
next

sync
loop