Computer Science Canada

Platformer Help.

Author:  Hijoxizmo [ Wed Oct 28, 2009 12:53 pm ]
Post subject:  Platformer Help.

What is it you are trying to achieve?
I am trying to make a platform game.


What is the problem you are having?
My character will not jump onto the platform, also it snaps onto the platform.


Describe what you have tried to solve this problem
I have tried different if statements, but I can't get anything to work.


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

Turing:


% To create a platform game
% Written By: Matt McMurray
% Date: Oct. 19, 2009

% Constants
const GROUND_HEIGHT := 5
const GROUND_TOP := 50
const LEFT_WALL := 11
const RIGHT_WALL := 628
const RUN_SPEED := 10
const JUMP_SPEED := 30
var GRAVITY := 2
const PLATFORM_1H := 50
const PLATFORM_1L := 250
const PLATFORM_1R := 400

% Window and Graphics
var win := Window.Open ("graphics:640;480,offscreenonly")
var chars : array char of boolean


% player position and velocity


var posx, posy, posmx, posmy : int
var velx, vely : real

posx := 20
velx := 0
posy := 400
vely := 0
posmx := 400
posmy := 50

loop
    Input.KeyDown (chars)
    if chars ('q') then

        exit
        % Trying to pause the game
    elsif chars ('p') then
        pause (10000)
    end if

    % Movement
    if chars (KEY_LEFT_ARROW) then
        velx := -RUN_SPEED
    elsif chars (KEY_RIGHT_ARROW) then
        velx := RUN_SPEED
    else
        velx := 0
    end if

    if chars (KEY_UP_ARROW) and posy = GROUND_HEIGHT then
        vely := JUMP_SPEED
    elsif chars (KEY_UP_ARROW) and posy = PLATFORM_1H then
        vely := JUMP_SPEED
   
    end if

    % Gravity
    vely -= GRAVITY
    posx += round (velx)
    posy += round (vely)

    % Collision
    if posy < GROUND_HEIGHT then
        posy := GROUND_HEIGHT
        vely := 0
    end if

    if posx > RIGHT_WALL - 21 then
        posx := RIGHT_WALL - 21
        velx := 0
    end if

    if posx < LEFT_WALL then
        posx := LEFT_WALL
        velx := 0
    end if

    if posy > PLATFORM_1H and posx > PLATFORM_1L and posx < PLATFORM_1R then
        posy := PLATFORM_1H
        vely := 10
        velx := 0
        GRAVITY := 2
    end if
   
   
   


    % Character
    drawfillbox (posx - 0, posy, posx + 20, posy + 25, white)

    % Background
    colourback (black)

    % Platforms
    Draw.Line (PLATFORM_1L, PLATFORM_1H, PLATFORM_1R, PLATFORM_1H, white)


    % Walls
    Draw.Line (LEFT_WALL, GROUND_HEIGHT, LEFT_WALL, maxy, white)
    Draw.Line (RIGHT_WALL, GROUND_HEIGHT, RIGHT_WALL, maxy, white)


    % Bottom line
    Draw.Line (LEFT_WALL, GROUND_HEIGHT, RIGHT_WALL, GROUND_HEIGHT, white)
    View.Update
    delay (15)
    cls

end loop





Please specify what version of Turing you are using
4.1 for windows

Author:  DemonWasp [ Wed Oct 28, 2009 1:19 pm ]
Post subject:  RE:Platformer Help.

