• 카테고리

    질문 & 답변
  • 세부 분야

    게임 프로그래밍

  • 해결 여부

    해결됨

디버그로 할때는 잘 실행되는데

20.09.14 20:55 작성 조회수 128

1

import pygame

pygame.init() # 초기화

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

# 타이틀
pygame.display.set_caption("dudcks game") # 게임 이름

# 배경
background = pygame.image.load("C:/Users/dudcks/Desktop/workspace/Python/pygame_basic/background.png")

# 캐릭터
character = pygame.image.load("C:/Users/dudcks/Desktop/workspace/Python/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 -= 1
            elif event.key == pygame.K_RIGHT:
                to_x += 1
            elif event.key == pygame.K_UP:
                to_y -= 1
            elif event.key == pygame.K_DOWN:
                to_y += 1

        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 = 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.quit() # 게임 종료

비주얼 스튜디오 코드 오른쪽에 있는 실행버튼 눌렀을때는 에러가 떠요

File "<stdin>", line 1
    & C:/Python3/Python38-32/python.exe c:/Users/dudcks/Desktop/workspace/Python/pygame_basic/4_keyboard_event.py
    ^
SyntaxError: invalid syntax

이렇게 떠요

이제부터 디버그로만 프로젝트를 실행해야 하나요?ㅠㅠ

답변 1

답변을 작성해보세요.

1

ㅎㅎ 실수로 한 줄 실행 모드가 되었네요. Shift + Enter 를 누르면 그렇게 된답니다. 터미널 우측 끝에 쓰레기통 모양 아이콘을 계속 클릭하셔서 터미널 창을 모두 닫은 후 다시 실행해보세요 !