• 카테고리

    질문 & 답변
  • 세부 분야

    게임 프로그래밍

  • 해결 여부

    미해결

왜 그런지 모르겠지만 다른 버그가 있습니다.

20.11.22 21:41 작성 조회수 145

0

공들을 오른쪽으로 나오는 공들을 다 터트리고 왼쪽으로 나눠진 공들을 터트리면 공이 나눠지는 것이 아닌 바로 없어집니다.

예를들어 큰공을 터트렸을때 그 다음 공들이 아래와 같이 나눠진다고 할때

balloon1 -> balloon2_1(왼쪽공) / balloon2_2(오른쪽공)

balloon2_2 -> balloon3_1 / Balloon3_2

balloon3-2 -> balloon4_1 / Balloon4_2

(Balloon2_1, Balloon3_1, Balloon4_1은 남아있음)

Balloon1 -> Balloon2_2 -> Balloon3_2 -> Balloon4_2

를 터트리고 Balloon2_1을 터트리면 공이 안나눠지고 바로 없어집니다.

답변 4

·

답변을 작성해보세요.

0

finalcut02님의 프로필

finalcut02

2021.01.08

아 그리고 그렇게 해도 안되면 제가 못찾은 부분이 있을 수도 있습니다.

0

finalcut02님의 프로필

finalcut02

2021.01.08

네! 저 ball_img_idx라는 변수를 지정할 때 'ball'_img.... 이라고 적었는데

아래 처럼 balloon_img_idx이라고 쓴게 많은데 --> 로 수정할 부분을 표시하도록 하겠습니다. (ball_img_idx로)

