Windows Drawing Sprite to Represent Every Fifth of a Variable

L

Lee Hartsell

Guest
Hello everyone!

First time posting. I was hoping for some help or advice on how to tackle something specific in my game.
I'm designing a simple tower defense game for my design class.

I'll briefly run you through the concept for some context:
Human explorers have invaded your planet! To ensure your race's survival, you have decided to waylay the humans as they attempt to flee your planet. Prevent as many astronauts from reaching their ship as you can!
Since I'll be counting astronauts, player health starts at 0 and ends at a number based on the level.

What am I trying to do here is:
  • count how many astronauts have reached the ship
  • when that number is divisible by five -
  • - draw an astronaut sprite at the top left hand corner
  • continue this until level ends
To summarize: I'm trying to have an astronaut sprite drawn at the top left hand corner of the screen for every five that reach their ship.

Currently, my code doesn't quite do that. Instead, it draws one after the first astronaut, and then again after every fifth astronaut. So at 1, then 6, then at 11, and so on.

Here's my code as is (note: player health atm is 15):
Code:
var astros = (obj_Controller.playerHp/5);

  for (var i = 0; i < astros; i++)
    {
        draw_sprite(spr_playerHp, 0, (i * 32) + 32, 32)
        if ((obj_Controller.playerHp mod 5) == 0)
        {
            draw_sprite(spr_playerHp, 0, (i * 32) + 32, 32)
        }
    }

I've wasted enough time with this feature here, but it's one I'd really like to have.

Since this is a school project, I am just looking for tips, or suggestions for a more effective way to do this.
I'd appreciate advice or pseudo-code rather than just straight code!
Thanks a lot! :banana:
 

TheouAegis

Member
var astros = obj_controller.playerHp div 5;

Use div to get an integer, as opposed to a fraction which is what / will do.

This will make it not draw anything until 5 astronauts have entered the ship. If you still want an icon for 1-4, then change it to

var astros = (obj_controller.playerHp + 4) div 5;

So now it will draw 1 icon for 1-5 and 2 icons for 6-10.

For the rest of your code, you can just use one single draw_sprite() in your for loop. The conditional is no longer needed.
 

Genetix

Member
Perhaps instead of dividing by 5 have 2 separate variables. One for the overall amount of times that an astronaut has hit the ship, and another variable that resets to 0 every time it hits 5.

Everytime the 2nd variable hits 5, draw another astronaut to the top of the screen then reset that variable to 0 so you can use it again to count up to 5. You can use another variable to keep track of the total number.

Hope that makes sense.
 
A

Ampersand

Guest
astronauts = 0 ;

when one is rescued

astronauts += 1 ;

to draw how many groups of five you've saved

for(i = 0; i < astronauts div 5; i += 1)
{
draw_sprite(spr_astronaut, 0, gui_x + astro_spr_w * i, gui_y) ;
}
 
Last edited by a moderator:
L

Lee Hartsell

Guest
Thanks guys.

Not my first time coding, but for sure my first time getting into the guts of game maker and using GML. Appreciate the help.

I used Theou's suggestion for the moment. But I'm making note of the other methods, might have use for them later in another project, if not this one.

Knew I was on the right track, figured it was some kind of detail. Usually is, hehe.

Thanks again!
 

TheouAegis

Member
Ampersand's is wrong. i mod 5 would make it so it'd draw one icon for each astronaut UP TO 5. Every multiple of 5 wouldn't draw any at all.

Now, it's not entirely wrong, just not right. His method is how you'd draw partial health. For example, let's say you had a special sprite for when you have 5 astronauts and then a mini sprite for every astronaut not a multiple of 5. E.g.:

O O O : :

So if you had 19 astronauts, it'd draw like that. In that case astronauts mod 5 would be how many little guys to draw (you don't want i mod 5, though, just astronauts mod 5, or in your case obj_control.playerHp mod 5).
 
A

Ampersand

Guest
Ampersand's is wrong. i mod 5 would make it so it'd draw one icon for each astronaut UP TO 5. Every multiple of 5 wouldn't draw any at all.

Now, it's not entirely wrong, just not right. His method is how you'd draw partial health. For example, let's say you had a special sprite for when you have 5 astronauts and then a mini sprite for every astronaut not a multiple of 5. E.g.:

O O O : :

So if you had 19 astronauts, it'd draw like that. In that case astronauts mod 5 would be how many little guys to draw (you don't want i mod 5, though, just astronauts mod 5, or in your case obj_control.playerHp mod 5).
You're right, I misspoke. It's fixed. Only thing wrong was the expression in the for loop, I mixed up mod and div. Thanks for pointing that out :)
 
Last edited by a moderator:
Top