Slow Downs

BlitzPlus Forums/BlitzPlus Programming/Slow Downs

En929(Posted 2009) [#1]
I was making a pac-man like game, and was wondering why my game slows down drastically when I add more Apple images? My game code is below. Thanks:



Graphics 900, 900
SetBuffer BackBuffer()



EATUM = LoadImage ("Eatum.png")
APPLE = LoadImage("Apple.png")
ORBE = LoadImage("Orbe.png")
DOOR = LoadImage("Door.png")


Type EATUM

Field x,y

End Type


Type APPLE

Field x,y

End Type

Type ORBE

Field x,y

End Type

Type DOOR

Field x,y

End Type



e.EATUM = New EATUM

e\x = 100
e\y = 200


For z = 1 To 5

a.Apple = New Apple
a\x = 100+30*z
a\y = 90*z
Next

o.ORBE = New ORBE
o\x = 100
o\y = -200

d.DOOR = New DOOR
d\x = 50
d\y = 50


While Not KeyDown (1)


Cls

DrawImage (EATUM,e\x,e\y)

For a.APPLE = Each APPLE

DrawImage(APPLE,a\x,a\y)
If ImagesCollide(EATUM,e\x,e\y,0,APPLE,a\x,a\y,0) Then Delete a

Next

DrawImage (ORBE,o\x,o\y)


If KeyDown(203)
e\x = e\x - 3

If ImagesCollide(EATUM,e\x,e\y,0,ORBE,o\x,o\y,0) Then o\x = o\x - 3

EndIf

If KeyDown(205)

e\x = e\x + 3

If ImagesCollide(EATUM,e\x,e\y,0,ORBE,o\x,o\y,0) Then o\x = o\x + 3


EndIf


If KeyDown(200)
e\y = e\y - 3


If ImagesCollide(EATUM,e\x,e\y,0,ORBE,o\x,o\y,0) Then o\y = o\y - 3

EndIf



If KeyDown(208)
e\y = e\y + 3

If ImagesCollide(EATUM,e\x,e\y,0,ORBE,o\x,o\y,0) Then o\y = o\y + 3


EndIf


DrawImage (DOOR,d\x,d\y)

If ImagesCollide(DOOR,d\x,d\y,0,ORBE,o\x,o\y,0) Then
Text 350,300, "Great Job"

EndIf


Flip

Wend


Mahan(Posted 2009) [#2]
disclaimer: I haven't tried your code, only skimmed trough it fast.

I'll cite a little bit from the manual for the command ImagesCollide:


The ImagesOverlap command is MUCH faster, however, but can only determine if ANY of the two images have overlapped (this INCLUDES transparent pixels).



This command is relatively slow, so you could try to optimize the code by only using this command on images where the command ImagesOverlap indicate an overlap in the first place.

Besides if you use the forum codes for code or codebox, your code will look much better in posts :-)


Code:

[ code]code goes here[ /code]

Code box:

[ codebox]code goes here[ /codebox]



(spaces in the brackets inserted to not trig the block).

Edit: Now that I think of it I get pretty certain that this is already done internally in the command, so it's not certain my advice helps at all :/