Moles in Holes

 Moles in holes:

  • The concept:

I wanted to make a minigame with a little more polish this time, so I decided to make a configurable wack-a-mole game. The game itself is pretty standard: Click on a mole, it goes away and you get a point. Missclick, and you lose a point. 

  • The challenge:

I wanted to put some effort into making sure the code was super clean. One way in which I did this was using helper functions to avoid duplicating long statements. For example:

    def GameCoordToScreenX(self, x):
        return (x * self.hole.get_size()[0] + (x + 1) * XSPACING)

    def GameCoordToScreenY(self, y):
        return (y * self.hole.get_size()[1] + (y + 1) * YSPACING + TOPBAR)

    def GameCoordToScreenPos(self, pos):
        return (self.GameCoordToScreenX(pos[0]), self.GameCoordToScreenY(pos[1]))

These functions allowed me to avoid hard to read functions, especially in cases when x and y are already somewhat complicated, such as the position of moles (m.pos[0]). It's much easier to just write this once than multiple times in a single statement. The maxim for when you should make something a function that I usually apply is if you're doing it multiple times (or you might be in the future), it's better as a function. However, I think there's also an argument for making functions simply to increase readabilty. One case where I'll regularly do this is in the main loop of a game; calling functions for drawing and reading input makes the general flow of the code far more readable than the mess you'd get if you tried to inline those functions, even though they are only called once.

  • The result:


 

A wack-a-mole game! It's configurable and you can restart it, and I replaced the cursor with a hammer which I think adds a lot to the game. I think it gives basically what you'd want from a wack-a-mole game, and I'm happy with how the spawning works too. See what you think!: https://reddragonmakesgames.itch.io/moles-in-holes. The source code is on github.

Comments

Popular posts from this blog

Armageddump (Boss Rush Jam 2023)

Introducing: Minigame Monday! (3D Minesweeper)

Minigame Monday: Covert Behaviour