TIL-Python

hs·2021년 7월 22일
1
post-thumbnail

전에 알고리즘에 대해 공부를 할때 파이썬을 사용하여 공부를 했었다. 그러다보니 새롭게 다시 언어를 공부할 생각을 하니 막막했다. 의욕도 잘 안나고 진도도 느리게 나갔다. 어떻게하면 다시 파이썬에 흥미를 가질 수 있을까 생각을 했다. 그러다 유튜브 나도코딩의 영상 중 어렸을 때 오락실에서 즐겼던 퍼즐 보블(Puzzle Bobble)을 파이썬으로 만드는 영상이 올라왔다. 이에 흥미가 생겨 영상을 보며 공부를 하며 조금 만들어 보았다.

시작

pygame 라이브러리를 처음 사용해보았다. 하지만 영상에 친절하게 설명이 되어 있는 부분이 있었고 잘 모르겠는 부분은 검색을 통해 공부하며 진행을 했다.

1. 프레임 및 창 설정

running = True
while running:
    clock.tick(60) # FPS 60 으로 설정
    for event in pygame.event.get():
        if event.type == pygame.QUIT:   # x 버튼을 눌러 종료
            running = False

프레임을 60으로 설정하고 pygame.event를 활용하여 종료 버튼을 생성

2. 배경

배경 이미지 불러오기
current_path = os.path.dirname(__file__)    # 현재 이 파일이 있는 디렉토리의 경로
background = pygame.image.load(os.path.join(current_path, "background.png"))
...
    screen.blit(background, (0, 0))
    pygame.display.update()

3. 버블

버블은 화면 전체를 맵으로 만든 뒤 행렬과 같이 칸을 나누어서 진행을 하였다.

#버블 클래스 생성
class Bubble(pygame.sprite.Sprite):
    def __init__(self, image, color, position):
        super().__init__()
        self.image = image
        self.color = color
        self.rect = image.get_rect(center=position)
#맵 만들기
def setup():
    global map
    map = [
        # ["R","R","Y","Y","B","B","G","G"]
        list("RRYYBBGG"),
        list("RRYYBBG/"),   # / : 버블이 위치 할 수 없는 곳
        list("BBGGRRYY"),
        list("BGGRRYY/"),
        list("........"),   # . : 비어 있는 곳
        list("......./"),
        list("........"),
        list("......./"),
        list("........"),
        list("......./"),
        list("........"),
    ]
    #공백 지정
    for row_idx, row in enumerate(map):
        for col_idx, col in enumerate(row):
            if col in [".", "/"]:
                continue
            position = get_bubble_position(row_idx, col_idx)
            image = get_bubble_image(col)
            bubble_group.add(Bubble(image, col, position)
#버블 위치 조정
def get_bubble_position(row_idx,col_idx):
    pos_x = col_idx * CELL_SIZE + (BUBBLE_WIDTH // 2)
    pos_y = row_idx * CELL_SIZE + (BUBBLE_HEIGHT // 2)
    if row_idx % 2 == 1:
        pos_x += BUBBLE_WIDTH // 2
    return pos_x, pos_y
#이미지 넣기
def get_bubble_image(color):
    if color == "R":
        return bubble_images[0] 
    elif color == "Y":
        return bubble_images[1]
    elif color == "B":
        return bubble_images[2] 
    elif color == "G":
        return bubble_images[3]
    elif color == "P":
        return bubble_images[4]
    else: # black
        return bubble_images[-1]

profile
무엇이든 끝까지 보람차게

0개의 댓글