Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 How can I create a platformer?
Index -> Programming, Turing -> Turing Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Snario




PostPosted: Wed Jan 05, 2011 10:28 am   Post subject: How can I create a platformer?

What is it you are trying to achieve?
Create a platformer similar to Mario Bros.

What is the problem you are having?
Side-scrolling and platforms.

Describe what you have tried to solve this problem
My program uses velocity and gravity to determine the movement of the character so for the platform I try to reset the y velocity to 0 when you're ontop of it but then it doesn't let you actually jump off it unless you walk off and fall. For the side-scrolling, I haven't thought of that yet.

Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)

Turing:


var chars : array char of boolean
var x1 : int := 1
var x2 : int := x1 + 10
var y1 : int := 1
var y2 : int := y1 + 10
var xv : real := 0
var yv : real := 0
var g : real := 1
var ground : boolean := true

setscreen ("graphics:1200;900,offscreenonly,nobuttonbar,title:Player Movement Tutorial")

loop
    Input.KeyDown (chars)

    if chars ('w') and y2 < maxy and ground = true then
        yv += 15
    end if
    if chars ('a') and x1 > 0 then
        xv -= 1
    elsif chars ('d') and x2 < maxx then
        xv += 1
    elsif chars (KEY_ESC) then
        exit
    end if

 
        if y1 > 0 then
            yv -= g
        elsif y1 < 0 then
            yv := 0
            y1 := 0
        end if
 

    if xv > 0 then
        xv -= 0.5
    elsif xv < 0 then
        xv += 0.5
    end if

    if x1 < 0 then
        x1 := 0
        xv := 0
    elsif x2 > maxx then
        x1 := maxx - 10
        xv := 0
    end if

    if y2 > maxy then
        y1 := maxy - 10
        yv := 0
    end if

    if y1 = 0 then
        ground := true
    elsif y1 >= 325 and y1 <= 330 and x1 >= 400 and x1 <= 600 then
        ground := true
        yv := 0
    else
        ground := false
    end if

    x1 += round (xv)
    y1 += round (yv)
    x2 := x1 + 10
    y2 := y1 + 10

    Draw.FillBox (x1, y1, x2, y2, black)
    Draw.Box (x1 - 1, y1 - 1, x2 + 1, y2 + 1, grey)
    locate (1, 1)

    drawfillbox (400, 300, 600, 325, 7)
    drawbox (399, 299, 601, 326, grey)

    View.Update
    delay (10)
    cls
end loop





Please specify what version of Turing you are using
The latest version.






If someone could give me some kind of general outline on how to make this game it would be MUCH appreciated! Thanks!
Sponsor
Sponsor
Sponsor
sponsor
TokenHerbz




PostPosted: Wed Jan 05, 2011 11:02 am   Post subject: RE:How can I create a platformer?

okay well first, Lets have your "JUMP" be aloud if the "mario" isn't actually in the air. So this way if he's even on the platform thats like above the ground, he can jump off as if he was on the ground.

I want you to put this inside a function/proc and get it working first Smile

We should also make it so that the "collision" actually works, Like i try your program out and the "mario" is above the ground and the platform i jump on.
EDIT: unless im color blind and looking at gray (looks like background tho lol i dont see gray at all in the program tho) -- i must got terrible eye sight.
DemonWasp




PostPosted: Wed Jan 05, 2011 11:05 am   Post subject: RE:How can I create a platformer?

Well, your platform has four sides. One prevents your player from moving up (the bottom), another prevents them from moving down (the top). You can probably figure out the last two.

If a player runs into the bottom edge of a platform, then they should stop moving upwards (though they can move downwards). All you want to do is check whether their velocity is up (positive, I assume) and if so, set it to zero. If it's down (negative?) then let it be, so they can fall away from the platform. Similarly for the top edge, except that you want to check whether they're moving down before zeroing their vertical velocity.

The trickiest part will be determining when a player has contacted a given edge. Treat each edge like a line, and only worry about one edge at a time. You need to consider the following cases (my language here assumes the top edge, modify as needed for the others):

1. The player is completely above the edge. (No collision)
2. The player is completely below the edge. (No collision)
3. The player is completely to the left of the edge. (No collision)
4. The player is completely to the right of the edge. (No collision).
5. The player is between the left and right bounds of the edge, and is partially above the edge and partially below the edge. (Collision!)

Really, you actually only need to detect case 5, you just have to make sure that cases 1-4 behave as you expect.

You will probably want to look into records in the Turing help files or in the Turing Tutorials sections here. Doing so will let you describe a platform relatively easily: you just need an (x,y) coordinate for the lower-left and a (width,height) size. That automatically gives you the coordinates of the top, bottom, left and right sides.

