-1

I made the movement of the character in the player class.

However, sometimes the character does not move even though the wasd button is pressed, and sometimes it suddenly accelerates. Also, it seems that the KEYUP event does not occur. please tell me why

class Player:
    playerpos = [0, height / 2] #주인공 초기 위치
    character_x_pos = 0
    character_y_pos = height / 2
    character_to_x_LEFT=0
    character_to_x_RIGHT=0
    character_to_y_up = 0
    character_to_y_down = 0
    
    def __init__(self):
        self.image = pygame.image.load(os.path.join(current_path1 + "\\character_image\\main_character.png")) #주인공 그림을 받아옴
        self.rect = pygame.Rect(self.image.get_rect())      #주인공을 사각형으로 받음
        self.mentality = 0      #주인공 정신도 수준

    def draw(self):
        # 수정1 : 기존의 character_to_x 를 왼쪽 방향, 오른쪽 방향 변수 2개로 나눔
        for event in pygame.event.get():

            # 수정2 : 키를 누를 때 LEFT, RIGHT 에 따라 서로 다른 변수의 값 조정
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_a:
                    self.character_to_x_LEFT -= 3 # 바뀐 부분
                elif event.key == pygame.K_d:
                    self.character_to_x_RIGHT += 3 # 바뀐 부분

                elif event.key == pygame.K_s:
                    self.character_to_y_up += 3 # 바뀐 부분
                elif event.key == pygame.K_w:
                    self.character_to_y_down -= 3 # 바뀐 부분

            # 수정3 : 키에서 손을 뗄 때 LEFT, RIGHT 를 각각 처리
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_a: # 이 부분은 모두 다 바뀜
                    self.character_to_x_LEFT = 0
                elif event.key == pygame.K_d:
                    self.character_to_x_RIGHT = 0
                elif event.key == pygame.K_s:
                    self.character_to_y_down = 0
                elif event.key == pygame.K_w:
                    self.character_to_y_up = 0


        # 수정4 : 두 변수의 값을 모두 더함
        self.character_x_pos += self.character_to_x_LEFT + self.character_to_x_RIGHT
        self.character_y_pos += self.character_to_y_up + self.character_to_y_down
        screen.blit(self.image, (self.character_x_pos, self.character_y_pos))

class Doctor:
    doctorpos = [1800, height / 2] #의사 초기 위치
    def __init__(self):
        self.image = pygame.image.load(os.path.join(image_path,current_path1 + '\\character_image\\sub_dummy.png')) #의사의 이미지를 불러옴
        self.rect = pygame.Rect(self.image.get_rect()) #의사 이미지를 사각형으로 받음
    def draw(self):      
        screen.blit(self.image, self.doctorpos)      #의사를 초기위치에 그림


def main():
    running = 1
    doctor = Doctor()
    user = Player()
    map_lotate = 0



    while running:
        #게임 종료
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    pygame.quit()
                    sys.exit()
        if map_lotate == 0:

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

            doctor.draw()
            user.draw()
            pygame.display.update()
main()

The output is not normal even though I copied the code I looked up on the Internet.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Read [Faster version of 'pygame.event.get()'. Why are events being missed and why are the events delayed?](https://stackoverflow.com/questions/58086113/faster-version-of-pygame-event-get-why-are-events-being-missed-and-why-are/58087070#58087070). This is exactly your problem. – Rabbid76 Sep 17 '22 at 08:32

0 Answers0