this is my first question. What I would like to achieve is in a normal window for the text-based game to be running, however I would also like to have a pygame window running as well that shows a map that updates. Thank you in advance.
4 Answers
Likely in the case of Pygame, you can have simultaneous output to the graphical window and the standard text output.
The challenge will be how to obtain user input asynchronously (without blocking).
While threads
are a good solution, there are others.
There are several solutions to that particular challenge in the following question: Non-blocking read on a subprocess.PIPE in python
Another idea is to get the input in the graphical window.
-
I disagree with the downvote, and insist that threads imply quite a lot of complexity. However I'm toning down the answer, thanks for your feedback. – icarito Sep 30 '16 at 06:19
-
Well, things put into perspective, your are entirely right : this is the way to perform async IO. – Flint Sep 30 '16 at 08:11
Pygame only supports one display. However, you could divide the screen into multiple Surfaces where one Surface is for the map and the other for the game.
Something like this:
screen = pygame.display.set_mode((100, 100))
top_rect = pygame.Rect((0, 0), (screen.get_width(), screen.get_height() // 2))
bottom_rect = pygame.Rect((0, screen.get_height() // 2), (screen.get_width(), screen.get_height() // 2))
top_screen = screen.subsurface(top_rect)
bottom_screen = screen.subsurface(bottom_rect)
And update them:
screen.blit(top_screen)
screen.blit(bottom_screen)
pygame.display.update()

- 9,146
- 4
- 29
- 50
-
It's a *text based* game, @Ted. Writing a simple command input is going to require a lot of work from him. – Flint Sep 17 '16 at 22:36
-
Yes it will require tremendous work, but I felt the question was more directed to whether you could have two windows or not. How to implement text and a map might be another question. Your answer is the simplest to start with but it assumes that the console window is a valid window (it might not be if the game is supposed to be distributed). – Ted Klein Bergman Sep 18 '16 at 05:59
The only way to have the text entries separate to the pygame window is to use someVar = input("A string")
so the text input is in the python shell or the command window/Linux terminal and then have pygame reference that var.

- 17
- 1
- 6
By normal window, I guess you mean the console window from which your program runs.
You need a thread.
In the following example, that thread is the one reading the standard input from the command line.
from threading import Thread
userInput= None
def readInput():
while True:
userInput = raw_input (" where to ? ")
print("%s ok."%userInput)
t = Thread( target=readInput )
t.start()
You could also have the while loop as the main loop of your program, and the thread would run the pygame loop. Or even have two threads, one for both.

- 1,651
- 1
- 19
- 29