'Bike Race Program 1 func Main() '-------------------------------------------- ' Setup Section '-------------------------------------------- dim Tots(5) as integer 'Total of all rotations dim Rots(5) as integer 'Current lap rotations dim Pos(5) as integer 'Current race position global Laps(5) as integer 'Total completed laps dim indat as string ClearAll() initBike(1) 'Init Com Port and Power up connector. Pass Com Port FormFont("-",9,1) 'Work Variables dim ticks as integer dim x as integer dim y as integer dim startticks as integer dim curticks as integer 'Set Starting point startticks = getms()/100 FormUpdateAutoOff() '-------------------------------------------- ' Main Loop '-------------------------------------------- loop: DoEvents() ticks= (getms() / 100) -startticks 'print "Ticks ",Ticks if ticks > 3 then startticks = getms()/100 if Random(1,10) > 4 then rots(2) = rots(2) + 1 endif if Random(1,10) > 4 then rots(3) = rots(3) + 1 endif if Random(1,10) > 4 then rots(4) = rots(4) + 1 endif DrawTrack() for x = 1 to 4 Rots(x)=PlotPlayers(x,Rots(x)) next 'Calculate Position '------------------------- for x = 1 to 4 Tots(x) = Laps(x) * 407 + Rots(x) Pos(x) = 0 next for x = 1 to 4 for y = 1 to 4 if Tots(x) > Tots(y) then Pos(x) = Pos(X)+1 next next for x = 1 to 4 Pos(x) = 4 - Pos(x) next 'Display Data '-------------------------- FormPrint(10,130,"Mike ") FormPrint(10,150,"Bot1") FormPrint(10,170,"Bot2") FormPrint(10,190,"Bot3") FormPrint(60,130,Laps(1)) FormPrint(60,150,Laps(2)) FormPrint(60,170,Laps(3)) FormPrint(60,190,Laps(4)) FormPrint(100,130,Pos(1)) FormPrint(100,150,Pos(2)) FormPrint(100,170,Pos(3)) FormPrint(100,190,Pos(4)) formupdate() endif 'Check Com Buffer for activity '-------------------------- if ComBuff(chBike) > 0 then indat=ComInput(chBike) Rots(1) = Rots(1) +1 print Rots(1) endif 'End check tick statement goto loop endfunc '-------------------------------------------------------- 'Draw a Player Pip '-------------------------------------------------------- func PlotPlayers(Player,Rots as integer) as integer dim Xpos,Ypos dim PlayMod if player = 1 then FormBrush(0,0,255) PlayMod = 0 endif if player = 2 then FormBrush(255,0,0) PlayMod = 6 endif if player = 3 then FormBrush(200,000,255) PlayMod = 12 endif if player = 4 then FormBrush(200,200,155) PlayMod = 18 endif if Rots < 92 then Xpos = Rots+111 Ypos = 31 - PlayMod endif if Rots > 91 and Rots < 122 then Xpos = 203 + PlayMod Ypos = Rots - 59 endif if Rots > 121 and Rots < 294 then Xpos = 324 - Rots Ypos = 63 + PlayMod endif if Rots > 293 and Rots < 326 then Xpos = 31 - PlayMod Ypos = 356 - Rots endif if Rots > 325 and Rots < 407 then Xpos = Rots - 291 Ypos = 31 - PlayMod endif if Rots >= 407 then Rots = 0 Xpos = Rots+111 Ypos = 31 - PlayMod Laps(Player) = Laps(Player) + 1 endif FormFillEllipse(Xpos,Ypos,6,6) FormEllipse(Xpos,Ypos,6,6) exit(Rots) endfunc '--------------------------------------------------------- ' Draw the track onto the screen '--------------------------------------------------------- func DrawTrack() 'Track FormBrush(0,255,0) 'Green FormFillRectangle(0,0,240,100) FormRectangle(0,0,239,99) FormBrush(248,240,112) 'Track Color FormFillRectangle(10,10,220,80) FormBrush(0,255,0) 'Green FormFillRectangle(40,40,160,20) 'Start Finisth Line FormLine(115,10,115,40) endfunc '--------------------------------------------------------- ' Open Com Port and Turn on Interface '--------------------------------------------------------- func initBike(tport as integer) gconst chBike 1 dim stat as integer if ComOpen(chBike,baud=9600,port = tport) = 0 then stat=MsgBox("Error Opening Com Port") end endif 'Power up the connector ComDTR(chBike,1) Pause(20) endfunc