News:

Function Finder  Find all the functions within source code files

Main Menu

Sonic the Hedgehog Loop Mock Up

Started by kevin, June 05, 2012, 02:47:18 AM

Previous topic - Next topic

kevin


Sonic the Hedgehog Loop Mock Up


  In this demo, the sonic character runs through the screen and around  the loop.   It works by simply aligning the character to a path, the tricky bit (in this example) was creating a path that matched the backdrop picture or there abouts.  


Video




PlayBASIC Code: [Select]
; PROJECT : Sonic_Loop_Mock_Up
; AUTHOR : Kevin Picone
; CREATED : 12/27/2011
; EDITED : 12/27/2011
; ---------------------------------------------------------------------


// Load the backdrop picture
loadimage "gfx\happybirthdaysonic_249856.jpg",1

// load the walking frames
Dim WalkFrames(6)
For lp =0 to 6
WalkFrames(lp)=LoadNewFXIMage("gfx\sonicwalk"+str$(lp+1)+".bmp")
next


// declare type to hold our points along our loop
Type Vector2D
x#,y#
EndType

// delcare an array
Dim Points(100) as Vector2D


// Set the Base location of the loop curse in the mock up demo
basex=340
basey=190


// Make the path that represents the loop in the mock up
PointCount=Make_Vert_List(BAseX,BaseY)


// define the Player structure
Type tPlayer
Segment
Dist#
x#
y#
sprite
frameIndex
CurrentAngle#
EndType


; create player variable
Dim player as tplayer

; init it's starting segments and distance moved
PLayer.Segment=0
PLayer.Dist#=0

; init the sprite the player will use for it's visual representation
iw=GetIMageWidth(WalkFrames(0))
ih=GetIMageHeight(WalkFrames(0))
Spr=NewSprite(0,0,WalkFrames(0))
SpriteDrawMode Spr,2
SpriteHandle Spr,iw/-2,-ih
scalesprite spr,1.5
spritefilter spr,on

; store the sprite index in the player structure
Player.Sprite=Spr


; set PB to limit the program to a max of 60 frames per second
setfps 60


; ------------------------------------------------------------------------
; ------------------------>> Main Loop <<---------------------------------
; ------------------------------------------------------------------------

do

; clear screen to black
cls


; draw the backdrop
DrawImage 1,0,0,false

// ----------------------------------------
// Show Path
// ----------------------------------------
lockbuffer
for lp =1 to PointCount
oldx#=points(lp-1).x
oldy#=points(lp-1).y
x#=points(lp).x
y#=points(lp).y
linec oldx#,oldy#,x#,y#,$ffffff
next
unlockbuffer

// ----------------------------------------
// move player
// ----------------------------------------
repeat

MoveFlag=true
oldx#=points(PLayer.Segment).x#
oldy#=points(PLayer.Segment).y#
nextx#=points(PLayer.Segment+1).x#
nexty#=points(PLayer.Segment+1).y#
SegLength#=GetDistance2D(oldx#,oldy#,nextx#,nexty#)

if PLayer.dist#>SegLength#
moveFlag=false
PLayer.dist#-=SegLength#
PLayer.Segment++
if PLayer.Segment=>PointCount
PLayer.Segment=0
Player.dist#=0
endif

endif

until MoveFlag=true


// calc normal for our movement vector
nx#=(Nextx#-oldx#)/SegLength#
ny#=(Nexty#-oldy#)/SegLength#

// get the players current screen position
x#=oldx#+nx#*PLayer.dist#
y#=oldy#+ny#*PLayer.dist#

// draw a circle at this position
circle x#,y#,5


// set the sprites current animation frame
SpriteImage PLayer.Sprite, WalkFRames(player.frameIndex)
PLayer.FrameIndex=mod(PLayer.FrameIndex+1,7)


// position the player sprite at players current position
PositionSprite Player.sprite,x#,y#


// get the angle of the floor in this section
FloorAngle#=getangle2d(0,0,nx#,ny#)

/// interpolate the players current angle with and change of angle
Login required to view complete source code



Related To

 This was originally posted in using rotate sprite to animate ambience circles



Download

 Source Code and PlayBASIC project Attached