在Pygame中进行屏幕输入

时间:2017-09-16 10:33:44

标签: python text pygame

对于学校的编程项目,我必须使用pygame创建一个拼写游戏。但是,由于我对这一切都很新,我无法设法如何让用户输入字母并让它们出现在游戏显示屏上。到目前为止,这是我的代码(以及我未能成功解决此问题):

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

2 个答案:

答案 0 :(得分:1)

pygame.KEYDOWN个事件具有unicode属性,您可以将其添加到字符串中,例如text += event.unicode。然后渲染文本并在主循环中显示。

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()

答案 1 :(得分:0)

这就是你解决问题的方法:

 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()