Blitzmax game suddenly slow

BlitzMax Forums/BlitzMax Beginners Area/Blitzmax game suddenly slow

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Hmm okay, don't see how that could help but alright. Careful or you might go blind from my n00b coding:

Strict
(whole lot of incbin's removed here, to save space)
Graphics 800,600,0
SetClsColor 100,100,100
SetMaskColor 255,255,255'White

AutoMidHandle True
(more image loading removed)

Include "powerup.bmx"
Include "particles.bmx"

Type obj
	Field x#, y#
	Field speed# = 0.1
	Field height#
	Field width#
EndType

Type tball Extends obj 'note: ball image must be 100x100 pixels
	Field oldx# = 0, oldy# = 0
	Field offsetx#
	Field dir# = 0
	Field radius# = 10
	Field oldspeed# = 0
	Field sticky = 0
	Function newball(radius)
		Local ball:tball = New tball
		ball.radius = radius
		balllist.addlast(ball)	
	EndFunction
	Method moveball()
		oldx# = x
		oldy# = y
		x#:+ Sin(dir)*speed*time
		y#:+ Cos(dir)*speed*time
	EndMethod
	Method collide()
		If x <= radius-3 Or x >= 646-radius Then x = oldx ; dir = (180 - dir) + 180 ' left wall and right wall collide check
		If y <= radius-3 Then y = oldy ; dir = 180 - dir ' top wall and bottom wall collide check
		Local paddleh# = paddle.height/2
		If y+radius >= paddle.y - paddleh	' check if ball is below paddle y
			Local paddlew# = paddle.width/2
			If x+radius > paddle.x-paddlew And x-radius < paddle.x+paddlew Then 'check if ball is between paddle in x
				If paddle.sticky = 1
					If sticky = 0 Then ' set the ball as sticky if it isn't, making it stop etc.
						sticky = 1
						oldspeed = speed
						speed = 0
						offsetx = x-paddle.x
						If x > paddle.x Then dir = 180 - (x - paddle.x)/2
						If x < paddle.x Then dir = 180 + (paddle.x - x)/2
					EndIf
					x = MouseX() + offsetx
					y = paddle.y - paddleh - radius	'fix the ball height if it's below the paddle
					If MouseHit(1) Then								For Local ball:tball = EachIn balllist 'make sure we release all balls from paddle when mousehit
							If ball.sticky = 1 Then 
								If ball.speed = 0 Then ball.speed = ball.oldspeed ; ball.oldspeed = 0
								ball.sticky = 0
							EndIf
						Next
					EndIf
				Else
					If x > paddle.x Then dir = 180 - (x - paddle.x)/2
					If x < paddle.x Then dir = 180 + (paddle.x - x)/2
				EndIf	
			Return
			EndIf
		EndIf
		If y >= 600 Then die()
	EndMethod
	Method draw()
		Local ratio# = radius / 100 * 2
		SetScale ratio,ratio
		DrawImage(ballimg, x, y)
		SetScale 1,1
	EndMethod
	Method die()
		balllist.remove(Self)
	EndMethod
EndType

Type TBrick Extends obj
	Field strength = 1
	Field r,g,b
	Field powerup = 0
	Field number# = 0
	Function create(x=200,y=200,h=20,w=70,r=200,g=200,b=255,str=1,powerup=0)
		Local newbrick:tbrick = New tbrick
		newbrick.x = x
		newbrick.y = y
		newbrick.height = h
		newbrick.width = w
		newbrick.r = r
		newbrick.g = g
		newbrick.b = b
		If randompowerups = True
		    Local random = Rand(1,5)
			If random = 1 Then newbrick.powerup = getrandomeffectnumber()
		EndIf
		bricklist.addlast(newbrick)
	EndFunction
	Method collide()
		For Local ball:tball = EachIn balllist
			Local brickw = width / 2
			If ball.x# + ball.radius => x# - brickw Then
				If ball.x# - ball.radius =< x# + brickw Then
					Local brickh = height / 2
					If ball.y# + ball.radius => y# - brickh Then
						If ball.y# - ball.radius =< y# + brickh Then
							If ball.oldy < y - brickh Then ball.dir = 180 - ball.dir ; ball.y = ball.oldy ; die() ; Return
							If ball.oldy > y + brickh Then ball.dir = 180 - ball.dir ; ball.y = ball.oldy ; die() ;  Return
							If ball.oldx < x - brickw Then ball.dir = 360 - ball.dir ; ball.x = ball.oldx ;  die() ; Return
							If ball.oldx > x + brickw Then ball.dir = 360 - ball.dir ; ball.x = ball.oldx ;  die() ; Return
						EndIf
					EndIf
				EndIf
			EndIf		
		Next
	EndMethod
	Method draw()
		If powerup > 0 Then
			r = 255
			g = 255
			b = 10	
		EndIf
		SetColor r,g,b
		DrawImage(brick1img, x, y)
		Select strength
			Case 1 DrawImage(cracked_3,x,y)
			Case 2 DrawImage(cracked_2,x,y)
			Case 3 DrawImage(cracked_1,x,y)
		EndSelect
	EndMethod
	Method die()
		If strength = 1 And powerup > 0 Then tpowerup.spawn(x,y,powerup)
		If strength = 1 Then bricklist.remove(Self)	 ; 'PlaySound explodesnd ; player1.score:+25
		If strength = 1 Then explode(x,y, width/2, height/2,r,g,b)
		If strength > 1 Then strength:-1 ; 'PlaySound pop ; player1.score:+10
	EndMethod
EndType

Type TPaddle Extends obj
	Field width# = 190
	Field leftwidth# = 15
	Field rightwidth# = 15
	Field height# = 17
	Field x = 0
	Field y = 580
	Field sticky = 0
	Method move()
		x = MouseX()
	EndMethod

	Method draw()
		Local realwidth = width - leftwidth - rightwidth
		Local halfwidth = realwidth/2 
		SetColor 255,255,255
		Local MouseX = MouseX()
		If MouseX < 15 + halfwidth Then MouseX = 15 + halfwidth
		If MouseX > 630 - halfwidth Then MouseX = 630 - halfwidth
		MoveMouse MouseX, 550
		DrawImage(paddle_leftimg, MouseX-halfwidth-7, 580)
		SetScale realwidth,1
		DrawImage(paddle_middleimg, MouseX, 580)
		SetScale 1,1
		DrawImage(paddle_rightimg, MouseX+halfwidth+8, 580)
	EndMethod
EndType

Type player
	Field lives = 3
	Field score = 0
EndType

Type background Extends obj
	Method draw()
		SetColor 255,255,255
		TileImage(tileimg,x,y)
		x#:+.4*time
		y#:+.5*time
	EndMethod
EndType

Global balllist:TList = CreateList()
Global bricklist:TList = CreateList()
Global sparklist:TList = CreateList()
Global state$ = "menu"
Global ball_hold = 1
Global newbackground:background = New background
Global player1:player = New player
Global time# = 1
Global paddle:tpaddle

Repeat
	If state = "menu" Then menu()
	If state = "started" Then started()
	If state = "started" And ListIsEmpty(balllist) Then die()
	If state = "started" And ListIsEmpty(bricklist) Then die()
	If state = "gameover" Then gameover()
	Flip; Cls
'	FlushMem -- this did not work with 1.16
Until KeyDown(Key_Escape)

Function die()
	If player1.lives = 0 Then state = "gameover" ; Return
	player1.lives:-1
	tball.newball(10)
	ball_hold = 1
EndFunction

Function gameover()
	If KeyHit(key_space) state = "menu" ; Return
	SetBlend(alphablend)
	newbackground.draw()
	SetRotation Cos(number)*20
	SetScale Cos(number)*1, Cos(Cos(number)*1)*1.2
	number:+1
	SetColor 0,0,0
	SetAlpha 0.1
	DrawImage(gameoverimg,400,400)
	SetColor 255,255,255
	SetAlpha 1
	DrawImage(gameoverimg,400,300)
	SetRotation 0
	SetColor 0,0,0
	SetScale 1,1
	DrawText "Press SPACE for menu", 200,550
EndFunction

Function menu()
	SetBlend(alphablend)
	newbackground.draw()
	SetAlpha .1
	DrawImage(logo_shadowimg, 400, 150)
	SetAlpha 1
	SetColor 255,255,255
	DrawImage(logoimg, 400, 100)
	SetColor 0,0,Rand(100,200)
	DrawText "Press SPACE to start a new game", 290,300
	If KeyHit(key_space) Then newgame()
EndFunction


Function newgame()
	tball.newball(10)
	paddle = New tpaddle
	player1.lives = 3
	player1.score = 0
	balllist.clear()
	bricklist.clear()
	poweruplist.clear
	debrislist.clear()
	Include "level1.bmx"
	state = "started"
EndFunction

Function started()
		newbackground.draw()
		For Local brick:tbrick = EachIn bricklist
			brick.collide()
			brick.draw()
		Next
		For Local particle:tparticle = EachIn particlelist
			particle.move()
			particle.draw()
		Next
		For Local debris:tdebris = EachIn debrislist
			debris.move()
			debris.draw()
		Next

		For Local ball:tball = EachIn balllist
			ball.moveball()
			ball.collide()
			ball.draw()
		Next

		For Local powerup:tpowerup = EachIn poweruplist
			powerup.move()
			powerup.collide(paddle.width)
			powerup.draw()
		Next

		paddle.move()
		paddle.draw()

		drawstatusbar()''		drawvalues()

		If ball_hold = 1
			ballhold()
		Else	
			If MouseDown(2) Then matrix() Else time = 1 ; SetAlpha 1
			If KeyHit(key_e) Then state = "edit"

			'If KeyHit(key_down) Then paddle.width:-50
			'If KeyHit(key_up) Then paddle.width:+50

			If KeyHit(key_down) Then For Local ball:tball = EachIn balllist ; ball.radius:-30 ; Next
			
			If KeyHit(key_up) Then For Local ball:tball = EachIn balllist ; ball.radius:+30 ; Next

			If KeyHit(key_x) Then For Local brick:tbrick = EachIn bricklist ; brick.die() ; Next

			If KeyHit(key_4) Then tbrick.create(Rand(40,600), Rand(20,700))
			If KeyHit(key_5) Then createballs(5, Rand(4,20))
			If KeyHit(key_6) Then Include "level1.bmx"
			If KeyHit(key_0) Then createballs(100, Rand(1,3))
			If KeyHit(key_space) Then 
				For Local ball:tball = EachIn balllist
					If ball.speed <> 0
						ball.speed = 0
					Else
						ball.speed = 5
					EndIf
				Next
			EndIf
		EndIf
EndFunction


Function matrix()
	DrawText "MATRIX MODE ON",700,580
	time = 0.1
EndFunction




Function ballhold()
	For Local ball:tball = EachIn balllist
		ball.speed = 0
		ball.y = MouseY()'550
		ball.x = MouseX()
	Next
	If MouseHit(1) Then launchball()
EndFunction


Function launchball()
	ball_hold = 0
	For Local ball:tball = EachIn balllist
		ball.speed = 6
		ball.dir = Rand(135,225)
	Next	
EndFunction

Function drawstatusbar()	SetColor 0,0,0
	DrawRect 645,0,5,600
	SetColor 201,203,219
	DrawRect 650,0,600,800
	SetColor 255,255,255
EndFunction

Function drawvalues()
	SetColor 0,0,0
	DrawText "Number of balls : "+balllist.Count(), 20, 40
	DrawText "Number of bricks : "+bricklist.Count(), 20, 20
	DrawText "Score: "+player1.score, 660,100
	DrawText "Lives: "+player1.lives, 660,120
EndFunction