Spend some time with a pen and paper thinking about your player: they have an (x,y) coordinate, plus they have some dimension (width, height). Make sure you account for their size in your collision calculations.
Snario




PostPosted: Thu Jan 06, 2011 10:36 am   Post subject: RE:How can I create a platformer?

Thanks for the input guys! I've taken your advice and created two platforms but now I have a small (very small) issue. For some reason when I jump from platform 2 to platform 1, I fall right through platform 1. Not sure what's going on. Here's the code:

Turing:
%%%%%%%%%%%%%%%%%%%%
%%%Mario movement%%%
%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%Variables%%%%%%%%%%%%%
var chars : array char of boolean
var marioX : int := 0
var marioWidth : int := 10
var marioX2 := marioX + marioWidth
var marioY : int := 0
var marioHeight : int := 20
var marioY2 := marioY + marioHeight
var marioXVelocity : real := 0
var marioYVelocity : real := 0
var ground : boolean := true
var platform1 : boolean := false
var platform2 : boolean := false
var plat1X1 : int := 300
var plat1X2 : int := 450
var plat1Y1 : int := 150
var plat1Y2 : int := 160
var plat2X1 : int := 700
var plat2X2 : int := 850
var plat2Y1 : int := 255
var plat2Y2 : int := 265

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%Constants%%%%%
const gravity := 1
%%%%%%%%%%%%%%%%%%'

%%%%%%%%%%%%%%%%%%PLATFORM%%%%%%%%%%%%%%%%%%%%
proc drawPlatform (colorbase : int)
    Draw.FillBox (plat1X1, plat1Y1, plat1X2, plat1Y2, 0)
    Draw.Box (plat1X1 - 1, plat1Y1 - 1, plat1X2 + 1, plat1Y2 + 1, grey)
    Draw.FillBox (plat1X1 + 20, plat1Y1 + 4, plat1X2 - 20, plat1Y2 - 4, colorbase)

    Draw.FillBox (plat2X1, plat2Y1, plat2X2, plat2Y2, 0)
    Draw.Box (plat2X1 - 1, plat2Y1 - 1, plat2X2 + 1, plat2Y2 + 1, grey)
    Draw.FillBox (plat2X1 + 20, plat2Y1 + 4, plat2X2 - 20, plat2Y2 - 4, colorbase)
end drawPlatform
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

setscreen ("graphics:1268;956,offscreenonly,nobuttonbar,title:Player Movement Tutorial")

loop
    drawfillbox (0, 0, maxx, maxy, 17)
    marioHeight := 20

    %%%%%%%%%%%%%%%%%%%%%%%%%INPUT%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    Input.KeyDown (chars)

    if chars ('w') and marioY2 not= maxy and (marioY = 1 or platform1 = true or platform2 = true) then
        marioYVelocity += 20
    elsif chars ('a') then
        if marioX > 0 then
            if marioXVelocity < 0 and marioXVelocity >= -20 then
                marioXVelocity -= 1 %accelerating
            else
                marioXVelocity -= 2 %slowing down
            end if
        end if
    elsif chars ('d') then
        if marioX2 < maxx then
            if marioXVelocity > 0 and marioXVelocity <= 20 then
                marioXVelocity += 1 %accelerating
            else
                marioXVelocity += 2 %slowing down
            end if
        else
            marioXVelocity := 0
            marioX := maxx - marioWidth
        end if
    elsif chars ('s') then
        marioHeight := 10
    elsif chars (KEY_ESC) then
        exit %exit game
    end if
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    %%%%%%BOUNDARIES%%%%%%%%%%
    if marioX < 0 then
        marioXVelocity := 0
        marioX := 0
    elsif marioX + marioWidth > maxx then
        marioXVelocity := 0
        marioX := maxx - marioWidth
    end if
    %%%%%%%%%%%%%%%%%%%%%%%%%%%

    %*** mario stops if no buttons are pressed
    if marioXVelocity > 0 and not chars ('d') then
        marioXVelocity -= 1
    elsif marioXVelocity < 0 and not chars ('a') then
        marioXVelocity += 1
    end if
    %***

    %%%%%%%%%%%%%%%%JUMPING%%%%%%%%%%%%%%%%%%%%%%%

    if marioY > 1 and marioY <= maxy - marioHeight then
        if platform1 = false and platform2 = false then
            marioYVelocity -= gravity
        end if
    elsif marioY < 1 then
        marioYVelocity := 0
        marioY := 1
    elsif marioY >= maxy - marioHeight then
        marioY := maxy - marioHeight
        marioYVelocity := 0
    end if
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%55

    %%%%%%%%%%%%%%%%%%PLATFORMS%%%%%%%%%%%%%%%%%%%%

    drawPlatform (0)

    %%%%%%%%%%%%%%%%%%PLATFORMS%%%%%%%%%%%%%%%%%%%%

    %%%%%%%%%%%%%%%%%%COLLISION DETECTION%%%%%%%%%%%%%%%%
    %%%1
    if (marioX >= plat1X1 or marioX2 + marioWidth >= plat1X1) and marioX <= plat1X2 and marioY >= plat1Y2 and marioY <= plat1Y2 + 10 and marioYVelocity < 0 then
        marioYVelocity := 0
        platform1 := true
    else
        platform1 := false
    end if

    %%%

    %%%2
    if (marioX >= plat2X1 or marioX2 >= plat2X1) and marioX <= plat2X2 and marioY >= plat2Y2 and marioY <= plat2Y2 + 11 and marioYVelocity < 0 then
        marioYVelocity := 0
        platform2 := true
    else
        platform2 := false
    end if

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    marioX += round (marioXVelocity) %horizontal movement
    marioX2 := marioX + marioWidth
    marioY += round (marioYVelocity)
    marioY2 := marioY + marioHeight

    Draw.FillBox (marioX, marioY, marioX2, marioY2, 0)
    Draw.Box (marioX - 1, marioY - 1, marioX2 + 1, marioY2 + 1, grey)

    View.Update

    delay (12)
    cls
