Why is attached code so slow?

BlitzPlus Forums/BlitzPlus Programming/Why is attached code so slow?

Markh999(Posted 2004) [#1]
The attached is a simple space invader type game (not complete !!) based on the SPIDER example that comes with BB. None of the spiders move or shoot and I limit it to 3 bullets from my ship maximum on the screen at a time. So WHY is it so slow - the ship crawls along the bottom , the bullets are slow and none os the spiders disappear when hit? I think the problem may be in the checkcollision routine but not sure. Can anyone point me in the right direction? Thanks in advance

Graphics 640,480,0,1
SetBuffer BackBuffer()

Type AlienData
Field xpos, ypos
End Type

Type BulletData
Field xpos,ypos
End Type

Const Alien_ypos_Start = 460
Const Alien_Anim_Pause = 4
Global Aliens = LoadImage("c:/Program Files/BlitzPlus/COURSE/AlienShapes.bmp")
Global Player = LoadImage("c:/Program Files/BlitzPlus/COURSE/Player.bmp")
Global Bullets = LoadImage("c:/Program Files/BlitzPlus/COURSE/Bullet.bmp")
Global shotgun = LoadSound("c:/Program Files/BlitzPlus/COURSE/shotgun.wav")
Global Alienhit = LoadSound("c:/Program Files/BlitzPlus/COURSE/toyduck.wav")
Global logo = LoadImage("c:/Program Files/BlitzPlus/COURSE/Alienlogo_small.bmp")
Global theend = LoadImage("c:/Program Files/BlitzPlus/COURSE/game_over.bmp")
Global score = 0
Global Alien.AlienData
Global Bullet.BulletData
Global GameOver = False
Global Playerxpos = 320
Global Playerypos = 360
Global livebullets =0

SoundPan Alienhit,-1
SoundPan shotgun,1

;IntroScreen()
InitAlien()

Repeat
If GameOver = True Then
GameoverScreen()
InitAlien()
GameOver = False
score = 0
Else
DrawScreen()
DrawEnemies()
DrawShip()
DrawBullets()
checkcollision()
Flip
EndIf
Until KeyDown(1)
TerminateProgram()

Function InitAlien()
For tempy=1 To 3
For tempx=1 To 10
Alien.AlienData = New AlienData
Alien\xpos = 40+tempx*50
Alien\ypos = tempy*40
Next
Next
End Function

Function GameoverScreen()
ClearScreens()
SetBuffer FrontBuffer()
DrawImage theend,50,80
Color 255,255,255
Text 200,200,"You reached a score of "+score
Text 300,310,"Press spacebar to continue",1,1
Repeat
Until KeyDown(57)
ClearScreens()
End Function

Function DrawScreen()
Viewport 0,0,640,480
Cls
Color 255,255,255
Text 0,0,"Score "+score
Color 0,255,0
Line 0,399,640,399
Line 0,15,640,15
Viewport 0,15,640,385
End Function

Function TerminateProgram()
FreeSound shotgun
FreeSound Alienhit
FreeImage Aliens
FreeImage logo
FreeImage theend
End
End Function

Function DrawEnemies()
For Alien = Each AlienData
DrawImage Aliens,Alien\xpos,Alien\ypos
Next
End Function

Function CheckCollision()
For bullet = Each bulletdata
For alien = Each aliendata
If ImagesOverlap(bullets,bullet\xpos,bullet\ypos,Aliens,Alien\xpos,Alien\ypos) Then
PlaySound Alienhit
score = score + 1000
Delete Alien
EndIf
Next
Next
If score Mod 10 = 0 Then InitAlien()
End Function

Function DrawShip()

DrawImage Player, Playerxpos, Playerypos
If KeyDown(205) Then playerxpos=playerxpos+1
If KeyDown(203) Then playerxpos=playerxpos-1

End Function

Function DrawBullets()
If KeyHit(57) And livebullets<3 Then
livebullets=livebullets+1
Bullet.BulletData = New BulletData
Bullet\xpos = playerxpos+10
Bullet\ypos = 350
PlaySound shotgun
EndIf
For Bullet = Each BulletData
DrawImage Bullets, Bullet\xpos, Bullet\ypos
Bullet\ypos=bullet\ypos-8
If bullet\ypos<0 Then
livebullets=livebullets-1
Delete bullet
EndIf
Next

End Function

Function ClearScreens()
Viewport 0,0,640,480
SetBuffer FrontBuffer()
Cls
SetBuffer BackBuffer()
Cls
End Function


aab(Posted 2004) [#2]
well, i have no idea why it would be as slow as you describe, but as for the disappearing:
When i use types i dont do it like this:
[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[
For bullet = Each bulletdata
For alien = Each aliendata
If ImagesOverlap(bullets,bullet\xpos,bullet\ypos,Aliens,Alien\xpos,Alien\ypos) Then
PlaySound Alienhit
score = score + 1000
Delete Alien
EndIf
Next
Next
[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[
instead, i do this:
[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[
For bullet.bulletdata = Each bulletdata
For alien.aliendata = Each aliendata
If ImagesOverlap(bullets,bullet\xpos,bullet\ypos,Aliens,Alien\xpos,Alien\ypos) Then
PlaySound Alienhit
score = score + 1000
Delete alien.aliendata
EndIf
Next
Next
]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
and well, things work.... IF thats a problem...

sorry if im not helping, but i try..


WolRon(Posted 2004) [#3]
aab and Markh999
Use the [code][/code[ or [codebox][/codebox[ tags for your codes.


aab(Posted 2004) [#4]
The Code Box would be too big: too many times have i made a code box with a big black space inbetween the base of the code and the next line


Mark Tiffany(Posted 2004) [#5]
use codebox instead. This has scrolly widgets on the sides and is a fixed size.


keyboard(Posted 2004) [#6]
the code is slow because you are making 30 instances of the type AlienData every loop through this function.

the line "If score Mod 10 = 0 Then InitAlien()" is 0 all the time, so although the aliens get killed, instantaneously new ones appear. Comment out the line and try it.


Function CheckCollision()
	For Bullet.BulletData = Each BulletData
		For Alien.AlienData = Each AlienData
			If ImagesOverlap(bullets,Bullet\xpos,Bullet\ypos,Aliens,Alien\xpos,Alien\ypos) Then
				PlaySound Alienhit
				score = score + 1000
				Delete Alien
			EndIf
		Next
	Next
	If score Mod 10 = 0 Then InitAlien()
End Function


and Mark, look for some nice code examples and check out the formatting, makes it so much easier to decipher if it is nicely formatted. And also, it is maybe better too, if you have a variable "Aliens" not to call it "aliens", and having a variable "Aliens" and another one "Alien" is OK, but kinda confusing too :)