-
카테고리
-
세부 분야
게임 프로그래밍
-
해결 여부
미해결
캐릭터 움직임의 오류
21.02.26 03:05 작성 조회수 236
0
안녕하세요 선생님 강의 잘 듣고 있습니다. 항상 감사드립니다.
제가 강의를 따라하다가 문제가 생겼는데 세 번이나 반복해봤지만 같은 상황이라 고민 중에 질문을 드립니다.
선생님께서 알려주신 대로 event type == pygame.KEYDOWN 부분 중 to_x, to_y 값에 +- 5를 했을 때 저는 캐릭터 상자가 너무 빨라서 창 안에서 보이질 않더라구요.
그래서 혹시나 값에 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("E:/Study/Python/PythonWorkSpace/pygame_basic/background.png")
character = pygame.image.load("E:/Study/Python/PythonWorkSpace/pygame_basic/character.png")
character_size = character.get_rect().size #이미지 크기 구해옴
character_width = character_size[0]
character_heigth = character_size[1]
character_x_pos = (screen_width / 2) - (character_width / 2)
character_y_pos = screen_height - character_heigth #화면 세로 크기 가장 아래에 위치
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
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 pygame.K_RIGHT :
to_x = 0
elif event.key == pygame.K_UP or 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_heigth :
character_x_pos = screen_height - character_heigth
screen.blit(background, (0, 0)) #배경그리기
screen.blit(character, (character_x_pos,character_y_pos))
pygame.display.update() #게임 화면 다시 그리기(반드시)
#pygame 종료
pygame.quit()
답변을 작성해보세요.
0
답변 1