Here is a
// Project: platformer
// Created: 2015-09-06
// set window properties
SetWindowTitle( "platformer" )
SetWindowSize( 1024, 768, 0 )
// set display properties
SetVirtualResolution( 1024, 768 )
SetOrientationAllowed( 0, 0, 1, 0 )
SetClearColor( 50, 200, 250 )
// set physics properties
SetPhysicsGravity( 0, 50000 )
SetPhysicsWallBottom( 0 )
SetPhysicsWallTop ( 0 )
// create base ground and platforms
CreateSprite(1, LoadImage("ground_left.png"))
SetSpritePosition(1, x, 704)
SetSpritePhysicsOn(1, 1)
SetSpriteDepth(1, 20)
x = x + 64
for g = 2 to 19
CreateSprite(g, LoadImage("ground_middle.png"))
SetSpritePosition(g, x, 704)
SetSpritePhysicsOn(g, 1)
SetSpriteDepth(g, 20)
x = x + 64
next g
CreateSprite(20, LoadImage("ground_right.png"))
SetSpritePosition(20, x, 704)
SetSpritePhysicsOn(20, 1)
SetSpriteDepth(20, 20)
x = 384
CreateSprite(21, LoadImage("ground_left.png"))
SetSpritePosition(21, x, 650)
SetSpritePhysicsOn(21, 1)
SetSpriteDepth(21, 21)
x = x + 64
for h = 22 to 24
CreateSprite(h, LoadImage("ground_middle.png"))
SetSpritePosition(h, x, 650)
SetSpritePhysicsOn(h, 1)
SetSpriteDepth(h, 21)
x = x + 64
next h
CreateSprite(25, LoadImage("ground_right.png"))
SetSpritePosition(25, x, 650)
SetSpritePhysicsOn(25, 1)
SetSpriteDepth(25, 21)
x = 832
CreateSprite(26, LoadImage("ground_left.png"))
SetSpritePosition(26, x, 550)
SetSpritePhysicsOn(26, 1)
SetSpriteDepth(26, 22)
x = x + 64
for j = 27 to 29
CreateSprite(j, LoadImage("ground_middle.png"))
SetSpritePosition(j, x, 550)
SetSpritePhysicsOn(j, 1)
SetSpriteDepth(j, 22)
x = x + 64
next j
CreateSprite(30, LoadImage("ground_right.png"))
SetSpritePosition(30, x, 550)
SetSpritePhysicsOn(30, 1)
SetSpriteDepth(30, 22)
// create a sprite for the piglet with ID that has no image
CreateSprite ( 201, 0 )
SetSpritePosition ( 201, 400, 650 )
SetSpriteShapeCircle( 201, 0, 0, 20 )
SetSpritePhysicsOn ( 201, 2 )
SetSpriteVisible ( 201, 1 )
// add individual images into animation lists
AddSpriteAnimationFrame ( 201, LoadImage ( "piglet1_left_01.png" ) )
AddSpriteAnimationFrame ( 201, LoadImage ( "piglet1_left_02.png" ) )
AddSpriteAnimationFrame ( 201, LoadImage ( "piglet1_idle_01.png" ) )
AddSpriteAnimationFrame ( 201, LoadImage ( "piglet1_idle_02.png" ) )
AddSpriteAnimationFrame ( 201, LoadImage ( "piglet1_right_01.png" ) )
AddSpriteAnimationFrame ( 201, LoadImage ( "piglet1_right_02.png" ) )
PlaySprite ( 201, 10, 1, 1, 2 )
do
// set and check collisions for the ground
for c = 1 to 30
// if piglet is touching the ground while decending from a jump then set flag to 0 for ending the jump
if GetPhysicsCollision(201, c) and jumping = 2
jumping = 0
endif
// if piglet is below this section of ground then turn physics off
if GetSpriteY(201) > GetSpriteY(c) - 15
SetSpritePhysicsOff(c)
else
// else the ground section is under piglet, so turn physics back on
SetSpritePhysicsOn(c, 1)
endif
next c
// check to see if we have walked off the edge of the ground to prevent air jumps
falling = 0
for c = 1 to 30
if GetPhysicsCollision(201, c) = 0 and jumping = 0
falling = falling + 1
endif
next c
if falling = 30
jumping = 2
endif
// check for jump and set flag to 1 for jumping
if GetRawKeyPressed(38) = 1 and jumping = 0
jumping = 1
jump_start = GetSpriteY(201)
endif
//perform jump
if jumping = 1
// if piglet has not reached jump apex and player is still holding jump button then subtract Y position to keep rising
if GetSpriteY(201) > (jump_start - 108) and GetRawKeyState(38) = 1
SetSpritePosition(201, 480, GetSpriteY(201) - 16)
else
// piglet is decending from a jump, so set flag to 2 and let gravity pull piglet down
jumping = 2
endif
endif
// Check for left movement with left arrow key if we have not fallen off the screen
if GetRawKeyState(37) and GetSpriteY(201) < 770
// set piglet animation for left run
if GetSpriteCurrentFrame(201) > 2
PlaySprite ( 201, 10, 1, 1, 2 )
endif
// adjust the ground for left piglet movement
for p = 1 to 30
x = GetSpriteX(p)
y = GetSpriteY(p)
SetSpritePosition ( p, x + 5, y )
next p
// Check for right movement with right arrow key if we have not fallen off the screen
elseif GetRawKeyState(39) and GetSpriteY(201) < 770
// set piglet animation for right run
if GetSpriteCurrentFrame(201) < 5
PlaySprite ( 201, 10, 1, 5, 6 )
endif
// adjust the ground for right piglet movement
for p = 1 to 30
x = GetSpriteX(p)
y = GetSpriteY(p)
SetSpritePosition ( p, x - 5, y )
next p
// Not moving so set the piglet animation to idle
elseif GetRawKeyState(37) = 0 and GetRawKeyState(39) = 0
PlaySprite ( 201, 10, 1, 3, 4 )
endif
Print( ScreenFPS() )
// maintain piglet's balance and ceter screen position
SetSpritePosition(201, 480, GetSpriteY(201))
SetSpriteAngle(201,0)
Sync()
if GetRawKeyPressed(27) = 1 then end
loop
that looks big, but that is only because of the white space and comments used for explanation.
This example is not the best, and could surely be done better way.
However, this way uses physics for a basic setup, and didn't take too long to whip up.
I used for loops here to make the ground for this example, but I would do this differently for loading levels from data files.
So, focus on the jump stuff and collisions, because that is what the example is supposed to help with.
I wasn't happy with my jump the first time I attempted a platformer, so I have been meaning to give this another go.
It isn't much, but I hope it helps in some way.
I have attached a zip that has the source and some media for watching it go.
Coding things my way since 1981 -- Currently using AppGameKit V2 Tier 1