1. multitasking delays [warning:kinda long]
- Posted by Hawke <mdeland at NWINFO.NET> Sep 02, 1998
- 996 views
- Last edited Sep 03, 1998
Ralf Nieuwenhuijsen wrote: >To wait for a key-press: >include get.e ?wait_key() >very multi-tasking friendly.. >(esspecially a preemtive one like win95).. >include machine.e > tick_rate (150) -- Increase time () accuracy > procedure wait (atom t) > t = t + time () > while t < time () do end while > end procedure >Maybe somebody can make a multi-tasking friendly version of this ? On this note, for the benefit of the new members on the list, I made a small program that demonstrates a number of euphoria features, most notably how to have multiple different time delays that are multitasking friendly, precise, and independant. It allows you to have any number of 'processes' doing different things, for any individual length of time. Basically, it's a variation of the theme of multitasking presented by the language wars program included with Euph. This is sort of a summary to that concept. It also demonstrates the basic premise behind screen flipping, how to operate on sequences in whole or part, function independence, and rudimentary collision detection. No, none of these concepts are presented either in full or as best they could be (i'm sure) but, for those new to Euph, I'm hoping the code can answer "how do I..." questions of syntax, data manipulation/display...etc... One caveat: for some reason, even with page flipping, there is still a fair amount of flicker... I'm thinking that text mode has a real real low refresh rate...anyone? If you think this code sucks, think quietly. :) take care all-- Hawke' (now, back to truecolor mouse driver coding--blech) /* ps:Pete, I'm gonna have to rip-off/shred/thrash mighty.e and parts thereof to make trumouse.e work... your machine code interrupt snagging is just too nice...k? */ ----------------BEGIN CODE-------------------- include machine.e include get.e include image.e constant TickSec = 1000, --1000 ticks per second NumShips = 10, --10 independent timers (10 ships) TRUE=1, FALSE=0, SCR=1, UP=1, DOWN=2, LEFT=3, RIGHT=4, FACINGS={"Up ","Down ","Left ","Right"}, MaxX = 80, MaxY = 23 sequence Timers, Ships integer Quit, Score, Facing, PlrX, PlrY, ActivePage, DEBUG atom Key, TimeNow, TimeThen, TimePassed, GameOverTime function BoundsX(integer x) if x<1 then return 1 elsif x>MaxX then return MaxX else return x end if end function function BoundsY(integer y) if y<1 then return 1 elsif y>MaxY then return MaxY else return y end if end function function Shoot(integer x, integer y, integer faces, sequence data) integer start,finish,found,vert found = FALSE if faces=UP then start=1 finish=y vert=TRUE elsif faces=DOWN then start=y finish=MaxY vert=TRUE elsif faces=LEFT then start=1 finish=x vert=FALSE elsif faces=RIGHT then start=x finish=MaxX vert=FALSE end if for test = start to finish do if vert then found = find({x,test},data) else found = find({test,y},data) end if if found then return found end if --need to bug out, gotta hit end for return found --most likely a missed target at this point end function function MoveShip(integer index, sequence data) data[index][1]=BoundsX(data[index][1]+rand(3)-2) data[index][2]=BoundsY(data[index][2]+rand(3)-2) return data end function --INIT clear_screen() tick_rate(TickSec) Quit =FALSE DEBUG =FALSE Score =0 PlrX =rand(MaxX) PlrY =rand(MaxY) Facing=UP Ships =repeat({1,1},NumShips) --ship positions on game board Timers=repeat(0,NumShips) --a set of independent timers for shipnum = 1 to NumShips do --0.5 to 1.5 sec delays for ship movement Timers[shipnum] = rand(TickSec) + (TickSec/2) Ships[shipnum] = {rand(MaxX),rand(MaxY)} end for ActivePage=0 TimeNow =time() TimeThen =TimeNow TimePassed=TimeNow - TimeThen GameOverTime = time() + 60*TickSec --60 seconds for game play --MAIN while Quit = FALSE do --flip pages from 0 to 1 to 0 etc... ActivePage = not ActivePage set_active_page(ActivePage) clear_screen() --note:x,y reversed as position wants line,col...blech for index = 1 to NumShips do position(Ships[index][2],Ships[index][1]) puts(SCR,'#') end for position(PlrY,PlrX) puts (SCR,'X') position(MaxY+1,1) printf(SCR,"Score:%d",Score) position(MaxY+1,30) puts (SCR,FACINGS[Facing]) if DEBUG then position (MaxY+1,40) printf( SCR,"Now:%10.3f,Then:%10.3f,Passed:%10.3f", {TimeNow, TimeThen, TimePassed} ) end if set_display_page(ActivePage) Key = get_key() if Key='8' then PlrY=PlrY-1 PlrY=BoundsY(PlrY) Facing=UP elsif Key='2' then PlrY=PlrY+1 PlrY=BoundsY(PlrY) Facing=DOWN elsif Key='4' then PlrX=PlrX-1 PlrX=BoundsX(PlrX) Facing=LEFT elsif Key='6' then PlrX=PlrX+1 PlrX=BoundsX(PlrX) Facing=RIGHT elsif Key=' ' then if Shoot(PlrX,PlrY,Facing,Ships) then Score=Score+1 end if elsif Key='D' then DEBUG=TRUE --turn on debugging elsif Key='Q' then Quit =TRUE --:( _sniff_ end if --game time up??? time to move a ship?? --the heart of this venture: the timers update... TimeNow = time() * TickSec if GameOverTime <= TimeNow then Quit = TRUE end if TimePassed = TimeNow - TimeThen Timers = Timers - TimePassed --count them all down for shipnum = 1 to NumShips do --anything ready to blast off? if Timers[shipnum] <= 0 then Ships = MoveShip(shipnum,Ships) Timers[shipnum]=rand(TickSec) + (TickSec/2) end if end for TimeThen = TimeNow end while --end of game set_active_page(0) set_display_page(0) clear_screen() puts (SCR,"Thanks For Playing\n") printf(SCR,"Final Score:%d",Score)
2. Re: multitasking delays [warning:kinda long]
- Posted by Merry000 Nov 05, 2012
- 1069 views
We are much obliged to you for your sharing. That's cool I know, that's really cool. http://www.xflip.com/digital-magazine-software.html