News:

Function Finder  Find all the functions within source code files

Main Menu

8 Direction / Way Movement Example

Started by kevin, August 06, 2008, 05:42:43 PM

Previous topic - Next topic

kevin

 This example demo's how you can control a character with the classic 8 way movement.  


PlayBASIC Code: [Select]
; limit Frame rate to a Max of 60 frames (redraws per second)
Setfps 60


Type tObject
X# ; Players X coordinate on the screen
Y# ; Players Y coordinate on the screen
Speed# ; Speed player is moveing
EndType

Dim Player as tObject
Player.X=getscreenwidth()/2
Player.Y=getscreenHeight()/2
Player.Speed=1


// -------------------------------------
// Start of Programs MAIN LOOP
// -------------------------------------
Do


// -------------------------------------
// First lets draw a shaded backdrop
// -------------------------------------
c1=rgb(100,20,40)
c2=rgb(20,220,40)
ShadeBox 0,0,getscreenWidth(),getscreenWidth(),c1,c1,c2,c2


// -------------------------------------
// Control Player (ARROW KEYS)
// -------------------------------------

if LeftKey()=true
// MOve the player left
Player.x#=Player.x#-PLayer.Speed#
endif

if RightKey()=true
// MOve the player RIGHT
Player.x#=Player.x#+PLayer.Speed#
endif

if UpKey()=true
// MOve the player UP
Player.Y#=Player.Y#-PLayer.Speed#
endif


if DownKey()=true
// MOve the player DOWN
Player.Y#=Player.Y#+PLayer.Speed#
endif

// -------------------------------------
// Draw Player as ellipse
// -------------------------------------
Ellipsec Player.x#,Player.Y#,50,100,true, Rgb(0,255,0)


// -------------------------------------
// DRaw the screen, and loop back to the do statement ot keep program running
// -------------------------------------

Sync
loop






ATLUS

#1
can add to him else run in left in right and shade downwards and else could jump=)

kevin

#2
 This example expands the previous demo and includes jumping.  ARROW keys to move and  LEFT CTRL key to jump


PlayBASIC Code: [Select]
; limit Frame rate to a Max of 60 frames (redraws per second)
Setfps 60


Type tObject
X# ; Objects X coordinate on the screen
Y# ; Objects Y coordinate on the screen
Speed# ; Speed Object is moving

Jumping ; Flag to indicate if the object uis jumping
JumpHeight# ; height (in pixels) this charcter can jump
JumpAngle# ; When jumping object moves this angle from 0 to 180 degrees,
; when the object is drawn, it displaced on the Y axis by SIN() of jump angle#

EndType

Dim Player as tObject
Player.X=getscreenwidth()/2
Player.Y=getscreenHeight()/2
Player.Speed=1

Player.JumpHeight# =100 ; object can jump 100 pixels



// -------------------------------------
// Start of Programs MAIN LOOP
// -------------------------------------
Do
setcursor 0,0

// -------------------------------------
// First lets draw a shaded backdrop
// -------------------------------------
c1=rgb(100,20,40)
c2=rgb(20,220,40)
ShadeBox 0,0,getscreenWidth(),getscreenWidth(),c1,c1,c2,c2


// -------------------------------------
// Control Player (ARROW KEYS)
// -------------------------------------

if LeftKey()=true
// MOve the player left
Player.x#=Player.x#-PLayer.Speed#
endif

if RightKey()=true
// MOve the player RIGHT
Player.x#=Player.x#+PLayer.Speed#
endif

if UpKey()=true
// MOve the player UP
Player.Y#=Player.Y#-PLayer.Speed#
endif


if DownKey()=true
// MOve the player DOWN
Player.Y#=Player.Y#+PLayer.Speed#
endif


// LEFT CTRL key to jump
if Ctrlkeys(1)=true
if player.Jumping=false
player.jumping=true
player.JumpAngle#=0
endif
endif

// Update the Jump angle
if Player.jumping=true
JumpAngle#=player.JumpAngle#+2
if JumpAngle#=>180
Player.Jumping=false
JumpAngle#=0
endif
Player.JumpAngle#=JumpAngle#
endif

// -------------------------------------
// Draw Player as ellipse
// -------------------------------------
;add the players jumping offset to the players screen position
Ypos#=PLayer.Y# -Sin(Player.JumpAngle#)*Player.JumpHeight#

Ellipsec Player.x#,Ypos#,50,100,true, Rgb(0,255,0)

print player.jumping
// -------------------------------------
// DRaw the screen, and loop back to the do statement ot keep program running
// -------------------------------------

Sync
loop






ATLUS

#3
 :o cool!!!!!!!!!!!!! Big thank you for code !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

+ shade = 3d effect

PlayBASIC Code: [Select]
; PROJECT : Project1
; AUTHOR : ATLUS
; CREATED : 08.08.2008
; ---------------------------------------------------------------------

; limit Frame rate to a Max of 60 frames (redraws per second)
Setfps 60


Type tObject
X# ; Objects X coordinate on the screen
Y# ; Objects Y coordinate on the screen
Speed# ; Speed Object is moving

Jumping ; Flag to indicate if the object uis jumping
JumpHeight# ; height (in pixels) this charcter can jump
JumpAngle# ; When jumping object moves this angle from 0 to 180 degrees,
; when the object is drawn, it displaced on the Y axis by SIN() of jump angle#

EndType
cirx#=getscreenwidth()/2
ciry#=getscreenHeight()/2
Dim Player as tObject
Player.X=getscreenwidth()/2
Player.Y=getscreenHeight()/2
Player.Speed=1

Player.JumpHeight# =100 ; object can jump 100 pixels



// -------------------------------------
// Start of Programs MAIN LOOP
// -------------------------------------
Do
setcursor 0,0

// -------------------------------------
// First lets draw a shaded backdrop
// -------------------------------------
c1=rgb(100,20,40)
c2=rgb(20,220,40)
ShadeBox 0,0,getscreenWidth(),getscreenWidth(),c1,c1,c2,c2


// -------------------------------------
// Control Player (ARROW KEYS)
// -------------------------------------

if LeftKey()=true
// MOve the player left
Player.x#=Player.x#-PLayer.Speed#
cirx#=cirx#-PLayer.Speed#
endif

if RightKey()=true
// MOve the player RIGHT
Player.x#=Player.x#+PLayer.Speed#
cirx#=cirx#+PLayer.Speed#
endif

if UpKey()=true
// MOve the player UP
Player.Y#=Player.Y#-PLayer.Speed#
ciry#=ciry#-PLayer.Speed#
endif


if DownKey()=true
// MOve the player DOWN
Player.Y#=Player.Y#+PLayer.Speed#
ciry#=ciry#+PLayer.Speed#
endif


// LEFT CTRL key to jump
if Ctrlkeys(1)=true
if player.Jumping=false
player.jumping=true
player.JumpAngle#=0
endif
endif

// Update the Jump angle
if Player.jumping=true
JumpAngle#=player.JumpAngle#+2
if JumpAngle#=>180
Player.Jumping=false
JumpAngle#=0
endif
Player.JumpAngle#=JumpAngle#
endif

// -------------------------------------
// Draw Player as ellipse
// -------------------------------------
;add the players jumping offset to the players screen position
Ypos#=PLayer.Y# -Sin(Player.JumpAngle#)*Player.JumpHeight#
Ellipsec cirx#,ciry#+100,50,10,true, Rgb(0,0,0)
Ellipsec Player.x#,Ypos#,50,100,true, Rgb(0,255,0)

print player.jumping
// -------------------------------------
// DRaw the screen, and loop back to the do statement ot keep program running
// -------------------------------------

Sync
loop