wolron - please help

Blitz3D Forums/Blitz3D Beginners Area/wolron - please help

767pilot(Posted 2004) [#1]
Hi
I have used the code you posted earlier regarding fps speed.
Can you please tell me why the framerate moves so fast. I have set the speed to 25 and 100 but I cant figure out whats wrong
thanks

www.geocities.com/gjpollitt/game.zip


WolRon(Posted 2004) [#2]
I just looked through the code, I didn't run the game.

Here is your code:
;==========================================================================================
;#Region setup
;create globals for video display
Global gfx_x=640
Global gfx_y=480
Global gfx_colour_depth=16
;Global man_walk

;==========================================================================================
;create window
Graphics  gfx_x,gfx_y,gfx_colour_depth,0 ;screen =640,480,16 bit colour
AppTitle="H.E.R.O."
SetBuffer BackBuffer()
;SeedRnd MilliSecs();get better random result
;HidePointer ;hide the mouse arrow
;#End Region

;==========================================================================================
;variables etc
;#Region variables etc
	
Global manfly ;rocket on? 1 yes 0 no
Global manleft ; 1 left 0 right
Global manflyframechange=0 ;change frame count depending on 1 flying or 0 walking 
Global manfirerange ; range of laser fire
Global manfire ;is man firing? 1 yes 0 no

Global gfx_manwalk=LoadAnimImage("gfx\gfx_manwalk.bmp" ,80,80,0,13)
Global gfx_manfly=LoadAnimImage("gfx\gfx_manfly.bmp",80,80,0,5)
Global gfx_manfire=LoadImage("gfx\gfx_laser.bmp")
ScaleImage (gfx_manfire,.4,.4)

Global gfx_bat=LoadAnimImage("gfx\gfx_bat.bmp",84,65,0,4)

Global gfx_background=LoadImage("gfx\gfx_backdrop.bmp")

MaskImage gfx_manwalk,255,0,255
MaskImage gfx_manfly,255,0,255
MaskImage gfx_bat,255,0,255

Global SpeedFactor
Global Debug = True
Global no0# = 0.00000000001	;avoids division by zero
;for example; any time you perform a division, add no0# to the divisor
;quotient# = dividend# / (divisor# + no0#)
;#End Region

;==========================================================================================
;create types
Type manwalk
	Field x,y,frame,gfx
End Type

Type bat
	Field x,y,frame,gfx,up,lft,alive,id,xmax,ymax,xmin,ymin
End Type

Type FrameRate
	Field TargetFPS#
	Field SpeedFactor#
	Field TimeElapsed#
	Field FPS#
	Field FPS2#
	Field FPS3#
	Field FPS4#
	Field FPS5#
	Field CurrentTicks
	Field FrameDelay
End Type


;==========================================================================================
;create objects from types
;#Region type objects

Global man.manwalk=New manwalk
man\frame =6
man\x=160
man\y=240
man\gfx=gfx_manwalk

Global bat1.bat=New bat
bat1\frame=0
bat1\x=320
bat1\y=160
bat1\gfx=gfx_bat
bat1\lft =0
bat1\up =0
bat1\alive=1
bat1\id =1
bat1\xmax =320
bat1\ymax =240
bat1\xmin =320
bat1\ymin=160

Global bat2.bat=New bat
bat2\frame=0
bat2\x=40
bat2\y=210
bat2\gfx=gfx_bat
bat2\lft =0
bat2\up =0
bat2\alive=1
bat2\id =2
bat2\xmax =40
bat2\ymax =220
bat2\xmin =40
bat2\ymin=60

Global FL.FrameRate = New FrameRate

;initialize frame limiter
FL\TargetFPS# = 30	;set this to whatever FPS your code is based on
FL\FrameDelay = MilliSecs()

;#End Region

;==========================================================================================
;main loop
;#Region main loop
timer=CreateTimer (25)

   While Not KeyHit(1)

        draw_objects
        move_man
        move_bat
        man_fire
        check_collisions
         
        Gosub frame_rate_delay
		Gosub debug
 
   Wend

   End
;#End Region

;==========================================================================================
;functions
Function move_man()	
		
		;movement	
		
		Local fr
		fr=man\frame
		
		If KeyDown(203) Then ;move left
			manleft=1
			man\x=man\x-8* SpeedFactor			
			;keep within frame range
	   			If manfly=0 Then ;walking
	   				man\gfx =gfx_manwalk
	   				fr=fr-1
	   				If fr<0 Then fr=fr+5 ;loop the frame
				Else ;flying
					man\gfx=gfx_manfly
					fr=fr-1
	   				If fr<0 Then fr=0
				End If
		
		ElseIf KeyDown(205) Then ;move right
			manleft=0
		    man\x=man\x+8* SpeedFactor			
			;keep within frame range
	   			If manfly=0 Then ;walking
	   				man\gfx =gfx_manwalk
	   				fr=fr+1
	   				If fr>12 Then fr=fr-5
				Else ;flying
					man\gfx=gfx_manfly
					fr=fr+1
					If fr>4 Then fr=4
				End If
			
		ElseIf KeyDown(200) Then ;move up			
	 			
	 			If KeyDown(205) Then man\x =man\x +8* SpeedFactor
	 			If KeyDown(203) Then man\x =man\x -8* SpeedFactor
	 			
	 			manfly=1 ;flying
				man\y=man\y-8* SpeedFactor ;move 8pixels up
				man\gfx=gfx_manfly ;flying pic
				
				If manflyframechange=0 Then ;when man flys and is using old framecount for walking (0-12)
				
				manflyframechange=1 ;change the frame count for man flying (0-5)
				
				Select True
					
					Case fr >= 0 And fr<= 4		
						fr=0 ;left		
					Case  fr=5
						fr=1 ;left
					Case  fr=6
						fr=2 ;facing you
					Case  fr=7
						fr=3 ;right
					Case  fr=>8 And fr<=12
						fr=4 ;right
					
				End Select
									
				End If
				
		ElseIf manfly=1 ;bring man back to earth! (keep flying image til at ground level)
			
			man\y=man\y+16* SpeedFactor
			If man\y >240 Then ;when man hits ground change graphic and reset flags
				man\y=240 : manfly=0 : man\gfx=gfx_manwalk : manflyframechange=0
				If fr>=3 Then fr=8 ;if frame is manflying right then change to man walking right
			End If
			
		End If
				
		;keep man on screen

		If man\x<0 Then man\x=0
		If man\x >gfx_x-80 Then man\x=gfx_x-80
		If man\y<0 Then man\y=0
		
		man\frame =fr ;change frame of man depending on walking or flying


End Function

Function move_bat()

For b.bat=Each bat
	
If b\alive =1 Then
	
	Local fr=b\frame
		
	If b\up =0 Then ;moving down
		fr=fr+1
		b\y=b\y+2* SpeedFactor
		If fr=>3 Then fr=0
	Else
		fr=fr-1 ;moving up
		b\y=b\y-2* SpeedFactor
		If fr<=0 Then fr=3
	End If
	
	If b\y>b\ymax Then 
		b\y=b\ymax: b\up=1 ;at bottom of screen so move bat up
	End If
	
	If b\y<b\ymin Then
		b\y=b\ymin: b\up=0 ;at top of screen so move bat down
	End If
	
	b\frame=fr

End If

Next

End Function

Function man_fire()

	If KeyDown(57) Then
		
		manfire=1
		
		If manleft=0 Then ;fire to the right
			
			manfirerange=man\x +100
			DrawImage (gfx_manfire,man\x+65 ,man\y+38 ) 
		
		Else	;fire to the left
			
			manfirerange=man\x -100
			DrawImage (gfx_manfire,man\x-115 ,man\y+38 ) 
		
		End If

	Else

		manfirerange=man\x 	
		
		manfire=0

	End If
	
End Function
	
Function draw_objects()
		
		Cls
   		DrawImage gfx_background,0,0
        DrawImage man\gfx,man\x,man\y,man\frame   ;Draw our image
       
       	For bats.bat=Each bat
       	
       		If bats\alive=1 Then
        		DrawImage bats\gfx,bats\x,bats\y,bats\frame
        	End If
        	
        Next
        
End Function

Function check_collisions()
	
For b.bat=Each bat
	
	a=0:c=0
	
	a= ImagesCollide (man\gfx ,man\x,man\y,0,b\gfx,b\x,b\y,0)
	
	Text(6,400,a)
	
		If manfire=1 Then
		
			c= ImagesCollide (gfx_manfire,manfirerange,man\y+35,0,b\gfx,b\x,b\y,0)
			Text(6,412,c)
		
				If c=1 Then b\alive =0

		End If

Next

End Function

;frame rate 
;#Region frame rate
.frame_rate_delay

;Set Speed Factor
FL\CurrentTicks = MilliSecs()
FL\SpeedFactor# = (FL\CurrentTicks - FL\FrameDelay) / (1000.0 / FL\TargetFPS#)
speedfactor=FL\SpeedFactor
;If FL\SpeedFactor# <= 0 Then FL\SpeedFactor# = no0#
	
FL\TimeElapsed# = (FL\CurrentTicks - FL\FrameDelay) / 1000.0

If Debug
	
	FL\FPS# = (FL\FPS2# + FL\FPS3# + FL\FPS4# + FL\FPS5# + FL\TargetFPS# / FL\SpeedFactor#) / 5
	FL\FPS5# = FL\FPS4# : FL\FPS4# = FL\FPS3# : FL\FPS3# = FL\FPS2# : FL\FPS2# = FL\FPS#

EndIf

FL\FrameDelay = FL\CurrentTicks



Return
;#End Region

;debug
;#Region debug
.debug
Local x,y
x=6:y=340

Text(x,y,"speed factor="+SpeedFactor)
y=y+12
Text(x,y,"man_x:"+man\x+" man_y:"+man\y+" man:"+man\frame+" fly:"+manfly +" frchange:"+manflyframechange )
y=y+12

For b.bat=Each bat

	Text(x,y,"bat id:"+ b\id +" x:"+b\x+" y:"+b\y+" fr:"+b\frame+" alive:"+b\alive)
	y=y+12
	
Next

Flip

Return
;#End Region


==============================================================

OK, I must apologize because my code I posted had an error in it. This is what it read:
;example usage of SpeedFactor#
;let's say your player normally moves 5 pixels (or units) per frame
If KeyDown(205)
	PlayerX = PlayerX + 5.0 * SpeedFactor#
EndIf
and this is what it should have been:
;example usage of SpeedFactor#
;let's say your player normally moves 5 pixels (or units) per frame
If KeyDown(205)
	PlayerX = PlayerX + 5.0 * FL\SpeedFactor#
EndIf
Notice the FL\ in front of SpeedFactor# ?

So looking back at your code, I see that you created a variable named SpeedFactor.
Two problems with that. First, it's not the one that the frame-limiting code is updating. You should be using FL\SpeedFactor#.
Second, your variable wasn't even a float (doesn't have the # symbol, therefore it's an integer). This is most likely why it ran fast, because instead of the SpeedFactor equaling something like 0.36 it rounded it down to 0.

erase this line:
Global SpeedFactor
and this line:
speedfactor=FL\SpeedFactor

and change all references in your code from SpeedFactor to FL\SpeedFactor#. (Use the Replace feature, it will make it easy)


By the way, you don't need to create a timer when using the frame limiting code I gave you
timer=CreateTimer (25)
so delete that line as well.


WolRon(Posted 2004) [#3]
When I got home, I tried your game out with the changes I suggested and it worked fine.

Nice graphics you got there...


767pilot(Posted 2004) [#4]
thanks for your help thats great

the frame changes too fast when the man is walking so i need to slow that otherwise looking good

also the graphics I pinched from another game, just 'borrowing' them while I get used to blitz :)