Code archives/Algorithms/Scroller
This code has been declared by its author to be Public Domain code.
Download source code
| Here is the link to the files (re-posted) http://www.denathorn.co.uk/Files/Scrolling.zip (Thanks goes to Michael Denathorn for hosting) Please comment on the code because this is my first one. I used it on one game I wrote a few years back. |
;********************************************************************************** ; Scroller by JP Hamilton. ; I use blocks of 64x64 for this example, but could be made for any block size. ; There is no array checking, so make sure you don't scroll of the edge of the map. ; Maze created with the help of 'mazemaker' by Curtastic - Cheers matey! ; Graphics were from a Turrican game somewhere and edited by me for this routine. ;********************************************************************************** Graphics 640,480,16,1 SetBuffer BackBuffer() Global framerate=CreateTimer(60) Global gfxwalls=LoadAnimImage("walls.png",64,64,0,64) Dim map(476,317) ; the array to hold the map data filein = ReadFile("level.dat") For y=0 To 317 For x=0 To 476 map(x,y)=ReadInt(filein) Next Next scrx=-64:scry=-80 ; starting points of both x and y for drawing to the screen mapx=9:mapy=13 ; Starting position within the map() array scroll_speed=4 ; Scroll speed - adjusted by the +/- keys on the numpad While Not KeyDown(1) WaitTimer(framerate) Cls If KeyHit(74) Then ; Slow down the scroll speed scroll_speed=scroll_speed-1 If scroll_speed<1 Then scroll_speed=1 EndIf If KeyHit(78) Then ; Speed up the scroll speed scroll_speed=scroll_speed+1 If scroll_speed>64 Then scroll_speed=64 EndIf If KeyDown(203) Then scrx=scrx+(scroll_speed) ;move left If KeyDown(205) Then scrx=scrx-(scroll_speed) ;move right If KeyDown(208) Then scry=scry-(scroll_speed) ;move up If KeyDown(200) Then scry=scry+(scroll_speed) ;move down If scrx<=-64 Then scrx=scrx+64:mapx=mapx+1 ; Wrap scrx if <= block width If scrx>=0 Then scrx=scrx-64:mapx=mapx-1 ; Wrap scrx if >= block width If scry>=-64 Then scry=scry-64:mapy=mapy-1 ; Wrap scry if >= -block width If scry<=-128 Then scry=scry+64:mapy=mapy+1 ; Warp scry if <= -block width times 2 cntx=0:cnty=0 ; draw the map onto the screen using scrx and scry For y=mapy To mapy+9 For x=mapx To mapx+10 DrawImage gfxwalls,cntx*64+scrx,cnty*64+scry,map(x,y) cntx=cntx+1 Next cntx=0:cnty=cnty+1 Next Text 10,10,"Scroll Speed="+scroll_speed Flip Wend EndGraphics End |