본문 바로가기

개발자 모드/파이썬(python)

파이썬 python 행맨 게임 만들기

728x90

# chapter 10-1
# 행맨 미니 게임 제작
# 기본 프로그램 제작 및 테스트
# 교재 완성 및 최종 테스트

import time

# CSV 처리
import csv
# 랜덤
import random
# 싸운드
import winsound



# 처음 인사

name = input("what is your name?")

print("hi" +  name, " Time is to play hangman game" )

print()

time.sleep(1)

print("Start Loading...")
print()

time.sleep(0.5)

# CSV 단어 리스트
words = []

# with open('./resource/word_list.csv', 'r') as f:
#     reader = csv.reader(f)

import os.path

my_path = os.path.abspath(os.path.dirname(__file__))
print(my_path)
path = my_path +('/resource/word_list.csv')
print(path)

with open(path) as f:
    reader = csv.reader(f)

    #  헤더 스킵
    next(reader)
    for c in reader :
        words.append(c)



# 리스트 섞기
random.shuffle(words)
q = random.choice(words)

print(q)


# 정답 단어
# word = 'secret'
word = q[0].strip()

#  추측 단어
guesses = ''

# 기회
turns = 10

# 핵심 while loop
# 찬스 카운트가 남아 있을 경우

while turns > 0 :
    # 실패 횟수
    failed = 0
    # 정답 단어 반복
    for char  in word :
        # 정답 단어 내에 추측 문자가 포함되어  있는 경우
        if char in guesses :
            # 추측  단어 출력
            print(char, end=' ')
        else :
            #틀린 경우 대시로 처리
            print('_',end =' ')
            failed +=1;

    # 단어 추측이 성공 한 경우
    if failed == 0 :
        print()
        print()
        #성공 사운드
        winsound.PlaySound('./sound/good.wav', winsound.SND_FILENAME)

        print('congration')
        # while 문 중단
        break
    print()

    # 추측 단어 문자 단위 입력
    print()

    print('hint : {}'.format(q[1].strip()))

    guess = input(" guess a character.")

    # 단어 더하기
    guesses += guess

    #정답 단어에 추측한 문자가 포함되어 있지 않으면

    if guess not in word :
        turns -= 1
        # 오류 메시지
        print("oops wrong")
        # 남은 기회 출력
        print("you have", turns, 'more guesses !')

        if turns == 0 :
            #실패 사운드
            winsound.PlaySound('./sound/bad.wav', winsound.SND_FILENAME)
            # 실패 메세지
            print("failed bye!")
728x90