GLBasic forum

Main forum => GLBasic - en => Topic started by: hanyon21 on 2013-Mar-14

Title: Why is my program so slow?
Post by: hanyon21 on 2013-Mar-14
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
Title: Re: Why is my program so slow?
Post by: Moru on 2013-Mar-14
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
Title: Re: Re: Why is my program so slow?
Post by: hanyon21 on 2013-Mar-14
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.
Title: Re: Why is my program so slow?
Post by: Moru on 2013-Mar-14
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 :-)
Title: Re: Why is my program so slow?
Post by: mentalthink on 2013-Mar-14
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.
Title: Re: Why is my program so slow?
Post by: matchy on 2013-Mar-14
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:
Title: Re: Why is my program so slow?
Post by: spacefractal on 2013-Mar-14
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.

Title: Updated your program with an animation timer
Post by: Darmakwolf on 2013-Mar-14
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!
Title: Re: Why is my program so slow?
Post by: CW on 2013-Mar-15
Good job Damra!

You have the start of a nice little game there, Hanyon.
It looks very good.
-CW
Title: Re: Why is my program so slow?
Post by: erico on 2013-Mar-16
Hello hanyon21, welcome aboard.
I did a bit pixelling with your first alien sprite, one flat another shaded, enjoy!
Title: Re: Why is my program so slow?
Post by: erico on 2013-Mar-16
Oh heck :P
and then, more aliens and a ship... aliens aliens aliens
Title: Re: Why is my program so slow?
Post by: erico on 2013-Mar-16
ops, the ship!
Title: Re: Why is my program so slow?
Post by: fuzzy70 on 2013-Mar-16
@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
Title: Re: Why is my program so slow?
Post by: erico on 2013-Mar-16
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?

Title: Re: Why is my program so slow?
Post by: fuzzy70 on 2013-Mar-17
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
Title: Re: Why is my program so slow?
Post by: erico on 2013-Mar-17
What! What is that?? Black mothership over white background?? Never seen it before, sorry can´t work non-standard references!  :D

Hehe, here goes the mother ship and another version of the main ship, not quite ok, but it is better now.
Title: Re: Why is my program so slow?
Post by: erico on 2013-Mar-17
I got around to do a better old school ship this time, here it goes.... :D :D :D
What the heck they were thinking when they drew the ship in space invaders?

ps: sorry hanyon21, I guess I invaded your post quite bad :-[
Title: Re: Why is my program so slow?
Post by: matchy on 2013-Mar-18
Quote from: erico on 2013-Mar-16
Matchy said he did the bullet... :P
No, I just put a yellow block for testing. Besides I wonder what hanyon21 thinks of this friendly sprite hijack.  :D :D
Title: Re: Why is my program so slow?
Post by: CW on 2013-Mar-18
Can you do a mother ship that looks like a Mardi gras float? Or maybe a school bus? lol
J.K. They look really good.
-CW

PS, what should we call the game? "Space-Invaders" is taken.
How about "The Not-From-Earth-Interlopers"?
Hmm.. sort of rolls of the tongue.
Title: Re: Why is my program so slow?
Post by: fuzzy70 on 2013-Mar-18
 :offtopic:

O.K seeing as the original poster was having problems with their code & somehow managed to get graphics made for them, If I posted some graphics could  someone code the game for me  :D :D

Lee
Title: Re: Why is my program so slow?
Post by: Ian Price on 2013-Mar-18
QuoteIf I posted some graphics could  someone code the game for me  :D :D
That would make a nice change. I'm free at the moment.   :D :P
Title: Re: Why is my program so slow?
Post by: spacefractal on 2013-Mar-18
hehe. Im do guess he is now more aware what LIMITFPS actuelly does. Like my new game, placeholder graphics is allways nice to have, and a Space Invarder game is a cool game to start with as a beginner.
Title: Re: Why is my program so slow?
Post by: erico on 2013-Mar-18
Sure shot, I hope this humble set of sprites help out to motivate, also for any new user.
It could be used on the GLB program examples and also be incorporated on Mr.Tatoad´s books (in a diskette?  :o).
Personally I prefer the flat versions over the shaded ones, but who knows?

I too, am happy to see new users posting whole programs like this.

Quote from: Ian Price on 2013-Mar-18
QuoteIf I posted some graphics could  someone code the game for me  :D :D
That would make a nice change. I'm free at the moment.   :D :P

:offtopic: What is wrong with the "I have an idea and need code+sound+gfx+etc" :D :D

One good way to extend the "Invaders from Space" ;) game would be to have a lot of aliens coming and able to shot a lot with your ship, but every destroyed alien carcass would fall down too, making it a tough challenge between the will to power zap and avoid the falling leftovers and bullets. (alien leftovers are undestructable here). I did start a bit of gfx on this idea but had to get back to the android-game before I have to go back to real-life-work :( But it was great fun!
Title: Re: Why is my program so slow?
Post by: CW on 2013-Mar-19
Quote
What is wrong with the "I have an idea and need code+sound+gfx+etc"

There is nothing wrong with it. That's why KittyHello needs to get busy on the DOITALLFORME$( ) command.
You just describe what you want, and GLBasic does the rest.

-CW
Title: Re: Why is my program so slow?
Post by: matchy on 2013-Mar-19
Sounds like a good excuse to spark up a mini-challenge topic in the Competitions thread. :-[
Title: Re: Why is my program so slow?
Post by: erico on 2013-Mar-19
That is so tempting! hanyon21, what do you think of all this?
Title: Re: Why is my program so slow?
Post by: fuzzy70 on 2013-Mar-19
Quote from: erico on 2013-Mar-19
That is so tempting! hanyon21, what do you think of all this?

We have probably scared hanyon21 off, poor person has only posted twice & doubt they expected it to "Expand" so to speak into what it has. God I love this forum  =D =D

Lee
Title: Re: Why is my program so slow?
Post by: erico on 2013-Mar-21
Well, hopefully he is just busy and will come back anytime :D
The quick-compo is tempting...I wish I had time to raise it up. Maybe between finishing this one game going and starting another will be a good time. :P
Title: Re: Why is my program so slow?
Post by: Darmakwolf on 2013-Apr-02
I think we scared him off...  :|
Title: Re: Why is my program so slow?
Post by: fuzzy70 on 2013-Apr-02
Quote from: Darmakwolf on 2013-Apr-02
I think we scared him off...  :|

:D :D :D

Lee