Python Forum
[PyGame] text-to-screen help - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Game Development (https://python-forum.io/forum-11.html)
+--- Thread: [PyGame] text-to-screen help (/thread-1548.html)



text-to-screen help - Zman350x - Jan-11-2017

hey I want to blit user input to screen, I know how to blit text:

(example, not real code)

text = font.render("Enter Text Here", True, color)
screen.blit(text,(position))

So, i put in:

(example, not real code)

input_variable = input("prompt")

text = font.render(input_variable, True, color)
screen.blit(text,(position))

but then a message comes up saying
"TypeError: text must be string or unicode"
it's talking about the line where I define text because input_variable is not in quotes, but when I put it in quotes the text appears the literal words input_variable. how do I fix this


RE: text-to-screen help - ichabod801 - Jan-11-2017

What version of Python are you using? In 2.x, the input function tries to evaluate the input before returning it, so you could be getting an integer instead of a string.

If that's the problem, use raw_input.


RE: text-to-screen help - Zman350x - Jan-11-2017

i'm using 2.7
the input im getting is an supposed to be an integer


RE: text-to-screen help - metulburr - Jan-11-2017

Im not sure why you are using the terminal to take input on a game using a window?

https://github.com/Mekire/pygame-textbox


RE: text-to-screen help - Zman350x - Jan-12-2017

ok, how would I take input another way?


RE: text-to-screen help - metulburr - Jan-12-2017

graphically

https://github.com/Mekire/pygame-textbox/blob/master/multi_example.py


RE: text-to-screen help - Zman350x - Jan-12-2017

can u give an example


RE: text-to-screen help - metulburr - Jan-12-2017

the link i provided is an example.


RE: text-to-screen help - Windspar - Jan-12-2017

Here a very simple example without cursor. Tinker around with both and make your own.
import pygame

class Textbox(object):
    def __init__(self, rect, font):
        self.text = ""
        self.font = font
        self.rect = rect
        self.image = None
        self.adjust = font.size("Iy") # use for centering
        self.height = rect.y + rect.h - (self.adjust[1] + rect.h) / 2

        self.box_image = pygame.Surface(rect.size)
        self.box_image.fill((100,100,150))

        self.text_color = (255,255,255)

        self.selected = False

    def keydown(self, event):
        if self.selected:
            if 32 <= event.key < 123:
                self.text += str(event.unicode)
            elif event.key == pygame.K_BACKSPACE:
                if len(self.text) > 1:
                    self.text = self.text[:-1]
                else:
                    self.text = ""
            elif event.key == pygame.K_DELETE:
                self.text = ""

            self.build()

    def mouse_button_down(self, event):
        if event.button == 1:
            if self.rect.collidepoint(event.pos):
                self.selected = True
                self.box_image.fill((100,150,100))
            else:
                self.selected = False
                self.box_image.fill((100,100,150))

    # have it fit inside box
    def build(self):
        length = len(self.text)
        if length == 0:
            self.image = None
        else:
            minus = self.adjust[0]
            for n in xrange(length):
                if self.font.size(self.text[n:])[0] < self.rect.w - minus:
                    self.image = self.font.render(self.text[n:], 1, self.text_color)
                    break

    def blit(self, surface):
        surface.blit(self.box_image, self.rect.topleft)
        if self.image:
            surface.blit(self.image, (self.rect.x + self.adjust[0] / 2, self.height))


def main():
    pygame.init()

    pygame.display.set_caption("Textbox Example")
    screen = pygame.display.set_mode((800, 600), 0, 0)
    clock = pygame.time.Clock()

    text = Textbox(pygame.Rect(10,10,150,35), pygame.font.SysFont("arial", 24))
    text2 = Textbox(pygame.Rect(10,60,150,35), pygame.font.Font(None, 30))

    running = True
    while(running):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.KEYDOWN:
                text.keydown(event)
                text2.keydown(event)
            elif event.type == pygame.MOUSEBUTTONDOWN:
                text.mouse_button_down(event)
                text2.mouse_button_down(event)

        screen.fill((25,100,150))

        text.blit(screen)
        text2.blit(screen)

        pygame.display.flip()
        clock.tick(60)

    pygame.quit()

main()



RE: text-to-screen help - Zman350x - Jan-13-2017

Smile