end loop
TerranceN




PostPosted: Thu Jan 06, 2011 6:49 pm   Post subject: RE:How can I create a platformer?

This is happening because during the larger fall, the velocity per frame of your person is greater than the depth of the platform, so your person is on the top one frame, then underneath the next, it is never in the middle, which your code uses as a test to see if the player hit the platform. If you limit the speed your person can fall, they will not go through the platform (I tried 15 pixels/frame, which worked fine).
Snario




PostPosted: Fri Jan 07, 2011 8:17 am   Post subject: RE:How can I create a platformer?

Here is an updated version of the code, I've fixed a few bugs but would like advice on how I can make this more friendly (more organized code).

Turing:
%%%%%%%%%%%%%%%%%%%%
%%%Mario movement%%%
%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%Variables%%%%%%%%%%%%%
var chars : array char of boolean
var marioX : int := 0
var marioWidth : int := 10
var marioX2 := marioX + marioWidth
var marioY : int := 0
var marioHeight : int := 20
var marioY2 := marioY + marioHeight
var marioXVelocity : real := 0
var marioYVelocity : real := 0
var ground : boolean := true
var onPlatform1, onPlatform2, onPlatform3 : boolean := false
var platform1LeftBoundary : int := 300
var platform1RightBoundary : int := 450
var platform1BottomBoundary : int := 140
var platform1TopBoundary : int := 150
var platform2LeftBoundary : int := 700
var platform2RightBoundary : int := 850
var platform2BottomBoundary : int := 255
var platform2TopBoundary : int := 265
var platform3LeftBoundary : int := 300
var platform3RightBoundary : int := 450
var platform3BottomBoundary : int := 390
var platform3TopBoundary : int := 400
var unlocked1, unlocked2, unlocked3 : boolean := false
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%Constants%%%%%
const gravity := 1
%%%%%%%%%%%%%%%%%%'

%%%%%%%%%%%%%%%%%%PLATFORM%%%%%%%%%%%%%%%%%%%%
proc drawPlatform (colorbase : int)
    Draw.FillBox (platform1LeftBoundary + 1, platform1BottomBoundary + 1, platform1RightBoundary - 1, platform1TopBoundary - 1, colorbase)
    Draw.Box (platform1LeftBoundary, platform1BottomBoundary, platform1RightBoundary, platform1TopBoundary, 23)
end drawPlatform

proc drawPlatform2 (colorbase : int)
    Draw.FillBox (platform2LeftBoundary + 1, platform2BottomBoundary + 1, platform2RightBoundary - 1, platform2TopBoundary - 1, colorbase)
    Draw.Box (platform2LeftBoundary, platform2BottomBoundary, platform2RightBoundary, platform2TopBoundary, 23)
end drawPlatform2

proc drawPlatform3 (colorbase : int)
    Draw.FillBox (platform3LeftBoundary + 1, platform3BottomBoundary + 1, platform3RightBoundary - 1, platform3TopBoundary - 1, colorbase)
    Draw.Box (platform3LeftBoundary, platform3BottomBoundary, platform3RightBoundary, platform3TopBoundary, 23)
end drawPlatform3
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

