2

For a programming project in school I have to create a spelling game using pygame. However, as I am fairly new to this whole thing, I can't manage to work out how to allow the user to input letters and make them appear on the game display. This is my code so far (along with my failing attempt at solving this problem):

import pygame
import random

pygame.init()

display_width = 800
display_height = 600

black = (0,0,0)
white = (255,255,255)
red = (255,0,0)

gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption("Spelling Game")
myfont = pygame.font.SysFont("Arial", 50)
clock = pygame.time.Clock()
mouse = pygame.mouse.get_pos()

city = pygame.image.load("city.png")

charac_str = ""  #new string to store written character
# font object to render str to surface
font_renderer = pygame.font.SysFont("Arial",30)

def background(x,y):
    gameDisplay.blit(city,(-200,-100))

with open("words.txt") as f:
    WORDS = f.read().split()

def random_word():
    return random.choice(WORDS) 


gameExit = False
word = random_word()

while not gameExit:
    #event-handling loop based on user input
    for event in pygame.event.get():
        if event.type == pygame.QUIT:            
            pygame.quit()
            quit() 

    gameDisplay.fill(white)

    background(0,0)
    textsurface = myfont.render(word, True, red)
    gameDisplay.blit(textsurface, (340, 400))     #the random word

    rendered_charac = font_renderer.render(charac_str, True, red)
    gameDisplay.blit(rendered_charac, (100,100))

    pygame.display.update()

    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if pygame.K_0 < event.key < pygame.K_9: #checks key pressed
                character = chr(event.key) #conv num to char
                charac_str += str(character) # add num to end of string
                gameDisplay.blit(charac_str) # display the input?doesn't work
skrx
  • 19,980
  • 5
  • 34
  • 48

2 Answers2

1

pygame.KEYDOWN events have a unicode attribute which you can simply add to a string, e.g. text += event.unicode. Then the text is rendered and displayed in the main loop.

import pygame as pg


def main():
    screen = pg.display.set_mode((640, 480))
    font = pg.font.Font(None, 32)
    clock = pg.time.Clock()
    color = pg.Color('dodgerblue2')
    text = ''

    while True:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                return
            elif event.type == pg.KEYDOWN:
                if event.key == pg.K_RETURN:
                    print(text)
                    text = ''
                elif event.key == pg.K_BACKSPACE:
                    text = text[:-1]
                else:
                    text += event.unicode

        screen.fill((30, 30, 30))
        txt_surface = font.render(text, True, color)
        screen.blit(txt_surface, (50, 100))

        pg.display.flip()
        clock.tick(30)


if __name__ == '__main__':
    pg.init()
    main()
    pg.quit()
skrx
  • 19,980
  • 5
  • 34
  • 48
  • I've removed the input box from the code example, so that you can just type and the text is immediately displayed on the screen. If anyone needs an input box check out this [answer](https://stackoverflow.com/a/46390412/6220679). – skrx Sep 24 '17 at 15:32
0

This is how you 're going to solve your little problem:

 def name():

        pygame.init()
        screen = pygame.display.set_mode((480, 360))
        name = ""
        font = pygame.font.Font(None, 50)

        while True:
            for evt in pygame.event.get():
                if evt.type == KEYDOWN:
                    if evt.unicode.isalpha():
                        name += evt.unicode
                    elif evt.key == K_BACKSPACE:
                        name = name[:-1]
                    elif evt.key == K_RETURN:
                        name = ""
                elif evt.type == QUIT:
                    return

        screen.fill ((0, 0, 0))
        block = font.render(name, True, (255, 255, 255))

        rect = block.get_rect()
        rect.center = screen.get_rect().center
        screen.blit(block, rect)
        pygame.display.flip()
wizofe
  • 494
  • 7
  • 19