L'animation des sprites n'a peut être pas été compris par tout le monde.
Rappel : La planche du sprite représentant l'oiseau est composée de 4 rangée d'animations.
-La première rangée représente l'animation déplacement à gauche
-La seconde rangée représente l'animation déplacement à droite
-La quatrième rangée représente l'animation de face
Nota: Je ne me sert pas de la 3eme rangée.
■ Le code qui suit contient toutes les explications. Il affiche un oiseau que vous pouvez contrôler avec les flèches gauche ou droite.
Télécharger animation.pb (Source Pure Basic, Image et son)
Code : Tout sélectionner
EnableExplicit
Enumeration
#mainform
#background
#ground
#bird
#soundforest
EndEnumeration
Global Event
;Structure type d'un sprite: Les informations obligatoires sont précédés d'un *
Structure Sprite
Actif.b ; Actif
x.i ; * Position en X du sprite
y.i ; * Position en Y du sprite
Animation.i ; * Animation en cours
CurrentFrame.i ; Indique quelle image de l'animation est actuellement en cours
FrameWidth.i ; * Largeur de chaque image de l'animation
FrameHeight.i ; * hauteur de chaque image de l'animation
FrameMax.i ; * Nombre d'image maximum pour une animation
FrameRate.i ; * Transition entre deux images (Ms)
FrameTimeLife.i ; Indique la durée d'affichage de l'image en cours
VelocityX.i ; Vitesse déplacement en X
VelocityY.i ; Vitesse déplacement en Y
SpriteWidth.i ; * Largeur du sprite
SpriteHeight.i ; * Hauteur du sprite
Life.i ; Nombre de vie d'un sprite (Aucune utilité dans ce code)
Opacity.i ; * Opacité (0 à 255)
EndStructure
;On instance la struture sprite à l'oiseau.
Global bird.Sprite
;Définition de l'animation
;Chaque variable détermine le numéro de la rangée de l'image qui sera pris en compte pour l'animation
;0 Sera la premiere rangée.
Global AnimationLeft = 0 ;1er rangée de l'image
Global AnimationRight = 1 ;2eme rangée de l'image
Global AnimationFace = 3 ;4eme rangéee de l'image
;Une rangée sera multipliée par la hauteur d'une image frame pour determiner ou commence la rangée suivante.
Procedure Init()
Protected cr.b = #True
UseJPEGImageDecoder()
UsePNGImageDecoder()
If InitSprite() = 0 Or InitKeyboard() = 0 Or InitMouse() = 0 And InitSound() = 0
MessageRequester("Error", "Sprite system can't be initialized", 0)
cr=#False
EndIf
ProcedureReturn cr
EndProcedure
;Mise à jour du jeu
Procedure GameUpdate()
DisplaySprite(#background, 0, 0)
DisplaySprite(#ground, 0, 500)
;Mise à jour de l'oiseau
If ElapsedMilliseconds() - bird\FrameTimeLife > bird\FrameRate
bird\FrameTimeLife = ElapsedMilliseconds()
If bird\CurrentFrame < bird\FrameMax-1
bird\CurrentFrame+1
Else
bird\CurrentFrame=0
EndIf
EndIf
ClipSprite(#bird, bird\CurrentFrame * bird\FrameWidth, bird\Animation * bird\FrameHeight, bird\FrameWidth ,bird\FrameHeight)
ZoomSprite(#bird, bird\SpriteWidth, bird\SpriteHeight)
DisplayTransparentSprite(#bird, bird\x, bird\y, bird\Opacity)
;A gauche
If KeyboardPushed(#PB_Key_Left)
bird\Animation = AnimationLeft ;L'animation change
bird\x - bird\VelocityX ;La position x change
EndIf
;A droite:
If KeyboardPushed(#PB_Key_Right)
bird\Animation = AnimationRight ;L'animation change
bird\x + bird\VelocityX ;La position x change
EndIf
;Si on relache une des deux touches Left or Right
If KeyboardReleased(#PB_Key_Left) Or KeyboardReleased(#PB_Key_Right)
bird\Animation = AnimationFace
EndIf
EndProcedure
Procedure GameStart()
If OpenWindow(#mainform, 0, 0, 800, 600, "Kill Birds", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
If OpenWindowedScreen(WindowID(#mainform), 0, 0, 800, 600)
;Ambience foret
LoadSound(#soundforest, "data/sounds/ambientforest.wav")
PlaySound(#soundforest, #PB_Sound_Loop)
;Image de fond et sol
LoadSprite(#background, "data/images/background.jpg")
LoadSprite(#ground, "data/images/ground.jpg")
;L'oiseau
LoadSprite(#bird, "data/images/bird.png", #PB_Sprite_AlphaBlending)
;Caractéristique de l'animation
;Chaque image fait 64x64 pixels et il y a 4 images par ligne
bird\FrameWidth = 64
bird\FrameHeight = 64
bird\FrameMax = 4
bird\FrameRate = 130
;Animation au depart du jeu
bird\Animation = AnimationFace
;Vélocité du sprite
bird\VelocityX = 5
;Coordonnées x,y et taille du sprite
bird\x = 400
bird\y = 200
bird\SpriteWidth = 80
bird\SpriteHeight = 80
bird\Opacity = 255
;-Boucle evenementielle
Repeat
Repeat
event = WindowEvent()
Select event
Case #PB_Event_CloseWindow
End
EndSelect
Until event=0
FlipBuffers()
ClearScreen(RGB(0,0,0))
ExamineKeyboard()
ExamineMouse()
GameUpdate()
Until KeyboardPushed(#PB_Key_Escape)
Else
MessageRequester("Oops", "Erreur lors de la création de la fenêtre 2D", 0)
EndIf
EndIf
EndProcedure
If Init()
GameStart()
Else
MessageRequester("Oops !", "Impossible d'intialiser l'environnement 2D")
EndIf
End