
-----------------------------------
goober999
Fri Jan 05, 2007 4:43 pm

Help with Side Scroller
-----------------------------------
Ok I am in grade 10 and this project is for my summative. I am designing a mario game and so far your forums have been very useful when I'm stuck. Currently, I am having a problem getting the mario sprite to interact with the tiles in the level i have created. I have created a somewhat working collision detection system, but it is very glitchy, especially while mario is jumping. Here is the code:

%%Tiling Engine For Mario v1.0
setscreen ("graphics:640;640")
setscreen ("offscreenonly")

%%Declare Variables
const td : int := 20    %%TILES DOWN ***
const ta : int := 40    %%TILES ACROSS (THE LENGTH OF THE LEVEL)
const TileWidth : int := 32 %%TILE WIDTH ***
const nt : int := 5     %%NUMBER OF TILES BEING USED
const background : int := Pic.FileNew ("tiles/Back2.bmp")
var loader : array 1 .. 400 of int
var level1 : array 0 .. 5 of int
var Across : array 1 .. ta of string
var rander, tiles, tnum, StartPosition, lchng : int
var Locations : array 1 .. (ta * td) of string
var TileImages : array 1 .. (ta * td) of int
var chars : array char of boolean
var lend : int := (TileWidth * ta - 640) * -1
var jc : int := 14
var mem2, poo : int := 1
var cntr : int := 0
open : tiles, "tiles.txt", get

%%Collision Variables
var cdir, cdir2 : string := "f"

%%Mario Parameters
var mx, my, mcount, chng, vchng, mbase : int
var chars1 : array char of boolean
var dir : string
mbase := 32
dir := "right"
mx := 1
my := mbase
chng := 8
mcount := 1
var mario : array 1 .. 8 of int
var jumping, jumping2 : string := "false"

%%Loads Mario
for i : 1 .. 8
    mario (i) := Pic.FileNew ("mario" + intstr (i) + ".BMP")
end for
Pic.Draw (mario (1), mx, my, picMerge)


%%Load The Background
Pic.Draw (background, 1, 1, picCopy)
%%Load the map into an array
StartPosition := 0
for i : 1 .. td
    for ii : 1 .. ta
        exit when eof (tiles)
        get : tiles, Across (ii)
    end for
    for ii : 1 .. ta
        Locations (ii + StartPosition) := Across (ii)
        if strint (Locations (ii + StartPosition)) not= 2 then
            cntr := cntr + 1
        end if
    end for
    StartPosition += ta
end for
close : tiles

%%Load up the tiles
for i : 1 .. 8
    TileImages (i) := Pic.FileNew ("tiles/" + intstr (i) + ".bmp")   %Load every image, in order, from the file folder that holds the tiles.
end for

%%Make White Tiles Transparent
for i : 1 .. (ta * td)
    Pic.SetTransparentColor (TileImages (strint (Locations (i))), white)
end for

lchng := 0
var cx, cy : array 1 .. cntr of int


%%The Tile Mapper
loop
    Input.KeyDown (chars)
    if chars (KEY_RIGHT_ARROW) then
        if mx >= 320 and lchng >= lend and cdir not= "r" then
            lchng := lchng - 5
        end if
    end if
    if chars (KEY_LEFT_ARROW) then
        if lchng < 0 and mx  cntr then
                mem2 := 1
            end if
            cx (mem2) := TileWidth * XPositionCounter + lchng
            cy (mem2) := TileWidth * YPositionCounter - TileWidth
            mem2 := mem2 + 1
        end if
        XPositionCounter += 1
    end for


    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%COLLISION DETECTION%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    for i : 1 .. cntr
        if (mx + 32) > cx (i) and mx < (cx (i) + 32) and (my + 56) > cy (i) and my < (cy (i) + 32) then
            if my + 56 = cy (i) then
                cdir := "d"
            elsif my = cy (i) + 32 then
                cdir := "u"
            end if
            if mx + 32 >= cx (i) + 20 then
                cdir2 := "r"
            elsif mx = cx (i) + 12 then
                cdir2 := "l"
            end if
        end if
    end for
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%MARIO%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %Walking Right Sequence
    Input.KeyDown (chars1)
    if chars1 (KEY_RIGHT_ARROW) and jumping = "false" and jumping2 = "false" then %Code for walking right
        dir := "right"
        if mcount = 2 or mcount > 2 then
            mcount := 1
        elsif mcount < 2 then
            mcount := mcount + 1
        end if
        if mx < 320 and cdir not= "r" then
            mx := mx + chng
        end if
        if lchng = 2) then
            mario_frame := 1
        else
            mario_frame := 2
        end if
    end if

    if chars (KEY_LEFT_ARROW) and jumping = false then
        dir := "left"
        if (mario_frame >= 5) then
            mario_frame := 4
        else
            mario_frame := 5
        end if
    end if

    %Draw Background
    Pic.Draw (background, 1, 1, picCopy)

    %Draw Tiles
    var XPositionCounter : int := 0
    var YPositionCounter : int := tilesVertical
    for i : 1 .. (tilesHorizontal * tilesVertical)
        if XPositionCounter = tilesHorizontal then
            YPositionCounter -= 1
            XPositionCounter := 0
        end if
        if YPositionCounter = 0 then
            YPositionCounter := 0
        end if
        Pic.Draw (TileImages (strint (Locations (i))), (TileWidth * XPositionCounter + lchng), (TileWidth * YPositionCounter - TileWidth), picMerge)
        if strint (Locations (i)) not= 2 then
            if mem2 > cntr then
                mem2 := 1
            end if
            cx (mem2) := TileWidth * XPositionCounter + lchng
            cy (mem2) := TileWidth * YPositionCounter - TileWidth
            mem2 := mem2 + 1
        end if
        XPositionCounter += 1
    end for

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%MARIO%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %Walking Right Sequence


    /*
     %Jumping Sequence
     if chars (KEY_UP_ARROW) then
     if jumping = "false" and jumping2 = "false" then
     jumping := "true"
     jc := 12
     end if
     end if
     if jumping = "true" and jumping2 = "false" then  %Code for Jumping
     if chars1 (KEY_RIGHT_ARROW) then
     if mario_x < 320 then
     mario_x := mario_x + chng
     end if
     if lchng 