/Game-Console

Full-featured Pygame console that can be integrated in your python game in order to execute python command/scripts/custom functions

Primary LanguagePythonGNU General Public License v3.0GPL-3.0

Game-Console

Full-featured game console based on pygame that can be integrated in your python game in order to execute python command/scripts/custom in-gamefunctions

screenshot

Main Features

  • fully configurable look&feel via json - fonts, pictures, commands, console layouts, paddings, scrolling text and more
  • history of inputed commands available upon pressing up/down buttons
  • scrollable output available upon pressing pgUp/pgDown buttons and/or mouse wheel
  • configurable transparency and wallpaper support
  • optional header/footer text that can contain dynamic in-game values (time, fps, anything else)
  • header/footer layouts supporting scrolling and more
  • optional fluent animation during showing/hiding of the console
  • support for running custom scripts (batches of console commands)
  • support for colored texts, big inputs, big outputs
  • easy integration into your existing code

Running the code

All logic including example game is implemented in game_console.py file. Make sure you have pygame 1.9.4 installed and run the code.

  • You will see pygame window with rectancle movind in random directions - simulation of game

  • By pressing F1 button you can toggle on/off console

  • By pressing Esc key or closing the window or typing 'quick'/'exit' will end the program

How to use console features

Python Commands

When instantiating Console class you need to specify reference to the main game class that you want to manage. Instance of this class is then referenced as 'game'. Using console, you can then use standard python code with this instance. Python commands must start either with 'shell' keyword or simple exclamation mark '!'.

Examples of couple python commands that can be used with the example game are below:

  • shell print('Hello world') # Prints Hello world on console
  • !print('Hello world') # Same as above
  • !game # Prints <main.TestObject object at 0xXXXXXXXX> i.e. reference to the main object that is govern by the console
  • !game.pos = [10,10] # Changes position of the game rectancle
  • !game.surf.fill((0,0,0)) # Changes the color of the game rectancle from white to black
  • !game.console.padding.up = 30 # Changes the space between upper console corner and first console element (header or other depending on console layout settings)
  • !game.cons_get_time() # Prints output of the function on the console output.
  • !1+1 # Prints 2 on the output
  • ![a for a in range(10)] # Prints list of values from 0 to 9 on the output
  • !import os # Prints 'invalid syntax'. Such python operations are not allowed from the console due to security reasons

screenshot

Custom Commands

With game-console you can specify your own commands. For implementing new command called for example dummy that takes one parameter and prints it on the console in the blue color, you need to perform following steps:

  • implement new function called do_dummy in the class CommandLineProcessor
  • New function can look as follows
  • It is good idea to return some value in case of failure. This is important if your custom command is part of some console script (read further).

def do_dummy(self, params):
try:
self.output.write(params, (0,0,255))
return None
except Exception as E:
self.output.write(str(E))
return -1

There is already one custom functionimplemented in the example game move. The function takes 2 parameters delimited by comma and changes position of the main game rectancle.

screenshot

Generic Commands

All the non-python shell commands can be listed by typing help or simply ? on the command line. The example of generic command can be exit that simply quicks the game. Also by typing help move or simply ?move will list description of the command.

screenshot

Console Scripts

Python, Custom and Generic commands can be combined together into the file (one command on each line) and can be executed as a script.

Example of invoking such simple script is below:

script pygame_console/scripts/example.cns

screenshot

If there is an error on some line of the script, you are notified on the console with the error message - see below. The error code corresponds to the return value returned in the exception statement in the code of your custom function.

screenshot

Dynamic information in header/footer

As mentioned above, header or footer can display dynamic data. Those data are gained as a resulf of calling of some function of the main game class. In the example game, you will see time and game object position as some of the examples of such dynamic values.

If you want to have dynamic values in your console, you need to do the following:

  • Implement functions that return the requested values in string time somewhere in your game class. In case of our example game there are functions cons_get_pos() and cons_get_time(). Check the code for details.
  • Specify the function in configuration json. See function get_console_config() for examples of different configuration. Alo, see below parameters taxt and text_params where the functions and its placement in the scrolling text is defined.

screenshot

Changing console layout/configuration

A mentioned in the list of features, console enables heavy configuration. I suggest you to see example game function get_console_config() and get inspiration from 6 configurations that are predifined there. Below you can see the pictures of those configurations in the game.

Sample Layout 1

  • Scrolling dynamic text in the header and footer
  • Transparency of all console parts + wallpaper
  • Animation upon displaying hidding of the console set to 2s
  • Different fonts for different console parts

screenshot

Sample Layout 2

  • Header and footer omitted by configuration
  • Transparency of all console parts + wallpaper
  • Animation upon displaying hidding of the console - from the bottom - default time 100ms
  • Command line input is above console output part
  • Different fonts for different console parts

screenshot

Sample Layout 3

  • Totaly minimalistic - only header with dynamic text shown
  • No transparency, no wallpapers

screenshot

Sample Layout 4

  • Minimalistic - only header and footer with dynamic text shown
  • No transparency, no wallpapers
  • Header and footer are scrolling by different speed

screenshot

Sample Layout 5

  • Minimalistic - only input and header with dynamic text shown
  • No transparency, no wallpapers

screenshot

Sample Layout 6

  • Minimalistic - only input and output with transparency

screenshot

How to integrate console into the game

  • Prepare configuration JSON. Use sample configuration dicts in get_console_config() for inspiration
  • Instantiate console class
  • For switching console on/off call toggle() function
  • For reading the input keys and process them by console call update() function
  • For showing the console use show() function. Animation effect is processed internally.

screenshot screenshot