pygame오류 NameError: name 'enemy1' is not defined라고 떠요..

조회수 944회
import pygame, sys
import random

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

pygame.display.set_caption("my game")


# FPS 
clock = pygame.time.Clock()


#화면 크기 설정
screen_width = 1150
screen_height = 648
screen = pygame.display.set_mode((screen_width, screen_height))

# 배경 이미지 불러오기
background = pygame.image.load("D:/python코딩/제목 없음.png")

# 캐릭터 불러오기
charactor = pygame.image.load("D:/python코딩/캐릭터.png")
pygame.transform.scale(charactor, (40, 40))
charactor_x = 650
charactor_y = 450

enemy1 = pygame.image.load("D:/python코딩/enemy1.png")
pygame.transform.scale(enemy1, (100, 100))
enemy1_x = 100
enemy1_y = 450

# 이동할 좌표
to_x = 0
to_y = 0

# 이동 속도
Charactor_speed = 1
enemy1_speed = 5


# 이벤트 루프

while True: # 게임 진행중일 동안
    dt = clock.tick(150)
    for event in pygame.event.get(): # 어떤 이벤트가 발생
        if event.type == pygame.QUIT: # 창이 닫히는 이벤트 방생
            pygame.quit() # pygame 종료
            sys.exit()

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                to_x -= Charactor_speed
            elif event.key == pygame.K_RIGHT:
                to_x += Charactor_speed
            elif event.key == pygame.K_UP:
                to_y -= Charactor_speed
            elif event.key == pygame.K_DOWN:
                to_y += Charactor_speed

        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:
                to_y = 0

    charactor_x += to_x * dt
    charactor_y += to_y * dt 

    if charactor_x < 0:
        charactor_x = 0
    elif charactor_x > 1150:
        charactor_x = 1150
    if charactor_y < 0:
        charactor_y = 0
    elif charactor_y > 450:
        charactor_y = 450

    enemy1_x += enemy1_speed

    # crash event
    charactor_rect = charactor.get_rect()
    charactor_rect.left = charactor_x
    charactor_rect.top = charactor_y

    enemy1_rect = enemy1.get_rect()
    enemy1_rect.left = enemy1_x
    enemy1_rect.top = enemy1_y

    #충돌 확인
    if charactor_rect.colliderect(enemy1_rect):
        del enemy1





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

    screen.blit(charactor, (charactor_x, charactor_y)) # 캐릭터 그리기

    screen.blit(enemy1, (enemy1_x, enemy1_y))

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

실행은 잘 되는데 내 캐릭터와 적이 충돌했을 때 적이 사라져야 하는데 충돌시 게임이 꺼지고 NameError: name 'enemy1' is not defined 라고 떠요. 뭐가 잘못되었는지 알려주세요!

1 답변

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)