Computer Science Canada

Help with Side Scroller

Author:  goober999 [ Fri Jan 05, 2007 4:43 pm ]
Post subject:  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:
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 <= 329 and cdir not= "l" then
            lchng := lchng + 5
        end if
    end if
    Pic.Draw (background, 1, 1, picCopy)
    var XPositionCounter : int := 0
    var YPositionCounter : int := td
    for i : 1 .. (ta * td)
        if XPositionCounter = ta 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


    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%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 <= lend and cdir not= "r" then
            mx := mx + chng
        end if
        Pic.Draw (mario (mcount), mx, my, picMerge)
    end if
    delay (20)


    %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 mx < 320 and cdir not= "r" then
                mx := mx + chng
            end if
            if lchng <= lend and cdir not= "r" then
                mx := mx + chng
            end if
            dir := "right"
        end if
        if chars1 (KEY_LEFT_ARROW) then
            if mx < 320 and cdir not= "l" then
                mx := mx - chng
            end if
            if lchng <= lend and cdir not= "l" then
                mx := mx - chng
            end if
            dir := "left"
        end if
        if dir = "right" then
            mcount := 3
        else
            mcount := 8
        end if
        for i : 1 .. jc
            my := my + 1
            exit when cdir = "d"
        end for
        jc := jc - 1
        if cdir = "d" or jc = 0 then
            jumping2 := "true"
            jumping := "false"
        end if
        Pic.Draw (mario (mcount), mx, my, picMerge)
    end if

    if jumping2 = "true" then
        if chars1 (KEY_RIGHT_ARROW) then
            if mx < 320 and cdir not= "r" then
                mx := mx + chng
            end if
            if lchng <= lend and cdir not= "r" then
                mx := mx + chng
            end if
            dir := "right"
        end if
        if chars1 (KEY_LEFT_ARROW) then
            if mx < 320 and cdir not= "l" then
                mx := mx - chng
            end if
            if lchng <= lend and cdir not= "l" then
                mx := mx - chng
            end if
            dir := "left"
        end if
        if dir = "right" then
            mcount := 6
        else
            mcount := 7
        end if
        if cdir not= "u" then
            for i : 1 .. jc
                my := my - 1
                exit when cdir = "u" or cdir = "d"
            end for
        end if
        if cdir not= "u" then
            jc := jc + 1
        end if
        Pic.Draw (mario (mcount), mx, my, picMerge)
        if cdir = "u" then
            jumping2 := "false"
            if dir = "right" then
                mcount := 1
            else
                mcount := 4
            end if
        end if
    end if

    %Walking Left Sequence
    if chars1 (KEY_LEFT_ARROW) and jumping = "false" and jumping2 = "false" then     %Code for walking left
        dir := "left"
        if mcount = 5 or mcount < 4 then
            mcount := 4
        elsif mcount < 5 then
            mcount := mcount + 1
        end if
        if lchng = 0 and cdir not= "l" then
            mx := mx - chng
        end if
        if lchng <= lend and cdir = "l" then
            mx := mx - chng
        end if
        Pic.Draw (mario (mcount), mx, my, picMerge)
    end if

    %If in mid air...
    if my > 32 and cdir not= "u" and jumping = "f" and jumping2 = "f" then
        jumping2 := "t"
    end if
    %Static Sequence
    if chars1 (KEY_RIGHT_ARROW) = false and chars1 (KEY_LEFT_ARROW) = false and chars1 (KEY_UP_ARROW) = false and jumping = "false" and jumping2 = "false" then     %If Static...
        if mcount = 4 or mcount = 5 then
            mcount := 4
            Pic.Draw (mario (mcount), mx, my, picMerge)
        elsif mcount = 1 or mcount = 2 then
            mcount := 1
            Pic.Draw (mario (mcount), mx, my, picMerge)
        end if
    end if
    %%%
    put cdir
    View.Update
    cls
    cdir := "f"
end loop


I apologize, sloppy code, and many useless variables :p Any help would be greatly apprecited! Oh, and attached is the files.

Author:  Ultrahex [ Fri Jan 05, 2007 5:27 pm ]
Post subject: 

Im Just Curious, But Did you ever actually write out this stuff on paper, had some initial plan before starting this???

and yes your code is quite horendus in terms of variable naming for someone to notice what your trying to do...

by running the program it appears as though you are only doing vertical collision and not horizontal collision at all

Ill look at it a little more, depends on how much my head hurts, but ill try...

Author:  Ultrahex [ Fri Jan 05, 2007 8:06 pm ]
Post subject: 

This is as far as i got trying to figure out what your code even does.....

... Yes i removed the jump and all collision detection for hope that it would help you somewhat.... the jump you implemented does not make logical sense to me BTW.

code:

%%Tiling Engine For Mario v1.0
setscreen ("graphics:640;640")
setscreen ("offscreenonly")


% Tile Variables

const tilesVertical : int := 20
const tilesHorizontal : int := 40
const TileWidth : int := 32

var levelMapFile : int
open : levelMapFile, "tiles.txt", get

% Images

const background : int := Pic.FileNew ("tiles/Back2.bmp")

var StilesHorizontalrtPosition, lchng : int
var Locations : array 1 .. (tilesHorizontal * tilesVertical) of string
var TileImages : array 1 .. (tilesHorizontal * tilesVertical) of int
var chars : array char of boolean
var lend : int := (TileWidth * tilesHorizontal - 640) * -1
var jc : int := 14
var mem2 : int := 1
var cntr : int := 0


%%Mario Parameters
var mario_x, mario_y, mario_frame, chng : int
var dir : string := "right"
mario_x := 1
mario_y := 32
chng := 8
mario_frame := 1
var mario : array 1 .. 8 of int
var jumping : boolean := false
var jump_count : int := 0

%%Loads Mario
for i : 1 .. 8
    mario (i) := Pic.FileNew ("mario" + intstr (i) + ".BMP")
end for
Pic.Draw (mario (1), mario_x, mario_y, picMerge)


%%Load The Background
Pic.Draw (background, 1, 1, picCopy)
%%Load the map into an array
StilesHorizontalrtPosition := 0
for i : 1 .. tilesVertical
    for ii : 1 .. tilesHorizontal
        exit when eof (levelMapFile)
        get : levelMapFile, Locations (ii + StilesHorizontalrtPosition)

        if strint (Locations (ii + StilesHorizontalrtPosition)) not= 2 then
            cntr := cntr + 1
        end if
    end for
    StilesHorizontalrtPosition += tilesHorizontal
end for
close : levelMapFile

%%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 .. (tilesHorizontal * tilesVertical)
    Pic.SetTransparentColor (TileImages (strint (Locations (i))), white)
end for

lchng := 0
var cx, cy : array 1 .. cntr of int

%%The Tile Mapper
loop

    %Character Movement
    Input.KeyDown (chars)

    % Right
    if chars (KEY_RIGHT_ARROW) then
        mario_x += 12
    end if

    % Left
    if chars (KEY_LEFT_ARROW) then
        mario_x -= 12
    end if

    %Sprite Animation

    if chars (KEY_RIGHT_ARROW) and jumping = false then
        dir := "right"
        if (mario_frame >= 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 <= lend then
     mario_x := mario_x + chng
     end if
     dir := "right"
     end if
     if chars1 (KEY_LEFT_ARROW) then
     if mario_x < 320 then
     mario_x := mario_x - chng
     end if
     if lchng <= lend then
     mario_x := mario_x - chng
     end if
     dir := "left"
     end if
     if dir = "right" then
     mario_frame := 3
     else
     mario_frame := 8
     end if
     for i : 1 .. jc
     mario_y := mario_y + 1
     end for
     jc := jc - 1
     if jc = 0 then
     jumping2 := "true"
     jumping := "false"
     end if
     Pic.Draw (mario (mario_frame), mario_x, mario_y, picMerge)
     end if

     if jumping2 = "true" then
     if chars1 (KEY_RIGHT_ARROW) then
     if mario_x < 320 then
     mario_x := mario_x + chng
     end if
     if lchng <= lend then
     mario_x := mario_x + chng
     end if
     dir := "right"
     end if
     if chars1 (KEY_LEFT_ARROW) then
     if mario_x < 320 then
     mario_x := mario_x - chng
     end if
     if lchng <= lend then
     mario_x := mario_x - chng
     end if
     dir := "left"
     end if
     if dir = "right" then
     mario_frame := 6
     else
     mario_frame := 7
     end if
     for i : 1 .. jc
     mario_y := mario_y - 1
     end for
     Pic.Draw (mario (mario_frame), mario_x, mario_y, picMerge)
     end if
     */
    if chars (KEY_RIGHT_ARROW) = false and chars (KEY_LEFT_ARROW) = false and chars (KEY_UP_ARROW) = false and jumping = false then
        if mario_frame = 5 then
            mario_frame := 4
        elsif mario_frame = 2 then
            mario_frame := 1
        end if
    end if

    Pic.Draw (mario (mario_frame), mario_x, mario_y, picMerge)
    put "block y: ", cy (10)
    put "block x: ", cx (10)
    put "mario y: ", mario_y
    put "mario x: ", mario_x
    View.Update
    delay (20)
    cls
end loop


Author:  ericfourfour [ Fri Jan 05, 2007 8:24 pm ]
Post subject: 

code:
const tilesVertical : int := 20
const tilesHorizontal : int := 40
const TileWidth : int := 32

The naming convention for constants is almost always all capitals. Check out wtd's draft of style guidelines for more information.

I also noticed some unnecessarily awkward variable names. chng, lend, and cntr. I assume the first means change, the second left_end and the third centre. Never sacrifice something as important as clarity for something as trivial as a few characters.

Author:  CodeMonkey2000 [ Sat Jan 06, 2007 11:15 am ]
Post subject: 

Wow that code is really messy, and has bad variable names. As for the code, i think it is you detection procedure, it should be a function and it should only return what you are looking for or "f".
code:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%COLLISION DETECTION%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function detect (n : string) : string
    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) + 20 and n = "u" then
                result "u"
            elsif my <= cy (i) + 12 and n = "d" then
                result "d"
            elsif mx + 32 >= cx (i) + 20 and n = "r" then
                result "r"
            elsif mx <= cx (i) + 12 and n = "l" then
                result "l"
            end if
        end if
    end for
    return "f"
