How to accept text input from a pygame GUI - python

I am working on a RPG game using Python and Pygame, and am trying to make a two-part GUI, including a lower part that is like the basic command line, and a top part that will show all graphical "action."
What I need to find out is a way to include both in a pygame window, instead of using a pygame window and terminal window. As well, are there any Pygame GUI toolkits that would be appropriate for this use?
Thanks All!

May I suggest using ezText instead?
It's a cool way to add text inupt bars to pygame. I used it before my self, and It's really easy to use.
http://www.pygame.org/project-EzText-920-.html
(feel free to leave a comment if you want help using it, although everything you need to know is in the example.py that comes with it)

Take a look here (http://wiki.python.org/moin/PythonGameLibraries) for a whole list of ToolKits for both Pygame and Pyglet. Albow (http://www.cosc.canterbury.ac.nz/greg.ewing/python/Albow/) has worked well for me in the past.
The easiest way to accomplish what you're talking about would be to make two Surfaces, one for each part of the interface, and then constantly update them in separate modules to finally blit them every frame. That way your main module can be simplified to something like:
import action_gui
import cl_gui
import pygame
pygame.init()
MAIN_SURF = pygame.display.set_mode((x, y))
pygame.display.set_caption('My Game')
while (True):
action_surf = action_gui.update()
cl_surf = cl_gui.update()
MAIN_SURF.blit(action_surf, my_position_1)
MAIN_SURF.blit(cl_surf, my_position_2)
Best of luck.

Related

How to make games that use graphic instead of text?

Hi I was wondering if anyone could answer my question, how would I go about making a game with graphics in python instead of text based games.
I'm only new though so if it involves another programming language then I'm probably can't do it yet.
Use tkinter module inside of python. To do this simply add from tkinter import * at the start of your program and open a window and use a mainloop to close it at the end. You can research how to use tkinter online on various different websites. I would suggest effbot as it's very useful for tkinter basics.
I would recommend using the tkinter library. You can makes games with graphics instead of text. One example would be here. And it is just Python :)

Python hide already printed text

I'm creating a simple two-player board game where each player must place pieces on their own boards. What I would like to do is by either:
opening a new terminal window (regardless which OS the program is run on) for both players so that the board is saved within a variable but the other player cannot scroll up to see where they placed their pieces.
clearing the current terminal completely so that neither player could scroll and see the other player's board. I am aware of the unix 'clear' command but it doesn't achieve the effect I'm after and doesn't work with all OS's (though this might be something that I'll have to sacrifice to get a working solution)
I have tried clearing the screen but haven't been able to completely remove all the text. I don't have a preference; whichever method is easier. Also, if it would be easier to use a different method that I haven't thought of, all other suggestions are welcome. Thanks in advance!
EDIT: Other solutions give the appearance that text has been cleared but a user could still scroll up and see the text that was cleared. I'd like a way to remove any way that a user could see this text.
EDIT 2: Please read the other answers and the comments as they provide a lot of information about the topic as a whole. In particular, thanks to #zondo.
Consider using a portable terminal handling library. They abstract away the system specifica of common tasks like erasing the "screen" (i.e. terminal), or placing output at a specific position on the "screen" (again, meaning the text terminal). However, to use such a library effectively, you often have to switch to its style of generating output on the screen instead of naively printing strings.
curses is one such library (based on the C library ncurses) and included in the Python standard library. To get started, be sure to have a look at the curses tutorial in the official Python documentation.
I'd personally just use this.
import os
os.system("cls" if os.name == "nt" else "clear") #"cls" for Windows, otherwise "clear"
I would recomend a simple ANSI escape code to move the cursor position, Cursor Escape Codes, to the start of the board everytime. There is also an ANSI escape code that completly clears the console though, so you can choose.
If you are on windows you must first import colorama a module that makes windows prompt be able to use the ANSI codes as such:
import colorama # OR: from colorama import init
colorama.init() # AND THEN: init()
So if your board has n rows, after the user input for their turn, you move the cursor UP n rows + however many were required for user input, so if you wrote Input row, col: ... then you would go UP n+1, etc...
A simple example:
numLines = 1
print("Hello world!")
print("\033[<{0}>A".format(numLines), "This came AFTER hello world line")
You may not like this, it's a bit higher level than a basic two player board game, but there is always using some sort of GUI.
I personally like tkinter myself.
You don't want the option of people scrolling up to see printed text, but you can't remove what has been printed, that's like asking a printer to remove ink off a page. It's going to stay there.
Research a GUI interface, and try and make the game in that. Otherwise, you could let me take a stab at creating a explanatory piece of code that shows you how to use tkinter. If you do, link me the game you have so I can understand what you want.

