How can I insert a console in to a pyGame Window?

2019-02-19 15:08发布

问题:

I'm making a text adventure, and I want to have pyGame animations and illustrations and a HUD!

How can I insert this console?

Thanks!

回答1:

I'm pretty sure that's impossible. If you want a console within a Pygame screen then you'll have to write your own, or find one written by someone else (e.g. http://pygame.org/project-pygame-console-287-.html)



回答2:

For your game, you can use subsurface, for the different screen 'sections'.

Using python 3x will have issues with multiple libraries, that are not precompiled for you. If you can, it will simplify things to Use 2.7 or 2.6. (There is a python2.7 binary, but not on the front page)

A console isn't too hard. You need to break down the components, deciding what you need. Start with a miniproject, implementing features one at a time.

  1. keyboard input, print letters to console
  2. render text from a string
    1. blit cached text. will have demo code later, if you are interested
  3. dict() of strings, for commands, with values of function names.
  4. draw the last 10 lines of text
  5. up = scroll through command history
  6. allow command aliases, like "n" and "north" will point to move_north
    1. Implement this using a class: Command() . Which stores a list of all aliases.

commands = { "n" : move_north, "s" : move_south, "fps" : toggle_fps, "help" : print_help }

On enter, call the dict's value, if key exists:

if cmd in commands:
    commands[cmd]()
    # same as commands["n"]()

You could even have the console's print_help() use the function docstrings.