inflearn logo
강의

Course

Instructor

Free Python Lecture (Usage Part 1) - Making a nostalgic arcade game (3 hours)

Keyboard Event

to_x, to_y 윈도우에서는 오류나오는데 대체 코드가 있나요?

499

tjseodl1259

2 asked

0

to_x, to_y 윈도우에서는 오류나오는데 대체 코드가 있나요?

python GUI pygame

Answer 8

0

nadocoding

^^ 감사합니다.

0

tjseodl1259

아아 감사합니다 잘움직이네요 !! 강의항상 잘보고 있어요

기본편부터 섭렵중입니다!

0

nadocoding

# 이벤트 루프, 

바로 윗줄에 

to_x = 0

이라고 넣어보시겠어요?

0

tjseodl1259

import pygame

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

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

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

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

# 캐릭터 (스프라이트) 불러오기
character = pygame.image.load("C:\\Users\\JunDragon\\Desktop\\PythonWorkspace\\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 # 화면 세로크기 가장 아래에 해당 하는 곳에 위치


# 이벤트 루프
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 event.key == pygame.K_RIGHT:
                to_x = 0
            if 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 = 1
    elif character_y_pos > screen_height - character_y_pos:
        character_y_pos = screen_height - character_y_pos

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

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

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

# pygame 종료
pygame.quit()

0

nadocoding

to_x 라는 변수가 선언되지 않았다네요. 윗쪽에 변수를 잘 만드셨는지, 오타는 없는지요?

소스코드 전체를 다 올려주시면 봐드리겠습니다 ^^

0

tjseodl1259

이렇게 뜨네요 ㅠ

0

tjseodl1259

keyboard_event.py", line 48, in <module>

    character_x_pos += to_x

NameError: name 'to_x' is not defined

0

nadocoding

안녕하세요

어떤 에러가 발생하나요?

작성하신 전체 코드를 올려주시면 확인 후 도움드리겠습니다 ^^

감사합니다.

pygame 설치오류

0

117

1

pip install pygame 이 실패합니다

0

203

1

pip install pygame 이 안되요

0

877

1

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

0

154

1

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

0

201

1

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

0

3576

2

무기 Y포지션 speed 감소

0

293

1

pygame 공부

0

503

0

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

0

263

1

공 쪼개기 문제

0

304

0

실행시 글자가 깨집니다.

0

308

1

키보드 이벤트 오류

0

568

1

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

0

293

1

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

0

533

0

캐릭터의 이동

0

327

0

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

0

297

1

시작을 하기에 앞서...

0

387

0

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

0

373

0

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

0

226

0

실행이 안됩니다.

0

319

0

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

0

300

0

배경색이 안 나와요

0

326

0

import pygame 실행이 안 돼요

0

2693

1

Error

0

149

0