Hey everyone,
I'm have a bit of trouble with my animated sprite in DBP. My issue is that he doesn't animate well. He seems to do twirls or moon walks. I've tried resetting the frame and it doesn't seem to help. I need to change the sprite sheet itself but for my issue it doesn't matter.
I've included my media as well. Any help on the matter is greatly appreciated
Cheers,
Christian
Setup:
set image colorkey 255,255,255
`allows user to setup screen to desired x and y
print "1. 1024 X 768" : Print "2. 1280 X 800"
input "Please pick your display size",UserInput
`checks for all input
if UserInput = 1
UserScreenX = 1024
UserScreenY = 768
goto StartMain
endif
if UserInput = 2
UserScreenX = 1280
UserScreenY = 800
goto StartMain
endif
if UserInput > 2
Print "Invalid Selection try again"
goto Setup
endif
StartMain:
`sets up screen constants
#constant SCREENSIZEX = UserScreenX
#constant SCREENSIZEY = UserScreenY
set display mode SCREENSIZEX,SCREENSIZEY,32
Main:
gosub DeclareVariables
gosub ScreenSetup
gosub CreateImages
gosub LoadData
gosub MakePlayer
`start the main loop
`play music 1
do
gosub Controls
gosub SpriteIntermediate
gosub Collision
gosub ScrollScreen
gosub Displayplayer
`update screen
sync
loop
end
gosub Cleanup
`end the program
DeclareVariables:
`store how many horizontal/verticle tiles there are
NumOfTilesX = 40
NumOfTilesY = 30
`store the size (in pixels) of a tile
TileSizeX = 32
TileSizeY = 32
PlayerSizeX = 32
PlayerSizeY = 64
#Constant DISPLACEMENTY = PlayerSizeY - TileSizeY
#Constant DISPLACEMENTX = PlayerSizeX - TileSizeX
`store the speed of the tile
SpeedX = 3
SpeedY = 3
`store the coordinates that will make the player be at the centre of the screen
ScreenCentreX = SCREENSIZEX / 2
ScreenCentreY = SCREENSIZEY / 2
`Work out out how many tiles there are on screen for x and y directions
TilesPerScreenX = SCREENSIZEX / TileSizeX
TilesPerScreenY = SCREENSIZEY / TileSizeY
`Works out if a tile is partially visible on screen, if so then add 1 to the
`TilesPerScreen variable.
if SCREENSIZEX mod TileSizeX then TilesPerScreenX = TilesPerScreenX + 1
if SCREENSIZEY mod TileSizeY then TilesPerScreenY = TilesPerScreenY + 1
`Calculates the largest co-ordinates the map can scroll too
MaxMapX = (TileSizeX * NumOfTilesX) - (SCREENSIZEX + SpeedX)
MaxMapY = (TileSizeY * NumOfTilesY) - (SCREENSIZEY + SpeedY)
`store predefined coordinates of player
X1 = ScreenCentreX
Y1 = ScreenCentreY
`store predefined coordinates of map
MapX = 0
MapY = 0
TempLeft = 0
TempRight = 0
TempUp = 0
TempDown = 0
return
ScreenSetup:
`delete anything stored in video memory
flush video memory
`setup syncing
sync on
sync rate 60
return
CreateImages:
`create a green box
Load image "media/Grass1.png",1
load image "media/sprite1.png",2
set image colorkey 255,255,255
sync
`create border
cls rgb(255, 255, 255)
get image 10, 0, 0, TileSizeX, TileSizeY, 1
load image "media/wall1.png",11
`load music "media/music/Overture.wav",1
return
LoadData:
dim TileMap(NumOfTilesX - 1, NumOfTilesY - 1)
for y = 0 to NumOfTilesY - 1
for x = 0 to NumOfTilesX - 1
read TileMap(x, y)
next x
next y
return
MakePlayer:
`make the player
create animated sprite 1,"media/sprite1.png",20,1,2
set image colorkey 255,255,255
Sprite 1, X1, Y1,1
set sprite 1, 0, 1
sync
return
Controls:
`store the map's x and y coordinates into the variables OldMapX and OldMapY
OldMapX = MapX
OldMapY = MapY
`store the x and y coordinates of the player into the variables oldX1 and oldY1
OldX1 = X1
OldY1 = Y1
`makes the player move by altering the sprites coordinates
`makes the map move by altering the maps coordinates, if the player is roughly at the
`centre of the screen
if leftkey() = 1
play sprite 1,11,15,200
if MapX < X1 - (ScreenCentreX - 5) and MapX > X1 - (ScreenCentreX + 5) then MapX = MapX - SpeedX
X1 = X1 - SpeedX
TempLeft = 1
`set sprite frame 1,11
endif
if rightkey() = 1
play sprite 1,16,20,200
if MapX < X1 - (ScreenCentreX - 5) and MapX > X1 - (ScreenCentreX + 5) then MapX = MapX + SpeedX
X1 = X1 + SpeedX
TempRight = 1
`set sprite frame 1,16
endif
if upkey() = 1
set sprite frame 1,6
play sprite 1,6,10,200
if MapY < Y1 - (ScreenCentreY - 5) and MapY > Y1 - (ScreenCentreY + 5) then MapY = MapY - SpeedY
Y1 = Y1 - SpeedY
TempUp = 1
`set sprite frame 1,6
endif
if downkey() = 1
set sprite frame 1,1
play sprite 1,1,5,200
if MapY < Y1 - (ScreenCentreY - 5) and MapY > Y1 - (ScreenCentreY + 5) then MapY = MapY + SpeedY
Y1 = Y1 + SpeedY
TempDown = 1
`set sprite frame 1,1
endif
return
SpriteIntermediate:
if TempLeft = 1 and leftkey()=0
set sprite frame 1,11
endif
if TempRight = 1 and rightkey() = 0
set sprite frame 1,16
endif
if TempUp = 1 and upkey() = 0
set sprite frame 1,6
endif
if TempDown = 1 and downkey() = 0
set sprite frame 1,1
endif
TempLeft = 0
TempRight = 0
TempUp = 0
TempDown = 0
return
Collision:
`gets which tiles the current left and right co-ordinates of the player are on
LeftTileNum = int((X1) / TileSizeX)
RightTileNum = int((X1 + (TileSizeX - 1)) / TileSizeX)
LeftTileNum2 = LeftTileNum
RightTileNum2 = RightTileNum
`gets which tiles the current top and bottom co-ordinates of the player are on
TopTileNum = int(Y1 / TileSizeY)
BottomTileNum = int((Y1 + (TileSizeY - 1)) / TileSizeY)
TopTileNum2 = TopTileNum + 1
BottomTileNum2 = BottomTileNum + 1
`gets which tiles the old left and right co-ordinates of the player are on
OldLeftTileNum = int(oldX1 / TileSizeX)
OldRightTileNum = int((oldX1 + (TileSizeX - 1)) / TileSizeX)
OldLeftTileNum2 = OldLeftTileNum
OldRightTileNum2 = OldRightTileNum
`gets which tiles the old top and bottom co-ordinates of the player are on
OldTopTileNum = int(oldY1 / TileSizey)
OldBottomTileNum = int((oldY1 + (TileSizeY - 1)) / TileSizeY)
OldTopTileNum2 = OldTopTileNum + 1
OldBottomTileNum2 = OldBottomTileNum + 1
`checks for top-left collision point
if TileMap(LeftTileNum, OldTopTileNum) >= 10 then X1 = oldX1 : MapX = OldMapX
if TileMap(OldLeftTileNum, TopTileNum) >= 10 then Y1 = oldY1 : MapY = OldMapY
`checks for top-right collision point
if TileMap(RightTileNum, OldTopTileNum) >= 10 then X1 = oldX1 : MapX = OldMapX
if TileMap(OldRightTileNum, TopTileNum) >= 10 then Y1 = oldY1 : MapY = OldMapY
`checks for bottom-left collision point
if TileMap(LeftTileNum, OldBottomTileNum) >= 10 then X1 = oldX1 : MapX = OldMapX
if TileMap(OldLeftTileNum, BottomTileNum) >= 10 then Y1 = oldY1 : MapY = OldMapY
`checks for bottom-right collision point
if TileMap(RightTileNum, OldBottomTileNum) >= 10 then X1 = oldX1 : MapX = OldMapX
if TileMap(OldRightTileNum, BottomTileNum) >= 10 then Y1 = oldY1 : MapY = OldMapY
`-----------------------------------------
`checks for top-left collision point
if TileMap(LeftTileNum2, OldTopTileNum2) >= 10 then X1 = oldX1
if TileMap(OldLeftTileNum2, TopTileNum2) >= 10 then Y1 = oldY1
`checks for top-right collision point
if TileMap(RightTileNum2, OldTopTileNum2) >= 10 then X1 = oldX1
if TileMap(OldRightTileNum2, TopTileNum2) >= 10 then Y1 = oldY1
`checks for bottom-left collision point
if TileMap(LeftTileNum2, OldBottomTileNum2) >= 10 then X1 = oldX1
if TileMap(OldLeftTileNum2, BottomTileNum2) >= 10 then Y1 = oldY1
`checks for bottom-right collision point
if TileMap(RightTileNum2, OldBottomTileNum2) >= 10 then X1 = oldX1
if TileMap(OldRightTileNum2, BottomTileNum2) >= 10 then Y1 = oldY1
if MapX < 0 then MapX = 0
if MapX >= MaxMapX then MapX = MaxMapX
if MapY < 0 then MapY = 0
if MapY >= MaxMapY then MapY = MaxMapY
return
ScrollScreen:
`works out the tile number within array to start displaying tiles at
TileNumX = MapX / TileSizeX
TileNumY = MapY / TileSizeY
`cache calculation to tempX and TempY variables for a minor speed increase
`this calculation works out how many pixels to offset the position where the tile
`images should be placed.
TempX = MapX - (TileNumX * TileSizeX)
TempY = MapY - (TileNumY * TileSizeY)
`reset values
Temp1 = 0
Temp2 = 0
`Calculates if the map has scrolled to the last tile, if so we must remove a column
`or/and row of tiles from the screen as they have no more map data to draw; also
`because of this it would create an error
if TilesPerScreenX + TileNumX >= NumOfTilesX then Temp1 = 1
if TilesPerScreenY + TileNumY >= NumOfTilesY then Temp2 = 1
`this pastes the images to screen in the correct order by working out where to start
`pasting the images from in the array, then displaying the images at the correct
`screen coordinates
for y = 0 to TilesPerScreenY - Temp2
for x = 0 to TilesPerScreenX - Temp1
paste image TileMap(x + TileNumX, y + TileNumY), (x * TileSizeX) - TempX, (y * TileSizeY) - TempY
next x
next y
return
DisplayPlayer:
`display sprite
sprite 1,X1 - MapX,Y1 - MapY,2
return
Cleanup:
`delete any media involved in this program
for x = 1 to 11
if image exist(x) then delete image x
if sprite exist(x) then delete sprite x
next x
`delete anything thats left in the video memory - if anything
flush video memory
return
`store tile data at the end of the program so its out of the way
data 10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10
data 10, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,10
data 10, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,10
data 10, 1, 1,11,11, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,11,11, 1, 1, 1, 1, 1, 1,11,11, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,11,11, 1, 1,10
data 10, 1, 1,11,11, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,11,11, 1, 1, 1, 1, 1, 1,11,11, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,11,11, 1, 1,10
data 10, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,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11, 1, 1, 1, 1, 1, 1,11, 1,11, 1, 1, 1, 1, 1, 1, 1, 1,11, 1,11, 1, 1,10
data 10, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,10
data 10, 1, 1,11,11, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,11,11, 1, 1, 1, 1, 1, 1,11,11, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,11,11, 1, 1,10
data 10, 1, 1,11,11, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,11,11, 1, 1, 1, 1, 1, 1,11,11, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,11,11, 1, 1,10
data 10, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,10
data 10, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,10
data 10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10
The most relevant parts of the code would be here:
MakePlayer:
`make the player
create animated sprite 1,"media/sprite1.png",20,1,2
set image colorkey 255,255,255
Sprite 1, X1, Y1,1
set sprite 1, 0, 1
sync
return
As well as:
if leftkey() = 1
play sprite 1,11,15,200
if MapX < X1 - (ScreenCentreX - 5) and MapX > X1 - (ScreenCentreX + 5) then MapX = MapX - SpeedX
X1 = X1 - SpeedX
TempLeft = 1
`set sprite frame 1,11
endif
if rightkey() = 1
play sprite 1,16,20,200
if MapX < X1 - (ScreenCentreX - 5) and MapX > X1 - (ScreenCentreX + 5) then MapX = MapX + SpeedX
X1 = X1 + SpeedX
TempRight = 1
`set sprite frame 1,16
endif
if upkey() = 1
set sprite frame 1,6
play sprite 1,6,10,200
if MapY < Y1 - (ScreenCentreY - 5) and MapY > Y1 - (ScreenCentreY + 5) then MapY = MapY - SpeedY
Y1 = Y1 - SpeedY
TempUp = 1
`set sprite frame 1,6
endif
if downkey() = 1
set sprite frame 1,1
play sprite 1,1,5,200
if MapY < Y1 - (ScreenCentreY - 5) and MapY > Y1 - (ScreenCentreY + 5) then MapY = MapY + SpeedY
Y1 = Y1 + SpeedY
TempDown = 1
`set sprite frame 1,1
endif
return
SpriteIntermediate:
if TempLeft = 1 and leftkey()=0
set sprite frame 1,11
endif
if TempRight = 1 and rightkey() = 0
set sprite frame 1,16
endif
if TempUp = 1 and upkey() = 0
set sprite frame 1,6
endif
if TempDown = 1 and downkey() = 0
set sprite frame 1,1
endif
TempLeft = 0
TempRight = 0
TempUp = 0
TempDown = 0
return