내일배움캠프 - python 게임 만들기 - (4) 3일차 개발일지

Dongwoo Kim·2022년 4월 27일
0

내일배움캠프 AI 웹개발자양성과정 2회차

2022.04.27. python 게임만들기 - (4) 3일차 개발일지

  1. 프로젝트 정보
    1) 스파르타 파이터 - pygame을 이용한 격투게임 만들기
    2) 목적 : 직접 게임을 설계해보고 python을 이용하여 설계한 내용을 구현해봄으로써 설계과 구현에 익숙해지고 python 실력을 향상시키고자 함
    3) 진행상황 : 궁극기 구현, 시작메뉴 및 인트로 추가, 캐릭터 충돌
    4) 개발 못한 부분 : 공격 콤보, 캐릭터 다양화, 태그매치 등

1. 궁극기 요소 추가

궁극기 요소를 캐릭터 특성에 추가

# 파이터 캐릭터 클래스
class Fighter:
...
    energy = 0  # 궁극기 에너지 - 0~100
...    
    self.attack_special_img = pygame.image.load(os.path.join(images_path, 'attack_special.png'))
    self.attack_special_ready = pygame.image.load(os.path.join(images_path, 'attack_special_ready.png'))
...

2. 타격/피격/수비 판정에 따른 궁극기 에너지 추가

    # 상단/중단/하단 공격
    # attack_type : 공격 종류 - 1:상단, 2:중단, 3:하단
    # enemy : 적의 캐릭터
    def attack(self, attack_type, enemy):
    ...
   		if not enemy.hit_bool:
            self.energy += 10
            if self.energy > 100:
                self.energy = 100	

3. 궁극기 공격 함수

attack()과 별개로 따로 정의한 이유 : attack()과 별개로 타격판정을 거치지않고 충돌 판정만 하면 됨

    # 궁극기 공격
    # enermy : 적의 캐릭터
    def attack_special(self, enemy):
        # 궁극기 rect를 만들어서
        rect = self.attack_special_img.get_rect()
        if self.vector == 100:
            rect.left = self.x_pos + self.vector
        else:
            width = self.attack_special_img.get_width()
            rect.left = self.x_pos - width
        rect.top = self.y_pos + self.high_rage

        # 적 rect와
        enemy_rect = enemy.char.get_rect()
        enemy_rect.left = enemy.x_pos
        enemy_rect.top = enemy.y_pos

        # 충돌했으면 무조건 데미지
        if rect.colliderect(enemy_rect):
            if not enemy.hit_bool:
                critical = False
                damage = self.damages[4]
                enemy.get_hit(2, critical, damage)

4. 캐릭터 충돌 판정

    # 캐릭터 이동
    # screen_height : 화면 높이
    # screen_width : 화면 너비
    # stage_height : 스테이지 높이
    # dt : 이동 프레임 수
    def move_char(self, screen_height, screen_width, stage_height, dt, enermy):
    	...
        # 적을 넘어갈 수 없음
        if self.position == -1:
            if self.x_pos + self.width > enermy.x_pos:
                self.x_pos = enermy.x_pos - self.width
        else:
            if self.x_pos < enermy.x_pos + enermy.width:
                self.x_pos = enermy.x_pos + enermy.width

main.py에 추가한 내용

5. 시작 메뉴 요소 추가

# 타이틀
title_font = pygame.font.Font(None, 100)
title_text = title_font.render(' Saparta Fighter ', True, (255, 0, 0))
title_text_width = title_text.get_width()
title_text_rect = title_text.get_rect(center=(int(screen_width / 2), int(screen_height / 5)))

# 시작 메뉴
menu_font = pygame.font.Font(None, 100)
menu_intro = menu_font.render(' Intro ', True, (0, 0, 255))
menu_play = menu_font.render(' Play ', True, (0, 255, 0))
menu_button_width = menu_button.get_width()
menu_button_height = menu_button.get_height()
menu_intro_rect = menu_intro.get_rect(center=(int(screen_width / 2), int(screen_height * 2 / 4)))
menu_play_rect = menu_play.get_rect(center=(int(screen_width / 2), int(screen_height * 3 / 4)))

6. 진행 단계 판단

flow를 통해 현재 게임 진행 단계 판단

# 게임 진행 단계
flows = ('menu', 'intro', 'play')
flow = flows[0]

flow별 이벤트 지정

running = True   # 게임 진행 변수
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:  # 종료 이벤트
            running = False
        elif flow == flows[0]:
		...
        elif flow == flows[1]:
        ...
        elif flow == flows[2]:

flow별 화면 그리기

	...
    # 시작 메뉴
    if flow == flows[0]:
    	...
    # 게임 설명
    elif flow == flows[1]:    
    	...    
	# 게임 시작
    elif flow == flows[2]:
    	...
profile
kimphysicsman

0개의 댓글