반응형
Python Level 1에서 배운 것들을 총동원해 행맨 게임 만들기!
import time
# 처음 인사
name = input("What is your name?")
print("Hi," + name + ". Let's Play Hangman!")
time.sleep(1)
print("Start Loading...")
time.sleep(0.5)
# 정답
word = "python"
# 추측 단어
guesses = ""
# 기회
turns = 10
# 기화가 남아 있을 경우
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()
print('Congratulations! The guesses is correct!')
break
print()
# 추측 단어 문자 단위 입력
print()
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:
# 실패 메시지
print("hangman game failed. Bye!")
위의 게임을 좀 더 엘레강스하게 바꿔보자!
- 무작위의 단어 선택 기능을 추가
resource 폴더에 word.csv파일을 만들어 준다.
# word.csv
Name,Hint
strawberry, fruit
grape, fruit
melon, fruit
orange, fruit
wagon, type of vehicle
van, type of vehicle
sedan, type of vehicle
import csv
import random
import time
# 처음 인사
name = input("What is your name?")
print("Hi," + name + ". Let's Play Hangman!")
print()
time.sleep(1)
print("Start Loading...")
print()
time.sleep(0.5)
# csv 단어 리스트
words = []
# 문제 csv 파일 로드
with open('./resource/word.csv', 'r') as f:
reader = csv.reader(f)
# Header skip
next(reader)
for c in reader:
words.append(c)
# 리스트 섞기
random.shuffle(words)
q = random.choice(words)
# 정답 단어
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
print('')
if failed == 0:
print()
print()
print('Congratulations! The guesses is correct!')
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:
# 실패 메시지
print("hangman game failed. Bye!")
게임을 좀 더 게임답게 사운드 추가
sound 폴더에 성공과 실패 시 재생할 파일을 넣어준다.
import csv
import random
import time
import winsound
# 처음 인사
name = input("What is your name?")
print("Hi," + name + ". Let's Play Hangman!")
print()
time.sleep(1)
print("Start Loading...")
print()
time.sleep(0.5)
# csv 단어 리스트
words = []
# 문제 csv 파일 로드
with open('./resource/word.csv', 'r') as f:
reader = csv.reader(f)
# Header skip
next(reader)
for c in reader:
words.append(c)
# 리스트 섞기
random.shuffle(words)
q = random.choice(words)
# 정답 단어
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
print('')
if failed == 0:
print()
print()
# 성공 사운드
winsound.PlaySound('./sound/good.mp3', winsound.SND_FILENAME)
print('Congratulations! The guesses is correct!')
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.mp3', winsound.SND_FILENAME)
# 실패 메시지
print("hangman game failed. Bye!")
반응형