Extreme lag; how do I streamline my code properly?

BlitzMax Forums/BlitzMax Beginners Area/Extreme lag; how do I streamline my code properly?

Uber-nerd(Posted 2006) [#1]
Ok the question is about as stupid as my code, I'm changing stuff all the time and leave useless commands that do nothing so try not to laugh. Anyway I think its to do with the collision code because it only lags whenever I'm shooting... my entire game completely defies reality, the odd asteroid explodes for no reason and the world slows down when you shoot too much.

You could just read my code but it would be slightly better (yet inconvenient) to add your own images in for the bullet, the player and the asteroid.

My blitzmax "work" is due in 2 days now (for school), I think I should give up because I don't need to do it and I've had blitzmax for about a week or less... if someone tries to fix my code then the sooner the better :) thanks, I'm going to read a few more tutorials to avoid looking like a moronic noob who depends on other people - BYE!!.

Graphics 800,600,32,85,0
SeedRnd MilliSecs()

Global coolfont:TImageFont=LoadImageFont( "C:\windows\fonts\ariblk.ttf",20,style=boldFONT )
Global dalist:tlist=CreateList()
Global dalist2:tlist=CreateList()
Global bullist:tlist=CreateList()


Global asttimer
Global px=800/2-16,py=600-36,pammo=100,phealth=100,psec=0,pridelay=MilliSecs()+300
Local ast:thing,star:thing,b:bul



SetMaskColor 255,0,255
ship=LoadImage("gfx\player.bmp")
asteroid=LoadImage("gfx\asteroid.bmp")
bullet=LoadImage("gfx\flamebul.png")

SetMaskColor 0,0,0
star1=LoadImage("gfx\star1.bmp")
star2=LoadImage("gfx\star2.bmp")
star3=LoadImage("gfx\star3.bmp")

SetMaskColor -1,-1,-1
medkit=LoadImage("gfx\medkit.jpg")
rifle=LoadImage("gfx\ammo.jpg")
clock=LoadImage("gfx\clock.jpg")
trophy=LoadImage("gfx\trophy.jpg")


Type thing
Field x,y,kind,spd,img,r,r_dir

Method killast()
dalist2.remove Self
EndMethod

Method killstar()
dalist.remove Self
EndMethod

EndType


Type bul
Field x,y,spd,kind

Method killbul()
bullist.remove Self
EndMethod

EndType


'---------------------------------------------------------------------------------------
'---------------------------------------------------------------------------------------
While Not KeyHit(key_escape)
Cls




For b:bul=EachIn bullist
CollideImage bullet,b.x,b.y,0,0,1
DrawImage bullet,b.x,b.y
b.y=b.y-b.spd
If b.y<100		b.killbul()
Next


For ast:thing=EachIn dalist2
CollideImage asteroid,ast.x,ast.y,0,0,2
Next

For ast:thing=EachIn dalist2
For b:bul=EachIn bullist

If CollideImage(asteroid,ast.x,ast.y,0,1,0)	ast.killast()
'If ImagesCollide2(asteroid:TImage,ast.x,ast.y,0,ast.r,0,0,bullet:TImage,b.x,b.y,0,0,0,0)
Next
Next



DrawImage medkit,0,0
DrawImage rifle,0,24
DrawImage clock,0,600-42
DrawImage trophy,800-43,600-42

SetImageFont (coolfont:Timagefont)
DrawText phealth,24,0
DrawText pammo,55,24
DrawText time,37,600-40
DrawText pscore,750,600-40


If asttimer<MilliSecs()
For jhgiwuoa=1 To 2
create_ast()
Next

asttimer=MilliSecs()+2000
EndIf


'render_star(star1,star2,star3)
render_ast(asteroid)
playerinput(bullet)

DrawImage ship,px,py


Flip()
Wend
End


Function create_star()

Local star:thing=New thing
star.x=Rand(0,800-19)
star.y=Rand(-19,-619)
star.spd=Rand(1,8)
star.img=Rand(1,3)
ListAddLast dalist,star

EndFunction



Function create_ast()

ast:thing=New thing
ast.x=Rand(30,800-30)
ast.y=-30
ast.spd=Rand(1,4)
ast.r=Rand(0,360)
ast.r_dir=Rand(0,1)
ListAddLast dalist2,ast

EndFunction



Function render_ast(asteroid)
For ast:thing=EachIn dalist2

SetRotation ast.r
If ast.r_dir=0		ast.r=ast.r-1
If ast.r_dir=1		ast.r=ast.r+1

ast.y=ast.y+ast.spd
DrawImage asteroid,ast.x,ast.y

If ast.y>600		
ast.killast
EndIf

SetRotation 1
Next
EndFunction



Function render_star(star1,star2,star3)
For star:thing=EachIn dalist

SetRotation 0
star.y=star.y+star.spd
If star.img=1		DrawImage star1,star.x,star.y
If star.img=2		DrawImage star2,star.x,star.y
If star.img=3		DrawImage star3,star.x,star.y

If star.y>600		
star.killstar()
EndIf

Next
EndFunction


Function playerinput(bullet)

If KeyDown(key_left)		px=px-5
If KeyDown(key_right)		px=px+5
If KeyDown(key_up)			py=py-5
If KeyDown(key_down)		py=py+5

If KeyDown(88) And pridelay<MilliSecs()
b:bul=New bul
b.x=px
b.y=py
b.spd=5
ListAddLast bullist,b
pridelay=MilliSecs()+300
EndIf

EndFunction



DJ Scantron(Posted 2006) [#2]
It seems to me, that you forgot to put a ResetCollisions call
in your main loop.

Try just before or after you cls.


Uber-nerd(Posted 2006) [#3]
*slaps self in the head* ... now if only there was some way to delete your own posts. It just happens to be in the tutorial I was reading, don't listen to music when you're programming... its not good.