setscreen ("graphics:1268;956,offscreenonly,nobuttonbar,title:Mario")

loop
    drawfillbox (0, 0, maxx, maxy, 17)
    marioHeight := 20

    %%%%%%%%%%%%%%%%%%%%%%%%%INPUT%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    Input.KeyDown (chars)

    if chars ('w') and marioY2 not= maxy and (marioY = 1 or onPlatform1 or onPlatform2 or onPlatform3 = true) then
       
            marioYVelocity += 20
       
    elsif chars ('a') then
        if marioX > 0 then
            if marioXVelocity < 0 and marioXVelocity >= -20 then
                marioXVelocity -= 1 %accelerating
            else
                marioXVelocity -= 2 %slowing down
            end if
        end if
    elsif chars ('d') then
        if marioX2 < maxx then
            if marioXVelocity > 0 and marioXVelocity <= 20 then
                marioXVelocity += 1 %accelerating
            else
                marioXVelocity += 2 %slowing down
            end if
        else
            marioXVelocity := 0
            marioX := maxx - marioWidth
        end if
    elsif chars ('s') then
        marioHeight := 10
    elsif chars (KEY_ESC) then
        exit %exit game
    end if
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    %%%%%%BOUNDARIES%%%%%%%%%%
    if marioX < 0 then
        marioXVelocity := 0
        marioX := 0
    elsif marioX + marioWidth > maxx then
        marioXVelocity := 0
        marioX := maxx - marioWidth
    end if
    %%%%%%%%%%%%%%%%%%%%%%%%%%%

    %*** mario stops if no buttons are pressed
    if marioXVelocity > 0 and not chars ('d') then
        marioXVelocity -= 1
    elsif marioXVelocity < 0 and not chars ('a') then
        marioXVelocity += 1
    end if
    %***

    %%%%%%%%%%%%%%%%JUMPING%%%%%%%%%%%%%%%%%%%%%%%

    if marioY > 1 and marioY <= maxy - marioHeight then
        if onPlatform1 = false and onPlatform2 = false and onPlatform3 = false then

            marioYVelocity -= gravity
        end if
    elsif marioY < 1 then
        marioYVelocity := 0
        marioY := 1
    elsif marioY >= maxy - marioHeight then
        marioY := maxy - marioHeight
        marioYVelocity := 0
    end if
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%55

    %%%%%%%%%%%%%%%%%%PLATFORMS%%%%%%%%%%%%%%%%%%%%
    if unlocked1 = false then
        drawPlatform (17)
    else
        drawPlatform (0)
    end if
    if unlocked2 = false then
        drawPlatform2 (17)
    else
        drawPlatform2 (0)
    end if
    if unlocked3 = false then
        drawPlatform3 (17)
    else
        drawPlatform3 (0)
    end if

    %%%%%%%%%%%%%%%%%%PLATFORMS%%%%%%%%%%%%%%%%%%%%

    %%%%%%%%%%%%%%%%%%COLLISION DETECTION%%%%%%%%%%%%%%%%
    %%%1

    if marioX2 >= platform1LeftBoundary and marioX <= platform1RightBoundary and marioY2 <= platform1TopBoundary + 21 and marioY >= platform1BottomBoundary and marioYVelocity <= 0 then
        marioY := platform1TopBoundary + 1
        marioYVelocity := 0
        onPlatform1 := true
        unlocked1 := true
    else
        onPlatform1 := false
        unlocked1 := false
    end if
    %%%
    %%%2
    if marioX2 >= platform2LeftBoundary and marioX <= platform2RightBoundary and marioY2 <= platform2TopBoundary + 21 and marioY >= platform2BottomBoundary and marioYVelocity <= 0 then
        marioY := platform2TopBoundary + 1
        marioYVelocity := 0
        onPlatform2 := true
        unlocked2 := true
    else
        unlocked2 := false
        onPlatform2 := false
    end if
    %%%
    %%%3
    if marioX2 >= platform3LeftBoundary and marioX <= platform3RightBoundary and marioY2 <= platform3TopBoundary + 21 and marioY >= platform3BottomBoundary and marioYVelocity <= 0 then
        marioY := platform3TopBoundary + 1
        marioYVelocity := 0
        onPlatform3 := true
        unlocked3 := true
    else
        unlocked3 := false
        onPlatform3 := false
    end if

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    marioX += round (marioXVelocity) %horizontal movement
    marioX2 := marioX + marioWidth
    marioY += round (marioYVelocity)
    marioY2 := marioY + marioHeight

    Draw.FillBox (marioX, marioY, marioX2, marioY2, 0)
    Draw.Box (marioX - 1, marioY - 1, marioX2 + 1, marioY2 + 1, grey)

    View.Update
    delay (12)

end loop
DemonWasp




PostPosted: Fri Jan 07, 2011 9:55 am   Post subject: RE:How can I create a platformer?

(Before you start on the below, SAVE A COPY of your program with the name "MyProgram_Revision1.t" or similar. You can easily break things when doing extensive work as below, and I don't want you to lose your working program. Now...


Types, arrays, methods, files.

First, realise that you have several similar (identical) object "types". There are platforms, which have a top, bottom, left and right. There are creatures, which have an X and Y and XV and YV (position, velocity).

If you represent those objects more directly, your code will become simpler. For example, let's make a type that describes a single platform:
Turing:

type platform :
    record
        top, bottom, left, right : int
    end record


This tells Turing that you want a new type of variable called "platform", and that it has "fields" called "top", "bottom", "left" and "right", all of which are integers. Then instead of:

Turing:

var platform1LeftBoundary : int := 300
var platform1RightBoundary : int := 450
var platform1BottomBoundary : int := 140
var platform1TopBoundary : int := 150


You have:

Turing:

var platform1 : platform
platform1.left := 300
platform1.right := 450
platform1.bottom := 140
platform1.top := 150


The syntax (variableName).(fieldName) refers to the field identified by fieldName inside the variable identified by variableName. You use this both for setting and getting the values.

You will now never run into an issue where you spelled top wrong in one place, and it's easy to modify platforms to all have additional variables. For example, you might add a field called isPlayerOn, a boolean, to take the place of onPlatform1, onPlatform2, etc.


Second, realise that you have lots of objects, and potentially want lots more. You have three platforms now, but potentially a few dozen for even simple levels. Do you really want to write collision code for that many platforms? No! Of course not. That's where arrays come in. An array is a group of variables, kept in a given order and accessed by their number. Your platforms could be:

Turing:
var platforms : array 1..3 of platform


or it could be:

Turing:
var platforms : array 1..1000 of platform


The difference is minimal: a one-line change. In your existing program, changing the number of platforms to 1000 would involve at least 4 more lines per platform. We'll discuss how you initialize your platforms in a moment.


Third, realise that you do the same thing in code many times. You have code to check for collisions with platform1, then with platform2, then more for platform3. Wouldn't it be easier if you had code that just checked any platform you gave it? Yes, yes it would. For example (I have stripped your code down a little; depending on what you choose to do with the onPlatformN variables, you will want to modify this):

Turing:

proc checkPlatformCollision ( p : platform )
   if marioX2 >= p.left and marioX <= p.left and marioY2 <= p.top + 21 and marioY >= p.bottom and marioYVelocity <= 0 then
        marioY := p.top + 1
        marioYVelocity := 0
    end if
end checkPlatformCollision


Now, to check for collisions:

Turing:

checkPlatformCollision ( platforms(1) )
checkPlatformCollision ( platforms(2) )
checkPlatformCollision ( platforms(3) )


Or the much better:
Turing:

for i : 1 .. upper(platforms)
    checkPlatformCollision ( platforms(i) )
end for


That last bit will check against all platforms, no matter how many of them you have.

See how easy that is?


Now, take what I've shown you above and apply it further. You should be able to make a type for creatures (Mario is a creature), with a position and velocity. You may even want to have a "nested type": if you make a type for "position", with fields "x" and "y", then creatures have a "pos" field which has type "position". You should be able to modify the checkPlatformCollision method above to take one creature and one platform and check whether they collide; for the moment, you'll only be passing Mario as the creature, but I suspect sooner or later you'll start wanting to put goombas in the game, right?


Finally, you'll have to learn a bit about loading from files. Doing so will make your life a lot easier, because you won't have to change your program to add or change platforms at all, just a text file that it reads. However, to do that nicely, you'll have to implement all of the above first. Post back here when you want further guidance, or if you run into any problems.
Snario




PostPosted: Fri Jan 07, 2011 11:30 am   Post subject: Re: How can I create a platformer?

Thank you very very much sir! I have edited my program to include types, arrays and better collision detection but I've encountered a bit of an issue where the detection doesn't actually work.. lol.

Here's what I've got:

Spoiler:
Turing:
%%%%%%%%%%%%%%%%%%%%
%%%Mario movement%%%
%%%%%%%%%%%%%%%%%%%%
setscreen ("graphics:1268;956,offscreenonly,nobuttonbar,title:Mario")
%%%%%%%%%TYPES%%%%%%%%%%%%%%%%%%%%%%%%%
type platform :
    record
        top, bottom, left, right : int
    end record
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5

%%%%%%%%%%%Variables%%%%%%%%%%%%%
var chars : array char of boolean
var marioX : int := 0
var marioWidth : int := 10
var marioX2 := marioX + marioWidth
var marioY : int := 0
var marioHeight : int := 20
var marioY2 := marioY + marioHeight
var marioXVelocity : real := 0
var marioYVelocity : real := 0
var ground : boolean := true
var onPlatform : boolean := false
var platforms : array 1 .. 3 of platform
var unlocked : array 1 .. 3 of boolean := init (false, false, false)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%PLATFORMS%%%%%%%%%%%%
platforms (1).left := 300
platforms (1).right := 450
platforms (1).bottom := 150
platforms (1).top := 140

platforms (2).left := 700
platforms (2).right := 850
platforms (2).bottom := 265
platforms (2).top := 255

platforms (3).left := 300
platforms (3).right := 450
platforms (3).bottom := 390
platforms (3).top := 400
%%%%%%%%%%%%PLATFORMS%%%%%%%%%%%%


%%%%Constants%%%%%
const gravity := 1
%%%%%%%%%%%%%%%%%%'

%%%%%%%%%%%%%%%%%%%%%COLLISION%%%%%%%%%%%%%%%%%%
proc checkPlatformCollision (p : platform)
    if marioX2 >= p.left and marioX <= p.right and marioY2 <= p.top + 21 and marioY >= p.bottom and marioYVelocity <= 0 then
        marioY := p.top + 1
        marioYVelocity := 0
        onPlatform := true
    end if
end checkPlatformCollision
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%PLATFORM%%%%%%%%%%%%%%%%%%%%
proc drawPlatform (colorbase : int)
    Draw.FillBox (platforms (1).left + 1, platforms (1).bottom + 1, platforms (1).right - 1, platforms (1).top - 1, colorbase)
    Draw.Box (platforms (1).left, platforms (1).bottom, platforms (1).right, platforms (1).top, 23)
end drawPlatform

proc drawPlatform2 (colorbase : int)
    Draw.FillBox (platforms (2).left + 1, platforms (2).bottom + 1, platforms (2).right - 1, platforms (2).top - 1, colorbase)
    Draw.Box (platforms (2).left, platforms (2).bottom, platforms (2).right, platforms (2).top, 23)
end drawPlatform2

proc drawPlatform3 (colorbase : int)
    Draw.FillBox (platforms (3).left + 1, platforms (3).bottom + 1, platforms (3).right - 1, platforms (3).top - 1, colorbase)
    Draw.Box (platforms (3).left, platforms (3).bottom, platforms (3).right, platforms (3).top, 23)
end drawPlatform3
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

colorback (17)

loop
    cls
    marioHeight := 20

    %%%%%%%%%%%%%%%%%%%%%%%%%INPUT%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    Input.KeyDown (chars)

    if chars ('w') and marioY2 <= maxy and (marioY = 1 or onPlatform = true) then
        marioYVelocity += 20
    elsif chars ('a') then
        if marioX > 0 then
            if marioXVelocity < 0 and marioXVelocity >= -20 then
                marioXVelocity -= 1 %accelerating
            else
                marioXVelocity -= 3 %slowing down
            end if
        end if
    elsif chars ('d') then
        if marioX2 < maxx then
            if marioXVelocity > 0 and marioXVelocity <= 20 then
                marioXVelocity += 1 %accelerating
            else
                marioXVelocity += 3 %slowing down
            end if
        else
            marioXVelocity := 0
            marioX := maxx - marioWidth
        end if
    elsif chars ('s') then
        marioHeight := 10
    elsif chars (KEY_ESC) then
        exit %exit game
    end if
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    %%%%%%BOUNDARIES%%%%%%%%%%
    if marioX < 0 then
        marioXVelocity := 0
        marioX := 0
    elsif marioX + marioWidth > maxx then
        marioXVelocity := 0
        marioX := maxx - marioWidth
    end if
    %%%%%%%%%%%%%%%%%%%%%%%%%%%

    %*** mario stops if no buttons are pressed
    if marioXVelocity > 0 and not chars ('d') then
        marioXVelocity -= 1
    elsif marioXVelocity < 0 and not chars ('a') then
        marioXVelocity += 1
    end if
    %***

    %%%%%%%%%%%%%%%%JUMPING%%%%%%%%%%%%%%%%%%%%%%%
    if marioY > 1 and marioY <= maxy - marioHeight then
        if onPlatform = false then
            marioYVelocity -= gravity
        end if
    elsif marioY < 1 then
        marioYVelocity := 0
        marioY := 1
    elsif marioY >= maxy - marioHeight then
        marioY := maxy - marioHeight
        marioYVelocity := 0
    end if
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%55

    %%%%%%%%%%%%%%%%%%PLATFORMS%%%%%%%%%%%%%%%%%%%%

    if unlocked (1) = false then
        drawPlatform (17)
    else
        drawPlatform (0)
    end if
    if unlocked (2) = false then
        drawPlatform2 (17)
    else
        drawPlatform2 (0)
    end if
    if unlocked (3) = false then
        drawPlatform3 (17)
    else
        drawPlatform3 (0)
    end if

    %%%%%%%%%%%%%%%%%%PLATFORMS%%%%%%%%%%%%%%%%%%%%

    %%%%%%%%%%%%%%%%%%COLLISION DETECTION%%%%%%%%%%%%%%%%
    for i : 1 .. upper (platforms)
        checkPlatformCollision (platforms (i))
    end for
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    marioX += round (marioXVelocity)     %horizontal movement
    marioX2 := marioX + marioWidth
    marioY += round (marioYVelocity)
    marioY2 := marioY + marioHeight

    Draw.FillBox (marioX, marioY, marioX2, marioY2, 0)
    Draw.Box (marioX - 1, marioY - 1, marioX2 + 1, marioY2 + 1, grey)

    %EYES
     /*
     if chars ('s') = false then
     drawfillbox (marioX + 2, marioY + 15, marioX + 4, marioY + 18, 17)
     drawbox (marioX + 1, marioY + 14, marioX + 5, marioY + 19, grey)
     drawfillbox (marioX + 6, marioY + 15, marioX + 8, marioY + 18, 17)
     drawbox (marioX + 5, marioY + 14, marioX + 9, marioY + 19, grey)
     Draw.ThickLine (marioX + 1, marioY + 10, marioX2 - 1, marioY + 10, 1, 20)
     end if
     */

    %EYES

    View.Update
    delay (12)

end loop
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: Fri Jan 07, 2011 1:31 pm   Post subject: RE:How can I create a platformer?

That would be because the code in checkPlatformCollision is wrong.

The easiest way for you to solve this problem is to completely forget the code that you currently have. Comment it out and then ignore it. Starting from scratch, figure out how to check whether your player has collided with a given side (start with the top) of a platform.

You know that if the player collides with the platform, the following will ALL be true:

1. The player's bottom is below the platform's top.
2. The player's top is above the platform's bottom.
3. The player's right edge is to the left of the platform's left edge.
4. The player's left edge is to the right of the platform's right edge.

Once you've figured out the if statement(s) that represent that, you should set the player's vertical speed to zero and their vertical location to the top of the platform (and set the onPlatform flag).
Snario




PostPosted: Wed Jan 12, 2011 10:08 am   Post subject: Re: How can I create a platformer?

Alright I've made a lot of changes and am very satisfied with how my game is looking right now. I do however have a question for you guys...

How can I condense this?

Turing:

platforms (1).left := 0
platforms (1).right := maxx
platforms (1).bottom := 0
platforms (1).top := 10

platforms (2).left := 300
platforms (2).right := 450
platforms (2).bottom := 140
platforms (2).top := 150

platforms (3).left := 700
platforms (3).right := 850
platforms (3).bottom := 255
platforms (3).top := 265

platforms (4).left := 300
platforms (4).right := 450
platforms (4).bottom := 390
platforms (4).top := 400

platforms (5).left := 0
platforms (5).right := 360
platforms (5).bottom := 500
platforms (5).top := 510

platforms (6).left := 400
platforms (6).right := maxx
platforms (6).bottom := 500
platforms (6).top := 510

platforms (7).left := 0
platforms (7).right := 30
platforms (7).bottom := 590
platforms (7).top := 600

platforms (8).left := 0
platforms (8).right := 30
platforms (8).bottom := 725
platforms (8).top := 735

platforms (9).left := 310
platforms (9).right := 1250
platforms (9).bottom := 840
platforms (9).top := 850

platforms (10).left := 1200
platforms (10).right := maxx
platforms (10).bottom := 730
platforms (10).top := 740


I'm sure there is some sort of solution.

Here is my code again:



playerUpdate1.01.t
 Description:
Latest version of my game.

Download
 Filename:  playerUpdate1.01.t
 Filesize:  6.54 KB
 Downloaded:  133 Time(s)

2goto1




PostPosted: Wed Jan 12, 2011 10:18 am   Post subject: RE:How can I create a platformer?

Why not store your platform coordinates in a text file and then initialize them by reading from the text file.
DemonWasp




PostPosted: Wed Jan 12, 2011 10:46 am   Post subject: RE:How can I create a platformer?

2goto1 is exactly correct. Learn about file input, using open and get (and presumably, close. Read up on those, then continue to my next paragraph.

Your file will need to give you just a few key pieces of information. First, you will need a count of the number of platforms in the file. This will let you create the array of platforms to the correct size. Second, you will need groupings of numbers, four to a line. These will be (left) (right) (top) (bottom), in whichever order you prefer, though you only get to choose ONE ordering.

You should be able to load all the platforms from the file using just a counted for loop.
Snario




PostPosted: Thu Jan 13, 2011 11:25 am   Post subject: Re: How can I create a platformer?

Alright, instead of turning them into a text file, I just put them all into their own turing files and then used include. Here is the updated version that works (nearly) perfectly. If anyone could give me some suggestions on how the program can be improved, please do so. Smile

(All three files are needed to run the game properly)

Thanks!

EDIT: I want to create more levels, any ideas?



platforms.t
 Description:
IMPORTANT : Platform dimensions

Download
 Filename:  platforms.t
 Filesize:  3.43 KB
 Downloaded:  180 Time(s)


walls.t
 Description:
IMPORTANT : Wall dimensions

Download
 Filename:  walls.t
 Filesize:  528 Bytes
 Downloaded:  140 Time(s)


playerUpdate1.01.t
 Description:
Main game

Download
 Filename:  playerUpdate1.01.t
 Filesize:  6.27 KB
 Downloaded:  160 Time(s)

TerranceN




PostPosted: Thu Jan 13, 2011 3:49 pm   Post subject: RE:How can I create a platformer?

The reason using text files are better, is because you can create one procedure with a file name parameter, which can load any map whose file name you send it. This will also make it a lot easier for your program to generate a map file in case you want to implement user created levels.

As for suggestions:

Create a trigger entity, that has a string type mapName property (and a box in which the entity is triggered), that when triggered, will load the new map. That way you can have a series of maps together.

Create obstacles for the player (and maybe even new weapons or tools to get past such obstacles)

Create a Main Menu with user profiles, with options for window size, a way to load a previously played map, and a a way to continue playing from where you left off.

Create a way for a user to create a custom map.
Snario




PostPosted: Fri Jan 14, 2011 10:57 am   Post subject: Re: RE:How can I create a platformer?

TerranceN @ Thu Jan 13, 2011 3:49 pm wrote:
The reason using text files are better, is because you can create one procedure with a file name parameter, which can load any map whose file name you send it. This will also make it a lot easier for your program to generate a map file in case you want to implement user created levels.

As for suggestions:

Create a trigger entity, that has a string type mapName property (and a box in which the entity is triggered), that when triggered, will load the new map. That way you can have a series of maps together.

Create obstacles for the player (and maybe even new weapons or tools to get past such obstacles)

Create a Main Menu with user profiles, with options for window size, a way to load a previously played map, and a a way to continue playing from where you left off.

Create a way for a user to create a custom map.


Alright I'm starting to do that but I'm having a few issues.

Here is just a simple example program that will take some dimensions from the file called "platforms.txt" and then put them on the screen.

Turing:


var fileNo : int
var noA : int := 0

var a : array 1 .. 29 of int
var b : array 1 .. 29 of int
var c : array 1 .. 29 of int
var d : array 1 .. 29 of int


open : fileNo, "platforms.txt", get
loop
exit when eof
    noA += 1
    get : fileNo, a (noA), b (noA), c (noA), d (noA)
end loop
close : fileNo


put noA
for i : 1 .. noA
    put a (i), b (i), c (i), d (i)
end for


.. and here is the file:

code:
-2100 maxx 0 10
 100 250 140 150
 500 650 255 265
 900 1050 390 400
 400 maxx 500 510
 -250 -200 530 540
 -360 -310 650 660
 -920 -650 660 670
 -1050 -1040 600 610
 -1180 -1170 540 550
 -1310 -1300 480 490
 -1440 -1430 420 430
 -1570 -1560 360 370
 -1900 -1690 230 240
 -1900 -1800 330 340
 -1790 -1690 450 460
 -1900 -1800 550 560
 -1790 -1690 670 680
 -2100 -1900 790 800
 -1900 -300 890 900
 -275 -265 890 900
 -240 0 890 900
 70 1200 900 910
1200 maxx 760 770
 -350 -320 550 560
 -350 -320 410 420
 -350 -320 270 280
 -350 -320 130 140


However it doesn't work and I'm not sure why.

Also if anyone could give me a detailed outline on how to format levels into the program, that would be great!
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 17 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: