inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

파이썬 무료 강의 (활용편1) - 추억의 오락실 게임 만들기 (3시간)

키보드 이벤트

캐릭터의 가로방향 움직임

202

KIN Game

작성한 질문수 1

0

캐릭터가 좌우로는 키를 눌렀을 때만 움직이는데

상하로는 키를 눌렀다가 때도 움직입니다.

코딩 어느 부분이 다른건지 모르겠네요.

(파이참을 써서 하고 있기는 한데 그게 이유는 아니겠죠?)

import pygame

pygame.init()

# screen
screen_width = 480
screen_height = 640
screen = pygame.display.set_mode((screen_width, screen_height))

# title
pygame.display.set_caption("MyGame")

# import background image
background = pygame.image.load("C:/Users/eagle/Desktop/PythonWorkspace/background.png")

# import sprite(character)
character = pygame.image.load("C:/Users/eagle/Desktop/PythonWorkspace/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

# moving position
to_x = 0
to_y = 0

# event loop
running = True # is the game running?
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 event.key == pygame.K_RIGHT:
                to_x = 0
            elif event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                tp_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, (int(character_x_pos), int(character_y_pos)))

    pygame.display.update()

# exit
pygame.quit()

python GUI pygame

답변 2

0

KIN Game

역시 고수의 눈은 다르군요! 감사합니다

0

나도코딩

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

이 부분에 오타가 있네요. to_y 라고 수정하시면 될 것으로 보입니다

pygame 설치오류

0

122

1

pip install pygame 이 실패합니다

0

205

1

pip install pygame 이 안되요

0

886

1

(4:00) linting enabled 항목이 보이지 않습니다

0

155

1

마지막 프로젝트 그림 파일들을 올려주실 수는 없을까요?

0

202

1

import pygame에서 계속 오류가 발생해요

0

3580

2

무기 Y포지션 speed 감소

0

294

1

pygame 공부

0

504

0

마지막 부분 스프라이트가 맨 왼쪽에 위치해있습니다

0

264

1

공 쪼개기 문제

0

305

0

실행시 글자가 깨집니다.

0

309

1

키보드 이벤트 오류

0

569

1

스크린의 높이를 680으로 설정했을 때 오류?

0

294

1

pygame.time.get_ticks() 질문있습니다.

0

534

0

캐릭터의 이동

0

328

0

적(똥)과 케릭터가 충돌했을 때 게임 오버가 뜨게 만들려고..

0

298

1

시작을 하기에 앞서...

0

388

0

이미지 배경을 투명하게 하려면?

0

374

0

파이게임이 실행은 되는데....

0

227

0

실행이 안됩니다.

0

320

0

똑같이 따라 쳤는데 오류가 생겨요

0

302

0

배경색이 안 나와요

0

328

0

import pygame 실행이 안 돼요

0

2697

1

Error

0

151

0