If you look carefully at the code for detecting when a player is on the platform, you'll find that the code for "snapping" him onto the platform runs whenever he's above the platform, no matter how far above (you detect that he's above, but not by how much). I would suggest the following:

A player snaps to a platform whenever:
- They are at the platform's X location (done!)
- They are currently above the platform (done!)
- They would go below the platform on this turn (todo)

Author:  Hijoxizmo [ Thu Oct 29, 2009 11:59 am ]
Post subject:  Re: Platformer Help.

Update: I have fixed it. You can jump onto the platform and you will not snap onto it. Although the player cannot go underneath the platform. It acts like a hill.

To see what I am talking about use the right arrow key to try and go underneath the platform. You sail right over top. Go back using the left arrow key and the same thing happens.

New Code:


% To create a platform game
% Written By: Matt McMurray
% Date: Oct. 19, 2009

% Constants
const GROUND_HEIGHT := 5
const GROUND_TOP := 50
const LEFT_WALL := 11
const RIGHT_WALL := 628
const RUN_SPEED := 10
const JUMP_SPEED := 30
var GRAVITY := 2
const PLATFORM_1H := 50
const PLATFORM_1L := 250
const PLATFORM_1R := 400

% Window and Graphics
var win := Window.Open ("graphics:640;480,offscreenonly")
var chars : array char of boolean


% player position and velocity


var posx, posy, posmx, posmy : int
var velx, vely : real

posx := 20
velx := 0
posy := 400
vely := 0
posmx := 400
posmy := 50

loop
Input.KeyDown (chars)
if chars ('q') then

exit
% Trying to pause the game
elsif chars ('p') then
pause (10000)
end if

% Movement
if chars (KEY_LEFT_ARROW) then
velx := -RUN_SPEED
elsif chars (KEY_RIGHT_ARROW) then
velx := RUN_SPEED
else
velx := 0
end if

if chars (KEY_UP_ARROW) and posy = GROUND_HEIGHT then
vely := JUMP_SPEED
elsif chars (KEY_UP_ARROW) and posy = PLATFORM_1H and posx > PLATFORM_1L and posx < PLATFORM_1R then
vely := JUMP_SPEED

end if

% Gravity
vely -= GRAVITY
posx += round (velx)
posy += round (vely)

% Collision
if posy < GROUND_HEIGHT then
posy := GROUND_HEIGHT
vely := 0
end if

if posx > RIGHT_WALL - 21 then
posx := RIGHT_WALL - 21
velx := 0
end if

if posx < LEFT_WALL then
posx := LEFT_WALL
velx := 0
end if

if posy < PLATFORM_1H and posx > PLATFORM_1L - 20 and posx < PLATFORM_1R then
posy := PLATFORM_1H
vely := 0
velx := 0
GRAVITY := 2
end if
% trying to go under platform
if posy = GROUND_HEIGHT and posx > PLATFORM_1L - 20 and posx < PLATFORM_1R then
posy := GROUND_HEIGHT
vely := 0
velx := 0
GRAVITY := 2
end if

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Character
drawfillbox (posx - 0, posy, posx + 20, posy + 25, white)

% Background
colourback (black)

% Platforms
Draw.Line (PLATFORM_1L, PLATFORM_1H, PLATFORM_1R, PLATFORM_1H, white)


% Walls
Draw.Line (LEFT_WALL, GROUND_HEIGHT, LEFT_WALL, maxy, white)
Draw.Line (RIGHT_WALL, GROUND_HEIGHT, RIGHT_WALL, maxy, white)


% Bottom line
Draw.Line (LEFT_WALL, GROUND_HEIGHT, RIGHT_WALL, GROUND_HEIGHT, white)
View.Update
delay (15)
cls

end loop

Author:  TheGuardian001 [ Thu Oct 29, 2009 10:33 pm ]
Post subject:  Re: Platformer Help.

Hijoxizmo @ Thu Oct 29, 2009 11:59 am wrote:

if posy < PLATFORM_1H and posx > PLATFORM_1L - 20 and posx < PLATFORM_1R then
posy := PLATFORM_1H
vely := 0
velx := 0
GRAVITY := 2
end if


In english:


if player is below platform and player between platform sides
move player onto platform
no more velocity
gravity = 2
end if

you are using < to check if the player is below the platform, then, if they are below it, moving them onto the platform. You want to be checking if they are above the platform, or rather if they are actually on the platform/within a certain range of the platform.

There was nothing wrong with the original check to see if they were should land on the platform, the only problem was that you were checking for everything below the platform's height. You just need to be more specific about where the platform is. It currently takes up everything from the white line down, when it should take up only the white line.

Author:  Hijoxizmo [ Fri Oct 30, 2009 11:51 am ]
Post subject:  RE:Platformer Help.

Would anyone mind writing some code for me?

Author:  Leakime [ Fri Oct 30, 2009 6:51 pm ]
Post subject:  RE:Platformer Help.

Hey I'm Hijoxizmo's partner and I figured it out. Thanks for the advice TheGuardian001!


: