• 카테고리

    질문 & 답변
  • 세부 분야

    게임 프로그래밍

  • 해결 여부

    미해결

공이 한번에 하나씩만 사라져요

20.12.28 01:34 작성 조회수 80

0

공이 가장 처음 쪼개질 때에는 문제 없는데, 이 후에 공격하면 한 번에 한 공만 쪼개집니다...

예를 들어 Ball을 B라고 가정하면, B1->B2,B'2 로 쪼개지고, 각 공에 공격을 가하면 쪼개져야 하는데, B1만 쪼개지고 B'2는 아무리 공격해도 안 없어지더라고요...이후에 B2가 다시 쪼개져도 B3에서 같은 현상이 벌어집니다ㅠㅠ

하단 이미지에선 반드시 해야 하는 설정은 넣지 않았습니다! 실제 코드에선 있어요

#1. 사용자 게임 초기화 (배경 이미지, 캐릭터, 좌표, 폰트, 속도 등)
current_path = os.path.dirname(__file__)
image_path = os.path.join(current_path, '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 - stage_height - character_height

character_to_x = 0
character_speed = 5

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

ball_images = [
    pygame.image.load(os.path.join(image_path, 'balloon1.png')),
    pygame.image.load(os.path.join(image_path, 'balloon2.png')),
    pygame.image.load(os.path.join(image_path, 'balloon3.png')),
    pygame.image.load(os.path.join(image_path, 'balloon4.png'))]

ball_speed = [-18, -15, -12, -9]

balls = []

balls.append({
    "pos_x" : 50,
    "pos_y" : 50,
    "img_idx"0,
    "to_x"3,
    "to_y": -6,
    "init_spd_y": ball_speed[0]
})

weapon_to_remove = -1
ball_to_remove = -1

game_font = pygame.font.Font(None40)
total_time = 100
start_ticks = pygame.time.get_ticks()


game_result = "Game Over"

##############################
running = True
while running:
    dt = clock.tick(60)

 #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

    weapons = [[w[0], w[1] - weapon_speed] for w in weapons]
    weapons = [[w[0], w[1]] for w in weapons if w[1]>0]
    
    for ball_idx, ball_val in enumerate(balls):
        ball_pos_x = ball_val["pos_x"]
        ball_pos_y = ball_val["pos_y"]
        ball_img_idx = ball_val["img_idx"]

        ball_size = ball_images[ball_img_idx].get_rect().size
        ball_width = ball_size[0]
        ball_height = ball_size[1]

        if ball_pos_x < 0 or ball_pos_x > screen_width - ball_width:
            ball_val['to_x'] = ball_val['to_x'] * -1
        if ball_pos_y >= screen_height - stage_height - ball_height:
            ball_val["to_y"] = ball_val['init_spd_y']
        else:
            ball_val['to_y'] += 0.5
        
        ball_val['pos_x'] += ball_val['to_x']
        ball_val['pos_y'] += ball_val['to_y']



#4. 충돌 처리

    character_rect = character.get_rect()
    character_rect.left = character_x_pos
    character_rect.top = character_y_pos

    for ball_idx, ball_val in enumerate(balls):
            ball_pos_x = ball_val["pos_x"]
            ball_pos_y = ball_val["pos_y"]
            ball_img_idx = ball_val["img_idx"]

            ball_rect = ball_images[ball_img_idx].get_rect()
            ball_rect.left = ball_pos_x
            ball_rect.top = ball_pos_y
            if character_rect.colliderect(ball_rect):
                running = False
                break
    for weapon_idx, weapon_val in enumerate(weapons):
        weapon_pos_x = weapon_val[0]
        weapon_pos_y = weapon_val[1]
        weapon_rect = weapon.get_rect()
        weapon_rect.left = weapon_pos_x
        weapon_rect.top = weapon_pos_y

        if weapon_rect.colliderect(ball_rect):
            weapon_to_remove = weapon_idx
            ball_to_remove = ball_idx 

            if ball_img_idx < 3:
                ball_width = ball_rect.size[0]
                ball_height = ball_rect.size[1]

                small_ball_rect = ball_images[ball_img_idx + 1].get_rect()
                small_ball_width = small_ball_rect.size[0]
                small_ball_height = small_ball_rect.size[1]

                balls.append({
                    "pos_x" : ball_pos_x + (ball_width / 2) - (small_ball_width / 2),
                    "pos_y" : ball_pos_y + (ball_height / 2) - (small_ball_height / 2),
                    "img_idx": ball_img_idx + 1,
                    "to_x": -3,
                    "to_y": -6,
                    "init_spd_y": ball_speed[ball_img_idx + 1]})

                balls.append({
                    "pos_x" : ball_pos_x + (ball_width / 2) - (small_ball_width / 2),
                    "pos_y" : ball_pos_y + (ball_height / 2) - (small_ball_height / 2),
                    "img_idx": ball_img_idx + 1,
                    "to_x"3,
                    "to_y": -6,
                    "init_spd_y": ball_speed[ball_img_idx + 1]})

            break
  

    if ball_to_remove > -1:
        del balls[ball_to_remove]
        ball_to_remove = -1

    if weapon_to_remove > -1:
        del weapons[weapon_to_remove]
        weapon_to_remove = -1

    if len(balls) == 0:
        game_result = "Mission Complete"
        running = False

#5. 회면에 그리기

    screen.blit(background, (0,0))
    for weapon_x_pos, weapon_y_pos in weapons:
        screen.blit(weapon, (weapon_x_pos, weapon_y_pos))
    screen.blit(stage, (0, screen_height - stage_height))
    screen.blit(character, (character_x_pos, character_y_pos))

    for idx, val in enumerate(balls):
        ball_pos_x = val['pos_x']
        ball_pos_y = val['pos_y']  
        ball_img_idx = val['img_idx']
        screen.blit(ball_images[ball_img_idx], (ball_pos_x, ball_pos_y))

    elapsed_time = (pygame.time.get_ticks() - start_ticks) / 1000
    timer = game_font.render("Time : {}".format(int(total_time - elapsed_time)), True, (255,255,255))
    screen.blit(timer, (10,10))
    
    if total_time - elapsed_time <= 0:
        game_result = "Time Over"
        running = False


    pygame.display.update()

msg = game_font.render(game_result, True, (255,255,0))
msg_rect = msg.get_rect(center = (int(screen_width / 2), int(screen_height / 2)))
screen.blit(msg, msg_rect)
pygame.display.update()

pygame.time.delay(2000)

pygame.quit()

답변 1

답변을 작성해보세요.

0

uki020226님의 프로필

uki020226

2021.03.10

            for weapon_idx, weapon_val in enumerate(weapons):

위 코드 이후로 탭 을 한번 확인 해보세요. 두칸씩 밀려야 합니다.  이 코드 위의 if 와 같은 수준에 이 코드의 for 문이 위치 해야 합니다. 

                    break

도 두수준 탭을 줘야 합니다. 

탭위치 항상 확인하세요.  어느거 안에서 실행되어야 하는지 아닌지 이런것들을 포함하게 되니까요. 

아래는 참고 사항 입니다.
for ~~~
   If ~~
  for~~~

하는 부분의 탭 위치 잘보세요. 아래는 제 코드인데, 저는 약간 변수 명이 달라서, 그대로 복사하면 변수들 바꿔달라고 아우성일겁니다. 


    for ball_idx, ball_val in enumerate(balls):
        ball_pos_x = ball_val["pos_x"]
        ball_pos_y = ball_val["pos_y"]
        ball_img_idx = ball_val["img_idx"]

        # 공 rext 정보 업데이트
        ball_rect = ball_images[ball_img_idx].get_rect()
        ball_rect.left = ball_pos_x
        ball_rect.top = ball_pos_y
        
        # 공과 캐릭터 충돌 처리
        if character_rect.colliderect(ball_rect):
            running = False
            break

        # 공과 무기 충돌 처리
        for weapon_idx, weapon_val in enumerate(weapons):
            weapon_x = weapon_val[0]
            weapon_y = weapon_val[1]

            # 무기 rect 정보 업데이트
            weapon_rect = weapon.get_rect()
            weapon_rect.left = weapon_x
            weapon_rect.top = weapon_y

            # 충돌 체크
            if weapon_rect.colliderect(ball_rect):
                weapon_to_remove = weapon_idx # 해당 무기 없애기 위한 값 설정
                ball_to_remove = ball_idx # 해당 공 없애기 위한 값 설정

                if ball_img_idx < 3:
                    # 현재 공 크기 정보
                    ball_width = ball_rect.size[0]
                    ball_hight = ball_rect.size[1]
                    # 나눠진 공 정보
                    small_ball_rect = ball_images[ball_img_idx + 1].get_rect()
                    small_ball_width = small_ball_rect.size[0]
                    small_ball_hight = small_ball_rect.size[1]

                    # 왼쪽파트
                    balls.append({
                        "pos_x" : ball_pos_x + (ball_width / 2) - (small_ball_width / 2),
                        "pos_y" : ball_pos_y + (ball_hight / 2) - (small_ball_hight / 2),
                        "img_idx" : ball_img_idx + 1,
                        "to_x" : -3,
                        "to_y" : -6,
                        "init_spd_y" : ball_speed_y[ball_img_idx + 1]
                        })

                    # 오른쪽 파트
                    balls.append({
                        "pos_x" : ball_pos_x + (ball_width / 2) - (small_ball_width / 2),
                        "pos_y" : ball_pos_y + (ball_hight / 2) - (small_ball_hight / 2),
                        "img_idx" : ball_img_idx + 1,
                        "to_x" : 3,
                        "to_y" : -6,
                        "init_spd_y" : ball_speed_y[ball_img_idx + 1]
                        })
                break