UnderwareDESIGN

PlayBASIC => Resources => Source Codes => Topic started by: LemonWizard on June 01, 2009, 05:28:46 AM

Title: 2d sidescroller engine with basic gravity
Post by: LemonWizard on June 01, 2009, 05:28:46 AM
This is a 2d sidescroller engine with basic gravity, no camera, and no external gfx.
Nothing special. Atleast not yet.
Just a skeleton I am going to build on.
I used the checkmapimpact example, because why re code something that's already done.
=p
The rest of it though, the gravity the player sprite, and the map, the tiles. those were hand coded.
Enjoy.
Also, comments are always nice.

[pbcode]
; PROJECT : Gravity
; AUTHOR  : LemonWizard
; CREATED : 5/31/2009
; EDITED  : 6/1/2009
; ---------------------------------------------------------------------
openscreen 640, 640, 32, 1
map=newmap(1)
createmapGFX map, 32, 32, 2, rgb(0, 0, 0)
rendertoscreen
cls rgb(0, 255, 0)
getmapblk map, 1, 0, 0
cls rgb (255, 0, 0)
getmapblk map, 2, 0, 0
cls rgb(0,0,0)
sprite=newimage(32, 32)
rendertoimage sprite
ink rgb(20, 100, 255)
cls rgb(255,255,255)
circle 10, 10, 10, 1
;imagemaskcolour sprite, rgb(0,0,0)
rendertoscreen
spx=5*32
spy=5*32
setfps 60
speed=3
PLayerWidth=32
PlayerHeight=32
jump=false
gravity=true

spr=newsprite(spx, spy, sprite)
spritecollisionmode spr, 6
positionsprite spr, spx, spy

level=newlevel(map, 20, 20)
for temp1=0 to 20
   pokeleveltile map, level, temp1, 0, 1
   pokeleveltile map, level, temp1, 19, 1
   next temp1
      
      for temp1=0 to 20
         pokeleveltile map, level, 0, temp1, 2
         pokeleveltile map, level, 19, temp1, 2
      next temp1
      
      pokeleveltile map, level, 5, 17, 1
      pokeleveltile map, level, 6, 17, 1
      pokeleveltile map, level, 7, 17, 2
      pokeleveltile map, level, 8, 17, 2
      
Main:
if jtime >0 then gravity=false
if jtime=0 then gravity=true
         PlayerNewX=spx
         PlayerNewY=spy
   
  ; Handle the players Movement
    If UpKey() then gosub jumpset
    if gravity=true then PlayerNewY=spy+Speed
    If LeftKey() Then PlayerNewX=spx-Speed
    If RightKey() Then PlayerNewX=spx+Speed
   
    if jump=true
       jtime=jtime-1
       PlayerNewY=PlayerNewY-speed
       if jtime=0
       jump=false
       fall=true
       endif
       
       endif
   
   
  ; ==========================
  ; Handle Collision
  ; ===========================
   
    If CheckMapImpact(map,level,spx,spy,PlayerNewX,PlayerNewY,PLayerWidth,PlayerHeight)
     ; If there was impact with the map, then read our
     ; new position back.
       spx=GetMapImpactX()
       spy=GetMapImpactY()
       
     ; Check what side the impact occured upon
     ; and display a message
       
       If GetMapImpactLeft()
          Print "Impact occured on the LEFT side"
       EndIf
       
       If GetMapImpactRight()
          Print "Impact occured on the RIGHT side"
       EndIf
       
       If GetMapImpactTop()
          Print "Impact occured on the TOP side"
       EndIf
       
       If GetMapImpactBot()
          Print "Impact occured on the Bottom side"
          fall=false
       EndIf
    Else
     ; No Collision, Then set the current players
     ; position to it's new position
       spx=PlayerNewX
       spy=PlayerNewY
       gravity=true
       fall=true
    EndIf

         
positionsprite spr, spx, spy

drawmap map, level, 0, 0
drawallsprites
sync

goto main

jumpset:
if jump=true then return
if jtime >0 then return
if fall=true then return
jtime=30
jump=true
return



[/pbcode]

Title: Re: 2d sidescroller engine with basic gravity
Post by: Big C. on July 17, 2012, 12:18:08 PM
One Hint: The Line

createmapGFX map, 32, 32, 2, rgb(0, 0, 0)

produce the fatal error: Map Block Index '2' out of Range (0 to 1). you have to correct the number of tiles from 2 to 3 to get the code running ==> createmapGFX map, 32, 32, 3, rgb(0, 0, 0)

Title: Re: 2d sidescroller engine with basic gravity
Post by: stevmjon on July 21, 2012, 07:22:53 AM
nice catch big c.

hey lemonwizard.
for fun i added a smooth jump and fall to the sprite. the fall also increases speed over time.
thought this would look cool and add to your project.

hope you like it,  stevmjon

Title: Re: 2d sidescroller engine with basic gravity
Post by: kevin on July 21, 2012, 11:08:13 AM

There's also stuff like Mario Demo / 2D Platform Game (http://www.underwaredesign.com/forums/index.php?topic=3101.0) hanging around the boards.