end detect
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

this should work, assuming that this collision system is correct. Normally I would have the detection function take in x1,y1,x2,y2 and see if they are colliding in anyway and return true or false, that way the same function can be use for detecting enemies too.

Author:  CodeMonkey2000 [ Sat Jan 06, 2007 11:27 am ]
Post subject: 

And if you are not sure how to use that in your program, here
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 : 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


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%COLLISION DETECTION%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function detect (n : string) : string
    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) + 20 and n = "u" then
                result "u"
            elsif my <= cy (i) + 12 and n = "d" then
                result "d"
            elsif mx + 32 >= cx (i) + 20 and n = "r" then
                result "r"
            elsif mx <= cx (i) + 12 and n = "l" then
                result "l"
            end if
        end if
    end for
    result "f"
end detect
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%The Tile Mapper
loop
    Input.KeyDown (chars)
    if chars (KEY_RIGHT_ARROW) then
        if mx >= 320 and lchng >= lend and detect ("r") not= "r" then
            lchng := lchng - 5
        end if
    end if
    if chars (KEY_LEFT_ARROW) then
        if lchng < 0 and mx <= 329 and detect ("l") not= "l" then
            lchng := lchng + 5
        end if
    end if
    Pic.Draw (background, 1, 1, picCopy)
    var XPositionCounter : int := 0
    var YPositionCounter : int := td
    for i : 1 .. (ta * td)
        if XPositionCounter = ta 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
    put detect ("u")

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%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 detect ("r") not= "r" then
            mx := mx + chng
        end if
        if lchng <= lend and detect ("r") not= "r" then
            mx := mx + chng
        end if
        Pic.Draw (mario (mcount), mx, my, picMerge)
    end if
    delay (20)


    %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 mx < 320 and detect ("r") not= "r" then
                mx := mx + chng
            end if
            if lchng <= lend and detect ("r") not= "r" then
                mx := mx + chng
            end if
            dir := "right"
        end if
        if chars1 (KEY_LEFT_ARROW) then
            if mx < 320 and detect ("l") not= "l" then
                mx := mx - chng
            end if
            if lchng <= lend and detect ("l") not= "l" then
                mx := mx - chng
            end if
            dir := "left"
        end if
        if dir = "right" then
            mcount := 3
        else
            mcount := 8
        end if
        for i : 1 .. jc
            my := my + 1
            exit when detect ("d") = "d"
        end for
        jc := jc - 1
        if detect ("d") = "d" or jc = 0 then
            jumping2 := "true"
            jumping := "false"
        end if
        Pic.Draw (mario (mcount), mx, my, picMerge)
    end if

    if jumping2 = "true" then
        if chars1 (KEY_RIGHT_ARROW) then
            if mx < 320 and detect ("r") not= "r" then
                mx := mx + chng
            end if
            if lchng <= lend and detect ("r") not= "r" then
                mx := mx + chng
            end if
            dir := "right"
        end if
        if chars1 (KEY_LEFT_ARROW) then
            if mx < 320 and detect ("l") not= "l" then
                mx := mx - chng
            end if
            if lchng <= lend and detect ("l") not= "l" then
                mx := mx - chng
            end if
            dir := "left"
        end if
        if dir = "right" then
            mcount := 6
        else
            mcount := 7
        end if
        for i : 1 .. jc
            my := my - 1
            exit when detect ("u") = "u" or detect ("d") = "d"
        end for
        if detect ("u") not= "u" then
            jc := jc + 1
        end if
        Pic.Draw (mario (mcount), mx, my, picMerge)
        if detect ("u") = "u" then
            jumping2 := "false"
            if dir = "right" then
                mcount := 1
            else
                mcount := 4
            end if
        end if
    end if

    %Walking Left Sequence
    if chars1 (KEY_LEFT_ARROW) and jumping = "false" and jumping2 = "false" then     %Code for walking left
        dir := "left"
        if mcount = 5 or mcount < 4 then
            mcount := 4
        elsif mcount < 5 then
            mcount := mcount + 1
        end if
        if lchng = 0 and detect ("l") not= "l" then
            mx := mx - chng
        end if
        if lchng <= lend and detect ("l") = "l" then
            mx := mx - chng
        end if
        Pic.Draw (mario (mcount), mx, my, picMerge)
    end if

    %Static Sequence
    if chars1 (KEY_RIGHT_ARROW) = false and chars1 (KEY_LEFT_ARROW) = false and chars1 (KEY_UP_ARROW) = false and jumping = "false" and jumping2 = "false" then     %If Static...
        if mcount = 4 or mcount = 5 then
            mcount := 4
            Pic.Draw (mario (mcount), mx, my, picMerge)
        elsif mcount = 1 or mcount = 2 then
            mcount := 1
            Pic.Draw (mario (mcount), mx, my, picMerge)
        end if
    end if
    put "block y: ", cy (10)
    put "block x: ", cx (10)
    put cdir
    put "mario y: ", my
    put "mario x: ", mx
    View.Update
    cls
    cdir := "f"
