Jeongchul Kim
DragonGame.py 본문
- # Dragon Game + edit version
- # 게임은 유저에게 체력과 보물이라는 스탯을 준다.
- # 용의 던전 탐험은 총 2가지의 던전으로 한 던전은 체력 감소를 다른 던전은 보물을 준다.
- # 유저의 총 체력이 0일 경우 죽음을 맞이하며 게임이 종료된다.
- # 종료 전에 유저에게 다시 게임을 할 것인지 묻는다.
- import random
- import time
- # def 함수이름():
- # def문은 def 키워드 다음에 함수의 이름,괄호, 그리고 콜론(:)이 나온다.
- # def문은 프로그램의 나중에 호출할 함수를 만들고 정의(define)한다.
- def displayIntro():
- print('You are in a land full of dragons. In front of You')
- print('you see two caves. In one cave, the dragon is friendly')
- print('and will share his treasuer with you. The other dragon')
- print('is greedy and hungry, and will eat you on sight')
- print()
- # 함수 정의는 함수를 호출하기 전에 선 정의해야 한다.
- def chooseCave():
- cave = ' ' # 빈 문자열 저장
- while cave != '1' and cave != '2':
- # !=,== : 불리언 연산자(bools), and,or,not연산자
- print('Which cave will you go into (1 or 2)')
- cave = input() # input()함수는 문자열을 반환한다.
- return cave # return 키워드 : 반환값
- # 변수 영역(scope)
- # - 전역 영역(Global Scope) : 함수의 바깥 영역
- # 수정은 함수 밖에서만 가능하며, 함수 안에서는 읽기 가능
- # - 지역 영역(Local Scope) : 함수의 내부 영역
- def checkCave(chosenCave): # ()괄호 안, 변수들 파라미터(parameter,매개변수), 인자를 넘겨준다고 한다.
- print('You approach the cave...')
- time.sleep(2) # time모듈의 sleep()함수 : 인자값을 통해 프로그램을 인자값 만큼 멈춰준다.
- print('It is dark and spooky...')
- time.sleep(2)
- print('A large drageon jumps out in front of you! He opens his jaws and...')
- print()
- time.sleep(2)
- frinedlyCave = random.randint(1,2)
- # 문자열과 정수를 비교하기 위해서는 데이터 타입을 맞춰야 한다.
- if chosenCave == str(frinedlyCave):
- print('Gives you his treasure!')
- return 1
- else:
- print('Gobbles you donw in one bite!')
- return 0
- # 전역 영역 변수
- health = 50
- treasure = 0
- playAgain = 'yes'
- while playAgain == 'yes' or playAgain == 'y': # yes일 경우 프로그램은 계속 실행된다.
- if str(health) == '0': # 조건식 : health가 0 일 경우 게임이 종료된다.
- print('Game is Over')
- print('Do you want to play again? (yes or no)') # 게임을 다시 할 지 유저에게 물어본다.
- playAgain = input()
- if( playAgain == 'yes'): # 재시작을 원할 경우 health를 초기화 시켜준다.
- health = 50
- displayIntro()
- print('Your treasure = '+str(treasure)) # 현재 유저의 체력과 보물의 양을 나타낸다.
- print('Your Health = '+str(health))
- caveNumber = chooseCave()
- isCheck = checkCave(caveNumber)
- if str(isCheck) == '1': # 반환 값이 1일 경우 보물의 양을 증가시킨다.
- treasure = treasure + 10
- else : # 0일 경우 체력을 감소시킨다.
- health = health - 10
'Computer Language' 카테고리의 다른 글
HangmanGame.py (0) | 2015.01.10 |
---|---|
bug & debugger (0) | 2015.01.09 |
IntroduceGame.py (0) | 2015.01.09 |
NumberGame.py (0) | 2015.01.09 |
hello.py (0) | 2015.01.09 |
Comments