Re: Earwax

@136
OK, so your post raises some good questions, one which I thought the documentation should answer, but I'm guessing doesn't:

Firstly, about making different levels. When I make something big, I'm going to have each level in its own file. The main file will provide the main menu, like the file you get with the

earwax game

command.

Then you can import the first level and have that pushed:

from earwax import Game, Menu
from pyglet.window import Window
from first_level import first_level

game: Game = Game(name='Test Game')
main_menu: Menu = Menu(game, 'Main Menu')


@main_menu.item(title='Play Game')
def play_game() -> None:
    game.replace_level(main_level)

if __name__ == '__main__':
    window: Window = Window(caption=game.name)
    game.run(window, initial_level=main_menu)

You could import the second level in the file which contains the first menu, since that's where the second level will be accessible from most likely.

Keep doing that until you've got enough levels.

Could always put a pause menu in another file so you can return to it at any point.

You can always find the level that was pushed before this one with either

game.levels[-2]

, or (if you're tidy),

game.levels[0]

.

Also, you're not *forced* to do anything. I could just as easily split Lucky 13 up, but I kind of wanted to to be a 1-file job. Admittedly it's fairly long, but not the longest file in the world.

If you want to add actions on a level, there are three ways:

Firstly, make an ActionMap instance, and bind all your actions there, using either the decorator syntax (

@action_map.action

), or by subclassing ActionMap, and overriding its __attrs_post_init__ method.

Then you could use add_actions on any levels you want those actions to apply to.

Secondly, the decorator syntax you have already talked about: Initialise a level, and use @level.action.

Thirdly, subclass Level, BoxLevel or whatever, and override __attrs_post_init__, to add whatever action you like:

        self.action('Quit', symbol=key.Q)(self.game.quit)

The yield statements are something I thought was documented on the ActionMap.action method, but it's not. My bad, I've created an issue to track that.

In a nut shell: The code before the yield statement runs when the key, mouse button, joystick button, or joystick hat is pressed down. Any code after the yield runs when it is released.

In the example you pasted above, if there was no yield, then when you pressed the t key, you'd get the editor pop up, and a t added, because Pyglet's on_text event fires when the key is released.

It's particularly useful if you've got a menu which pops up another menu, so your enter key doesn't move you in the second menu, or whatever you pressed to load up the first menu doesn't perform a search.

I'm really sorry that was unclear, I've been using it for so long, I thought it was obvious, so a massive oversite on my part. Thank you for picking it up.

Finally, about state: You'd been keeping your level classes in separate files, so just initialise them with the game attribute of the previous level. You could always subclass

earwax.Game

to keep your state about, or just create another state object using something like

earwax.Config

, which can be saved, and is a good choice for including things like player options, because you can use a

earwax.ConfigMenu

to make a slightly ugly, but serviceable UI.

If you don't want the extra hassle of typing

.value

every time, you can simply subclass

earwax.mixins.DumpLoadMixin

for the job.

For example:

from pathlib import Path
from attr import attrs, attrib
from earwax import Game
from earwax.mixins import DumpLoadMixin

state_file: Path = Path('state.yaml')


@attrs(auto_attribs=True)
class GameState(DumpLoadMixin):
    player_name: str = 'Unnamed Player'
    player_health: int = 500
    # ...


@attrs(auto_attribs=True)
class MyGame(Game):
    state: GameState = attrib(repr=False)

    @state.default
    def get_default_state(instance: 'MyGame') -> GameState:
        return GameState.from_filename(state_file)

    def after_run(self) -> None:
        self.state.save(state_file)
        return super().after_run()

Please let me know if I've not covered something there, and thanks for bringing these things to my attention.

-- 
Audiogames-reflector mailing list
Audiogames-reflector@sabahattin-gucukoglu.com
https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
  • ... AudioGames . net Forum — Developers room : ashleygrobler04 via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : chrisnorman7 via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : ashleygrobler04 via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : chrisnorman7 via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : ashleygrobler04 via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : ashleygrobler04 via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : GrannyCheeseWheel via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : ashleygrobler04 via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : chrisnorman7 via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : chrisnorman7 via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : chrisnorman7 via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : chrisnorman7 via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : chrisnorman7 via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : BoundTo via Audiogames-reflector
  • ... AudioGames . net Forum — Developers room : amerikranian via Audiogames-reflector

Reply via email to