pygame only read uppercase keys

38 Views Asked by At

I am writing a fighting game by using python.But it seems my pygame can only read uppercase keys. I only play my game after I have pressed Capslook...And once I press it while I am playing,I can't move. Are there any method to solve this problem? Here is my codes:

def move(self,screen_wide,screen_high,screen,target,round_over): self.effect.draw(screen,self.rect.centerx,self.rect.y,self.face)

    self.cooldown()

    self.moving = False
    self.attack_type = 0

    self.key_press = pygame.key.get_pressed()

    self.defence_method()

    if self.defence == True and self.face != target.face:
        self.attackable = False
    else:
        self.attackable = True

    #movement
    #确保攻击僵直
    if self.attacking == False and self.alive == True and round_over == False:

        #检查角色控制
        if self.player == 1:

            if self.key_press[pygame.K_a]:
                self.rect.x -= self.speed
                self.face = 'right'
                self.moving = True

            if self.key_press[pygame.K_w] and not self.jumping and not self.displacement:
                self.jumping = True
            if self.key_press[pygame.K_d]:
                self.rect.x += self.speed
                self.face = 'left'
                self.moving = True

            #攻击
            if self.key_press[pygame.K_q] or self.key_press[pygame.K_e] or self.key_press[pygame.K_r]:
                self.voice.attack_voice(1)

                #攻击类型
                if self.key_press[pygame.K_q]:
                    self.attack_type = 1
                    self.attacking = True

                if self.key_press[pygame.K_e]:
                    self.attack_type = 2
                    self.attacking = True

                if self.key_press[pygame.K_r] and self.attack_3_cooldown <= 0:
                    self.attack_type = 3
                    self.attack_3_cooldown = 70
                    self.attacking = True

            if self.key_press[pygame.K_SPACE]:
                if self.jumping == False and self.displacement_cooldown == 0:
                       self.displacement = True
                       self.displacement_cooldown = 70

Is there any method to solve this problem?

1

There are 1 best solutions below

0
D.L On

i just tried example keypress myself and it is done like this:

import pygame

pygame.init() 

screen = pygame.display.set_mode((400, 300))

running = True
while running:

    for event in pygame.event.get():

        if event.type == pygame.QUIT:
            running = False

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                print("Left arrow key pressed")
            if event.key == pygame.K_RIGHT:
                print("Right arrow key pressed")
            if event.key == pygame.K_x:
                print("x key pressed")
                # Do something here when x is pressed
pygame.quit()

the last if statement in the example works fine for the lower case "x" key.

I also note that the question itself is missing part of the class construction...