Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Awsome yet lagging shooting game
Index -> Programming, Turing -> Turing Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Raknarg




PostPosted: 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 Smile
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 Smile

Turing:


setscreen ("offscreenonly,graphics:1000;500")
var bullets, bulletsy, bullets2, bulletsy2 : flexible array 1 .. 0 of int
var deadbullet : flexible array 1 .. 0 of boolean
var enemy, enemyy : array 1 .. 5 of int
var windowy : array 1 .. 3 of int := init (201, 205, 201)
var windowx : array 1 .. 3 of int := init (11, 11, 16)
var trigger : array char of boolean
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)
end for

loop

    Input.KeyDown (trigger)

    if trigger ('g') and counter >= 50 then
        new bullets, upper (bullets) + 1
        new bulletsy, upper (bulletsy) + 1
        new deadbullet, upper (deadbullet) + 1
        bullets (upper (bullets)) := 0
        bulletsy (upper (bullets)) := y
        counter := 0
    end if

    if trigger (KEY_UP_ARROW) and y <= maxy then
        y := y + 2
        windowy (1) := windowy (1) + 2
        windowy (2) := windowy (2) + 2
        windowy (3) := windowy (3) + 2
    elsif trigger (KEY_DOWN_ARROW) and y >= 0 then
        y := y - 2
        windowy (1) := windowy (1) - 2
        windowy (2) := windowy (2) - 2
        windowy (3) := windowy (3) - 2
    end if
   
    colorback(7)

    Draw.FillBox (0, y - 5, 10, y + 5, darkgrey)
    Draw.FillBox (11, y - 5, 16, windowy (3), darkgrey)
    Draw.FillBox (17, y - 4, 20, windowy (3) - 1, green)
    Draw.FillPolygon (windowx, windowy, 3, cyan)

    for i : lower (bullets) .. upper (bullets)
        for j : 1 .. 5
            if ((bullets (i) < enemy (j) - 5 and bullets (i) > enemy (j) + 5) and
                    (bulletsy (i) >= enemyy (j) - 3 and bulletsy (i) <= enemyy (j) + 13)) or
                    bullets (i) > maxx then
                deadbullet (i) := true
                bullets (i) := 10000
            else
                deadbullet (i) := false
            end if
        end for

        if deadbullet (i) not= true then
            bullets (i) := bullets (i) + 3
            Draw.FillOval (bullets (i), bulletsy (i), 3, 3, brightgreen)
        end if

    end for

    for i : 1 .. 5

        enemy (i) := enemy (i) - 1

        Draw.FillBox (enemy (i), enemyy (i), enemy (i) + 10, enemyy (i) + 10, 12)

        for j : 1 .. upper (bullets)
            if ((bullets (j) < enemy (i) + 5 and bullets (j) > enemy (i) - 5) and
                    (bulletsy (j) >= enemyy (i) - 3 and bulletsy (j) <= enemyy (i) + 13)) then
                enemy (i) := Rand.Int (maxx, maxx + 100)
                enemyy (i) := Rand.Int (0, maxy - 10)
                score := score + 10
            end if
        end for

        if enemy (i) < 0 then
            enemy (i) := maxx
            enemyy (i) := Rand.Int (0, maxy - 10)
        end if

    end for

    Font.Draw ("Score: " + intstr (score), 0, maxy - 10, scorefont, white)
    View.Update

    counter := counter + 1

    delay (5)
    cls

end loop



turing 4.1, btw
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: 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




PostPosted: 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




PostPosted: 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




PostPosted: 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




PostPosted: 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




PostPosted: 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




PostPosted: 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
Sponsor
sponsor
Lucas




PostPosted: 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




PostPosted: 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




PostPosted: 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




PostPosted: Fri Mar 25, 2011 9:07 pm   Post subject: RE:Awsome yet lagging shooting game

K, thanks, i'll look into records then.
Dragon20942




PostPosted: 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




PostPosted: 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




PostPosted: 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.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 18 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: