TGC Codebase Backup



a 2d side scrolling example with foreground and background scrolling by CloseToPerfect

16th Nov 2003 16:01
Summary

a 2d side scrolling example with foreground and background scrolling



Description

a 2d side scrolling example with foreground and background scrolling. Nothing to add, really says it all.

oh, no media needed.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    Rem Project: simple side scroller
Rem Created: 11/16/03 1:26:03 PM
rem by John Chase

rem ***** Main Source File *****

sync rate 30
sync on


rem set up some 32x32 blocks for the demo
rem 5 should be enough
for i = 1 to 5
ink rgb(rnd(255),rnd(255),rnd(255)),0
box 0,0,32,32
get image i,0,0,32,32
next i
paste image 3,0,0
circle 15,15,10
get image 3,0,0,31,31
rem image 1,2 background
rem image 3 character
rem image 4,5 foreground

global backgroundscrollrate : backgroundscrollrate = 1
global foregroundscrollrate : foregroundscrollrate = 2
global backgroundxoffset : backgroundxoffset = 0
global foregroundxoffset : foregroundxoffset = 0
global backgroundx : backgroundx = 0
global foregroundx : foregroundx = 0
global characterx : characterx = 10
global charactery : charactery = 5
global characterspeed : characterspeed = 3

rem build the map
rem a map editor is a must make thing
dim map(500,20,3)
for x=0 to 500
for y = 0 to 20
map(x,y,1) = rnd(1)+1

if rnd(100) > 95
   map(x,y,2) = rnd(1)+4 : rem only 5 percent of the for ground will be there
else
   map(x,y,2) = 0
endif
next y
next x


do
scroll_background()
scroll_foreground()
if foregroundx > 480 then end
move_character()
draw_background()
paste image 3, characterx, charactery
draw_foreground()
sync
loop

function move_character()
if upkey()    then dec charactery, characterspeed : if charactery < 0 then charactery = 0
if downkey()  then inc charactery, characterspeed : if charactery > 448 then charactery = 448
if rightkey() then inc characterx, characterspeed : if characterx > 608 then characterx = 608
if leftkey()  then dec characterx, characterspeed : if characterx < 0 then characterx = 0
endfunction

function scroll_background()
inc backgroundxoffset, backgroundscrollrate
if backgroundxoffset > 31
   backgroundxoffset = 0
   inc backgroundx, 1
endif
endfunction

function scroll_foreground()
inc foregroundxoffset, foregroundscrollrate
if foregroundxoffset > 31
   foregroundxoffset = 0
   inc foregroundx, 1
endif
endfunction

function draw_background()
for x = 0 to 20
   for y = 0 to 15
      paste image map(x+backgroundx,y,1),x*32-backgroundxoffset,y*32
   next y
next x
endfunction

function draw_foreground()
for x = 0 to 20
   for y = 0 to 15
      if map(x+foregroundx,y,2) > 0 then paste image map(x+foregroundx,y,2),x*32-foregroundxoffset,y*32
   next y
next x
endfunction