Character Control using Python

I was wondering if anyone knows how to import a sprite and let it move when pressing the arrow keys in python, without using pygame, or some other library. This is purely out of curiosity, because I was just thinking about some sort of personal challenge, and that's what came to mind: a python game, without the help of pygame or libtcod. Is this possible, or do you need a library to do this for you? I'd appreciate anyone's input on this one.
Thanks.
What comes to my mind is using Tkinter's Canvas class. It is quite possible to do a simple program like that described using bindings on this class.
A good example of this may be found in Mark Lutz's Programming Python (http://shop.oreilly.com/product/9780596158118.do), the Moving Pics example.
If you don't own this, you can still look at the code for it as a reference by downloading the code using the link on the right side of the page. The path to the appropriat folder is /PP4E-Examples-1.3.1/Examples/PP4E/Gui/MovingPics/.

Is there anything I need aware of using Tkinter and pygame together?

I am using pygame to write a program and I need some GUI configuration text field and button for control. I've already made the button using pygame, but I just can write a text field out of pygame. Maybe I need to use tkinter together with pygame.
I think if there is no way to made to pygame part and tkinter part together in 1 window, then I could put them into 2 separate windows.
I hope the tkinter part can update the global variable in my pygame part, would there if any problem? I might create a child process of tkinter from the pygame part so that the tkinter part can probably "see" the global variable in pygame part and modify them.
Can I do this? Are there any pitfalls?
Both Tkinter and Pygame have their own event loops, so doing what you want is far from simple. The problem is that Pygame wants to control both the screen and the events the user feeds into the computer. This doesn't work well with GUI libraries, which also want to be "in control".
I would suggest sticking with Pygame, it has some nice GUI toolkits that will help you create buttons and other controls. Go over this page - it should clear things out. You may also find this discussion useful.
Apart from the practical aspects, a GUI created with Pygame is also IMHO more suitable for a game than something done with Tkinter, since games usually have original, thematical user interfaces and not the bland "text box + button" windows we're used to in other applications.
Take a look at some of the sample games on the Pygame wiki, many have GUIs and you can borrow ideas and code from them.
from tkinter import *
import pygame
import random
import os
global playing
playing=False
def playpause():
global playing
if playing==True:
playing=False
else:
playing=True
root = Tk()
embed = Frame(root, width=640, height=480)
embed.grid(row=0,column=2)
playpausebutton=Button(root, command=playpause, text="Play/Pause")
playpausebutton.grid(row=1,column=2)
root.update()
os.environ['SDL_WINDOWID'] = str(embed.winfo_id())
os.environ['SDL_VIDEODRIVER'] = 'windib'
pygame.display.init()
screen = pygame.display.set_mode((640,480))
pygame.display.flip()
while True:
#your code here
if playing:
screen.fill((random.randint(0,255),random.randint(0,255),random.randint(0,255)))
pygame.display.flip()
root.update()
This works just great, I have used this method successfully in multiple cases.
I have also found pgu is awful. However, what you say about the tkinter event loop taking control is wrong. You just call root.update instead of mainloop, and this can go inside of a while loop to replace the mainloop. Answering your main question however, there is something you should be aware of. It seems that whenever I run the two programs alongside each other, this traceback occurs:
TclError: expected boolean value but got "-1"
Fatal Python error: (pygame parachute) Segmentation Fault
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
Because of this I would avoid the combination, although I suspect this particular issue may pertain to my use of threads.

Text-based game graphics in Python

I'm pretty new to programming, and I'm creating a simple text-based game.>
I'm wondering if there is a simple way to create my own terminal-type window with which I can place coloured input etc.
Is there a graphics module well suited to this?
I'm using Mac, but I would like it to work on Windows as well
Thanks
The Tkinter Text Widget will do what you ask. the IDLE main window is implemented as one, if you want to play with an example.
You could use the termcolor library - it that what you're looking for?
On Windows things are trickier. See this SO answer - you should resort to win32console and some ctypes. The answer has some code and links to other articles.
For game programming with Python, I would always recommend PyGame.
It is not very complex and enables you to easily use input, graphics and sound.
As a start:
http://www.penzilla.net/tutorials/python/pygame/

Categories