Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Making a scrolling shooter; how to limit one shot on screen at a time?

This thread is locked; no one can reply to it. rss feed Print
Making a scrolling shooter; how to limit one shot on screen at a time?
Bart95
Member #15,562
March 2014

Currently, pressing left click will instantly reset and 'fire' the missile object regardless of it's current position. If it's half-way up the screen, for example, and I left click again it 'teleports' back to the playership and starts moving up again.

I've fiddled with some tutorials and such, but none I've found seem to indicate how to limit the game so only one shot can appear on screen at a time/disallow firing again until the shot is off-screen or destroyed by an enemy (though the latter is irrelevant currently as I do not have any enemies yet).

I can post code if required when I get home.

P.S: Using Allegro 4.2

pkrcel
Member #14,001
February 2012

Bart95 said:

P.S: Using Allegro 4.2

Going with the new trend, I'd warmly suggest to upgrade to allegro 5.

Even thou, maybe you're doing this in an educational environment (meaning you have class material which is already written for Allegro 4.2)?

Anyway, if you want only one (or any fixed number) "shot" appear on the screen at a time, you have to save the state of the alive bullet(s) somewhere and prevent a new one is created till the old one is no longer live.

pseudocode:

#SelectExpand
1 2[...inside input manager...] 3//here the user has just pressed FIRE, we fire a new shot only if the onld one is no longer live 4 if (EVENT_RECEIVED == USER_PRESSED_FIRE) && !bullet.live { 5 create_new_bullet(); 6 apply_initialization_to_new_bullet(); 7 bullet.live = true; 8 } 9 10[...inside collision manager...] 11// 12 if (check_bullet_collision()) { 13 resolve_collision_as_usual(); 14 bullet.live = false; 15 }

If you want more than one bullet you need to track the current numenr of live bullets.

The tutorials from Mike Geig here do talk about this, have a look.

It is unlikely that Google shares your distaste for capitalism. - Derezo
If one had the eternity of time, one would do things later. - Johan Halmén

Bart95
Member #15,562
March 2014

Sadly, my uni is adamant that 4.2 is used, likely because there's plenty of documentation on it already and they don't fancy having to readjust and relearn the new version. :P

In any case, good tip, will give it a try tomorrow along with any other suggestions.

Thomas Fjellstrom
Member #476
June 2000
avatar

Bart95 said:

Sadly, my uni is adamant that 4.2 is used, likely because there's plenty of documentation on it already and they don't fancy having to readjust and relearn the new version. :P

4.4 is 4.2 with some fixes and a few extra bits. Same api.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Bart95
Member #15,562
March 2014

Sadly, I don't get to make the units.

beoran
Member #12,636
March 2011

Sorry to be unhelpful but I'm facepalming at unis requiring the use of Allegro 4.2... I'd actually like to contact your professors and ask them (respectfully) if there would be willing to consider upgrading to Allegro 5, since Allegro 4 is being deprecated.

Thomas Fjellstrom
Member #476
June 2000
avatar

Or at the very least 4.4, since there are many bug fixes and additions that would likely be very useful to them. At least with 4.4 they wouldn't have to redo their entire course.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Bart95
Member #15,562
March 2014

Heh, I'll direct them to this forum tomorrow and see what they have to say. :P

l j
Member #10,584
January 2009
avatar

I had a teacher who taught us Zend Framework 1 because upgrading to 2 would mean he had to find a new book on it and rewrite his lessons. A really lazy teacher :p, too many out there, at least around here. I hope yours does like innovation.

But yes, Allegro 4.4 is just like allegro 4.2 but improved and some bugs were fixed if I recall.

Bart95
Member #15,562
March 2014

Well so far we haven't had any issues or such with 4.2, perhaps there's something about it that makes it suitable for beginners to progrumming?

StevenVI
Member #562
July 2000
avatar

No "teacher" or "educator" worth his beans should behave like that . . . Every upgrade with the Allegro library, he should follow up with, and adapt his class with.

What is wrong with learning to do something with tried and true methods?

The purpose of university is not to learn APIs. It's to learn how to think. You're expected to learn the specifics of what interests you on your own time.

__________________________________________________
Skoobalon Software
[ Lander! v2.5 ] [ Zonic the Hog v1.1 ] [ Raid 2 v1.0 ]

l j
Member #10,584
January 2009
avatar

StevenVI said:

The purpose of university is not to learn APIs. It's to learn how to think. You're expected to learn the specifics of what interests you on your own time.

The very purpose of that course however, was to learn Zend because it's such a popular framework, not to learn how to think. Given that the teacher himself openly stated that we were still using Zend 1 because he did not want to bother upgrading, speaks not well for him.

However, your reasoning is why allegro 4 is probably still popular. Almost no boilerplate, you just have to call an init method and set the graphics mode and you can start drawing. Allegro 5 requires you to set up an event queue and register event sources and handle events. While it's not hard and not too much it takes away from the core purpose of teaching how to solve problems.

beoran
Member #12,636
March 2011

Hmm, this suggests we need something like a bool al_easy_init(ALLEGRO_DISPLAY **, ALLEGRO_EVENT_QUEUE ** queue, int screen_w, int screen_h, bool fullscreen) function or such for similar use cases... I think I'll suggest it for 5.1 when I get to working it.

Yodhe23
Member #8,726
June 2007

University is just another industry/con-trick these days.

A teacher still teaching 4.2 without bothering to even up-date to 4.4 is not worth his salary at university level, and is doing a disservice to both his students, and the faculty.

www.justanotherturn.com

Bart95
Member #15,562
March 2014

I have since managed to resolve this!

As for teaching 4.2, I prodded my lecturer about this. He said that as far as he's aware they're only using it to teach the fundamentals and logic behind programming using a pre-made library, not that he's teaching specifically to use Allegro 4.2.

beoran
Member #12,636
March 2011

Well, then I think you should ask his permission to use Allegro 5 (edit or at least 4.4), if the library doesn't matter. If he's true to his word he shouldn't have any problems with that.

Bart95
Member #15,562
March 2014

After this assignment, I don't think we're touching Allegro again as far as I know anyway. :L

pkrcel
Member #14,001
February 2012

Not at all, he has to give consistent assignments, and using different tools woudl lead to not comparable result, or a too broad spectrum of choices.

There's nothing wrong in usng 4.2 for educational purposes, as long as they stick to Allegro, besides they are not teaching Allegro itself.

This assignemtn is NOT to teach a modern API but to have the students get on simple problems and learn how to think and solve, StevenVI was right to the point.

While it would be good anyway an upgrade to Allegro5, questioning the use of A4 in this context would be comparable to (IMO) questioning the use of a blackboard for quick sketching or going with hand-count for simple arithmetics as opposed to using a calculator, or maybe doing some Matrix Algebra at hand instead of begining right into MatLab.

Anyway in Taron's case no excuse is valid, if they're teaching you 'something' as a target (like a framework) the most (reasonably!) recent material should be used...otherwise here's a fair share of laziness.

Bart95 said:

I have since managed to resolve this!

How did you achieve this? Do not post code, just try to explain.

It is unlikely that Google shares your distaste for capitalism. - Derezo
If one had the eternity of time, one would do things later. - Johan Halmén

Bart95
Member #15,562
March 2014

I can't recall the exact code anyway, heh.

Essentially I used a boolean status on the missile as to whether the projectile was 'alive', then tied it to an if statement that went something like;

if(Missile_y < 0)
{
Missile_alive=false
}

In addition, I added an extra requirement to the if statement used to fire;

if(<LMouse button pressed> && Missile_alive == false)
{
<Draw missile>
Missile_y -= 3
Missile_alive = true
}

Something along those lines anyway, the idea was to have the code check "Is the missile on screen?" and then allow the ability to fire again only when the missile is no longer 'alive' or on screen. The 'alive' status will also be applied in future when I begin to add collisions to enemies.

beoran
Member #12,636
March 2011

pckrel, well I guess what you say is fair enough...

However, I wonder if using or upgrading to Allegro 5 is really that much more complicated than using Allegro 4? If that is the case, we should think about how to close the gap... I'll stop whining on this thread about that though, sorry for going off topic.

l j
Member #10,584
January 2009
avatar

Bart95 said:

Something along those lines anyway, the idea was to have the code check "Is the missile on screen?" and then allow the ability to fire again only when the missile is no longer 'alive' or on screen. The 'alive' status will also be applied in future when I begin to add collisions to enemies.

Sounds like you've got the basic logic down.

pkrcel
Member #14,001
February 2012

beoran said:

However, I wonder if using or upgrading to Allegro 5 is really that much more complicated than using Allegro 4?

Don't think so...the teacher most probably already posted code in the class unit that's "boilerplate" for the A4 code, I guess that a competent coder would do just the same for the A5 boilerplate and face it, A5 is a bit more verbose but...nothing that young coders canont handle I'm sure....the point is, no need to rewrite solid intro material for the sake of upgrading.

I should point out that IN THE VERY LATE NINTIES my first CS class in uni taught me some VERY DANG BASICS in (rolling drums)...*MODULA-2* (!)

And I might say that is was good enuff to teach me what I needed to be taught, even thou I don't remeber squat about MODULA-2...apart from a nasty compiler bug that prevented me to use a file seek routine in my final project for the class and detracted me 0.3 on the final score. Umpf...I could have been annoyed if I woudl have been grumpy as I am today.

Quote:

I'll stop whining on this thread about that though, sorry for going off topic.

...well met beoran, you must be new here ;D

taron  said:

Sounds like you've got the basic logic down.

yep, good job!

It is unlikely that Google shares your distaste for capitalism. - Derezo
If one had the eternity of time, one would do things later. - Johan Halmén

Bart95
Member #15,562
March 2014

I'm good with the logic, having made a very similar game in game maker years ago. Just the actual code that tends to do my head in, now to try and implement a health bar. D: Already have a PlayerLife value, I have a bitmap image of a 'chunk' of health, just trying to figure out how to get three to appear and disappear in relation to PlayerLife (Maximum of 3, I'd love to do shields and the like, but no time, ha).

pkrcel
Member #14,001
February 2012

Well, for a very straight implementation you could reason just like as each chick was an object on its own like any other.

Each chuck is "alive" based on the value of PlayerLife and thus it's drawn accordingly..like:

#SelectExpand
1// Pseudocode 2 3int PlayerLife = 3 4LIFE_CHUNK *life_chunks[MAX_LIFE] 5 6[cut] 7 8//during update 9if (player_is_hit ) PlayerLife-- 10 11[later...] 12 13for(i=0; i<MAX_LIFE; ++i){ 14 if (i<=Playerlife) 15 life_chunk[i].live=true; 16 else 17 life_chunk[i].live=false; 18 19// when drawing 20 21for(i=0; i<MAX_LIFE; ++i){ 22 if (i<=Playerlife) 23 al_draw_bitmap(life_chunk[i])

....of course you can spot a weakness there....

It is unlikely that Google shares your distaste for capitalism. - Derezo
If one had the eternity of time, one would do things later. - Johan Halmén

Go to: