Why is my program so slow?

Previous topic - Next topic

hanyon21

I make space invader game.
Code (glbasic) Select

SETSCREEN 448,512,0
LIMITFPS 2

SETTRANSPARENCY RGB(0,0,0)

LOADANIM "ship.bmp",0,32,16

LOADSPRITE "bullet.bmp",1

LOADANIM "alien1.bmp",2,32,16
LOADANIM "alien2.bmp",3,32,16
LOADANIM "alien3.bmp",4,32,16

GLOBAL px=224
GLOBAL py=432
GLOBAL speed=2
GLOBAL pldir=2
GLOBAL planim=0

GLOBAL bulletspeed=20
GLOBAL bulletfired=0

TYPE bullet
bulx
buly
buldifx
buldify
ENDTYPE
GLOBAL bullets[] AS bullet

TYPE enemy
enx
eny
difx
dify
    frame
status

ENDTYPE
GLOBAL enemy1[] AS enemy
GLOBAL enemy2[] AS enemy
GLOBAL enemy3[] AS enemy

GLOBAL en1 AS enemy
GLOBAL en2 AS enemy
GLOBAL en3 AS enemy



Init_Game_Objects()

WHILE KEY(01)=FALSE

UpdateBullets()
UpdatePlayer()
    UpdateEnemy()
DrawTheScreen()

WEND

END



FUNCTION AddBullet:

LOCAL bull AS bullet

bull.bulx=px
bull.buly=py-10

bull.buldifx=0
bull.buldify=-8

DIMPUSH bullets[],bull
        bulletfired=bulletspeed


ENDFUNCTION

FUNCTION UpdateBullets:

FOREACH bull IN bullets[]
bull.bulx=bull.bulx+bull.buldifx
bull.buly=bull.buly+bull.buldify
IF bull.bulx<0 OR bull.bulx>448 OR bull.buly<0 OR bull.buly>512 THEN DELETE bull
NEXT

ENDFUNCTION

FUNCTION UpdatePlayer:

LOCAL dirx,diry

IF KEY(203) THEN dirx=-1
IF KEY(205) THEN dirx=1

px=px+(speed*dirx)

IF px<0 THEN px=0
IF px>448 THEN px=448

IF dirx=-1 THEN pldir=0
IF dirx=1 THEN pldir=1

IF bulletfired=0
IF KEY(29) OR KEY(157) THEN AddBullet()
ELSE
bulletfired=bulletfired-1
ENDIF

ENDFUNCTION

FUNCTION UpdateEnemy:

FOREACH en1 IN enemy1[]
en1.frame=en1.frame+1
NEXT

FOREACH en2 IN enemy2[]
en2.frame=en2.frame+1
NEXT

FOREACH en3 IN enemy3[]
en3.frame=en3.frame+1
NEXT
IF en1.frame>1 THEN en1.frame=0
IF en2.frame>1 THEN en2.frame=0
IF en3.frame>1 THEN en3.frame=0
ENDFUNCTION

FUNCTION Init_Game_Objects:

LOCAL Xpos=48
LOCAL Ypos=112
LOCAL Speed
FOR lp=1 TO 1

REPEAT
    Speed=0.1
  UNTIL Speed<>0

SpawnRowOfCharacters(Xpos,Ypos,11)
NEXT

ENDFUNCTION

FUNCTION SpawnRowOfCharacters: Xpos,Ypos,Count

LOCAL space=32

FOR lp=1 TO Count

  en1.enx=Xpos + (space * (lp-1) )
  en1.eny=Ypos
  en1.status=0
  DIMPUSH enemy1[],en1

NEXT

FOR lp=1 TO Count

  en2.enx=Xpos + (space * (lp-1) )
  en2.eny=Ypos+32
  en2.status=0
  DIMPUSH enemy2[],en2

NEXT

FOR lp=1 TO Count

  en2.enx=Xpos + (space * (lp-1) )
  en2.eny=Ypos+64
  en2.status=0
  DIMPUSH enemy2[],en2

NEXT


FOR lp=1 TO Count

  en3.enx=Xpos + (space * (lp-1) )
  en3.eny=Ypos+96
  en3.status=0
  DIMPUSH enemy3[],en3

NEXT

FOR lp=1 TO Count

  en3.enx=Xpos + (space * (lp-1) )
  en3.eny=Ypos+128
  en3.status=0
  DIMPUSH enemy3[],en3

NEXT

ENDFUNCTION

FUNCTION DrawTheScreen:

FOREACH bull IN bullets[]
DRAWSPRITE 1,bull.bulx,bull.buly
NEXT

FOREACH en1 IN enemy1[]
DRAWANIM 2,en1.frame,en1.enx,en1.eny
NEXT

FOREACH en2 IN enemy2[]
DRAWANIM 3,en2.frame,en2.enx,en2.eny
NEXT

FOREACH en3 IN enemy3[]
DRAWANIM 4,en3.frame,en3.enx,en3.eny
NEXT



DRAWANIM 0,0,px,py

SHOWSCREEN

ENDFUNCTION

Moru

#1
You limit frames per second to 2. This needs to be at least 25 to get any sort of smothness.

I'm guessing this is because animations will be too fast if you set higher FPS limit. Try this:
http://www.glbasic.com/forum/index.php?topic=2409.msg18208#msg18208

hanyon21

Thank you very much.
I changed alien image files.
Original frame has one frame per one image.
So I increased ten frames per each emage and did set LIMITFPS to 25.

For example, changed alien1 frame is below.

Moru

That will give you a lot of work if you want more enemies or other images. Just use a timer to keep track of if it's time to animate or not :-)

mentalthink

hanyon21 , I think you area bit confused whit Limit FPS...

This don't says, hey put me only 25 frames... this it's only the maximun Speed for refresh the screen... You normaly
have to put this at 60 FPS or simply don't add the command it's the same... If you put -1, then you game will run the most quick runs your computer...

Don't care about your number of graphics, in example in my game I have perhaps 200 and 3D, and I put at 45FPS, because the game how have a lot of things at 60FPS in some cases I nottice how ralentize and this it's ugly for the gamer...

Put the FPS a bit down, only it's if your game have a lot of things, and don't runs fine at 60FPS, but don't have any relation whit number of frames or numbers or audio files... nothing of this...

Change to 60 FPS, and you will show your game, very very smooth, don't care for make a bit down the FPS, you have to agree a lot , but a lot of graphics, physics 3d and all can do GLBasic.

matchy

#5
S'invader - better than pong.  :D I really love the idea of one cut and pasting fresh complete game code. It's like the closest thing to typing a game from an 80s magazine.  :-[

Although to get it running aside the slowness, I had the change the filenames to png, put then in the media folder and set that. Created a bullet png also but there was no collision with the aliens. These can be place on one png image.   8) Anyhow, it's cool and perhaps more fun if there's some simple thumping music, pew pew lazer sounds and scoring in the next version?  :whistle:

spacefractal

add a animation timer to your aliens, rather than limit that by LIMITFPS. So its correct what other say, the game did simply just run 2 frames per second as you have set it to.

in my games im often uses 30fps as Greedy Mouse was set to, due its uses very much graphics. A Space invarders game like this should go for 60fps without issue.

Genius.Greedy Mouse - Karma Miwa - Spot Race - CatchOut - PowerUp Elevation - The beagle Jam - Cave Heroes 2023 - https://spacefractal.itch.io/

Darmakwolf

Attached you'll find your project plus some improvements, such as adjusting the bullet starting point and making the aliens have an animation timer so you don't waste time making all those frames. Enjoy!

CW

Good job Damra!

You have the start of a nice little game there, Hanyon.
It looks very good.
-CW

erico

Hello hanyon21, welcome aboard.
I did a bit pixelling with your first alien sprite, one flat another shaded, enjoy!

erico

Oh heck :P
and then, more aliens and a ship... aliens aliens aliens

erico


fuzzy70

@erico, seems you had some fun there mate with the gfx  :good:

BTW you forgot the bullets & the mothership that flies along the top of the screen  :whip:  :D :D :D

Lee
"Why don't you just make ten louder and make ten be the top number and make that a little louder?"
- "These go to eleven."

This Is Spinal Tap (1984)

erico

Got carried away...trying to look at something else other then android to easy up my mind! ;)

Matchy said he did the bullet... :P
Mother ship needs a reference. :-[

The main ship also did not come out good, 8-bit ships can only be shaded sydeways maybe?


fuzzy70

You don't know the Space Invaders mothership  :blink:

Only joking,  =D

Here's the mothership for you so you can have another break from your Android stress  :D

Lee
"Why don't you just make ten louder and make ten be the top number and make that a little louder?"
- "These go to eleven."

This Is Spinal Tap (1984)