파이썬 관련 질문입니다!

파이썬 관련 질문입니다!

QA

신고된 글파이썬 관련 질문입니다!

본문

신고가 접수되어 자동으로 블라인드 된 글입니다.
원글을 보시려면 를 클릭하세요.

현재 유튜브 파이썬 강의를 보면서 따라해보고 있는 중인데, 스페이스바로 작동되어야 할 이벤트가

ALT+스페이스바로 작동되는 현상이 발생했습니다..

 

이게 코드 내의 문제인지 키보드 자체의 문제인지 알 수 없어서 코드와 함께 질문 드립니다ㅠ

 

 


import pygame
import os
#########################################################
# 기본 초기화 (반드시 해야 하는 것들)
pygame.init() # 초기화 (반드시 필요)
 
# 화면 크기 설정
screen_width = 640 # 가로 크기
screen_height = 480 # 세로 크기
screen = pygame.display.set_mode((screen_width, screen_height))
 
# 화면 타이틀 설정
pygame.display.set_caption("Nado Pang")
 
# FPS
clock = pygame.time.Clock()
#########################################################
 
# 1. 사용자 게임 초기화 (배경화면, 게임 이미지, 좌표, ,속도, 폰트 등)
current_path = os.path.dirname(__file__) # 현재 파일의 위치 반환
image_path = os.path.join(current_path, "images") # images 폴더 위치 반환

 
# 배경 만들기
background = pygame.image.load(os.path.join(image_path, "background.png"))
 
# 스테이지 만들기
stage = pygame.image.load(os.path.join(image_path, "stage.png"))
stage_size = stage.get_rect().size
stage_height = stage_size[1] # 스테이지의 높이 위에 캐릭터를 두기 위해 사용
 
# 캐릭터 만들기
character = pygame.image.load(os.path.join(image_path, "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 - stage_height
 
# 캐릭터 이동 방향
character_to_x = 0
 
# 캐릭터 이동 속도
character_speed = 1
 
# 무기 만들기
weapon = pygame.image.load(os.path.join(image_path, "weapon.png"))
weapon_size = weapon.get_rect().size
weapon_width = weapon_size[0]
 
# 무기는 한 번에 여러 발 발사 가능
weapons = []
 
# 무기 이동 속도
weapon_speed = 10
 
running = True
while running:
    dt = clock.tick(30)
 
    # 2. 이벤트 처리 (키보드, 마우스 등)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
   
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_LEFT: # 캐릭터를 왼쪽으로
            character_to_x -= character_speed
        elif event.key == pygame.K_RIGHT: # 캐릭터를 오른쪽으로
            character_to_x += character_speed
        elif event.key == pygame.K_SPACE: # 무기 발사
            weapon_x_pos = character_x_pos + (character_width / 2) - (weapon_width / 2)
            weapon_y_pos = character_y_pos
            weapons.append([weapon_x_pos, weapon_y_pos])
 
    if event.type == pygame.KEYUP:
        if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
            character_to_x = 0
   
    # 3. 게임 캐릭터 위치 정의
    character_x_pos += character_to_x
 
    if character_x_pos < 0:
        character_x_pos = 0
    elif character_x_pos > screen_width - character_width:
        character_x_pos = screen_width - character_width
 
    # 무기 위치 조정
    # 100, 200 → 180, 160, 140, .... / y 좌표가 점점 줄어들어야 함
    weapons = [ [w[0], w[1] - weapon_speed] for w in weapons]
 
    # 4. 충돌 처리
       
    # 5. 화면에 그리기
    screen.blit(background, (0, 0))
    screen.blit(stage, (0, screen_height - stage_height))
    screen.blit(character, (character_x_pos, character_y_pos))
   
    for weapon_x_pos, weapon_y_pos in weapons:
        screen.blit(weapon, (weapon_x_pos, weapon_y_pos))
 
    pygame.display.update()
 
pygame.quit()
 

이 질문에 댓글 쓰기 :

답변 1

요즘 python 질문이 올라오네요.

 

https://realpython.com/pygame-a-primer/

 

여기 참조로 다시 따라 해보세요. K_SPACE가 어떻게 정의 되었는지에 따라 달라지겠네요.

답변을 작성하시기 전에 로그인 해주세요.
전체 123,120 | RSS
QA 내용 검색

회원로그인

(주)에스아이알소프트 / 대표:홍석명 / (06211) 서울특별시 강남구 역삼동 707-34 한신인터밸리24 서관 1404호 / E-Mail: admin@sir.kr
사업자등록번호: 217-81-36347 / 통신판매업신고번호:2014-서울강남-02098호 / 개인정보보호책임자:김민섭(minsup@sir.kr)
© SIRSOFT