• 카테고리

    질문 & 답변
  • 세부 분야

    게임 프로그래밍

  • 해결 여부

    미해결

키 반복 입력이 안됐었습니다.

20.07.20 20:25 작성 조회수 1.09k

1

안녕하세요.

저는 강의를 듣는 수강생입니다.

질문 드리고 싶은게 있는데, key 이벤트를 작성을 다 했는데 키보드를 꾹 누르고 있어도 한 번만 눌리게 처리가 되었었습니다. 그런데 이것은 인터넷에 찾아서 처리를 했습니다.

# 키 반복(key repeat)
delay = 100
interval = 50
pygame.key.set_repeat(delay, interval)

이것을 While running: 문 위에 적으니 잘 해결되었습니다. 

그런데 강좌에서는 별다른 입력 없이도 문제가 없는 것 같은데, 무슨 차이인 걸까요?

항상 잘 배우고 있습니다.

수강생 올림.

답변 4

·

답변을 작성해보세요.

1

들여쓰기가 잘못된 구간이 있어 주석과 함께 수정해드렸으니 확인해보세요!

import pygame



pygame.init() # 초기화 (반드시 필요)



# 화면 크기 설정

screen_width = 480 # 가로 크기

screen_height = 640 # 세로 크기

screen = pygame.display.set_mode((screen_width, screen_height))



# 화면 타이틀 설정

pygame.display.set_caption("Nado Game") # 게임 이름



# 배경 이미지 불러오기

background = pygame.image.load("C:/Users/Lazistar/Desktop/PythonWorkspace/Nado_Game/pygame_basic/background.png")



# 캐릭터(스프라이트) 불러오기

character = pygame.image.load("C:/Users/Lazistar/Desktop/PythonWorkspace/Nado_Game/pygame_basic/character.png")

character_size = character.get_rect().size # 이미지의 크기를 구해옴

character_width = character_size[0] # 캐릭터의 가로 크기

character_height = character_size[1] # 캐릭터의 세로 크기

character_x_pos = (screen_width / 2) - (character_width / 2) # 화면 가로의 절반 크기에 해당하는 곳에 위치 (가로)

character_y_pos = screen_height - character_height # 화면 세로 크기 가장 아래에 해당하는 곳에 위치 (세로)



# 이동할 좌표

to_x = 0

to_y = 0



# 이벤트 루프

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: # 캐릭터를 왼쪽으로

                to_x -= 5 # to_x = to_x - 5

            elif event.key == pygame.K_RIGHT: # 캐릭터를 오른쪽으로

                to_x += 5

            elif event.key == pygame.K_UP: # 캐릭터를 위로

                to_y -= 5  

            elif event.key == pygame.K_DOWN: # 캐릭터를 아래로

                to_y += 5



        if event.type == pygame.KEYUP: # 방향키를 떼면 멈춤

            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:

                to_x = 0

            elif event.key == pygame.K_UP or event.key == pygame.K_DOWN:

                to_y = 0


# 여기부터 들여쓰기가 잘못되어 있었네요
    character_x_pos += to_x

    character_y_pos += to_y



    # 가로 경계값 처리

    if character_x_pos < 0:

        character_x_pos = 0

    elif character_x_pos > screen_width - character_width:

        character_x_pos = screen_width - character_width

    

    # 세로 경계값 처리

    if character_y_pos < 0:

        character_y_pos = 0

    elif character_y_pos > screen_height - character_height:

        character_y_pos = screen_height - character_height
# 여기까지 들여쓰기가 잘못되어 있었네요


    screen.blit(background, (0, 0)) # 배경 그리기



    screen.blit(character, (character_x_pos, character_y_pos))



    pygame.display.update() # 게임화면을 다시 그리기!



# pygame 종료

pygame.quit()

0

Lazistar님의 프로필

Lazistar

질문자

2020.07.22

감사합니다!  잘 해결됐습니다!

0

Lazistar님의 프로필

Lazistar

질문자

2020.07.22

import pygame

pygame.init() # 초기화 (반드시 필요)

# 화면 크기 설정
screen_width = 480 # 가로 크기
screen_height = 640 # 세로 크기
screen = pygame.display.set_mode((screen_width, screen_height))

# 화면 타이틀 설정
pygame.display.set_caption("Nado Game"# 게임 이름

# 배경 이미지 불러오기
background = pygame.image.load("C:/Users/Lazistar/Desktop/PythonWorkspace/Nado_Game/pygame_basic/background.png")

# 캐릭터(스프라이트) 불러오기
character = pygame.image.load("C:/Users/Lazistar/Desktop/PythonWorkspace/Nado_Game/pygame_basic/character.png")
character_size = character.get_rect().size # 이미지의 크기를 구해옴
character_width = character_size[0# 캐릭터의 가로 크기
character_height = character_size[1# 캐릭터의 세로 크기
character_x_pos = (screen_width / 2) - (character_width / 2# 화면 가로의 절반 크기에 해당하는 곳에 위치 (가로)
character_y_pos = screen_height - character_height # 화면 세로 크기 가장 아래에 해당하는 곳에 위치 (세로)

# 이동할 좌표
to_x = 0
to_y = 0

# 이벤트 루프
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: # 캐릭터를 왼쪽으로
                to_x -= 5 # to_x = to_x - 5
            elif event.key == pygame.K_RIGHT: # 캐릭터를 오른쪽으로
                to_x += 5
            elif event.key == pygame.K_UP: # 캐릭터를 위로
                to_y -= 5  
            elif event.key == pygame.K_DOWN: # 캐릭터를 아래로
                to_y += 5

        if event.type == pygame.KEYUP: # 방향키를 떼면 멈춤
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                to_x = 0
            elif event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                to_y = 0

        character_x_pos += to_x
        character_y_pos += to_y

        # 가로 경계값 처리
        if character_x_pos < 0:
            character_x_pos = 0
        elif character_x_pos > screen_width - character_width:
            character_x_pos = screen_width - character_width
        
        # 세로 경계값 처리
        if character_y_pos < 0:
            character_y_pos = 0
        elif character_y_pos > screen_height - character_height:
            character_y_pos = screen_height - character_height

    screen.blit(background, (00)) # 배경 그리기

    screen.blit(character, (character_x_pos, character_y_pos))

    pygame.display.update() # 게임화면을 다시 그리기!

# pygame 종료
pygame.quit()

여기 있습니다! 

0

코드상에 실수가 있었던 것 같은데요?

작성하신 코드 전체를 올려주시면 어디가 잘못되었는지 확인해보겠습니다 ^^