CMM2 Defender Clone


Author Message
PeteCotton

Guru

Joined: 13/08/2020
Location: Canada
Posts: 369
Posted: 12:25am 09 Sep 2020      

As promised, here's the code to draw the mountains, and the code to draw the foreground scroll.


DrawBackground.zip

Here are the relevant code modules. The first one is for the mountains

mode 1,8
cls
dim float height(100,100)
dim integer xarray (3)
dim integer yarray (3)

page write 0
DrawMountains(1)
DrawMountains(2)
DrawMountains(3)

sub DrawMountains(formula)
 ' Calculate the grid of values
 for x=-50 to 50
   for y=-50 to 50
     distance = sqr(x*x+y*y)
     distance2 = sqr ((X-10) * (x-10) + (y-10) * (y-10))
     distance3 = sqr(abs ((x+22) * (x+12) + (y+11) * (y+3)))
     if formula=1 then height(x+50, y+50) =cos(distance2/3) * sin(distance/4)* sin(distance3) * (50-distance)* 2.4
     if formula=2 then height(x+50, y+50) = sin(distance2/10) * cos(distance/4) * cos(distance3/2) * (30-distance)*1.6
     if formula=3 then height(x+50, y+50) = sin(x/10) * cos(distance/4) * cos(distance3/2) * (30-distance)*0.8
     if height(x+50,y+50)<1 then height (x+50, y+50)=1

     'Make the last height equal to the first so that it is seamless
     if x=50 then height(x+50,y+50) = height(0,y+50)
   next y
 next x

 ' Plot each rectangle as a filled polygon
 for x=1 to 100
   for y=1 to 100
     ' Is this the second (wrapped) line?
     tempx = GetOnScreenX(x,y)
     yoffset=(formula-1)*200
     xoffset=0
     if (tempx>799) then
       xoffset=-800
       yoffset=(formula-1)*200 + 100
     end if    

     xarray(0)=GetOnScreenX(x-1,y-1) + xoffset
     yarray(0)=GetOnScreenY(x-1, y-1) + yoffset
     xarray(1)=GetOnScreenX(x,y-1) + xoffset
     yarray(1)=GetOnScreenY(x,y-1) + yoffset
     xarray(2)=GetOnscreenX(x,y) + xoffset
     yarray(2)=GetOnScreenY(x,y) + yoffset
     xarray(3)=GetOnScreenX(x-1,y) + xoffset
     yarray(3)=GetOnScreenY(x-1,y) + yoffset
   
     r = abs(height(x,y))*6
     if r>255 then r=255
     if (formula=1) then polygon 4, xarray(), yarray(), RGB(0,80,0), RGB(0,0,0)
     if (formula=2) then polygon 4, xarray(), yarray(), RGB(0,128,0), RGB(0,40,0)
     if (formula=3) then polygon 4, xarray(), yarray(), RGB(0,255,0), RGB(0,80,0)
   next y
 next x
end sub

function GetOnscreenX (x,y)
 GetOnScreenX = y*2 + (x*16)
 rem if GetOnscreenx > 800 then GetOnscreenX = GetOnScreenX-800
end function

function GetOnScreenY (x,y)
 rem GetOnScreenY = 500 - (y*2) - height(x,y)
 GetOnScreenY = 100 - height(x,y)
 rem if GetOnScreenX(x,y) > 800 then GetOncreenY = GetOnScreenY + 200
end function



and for the foreground/ground level


mode 1,8
cls
for animation=1 to 20
 yoffset= 200+(animation-1)*10
 line 0,yoffset,800,yoffset,1,rgb(0,255,0)
 line 0,yoffset+4, 800, yoffset+4,1,rgb(0,255,0)
 line 0,yoffset+9, 800, yoffset+9,1,rgb(0,255,0)

 for verticals = 1 to 10
   xoffset = animation*4
   x1 = xoffset+ (80*verticals)
   x2 = 400 + ((x1-400)*1.2)
   line x1,yoffset, x2, yoffset+9,1,rgb(0,255,0)
 next verticals
next animation


Edited 2020-09-09 10:26 by PeteCotton