그리고 되도록이면 영상속 변수 이름과 똑같이 쓰는게 혼동되지 않기 때문에 좋습니다.

                # 가장 작은 크기의 풍선이 아니라면 다음 단계의 풍선으로 나눠주기
                if 요기 --> balloon_img_idx <-- < 3 :
                    # 현재 풍선 크기 정보를 가지고 옴
                    balloon_width = balloon_rect.size[0]
                    balloon_height = balloon_rect.size[1]
                    # 나눠진 공 정보
                    small_balloon_rect = balloon_images[--> balloon_img_idx <-- +1].get_rect()
                    small_balloon_width = small_balloon_rect.size[0]
                    small_balloon_height = small_balloon_rect.size[1]

                    # 왼쪽으로 나눠지는 작은 풍선
                    balloons.append({
                        "pos_x" : balloon_x_pos + balloon_width / 2 - small_balloon_width / 2, # 풍선의 x 좌표
                        "pos_y" : balloon_y_pos + balloon_height / 2 - small_balloon_height / 2, # 풍선의 y 좌표
                        "img_idx" : --> balloon_img_idx <-- +1, # 풍선의 이미지 인덱수
                        "to_x": -3, # x축 이동방향, -3 이면 왼쪽으로, 3이면 오른쪽으로
                        "to_y": -6, # y축 이동방향, 
                        "init_spe_y": balloon_speed_y[--> balloon_img_idx <-- +1]})# y 최초 속도

                    # 오른쪽으로 나눠지는 작은 풍선
                    balloons.append({
                        "pos_x" : balloon_x_pos + balloon_width / 2 - small_balloon_width / 2, # 풍선의 x 좌표
                        "pos_y" : balloon_y_pos + balloon_height / 2 - small_balloon_height / 2, # 풍선의 y 좌표
                        "img_idx" : --> balloon_img_idx <-- +1, # 풍선의 이미지 인덱수
                        "to_x": 3, # x축 이동방향, -3 이면 왼쪽으로, 3이면 오른쪽으로
                        "to_y": -6, # y축 이동방향, 
                        "init_spe_y": balloon_speed_y[ --> balloon_img_idx <-- +1]})# y 최초 속도
                break
        else: # 계속 게임을 진행
            continue # 안쪽 for 문 조건이 맞지 않으면 continue, 바깥 for 문 계속 수행
        break
    
    # 이중 for문을 동작시킬때 내부동작에서 특정조건시 모든 for문 종료 하고 싶을때 사용하는 코딩
    # for 외부조건:
    #     외부동작
    #     for 내부조건:
    #         내부동작
    #         if 특정조건:
    #             break
    #     else:
    #         continue
    #     break

    # 충돌된 풍선 or 무기 없애기
    if balloon_to_remove > -1:
        del balloons[balloon_to_remove]
        balloon_to_remove = -1

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

    # 모든 풍선을 없앤 경우 게임 종료 (Mission Complete)
    if len(balloons) == 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))

    for idx, val in enumerate(balloons):
        balloon_x_pos = val["pos_x"]
        balloon_y_pos = val["pos_y"]
        --> balloon_img_idx <-- = val["img_idx"]
        screen.blit(balloon_images[--> balloon_img_idx <--], (balloon_x_pos, balloon_y_pos))

    screen.blit(stage, (0, screen_height-stage_height))
    screen.blit(character, (character_x_pos, character_y_pos))

    # 경과 시간 계산
    elapsed_time = (pygame.time.get_ticks() - start_ticks) / 1000
    timer = game_font.render(f"Time : {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()  # 게임화면을 다시 그리기! (반드시 호출되어야 함)

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

#2초 대기
pygame.time.delay(2000)

# pygame 종료
pygame.quit()

0

김영진님의 프로필

김영진

질문자

2021.01.07

다음과 같이 작성하였습니다.

# 1. 모든 공을 없애면 게임 종료 (성공)
# 2. 캐릭터는 공에 닿으면 게임 종료 (실패)
# 3. 시간 제한 99초 초과 시 게임 종료 (실패)

import os
import pygame
####################################################################################################
# 기본 초기화 (반드시 해야하는 것들)
pygame.init()  # 초기화

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


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

# 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 = 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

# 풍선 만들기 (4개 크기에 대해 따로 처리)
balloon_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"))]

# 풍선 크기에 따른, - 최초 속도 설정
balloon_speed_y = [-12, -9, -6, -3]  # index 0, 1, 2, 3에 해당하는 값

# 풍선 처리
balloons = []

balloons.append({
    "pos_x" : 50, # 풍선의 x 좌표
    "pos_y" : 50, # 풍선의 y 좌표
    "img_idx" : 0, # 풍선의 이미지 인덱수
    "to_x": 3, # x축 이동방향, -3 이면 왼쪽으로, 3이면 오른쪽으로
    "to_y": -6, # y축 이동방향, 
    "init_spe_y": balloon_speed_y[0]# y 최초 속도
})

# 풍선과 무기 충돌시 소멸시킬 각각의 정보 저장 변수
weapon_to_remove = -1
balloon_to_remove = -1

# Font 정의
game_font = pygame.font.Font(None, 40)
total_time = 100
start_ticks = pygame.time.get_ticks() # 시작 시간 정의

# 게임 종료 메시지
# Time out, Mission Complete, Game Over
game_result = "Game Over"

# 이벤트 루프
running = True  # 게임이 진행중인가? - True
while running:
    dt = clock.tick(60)  # 게임화면의 초당 프레임 수를 설정 (dt = delta)

####################################################################################################
    # 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 ...
    # 500, 200 -> 180, 160, 140 ...
    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 balloon_idx, balloon_val in enumerate(balloons): #풍선의 index정보와 value값을 가져올때 사용하는 함수
        balloon_x_pos = balloon_val["pos_x"]
        balloon_y_pos = balloon_val["pos_y"]
        ball_img_idx = balloon_val["img_idx"]

        balloon_size = balloon_images[ball_img_idx].get_rect().size
        balloon_width = balloon_size[0]
        balloon_height = balloon_size[1]

        # 풍선이 가로벽에 닿았을때 풍선의 튕기는 방햔 변경 처리 (튕겨 나오는 효과)
        if balloon_x_pos <= 0 or balloon_x_pos > screen_width - balloon_width:
            balloon_val["to_x"] = balloon_val["to_x"] * -1

        # 풍선이 무대에 닿았을때 풍선의 튕기는 방햔 변경 처리 (튕겨 나오는 효과)
        if balloon_y_pos >= screen_height - stage_height - balloon_height:
            balloon_val["to_y"] = balloon_val["init_spe_y"]
        else: # 그 외 모든 모든 경우에는 속도를 증가 
            balloon_val["to_y"] += 0.5

        balloon_val["pos_x"] += balloon_val["to_x"]
        balloon_val["pos_y"] += balloon_val["to_y"]

    # 4. 충돌 처리
    # 캐릭터 rect 정보 업데이트
    character_rect = character.get_rect()
    character_rect.left = character_x_pos
    character_rect.top = character_y_pos

    for balloon_idx, balloon_val in enumerate(balloons): #풍선의 index정보와 value값을 가져올때 사용하는 함수
        balloon_x_pos = balloon_val["pos_x"]
        balloon_y_pos = balloon_val["pos_y"]
        ball_img_idx = balloon_val["img_idx"]

        # 풍선 rect 정보 업데이트
        balloon_rect = balloon_images[ball_img_idx].get_rect()
        balloon_rect.left = balloon_x_pos
        balloon_rect.top = balloon_y_pos
        
        # 풍선과 캐릭터 충돌 처리
        if character_rect.colliderect(balloon_rect):
            running = False
            break

        #풍선과 무기들 충돌 처리
        for weapon_idx, weapon_val in enumerate(weapons):
            weapon_pos_x = weapon_val[0]
            weapon_pos_y = weapon_val[1]

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

            #충돌 체크
            if weapon_rect.colliderect(balloon_rect):
                weapon_to_remove = weapon_idx # 해당 무기 없애기 위한 값 설정
                balloon_to_remove = balloon_idx # 해장 풍선 없애기 위한 값 설정

                # 가장 작은 크기의 풍선이 아니라면 다음 단계의 풍선으로 나눠주기
                if balloon_img_idx < 3 :
                    # 현재 풍선 크기 정보를 가지고 옴
                    balloon_width = balloon_rect.size[0]
                    balloon_height = balloon_rect.size[1]

                    # 나눠진 공 정보
                    small_balloon_rect = balloon_images[balloon_img_idx +1].get_rect()
                    small_balloon_width = small_balloon_rect.size[0]
                    small_balloon_height = small_balloon_rect.size[1]

                    # 왼쪽으로 나눠지는 작은 풍선
                    balloons.append({
                        "pos_x" : balloon_x_pos + balloon_width / 2 - small_balloon_width / 2, # 풍선의 x 좌표
                        "pos_y" : balloon_y_pos + balloon_height / 2 - small_balloon_height / 2, # 풍선의 y 좌표
                        "img_idx" : balloon_img_idx+1, # 풍선의 이미지 인덱수
                        "to_x": -3, # x축 이동방향, -3 이면 왼쪽으로, 3이면 오른쪽으로
                        "to_y": -6, # y축 이동방향, 
                        "init_spe_y": balloon_speed_y[balloon_img_idx+1]})# y 최초 속도

                    # 오른쪽으로 나눠지는 작은 풍선
                    balloons.append({
                        "pos_x" : balloon_x_pos + balloon_width / 2 - small_balloon_width / 2, # 풍선의 x 좌표
                        "pos_y" : balloon_y_pos + balloon_height / 2 - small_balloon_height / 2, # 풍선의 y 좌표
                        "img_idx" : balloon_img_idx+1, # 풍선의 이미지 인덱수
                        "to_x": 3, # x축 이동방향, -3 이면 왼쪽으로, 3이면 오른쪽으로
                        "to_y": -6, # y축 이동방향, 
                        "init_spe_y": balloon_speed_y[balloon_img_idx+1]})# y 최초 속도
                break
        else: # 계속 게임을 진행
            continue # 안쪽 for 문 조건이 맞지 않으면 continue, 바깥 for 문 계속 수행
        break
    
    # 이중 for문을 동작시킬때 내부동작에서 특정조건시 모든 for문 종료 하고 싶을때 사용하는 코딩
    # for 외부조건:
    #     외부동작
    #     for 내부조건:
    #         내부동작
    #         if 특정조건:
    #             break
    #     else:
    #         continue
    #     break

    # 충돌된 풍선 or 무기 없애기
    if balloon_to_remove > -1:
        del balloons[balloon_to_remove]
        balloon_to_remove = -1

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

    # 모든 풍선을 없앤 경우 게임 종료 (Mission Complete)
    if len(balloons) == 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))

    for idx, val in enumerate(balloons):
        balloon_x_pos = val["pos_x"]
        balloon_y_pos = val["pos_y"]
        balloon_img_idx = val["img_idx"]
        screen.blit(balloon_images[balloon_img_idx], (balloon_x_pos, balloon_y_pos))

    screen.blit(stage, (0, screen_height-stage_height))
    screen.blit(character, (character_x_pos, character_y_pos))

    # 경과 시간 계산
    elapsed_time = (pygame.time.get_ticks() - start_ticks) / 1000
    timer = game_font.render(f"Time : {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()  # 게임화면을 다시 그리기! (반드시 호출되어야 함)

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

#2초 대기
pygame.time.delay(2000)

# pygame 종료
pygame.quit()

0

finalcut02님의 프로필

finalcut02

2021.01.05

코드를 주셔야 답변 할 수 있는데여..