end loop

It doesn't work right. Mabey because you didnt implement it right or I did it wrong. But at least Mario get stuck in the blocks Razz

Author:  goober999 [ Sat Jan 06, 2007 2:57 pm ]
Post subject: 

Ok thanks a lot for the help I have taken spearmonkey2000's idea of using functions and expanded on it a bit using two functions: 1 for verticle collision, and one for horrizontal collision. Now a new problem Razz, for some reason, mario freezes when he encounters either type of collision. I'm oblivious to this any idea's?
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 : int := 1
var cntr : int := 0
open : tiles, "tiles.txt", get

%%Collision Variables
var cdir : 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


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%COLLISION DETECTION VERTICLE%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function detect (n : string) : string
    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 <= cy (i) + 32 and n = "u" then
                result "u"
            elsif my <= cy (i) + 12 and n = "d" then
                result "d"
            end if
        end if
    end for
    result "f"
end detect
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%COLLISION DETECTION HORIZONTAL%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function detect2 (n : string) : string
    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 mx + 32 >= cx (i) and n = "r" then
                result "r"
            elsif mx <= cx (i) + 32 and n = "l" then
                result "l"
            end if
        end if
    end for
    result "f"
end detect2
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%The Tile Mapper
loop
    Input.KeyDown (chars)
    if chars (KEY_RIGHT_ARROW) then
        if mx >= 320 and lchng >= lend and detect2 ("r") not= "r" then
            lchng := lchng - 5
        end if
    end if
    if chars (KEY_LEFT_ARROW) then
        if lchng < 0 and mx <= 329 and detect2 ("l") not= "l" then
            lchng := lchng + 5
        end if
    end if
    Pic.Draw (background, 1, 1, picCopy)
    var XPositionCounter : int := 0
    var YPositionCounter : int := td
    for i : 1 .. (ta * td)
        if XPositionCounter = ta 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
    put detect ("u")

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%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 detect2 ("r") not= "r" then
            for i : 1 .. chng
                mx := mx + 1
            end for 
        end if
        if lchng <= lend and detect2 ("r") not= "r" then
            for i : 1 .. chng   
                mx := mx + 1
            end for
        end if
        Pic.Draw (mario (mcount), mx, my, picMerge)
    end if
    delay (20)


    %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 mx < 320 and detect2 ("r") not= "r" then
                for i : 1 .. chng
                    mx := mx + 1
                end for
            end if
            if lchng <= lend and detect2 ("r") not= "r" then
                for i : 1 .. chng
                    mx := mx + 1
                end for
            end if
            dir := "right"
        end if
        if chars1 (KEY_LEFT_ARROW) then
            if mx < 320 and detect2 ("l") not= "l" then
                for i : 1 .. chng
                    mx := mx - 1
                end for
            end if
            if lchng <= lend and detect2 ("l") not= "l" then
                mx := mx - chng
            end if
            dir := "left"
        end if
        if dir = "right" then
            mcount := 3
        else
            mcount := 8
        end if
        for i : 1 .. jc
            my := my + 1
            exit when detect ("d") = "d"
        end for
        jc := jc - 1
        if detect ("d") = "d" or jc = 0 then
            jumping2 := "true"
            jumping := "false"
        end if
        Pic.Draw (mario (mcount), mx, my, picMerge)
    end if

    if jumping2 = "true" then
        if chars1 (KEY_RIGHT_ARROW) then
            if mx < 320 and detect2 ("r") not= "r" then
                mx := mx + chng
            end if
            if lchng <= lend and detect2 ("r") not= "r" then
                for i : 1 .. chng
                    mx := mx + 1
                end for
            end if
            dir := "right"
        end if
        if chars1 (KEY_LEFT_ARROW) then
            if mx < 320 and detect2 ("l") not= "l" then
                for i : 1 .. chng
                    mx := mx - 1
                end for
            end if
            if lchng <= lend and detect2 ("l") not= "l" then
                mx := mx - chng
            end if
            dir := "left"
        end if
        if dir = "right" then
            mcount := 6
        else
            mcount := 7
        end if
        for i : 1 .. jc
            my := my - 1
            exit when detect ("u") = "u" 
        end for
        if detect ("u") not= "u" then
            jc := jc + 1
        end if
        Pic.Draw (mario (mcount), mx, my, picMerge)
        if detect ("u") = "u" then
            jumping2 := "false"
            if dir = "right" then
                mcount := 1
            else
                mcount := 4
            end if
        end if
    end if

    %Walking Left Sequence
    if chars1 (KEY_LEFT_ARROW) and jumping = "false" and jumping2 = "false" then     %Code for walking left
        dir := "left"
        if mcount = 5 or mcount < 4 then
            mcount := 4
        elsif mcount < 5 then
            mcount := mcount + 1
        end if
        if lchng = 0 and detect2 ("l") not= "l" then
            for i : 1 .. chng
                mx := mx - 1
            end for
        end if
        if lchng <= lend and detect2 ("l") = "l" then
            for i : 1 .. chng
                mx := mx - 1
            end for
        end if
        Pic.Draw (mario (mcount), mx, my, picMerge)
    end if

    %Static Sequence
    if chars1 (KEY_RIGHT_ARROW) = false and chars1 (KEY_LEFT_ARROW) = false and chars1 (KEY_UP_ARROW) = false and jumping = "false" and jumping2 = "false" then     %If Static...
        if mcount = 4 or mcount = 5 then
            mcount := 4
            Pic.Draw (mario (mcount), mx, my, picMerge)
        elsif mcount = 1 or mcount = 2 then
            mcount := 1
            Pic.Draw (mario (mcount), mx, my, picMerge)
        end if
    end if
    put "block y: ", cy (10)
    put "block x: ", cx (10)
    put detect2 ("r")
    put "mario y: ", my
    put "mario x: ", mx
    View.Update
    cls
    cdir := "f"
end loop

Author:  CodeMonkey2000 [ Mon Jan 08, 2007 4:50 pm ]
Post subject:  Re: Help with Side Scroller

You need to rethink your collision system. You should be using only 1 function for detection. It should be taking in x1,y1,x2,y2 for mario and sx1,sy2,sx2,sy2 for blocks/enemies. When you call the function you should shouldn't input mario's current position, but rather the position he is going to be in. Check with all the objects, and if any of them return true then dont allow mario to move if they all return false then it is safe to move.

Author:  ericfourfour [ Mon Jan 08, 2007 8:49 pm ]
Post subject:  Re: Help with Side Scroller

Turing:
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")

Don't hardcode these values. What you should do is read them from a file along with the tile map. The file would look something like this:
file:
20
40
32
5
tiles/Back2.bmp

This way if you want to change maps you can choose which file to load it from.

Author:  Clayton [ Mon Jan 08, 2007 9:44 pm ]
Post subject:  Re: Help with Side Scroller

an even better idea for the number of tiles being used is to have a flexible array that has it's upper bounds changed for each tile that comes into the game. Check out the flexible arrays tutorial in the Turing Walkthrough.


: