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

删除回忆录丶 提交于 2019-12-01 09:36:39

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)

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!