使用pygame键盘在屏幕上移动文本

时间:2014-12-08 20:32:45

标签: python pygame

我正在尝试使用键盘箭头在屏幕的左侧或右侧移动文本但无法使其工作。我的代码是:

while True :

    font = pygame.font.SysFont("Times New Roman", 20) # Font type # Font style 
    frame = pygame.display.set_mode ((Renderer.CANVAS_WIDTH, Renderer.CANVAS_HEIGHT))

    frame.fill ((49,33,148))

    for event in pygame.event.get ():
        if event.type == QUIT :
            pygame.quit ()
            sys.exit    ()

        if event.type == pygame.KEYDOWN :

                f = pygame.font.SysFont("Times New Roman", 20) # Font type # Font style 
                Track = font.render("Track : ", True, (255,255,0) )



    Game ()
    pygame.draw.rect(frame, (30,21,95),(50,400,700,170)) # draw a blue box on the screen 
    Track = font.render("Track : ", True, (255,255,0) )
    frame.blit(Track,(80,410))

    Labs = font.render ("Labs :", True, (255,255,0))
    frame.blit(Labs,(80,440))

    Distance = font.render ("Distance :", True, (255,255,0))
    frame.blit(Distance,(80,480))


    pygame.display.update ()  # Update the screen 






pygame.quit ()
quit ()

1 个答案:

答案 0 :(得分:0)

您应该使用此代码使用pygame

使用键盘移动文本
f = pygame.font.SysFont("Times New Roman", 20) # Font type # Font style 
Track = font.render("Track : ", True, (255,255,0) )

xpos = 10
ypos = 10

while True:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_w:
                ypos -= 5
            if event.key == pygame.K_s:
                ypos += 5
            if event.key == pygame.K_a:
                xpos -= 5
            if event.key == pygame.K_d:
                xpos += 5

    frame.fill((255, 255, 255))
    frame.blit(Track, (xpos, ypos))
    pygame.display.flip()

然而,这只有在您按下按键时才会起作用,它不会让您按住它。要做到这一点,它会更复杂一些。参见:

f = pygame.font.SysFont("Times New Roman", 20) # Font type # Font style 
Track = font.render("Track : ", True, (255,255,0) )

moveUp = False
moveDown = False
moveRight = False
moveLeft = Falsed

xpos = 10
ypos = 10

while True:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_w:
                moveUp = True
            if event.key == pygame.K_s:
                moveDown = True
            if event.key == pygame.K_a:
                moveLeft = True
            if event.key == pygame.K_d:
                moveRight = True
        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_w:
                moveUp = False
            if event.key == pygame.K_s:
                moveDown = False
            if event.key == pygame.K_a:
                moveLeft = False
            if event.key == pygame.K_d:
                moveRight = False

    if moveUp:
        ypos -= 5
    elif moveDown:
        ypos += 5
    if moveLeft:
        xpos -= 5
    elif moveRight:
        xpos += 5

    frame.fill((255, 255, 255))
    frame.blit(Track, (xpos, ypos))
    pygame.display.flip()