Python PyGame中密钥的检测与识别

Python PyGame中密钥的检测与识别,python,pygame,Python,Pygame,由于某种原因,当我按下右键时,charspeed不会增加100。我想用一个时钟做一个游戏,但它似乎不起作用。我是pygame的新手,如你所见,请提供帮助 问题在于: bg = "lv1.jpg" ch = "char.png" import pygame, sys from pygame.locals import * pygame.init() screen = pygame.display.set_mode((640, 400), 0, 3

由于某种原因,当我按下右键时,charspeed不会增加100。我想用一个时钟做一个游戏,但它似乎不起作用。我是pygame的新手,如你所见,请提供帮助

问题在于:

    bg = "lv1.jpg"
    ch = "char.png"

    import pygame, sys
    from pygame.locals import *

    pygame.init()
    screen = pygame.display.set_mode((640, 400), 0, 32)
    background = pygame.image.load(bg).convert()
    char = pygame.image.load(ch).convert_alpha()


    clock = pygame.time.Clock()
    charspeed = 0

    charx = 100
    chary = 200

    running = True

    while running:
        for event in pygame.event.get():
            if event.type == QUIT:
                running = False
                pygame.quit()
                sys.exit()
            if event.type== KEYDOWN:
                if event.type == K_LEFT:
                    charspeed = -100
                elif event.type == K_RIGHT:
                    charspeed = +100

            if event.type== KEYUP:
                if event.key==K_LEFT:
                    charspeed=0
                elif event.key==K_RIGHT:
                    charspeed=0


        screen.blit(background, (0,0))


        milli = clock.tick()
        seconds = milli/1000.
        chardm = seconds*charspeed 
        charx += chardm

        screen.blit(char, (charx, chary))

        pygame.display.update()
event.type
KEYDOWN
,因此它不能是
K\u RIGHT

您需要的是
event.key==K_RIGHT

详见文档

if event.type== KEYDOWN:
    if event.type == K_LEFT:
        charspeed = -100
    elif event.type == K_RIGHT:
        charspeed = +100