Posted: Thu Mar 24, 2011 6:59 am Post subject: Awsome yet lagging shooting game
I'm basically trying to make a kind of simple simple shooting game (for kicks, really) and it works fine. I'm using a flexible array for the bullets. For this, I add one to the upper limit of the array every time it shoots. There's just this one issue; I can't figure out how to stop the bullets existance, because eventually the amount of objects on the screen piles up so high, it starts to lag really bad.
I've tried to increase the lower bound of the array when the bullet is dead. However, if I do that, the game crashes after a few seconds.
Any ideas appreciated
If anything needs clearing up, just ask.
p.s. Thank you to Clayton, for giving me the ability to do such a thing. I never would've found out about flexible arrays without that tutorial
Turing:
setscreen("offscreenonly,graphics:1000;500") var bullets, bulletsy, bullets2, bulletsy2 :flexiblearray1.. 0ofint var deadbullet :flexiblearray1.. 0ofboolean var enemy, enemyy :array1.. 5ofint var windowy :array1.. 3ofint:=init(201, 205, 201) var windowx :array1.. 3ofint:=init(11, 11, 16) var trigger :arraycharofboolean var counter :int:=50 var y :int:=200 var score :=0 var scorefont :int:=Font.New("Ariel:10")
for i :1.. 5
enemy (i):= Rand.Int (maxx, maxx + 200)
enemyy (i):= Rand.Int (0, maxy - 10) endfor
Posted: Thu Mar 24, 2011 9:34 am Post subject: RE:Awsome yet lagging shooting game
Every time you resize your array, you are in effect doing the following:
1. Create a new array that is one greater in size than your previous array.
2. Copy every element from the old array into the new array.
3. Remove the old array.
If you do this every time a bullet is fired, it will eventually cost you a lot of time. You can mitigate this cost by making the increases a more reasonable number. Whenever you run out of bullet-positions in the array, double the size of the array (that is, don't resize for every single bullet, just for the ones where you no longer have enough space).
Of course, you should also remove or reuse "dead" bullets.
As a side issue, you should research Turing's record feature in the Turing Walkthrough: it will let you combine your bullets, bulletsy, bullets2, bulletsy2, and deadbullet arrays into a single, easier-to-manage array.
Raknarg
Posted: Thu Mar 24, 2011 4:38 pm Post subject: RE:Awsome yet lagging shooting game
Thank you, thats all i needed.
The deadbullet thing was for when i didn't know how to get rid of the array effectively, I just stopped them to reduce the lagging.
Lucas
Posted: Thu Mar 24, 2011 6:54 pm Post subject: RE:Awsome yet lagging shooting game
I did something like once, what i did was whenver a bullet went of screen, I made all the other bullets move down by one in my array that stored the information, this I could make an array of size 10 and not have to make it flexible, since for my game the max amount you could possible have at once on screen was 10. worked pretty good
SNIPERDUDE
Posted: Thu Mar 24, 2011 8:28 pm Post subject: RE:Awsome yet lagging shooting game
A good way to think of effects like bullets is create a mini engine for them similar in nature to a particle engine. Actually, if you end up using a particle engine (self-made one) just use that for bullets too, adding the necessary properties needed.
Just a thought.
Raknarg
Posted: Fri Mar 25, 2011 10:29 am Post subject: RE:Awsome yet lagging shooting game
Ok.
Quick question; Is it possible to change the bottom limit of a flexible array?
chrisbrown
Posted: Fri Mar 25, 2011 3:51 pm Post subject: RE:Awsome yet lagging shooting game
No. And here's a question for you, the answer of which should tell you why you should consider a different approach:
What happens when you shoot two bullets, and the second hits a short-range target before the first hits anything at all or goes off-screen?
SNIPERDUDE
Posted: Fri Mar 25, 2011 4:35 pm Post subject: RE:Awsome yet lagging shooting game
There are easy work-arounds, depends exactly how he implements it. I have my own ideas.
Sponsor Sponsor
Lucas
Posted: Fri Mar 25, 2011 5:06 pm Post subject: RE:Awsome yet lagging shooting game
@chrisbrown: the bullets ceep going regardless of if they hit an enemy or not, so why should a second bullet hitting an enemy before a first bullet goes of screen make any difference?
Raknarg
Posted: Fri Mar 25, 2011 5:45 pm Post subject: RE:Awsome yet lagging shooting game
Thats why i had originally implemented the deadbullet thing, attempting to keep track of bullets that had either gone off-screen or had already hit a target.
Obvs im rearranging it now, that was just v 1.0 of my program. I'll probably just use a regular array, like lucas said.
chrisbrown
Posted: Fri Mar 25, 2011 7:06 pm Post subject: RE:Awsome yet lagging shooting game
@Lucas:
I assumed he was planning to increase the lower bound by 1 once a bullet was no longer active, which only works when bullets are destroyed in the order they are created, i.e. in queue fashion.
And to comment on your earlier post, and also advice @Raknarg:
Shifting every array element whenever a bullet is destroyed is unnecessary and costly. Like DemonWasp said, use a record for each bullet, and have a boolean field called "dead" or something.
Raknarg
Posted: Fri Mar 25, 2011 9:07 pm Post subject: RE:Awsome yet lagging shooting game
K, thanks, i'll look into records then.
Dragon20942
Posted: Sat Mar 26, 2011 10:50 am Post subject: RE:Awsome yet lagging shooting game
"Flexible arrays", "lower", "upper", and "new" weren't taught to me. So my culminating had a lot more lines than it should have. I still dont know them!I think if I could modify your code and figure out some stuff, I could shorten my code by a good 500 lines or so.
SNIPERDUDE
Posted: Sat Mar 26, 2011 12:57 pm Post subject: RE:Awsome yet lagging shooting game
I found that High school hardly taught programming, and certainly not efficient code.
Raknarg
Posted: Sat Mar 26, 2011 2:40 pm Post subject: RE:Awsome yet lagging shooting game
Yeah... well, in my class for instance, the teacher could only teach the absolute basics, cause most people sucked and thought the course was an "easy mark". Me and Lucas basically got 100 in that class easily.