Page 1 of 1
ClipSprite()
Posted: Wed Oct 05, 2005 5:20 pm
by wolfwood2x
Anyone know some good tutorials on this? All the ones I have found are in the code archive and are kind of obscure.
I understand the basics of this command enough to know that I can set which part is visible at a given time. But how do i scroll that to show an animation as i move. For example in the helicopter game tutorial refferenced in this forum a sprite strip is used with mutiple frames.
Is there a reccommended number of frames in a strip if you are animating it? like If i wanted to do something 35fps or 60 fps how many frames would i need? Does it even matter or is it all personal preference.
So in this case lets just say I have a sprite with only 2 frames for examples sake in a 40x80 strip.
So
Code: Select all
ClipSprite(#example, 0, 40, 40, 80)
would only show the first fram of the sprite but doing an if condition on a movement of the sprite to clip to the next section doesnt seem to display frame 2 however just a single command for frame 2 with no reference at all to the prior code seems to call frame 2 properly.
Code: Select all
ClipSprite(#example, 40, 40, 40, 80)
So how would you animate between the 2 frames in a loop for movement?
Posted: Wed Oct 05, 2005 5:27 pm
by Comtois
Posted: Wed Oct 05, 2005 5:53 pm
by wolfwood2x
Thanks for trying at least but i do not speak a word of french let alone read it.
If i spoke french or german i do not think i would be having this problem as there are far more sites in those languages
Posted: Wed Oct 05, 2005 6:03 pm
by netmaestro
There is an excellent animsprite library included in the PBOSL lib, simple to use and good documentation with examples is supplied with it.
Posted: Wed Oct 05, 2005 6:18 pm
by wolfwood2x
Isnt it written in C?
"PBOSL_SRC/PBOSL/LIBS/C/PBOSL_AnimSprite"
Like what im looking for is how to do this myself not use a ready out of the box lib function that someone else has written. These files are not ".pb" can i still open them with the editor?
I just want someone to in simple terms for a beginner to understand explain how to move from frame one to frame 2. The french tutorial looked great if some can translate the important parts hehe.
I am going to try babel fish now
Im sure if i have not annoyed the users on this forum yet, I will soon. I am very inquisitive ^_^
Posted: Wed Oct 05, 2005 7:22 pm
by wolfwood2x
Well babel fish sucks. Google was a little more help. Bust some of the finer pooints are still lost on me. I get that you use a structure
Code: Select all
Structure Sprite
Direction.l
Column.l
line.l
Progress?
Max
Cutx
cuty
X
Y
Speed
but i fail to understand from the translation how it works it seems like a ton of factors just to move 3 frames over and over
Re: ClipSprite()
Posted: Wed Oct 05, 2005 7:43 pm
by dracflamloc
wolfwood2x wrote:Anyone know some good tutorials on this? All the ones I have found are in the code archive and are kind of obscure.
I understand the basics of this command enough to know that I can set which part is visible at a given time. But how do i scroll that to show an animation as i move. For example in the helicopter game tutorial refferenced in this forum a sprite strip is used with mutiple frames.
Is there a reccommended number of frames in a strip if you are animating it? like If i wanted to do something 35fps or 60 fps how many frames would i need? Does it even matter or is it all personal preference.
So in this case lets just say I have a sprite with only 2 frames for examples sake in a 40x80 strip.
So
Code: Select all
ClipSprite(#example, 0, 40, 40, 80)
would only show the first fram of the sprite but doing an if condition on a movement of the sprite to clip to the next section doesnt seem to display frame 2 however just a single command for frame 2 with no reference at all to the prior code seems to call frame 2 properly.
Code: Select all
ClipSprite(#example, 40, 40, 40, 80)
So how would you animate between the 2 frames in a loop for movement?
From what I can tell you're function calls are wrong. Can you post the code you are using with the "if"
If each frame is 40x40, and you have a 40x80 strip (thats vertical):
Do this:
Code: Select all
Repeat
Delay(1000)
if frame=0
frame=1
else
frame=0
endif
ClipSprite(#Example,0,frame*40,40,40)
DisplaySprite(#Example,0,0)
Forever
Posted: Wed Oct 05, 2005 8:18 pm
by netmaestro
OK, here is a small example for you. You can't run this code without the bmp file containing the sprite image. It is a strip of 8 images in sequence and you can download it from here:
http://www.networkmaestro.com/sonic.bmp
Make a folder, put this image in it, and load this code into PB:
Code: Select all
InitSprite()
Enumeration
#sonic
#window
EndEnumeration
OpenWindow(#window,0,0,400,400,#PB_Window_ScreenCentered|#PB_Window_MinimizeGadget,"Sprite animation example by Network Maestro")
OpenWindowedScreen(WindowID(#window),0,0,400,400,0,0,0)
LoadSprite(#sonic,"sonic.bmp")
x=0
Repeat
Event=WindowEvent()
ClipSprite(#sonic,x,0,40,40)
ClearScreen(255,0,255)
DisplaySprite(#sonic,200,200)
FlipBuffers()
Delay(100)
x+40:If x>280:x=0:EndIf
Until Event=#PB_Event_CloseWindow
Save the program as "testsprite" or whatever in the SAME folder with the image so that the program will find the image when it runs
Run the test program and watch Sonic stride purposefully to the nearest computer to register a copy of PureBasic.
Posted: Thu Oct 06, 2005 4:49 pm
by wolfwood2x
That was a great example
Thanks so much
Posted: Thu Oct 06, 2005 5:10 pm
by wolfwood2x
One question though. Using Similar code my pic animates properly but if i have it constanly animating movement around the map seems very sluggish and choppy compared to moving with no animation. Is there a way to speed this up or is there a problem with my code?
Code: Select all
;--Variable Declarations--
imagex.l=800
imagey.l=600
points.l=0
;--pointer section--
Structure POINT
X.l
Y.L
EndStructure
NewList Burger.Point()
AddElement(burger())
Burger()\x=125
Burger()\y=115
AddElement(burger())
Burger()\x=160
Burger()\y=115
If InitSprite() = 0 Or InitKeyboard() = 0 Or InitSound() = 0
MessageRequester("error", "Can't open", 0)
End
EndIf
If OpenScreen(1024,768,16,"Gut Check")
;--Sound Loader--
UseOGGSoundDecoder()
LoadSound(2, "Chomp.wav")
LoadSound(1, "opening.wav")
;PlaySound(1, 0) ;commented out during alpha phase
UseJPEGImageDecoder()
;--Sprite Loader--
LoadSprite(0,"level.jpg",0)
LoadSprite(2, "walls.jpg",0)
LoadSprite(1,"anim.jpg",0)
StartDrawing(SpriteOutput(1))
StopDrawing()
LoadSprite(1000, "smburger2.jpg", 0)
StartDrawing(SpriteOutput(1000))
StopDrawing()
LoadFont(1,"Arial",20,#PB_Font_Underline)
;--Game Engine--
Repeat
FlipBuffers()
ClearScreen(0,0,0)
ExamineKeyboard()
If KeyboardPushed(#PB_Key_A) Or KeyboardPushed(#PB_Key_Left)
Imagex= imagex-3
Gosub WallCheck
EndIf
If KeyboardPushed(#PB_Key_D) Or KeyboardPushed(#PB_Key_Right)
Imagex= imagex+3
Gosub WallCheck
EndIf
If KeyboardPushed(#PB_Key_W) Or KeyboardPushed(#PB_Key_Up)
Imagey= imagey-3
Gosub WallCheck
EndIf
If KeyboardPushed(#PB_Key_S) Or KeyboardPushed(#PB_Key_Down)
Imagey= imagey+3
Gosub WallCheck
EndIf
Gosub Update_back
Gosub animate
Gosub Burger_count
Gosub Check_score
Until KeyboardPushed(#PB_Key_Escape)
EndIf
End
;--Update Screen Functions--
Update_back:
DisplaySprite(0,0,0)
Return
animate:
ClipSprite(1,x,0,31,32)
DisplayTransparentSprite(1,imagex, imagey)
Delay(110)
x+31:If x>61:x=0:EndIf
Return
;--burger checking--
Burger_count:
ForEach Burger()
DisplayTransparentSprite(1000,burger()\x,Burger()\y)
If SpriteCollision(1, imagex, imagey, 1000, burger()\x, burger()\y)
LoadSound(2, "chomp.wav")
;PlaySound(2, 0)
points + 25
DeleteElement(Burger())
Continue
EndIf
Next
Return
;--Wall Checking Procedure
WallCheck:
;If SpritePixelCollision(1, imagex,imagey, 2, 262,415)
; imagex= imagex+2
;EndIf
Return
;--Scoring Algorithm--
Check_score:
StartDrawing(ScreenOutput())
DrawingMode(1)
DrawingFont(UseFont(1))
FrontColor(255,255,255):Locate(700,100):DrawText("HIGH SCORE: " + Str(points))
FrontColor(255,255,255):Locate(700,200):DrawText("1UP: " + Str(points))
StopDrawing()
Return
just an fyi this game is made soley to poke fun a t a co-worker
Posted: Thu Oct 06, 2005 7:10 pm
by netmaestro
I can see a couple problems off the bat, but post your images so I can test changes, that would be the best way to help you.
Posted: Thu Oct 06, 2005 7:38 pm
by Comtois
I replace delay(110) by ElapsedMilliseconds()
And post your images to test !
Code: Select all
;--Variable Declarations--
imagex.l=800
imagey.l=600
points.l=0
#TimeMax = 110 ; 110 ms
Tps=ElapsedMilliseconds()
;--pointer section--
Structure POINT
X.l
Y.L
EndStructure
NewList Burger.Point()
AddElement(burger())
Burger()\x=125
Burger()\y=115
AddElement(burger())
Burger()\x=160
Burger()\y=115
If InitSprite() = 0 Or InitKeyboard() = 0 Or InitSound() = 0
MessageRequester("error", "Can't open", 0)
End
EndIf
If OpenScreen(1024,768,16,"Gut Check")
;--Sound Loader--
UseOGGSoundDecoder()
LoadSound(2, "Chomp.wav")
LoadSound(1, "opening.wav")
;PlaySound(1, 0) ;commented out during alpha phase
UseJPEGImageDecoder()
;--Sprite Loader--
LoadSprite(0,"level.jpg",0)
LoadSprite(2, "walls.jpg",0)
LoadSprite(1,"anim.jpg",0)
LoadSprite(1000, "smburger2.jpg", 0)
LoadFont(1,"Arial",20,#PB_Font_Underline)
;--Game Engine--
Repeat
FlipBuffers()
ClearScreen(0,0,0)
ExamineKeyboard()
If KeyboardPushed(#PB_Key_A) Or KeyboardPushed(#PB_Key_Left)
Imagex - 3
EndIf
If KeyboardPushed(#PB_Key_D) Or KeyboardPushed(#PB_Key_Right)
Imagex + 3
EndIf
If KeyboardPushed(#PB_Key_W) Or KeyboardPushed(#PB_Key_Up)
Imagey - 3
EndIf
If KeyboardPushed(#PB_Key_S) Or KeyboardPushed(#PB_Key_Down)
Imagey + 3
EndIf
DisplaySprite(0,0,0)
Gosub animate
Gosub Burger_count
Gosub Check_score
Until KeyboardPushed(#PB_Key_Escape)
EndIf
End
animate:
If ElapsedMilliseconds()-Tps>#TimeMax
x+31:If x>61:x=0:EndIf
Tps=ElapsedMilliseconds()
EndIf
ClipSprite(1,x,0,31,32)
DisplayTransparentSprite(1,imagex, imagey)
Return
;--burger checking--
Burger_count:
ForEach Burger()
DisplayTransparentSprite(1000,burger()\x,Burger()\y)
If SpriteCollision(1, imagex, imagey, 1000, burger()\x, burger()\y)
PlaySound(2, 0)
points + 25
DeleteElement(Burger())
EndIf
Next
Return
;--Scoring Algorithm--
Check_score:
StartDrawing(ScreenOutput())
DrawingMode(1)
DrawingFont(UseFont(1))
FrontColor(255,255,255):Locate(700,100):DrawText("HIGH SCORE: " + Str(points))
FrontColor(255,255,255):Locate(700,200):DrawText("1UP: " + Str(points))
StopDrawing()
Return
Posted: Fri Oct 07, 2005 1:39 pm
by wolfwood2x
http://purebasic.myftp.org/?filename=fi ... tCheck.zip]GutCheck.zip
theres my file with the images and sounds so you can get a feel for it. It animates when you hit right or left and moves very slowly compared to when you hit up or down with no animation. I am open to both constructive and destructive criticism as I am just learning right now. If you want to tell me I am coding all wrong go right ahead, It will help me in the long run

Posted: Fri Oct 07, 2005 7:35 pm
by Comtois