Python Forum
[PyGame] text-to-screen help
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] text-to-screen help
#1
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
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
i'm using 2.7
the input im getting is an supposed to be an integer
Reply
#4
Im not sure why you are using the terminal to take input on a game using a window?

https://github.com/Mekire/pygame-textbox
Recommended Tutorials:
Reply
#5
ok, how would I take input another way?
Reply
#6
graphically

https://github.com/Mekire/pygame-textbox...example.py
Recommended Tutorials:
Reply
#7
can u give an example
Reply
#8
the link i provided is an example.
Recommended Tutorials:
Reply
#9
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()
99 percent of computer problems exists between chair and keyboard.
Reply
#10
Smile
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGame] When I hit the space bar from the home screen, it sends me to the game over screen JesusisKing 1 954 Apr-30-2023, 10:11 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020