@ron77
You only need one SPRITE class
Code below is a move in that direction.
Remember literals are not a good thing. You need to be able to handle different sized sprites.
Attached are larger sprites as they are easier for me to see :)
If you want to try the large sprites just edit these lines,
Code: Select all
const SPRW = 24
const SPRH = 22
'const SPRW = 48
'const SPRH = 44
and
Code: Select all
sheet = imagecreate( 288, 22 )
BLOAD "sprites.bmp", sheet
'sheet = imagecreate( 576, 78 )
'BLOAD "warriorLarge.bmp", sheet
Code: Select all
#INCLUDE "fbgfx.bi"
using fb
const SPRW = 24
const SPRH = 22
'const SPRW = 48
'const SPRH = 44
const screen_w = 640
const screen_h = 480
type SPRITE
x as single
y as single
speed as single
frame as long
direction as long
xpos as integer 'position on sprite sheet
move as boolean
attack as long
alive as long
sheet as any ptr
declare sub chase(s as SPRITE)
declare sub Initialize(sheet as any ptr, x as single, y as single, s as single, d as long)
declare sub Update()
declare sub Draw()
end type
SUB SPRITE.chase(s as SPRITE)
IF this.x < s.x THEN
this.x = this.x + this.speed
this.direction = 1
this.move = TRUE
END IF
IF this.x > s.x THEN
this.x = this.x - this.speed
this.direction = 2
this.move = TRUE
END IF
IF this.y < s.y THEN
this.y = this.y + this.speed
this.direction = 3
this.move = TRUE
END IF
IF this.y > s.y THEN
this.y = this.y - this.speed
this.direction = 4
this.move = TRUE
END IF
END SUB
Sub SPRITE.Initialize(sheet as any ptr, x as single,y as single,s as single,d as long)
this.x = x
this.y = y
this.speed = s
this.direction = d
this.sheet = sheet
End SUb
sub SPRITE.Draw()
put( this.x, this.y ), this.sheet, ( this.xpos + this.frame * SPRW, 0 )-( this.xpos + this.frame * SPRW + SPRW-1, SPRH-1 ), trans
end sub
sub SPRITE.update()
'test for border overlap
if( this.x < 0 ) then
this.move = false
this.x = 0
else
if( this.x > screen_w - SPRW ) then
this.move = false
this.x = screen_w - SPRW
end if
end if
if( this.y < 0 ) then
this.move = false
this.y = 0
else
if( this.y > screen_h - SPRH ) then
this.move = false
this.y = screen_h - SPRH
end if
end if
'set xpos number for given direction
if( this.direction = 1 ) then this.xpos = 6 * SPRW
if( this.direction = 2 ) then this.xpos = 4 * SPRW
if( this.direction = 3 ) then this.xpos = 0 * SPRW
if( this.direction = 4 ) then this.xpos = 2 * SPRW
'update frame
this.frame = this.frame + 1
if this.frame=2 then this.frame = 0
end sub
DIM player as SPRITE
DIM enemy AS SPRITE
screenres( screen_w, screen_h, 32 )
color rgb( 0, 0, 0), rgb( 120, 120, 120 ) : cls
dim as any ptr sheet
sheet = imagecreate( 288, 22 )
BLOAD "sprites.bmp", sheet
'sheet = imagecreate( 576, 78 )
'BLOAD "warriorLarge.bmp", sheet
player.Initialize(sheet,150,90,1,1)
enemy.Initialize(sheet,20,10,0.5,1)
dim as double start_time
start_time = timer()
do
'USER INPUT
player.move = false
if( multikey( SC_RIGHT ) ) then
player.x = player.x + player.speed
player.direction = 1
player.move = true
else
if( multikey( SC_LEFT ) ) then
player.x = player.x - player.speed
player.direction = 2
player.move = true
end if
end if
if( multikey( SC_DOWN ) ) then
player.y = player.y + player.speed
player.direction = 3
player.move = true
else
if( multikey( SC_UP ) ) then
player.y = player.y - player.speed
player.direction = 4
player.move = true
end if
end if
if( timer() - start_time > 0.05 ) then ' next frame
start_time = timer() ' reset start_time
player.update()
enemy.update()
screenlock()
cls
player.Draw()
enemy.Draw()
screenunlock()
end if
sleep( 15 )
enemy.CHASE(player) 'chase the player
loop until multikey(SC_ESCAPE) ' escape key