Notice
Recent Posts
Recent Comments
Today
Total
05-17 21:51
Archives
관리 메뉴

Jeongchul Kim

DragonGame.py 본문

Computer Language

DragonGame.py

김 정출 2015. 1. 9. 17:58
  1. # Dragon Game + edit version
  2. # 게임은 유저에게 체력과 보물이라는 스탯을 준다.
  3. # 용의 던전 탐험은 총 2가지의 던전으로 한 던전은 체력 감소를 다른 던전은 보물을 준다.
  4. # 유저의 총 체력이 0일 경우 죽음을 맞이하며 게임이 종료된다.
  5. # 종료 전에 유저에게 다시 게임을 할 것인지 묻는다.
  6.  
  7. import random
  8. import time
  9.  
  10. # def 함수이름():
  11. # def문은 def 키워드 다음에 함수의 이름,괄호, 그리고 콜론(:)이 나온다.
  12. # def문은 프로그램의 나중에 호출할 함수를 만들고 정의(define)한다.
  13. def displayIntro():
  14.     print('You are in a land full of dragons. In front of You')
  15.     print('you see two caves. In one cave, the dragon is friendly')
  16.     print('and will share his treasuer with you. The other dragon')
  17.     print('is greedy and hungry, and will eat you on sight')
  18.     print()
  19. # 함수 정의는 함수를 호출하기 전에 선 정의해야 한다.
  20.  
  21. def chooseCave():
  22.     cave = ' ' # 빈 문자열 저장
  23.     while cave != '1' and cave != '2':
  24.     # !=,== : 불리언 연산자(bools), and,or,not연산자
  25.         print('Which cave will you go into (1 or 2)')
  26.         cave = input() # input()함수는 문자열을 반환한다.
  27.        
  28.     return cave # return 키워드 : 반환값
  29.  
  30. # 변수 영역(scope)
  31. #  - 전역 영역(Global Scope) : 함수의 바깥 영역
  32. #       수정은 함수 밖에서만 가능하며, 함수 안에서는 읽기 가능
  33. #  - 지역 영역(Local Scope) : 함수의 내부 영역
  34.  
  35. def checkCave(chosenCave)# ()괄호 안, 변수들 파라미터(parameter,매개변수), 인자를 넘겨준다고 한다.
  36.     print('You approach the cave...')
  37.     time.sleep(2) # time모듈의 sleep()함수 : 인자값을 통해 프로그램을 인자값 만큼 멈춰준다.
  38.     print('It is dark and spooky...')
  39.     time.sleep(2)
  40.     print('A large drageon jumps out in front of you! He opens his jaws and...')
  41.     print()
  42.     time.sleep(2)
  43.  
  44.     frinedlyCave = random.randint(1,2)
  45.  
  46.     # 문자열과 정수를 비교하기 위해서는 데이터 타입을 맞춰야 한다.
  47.     if chosenCave == str(frinedlyCave):
  48.         print('Gives you his treasure!')      
  49.         return 1
  50.     else:
  51.         print('Gobbles you donw in one bite!')
  52.         return 0
  53.  
  54. # 전역 영역 변수
  55. health = 50
  56. treasure = 0
  57. playAgain = 'yes'
  58.  
  59. while playAgain == 'yes' or playAgain == 'y'# yes일 경우 프로그램은 계속 실행된다.
  60.  
  61.     if str(health) == '0'# 조건식 : health가 0 일 경우 게임이 종료된다.
  62.         print('Game is Over')
  63.         print('Do you want to play again? (yes or no)') # 게임을 다시 할 지 유저에게 물어본다.
  64.         playAgain = input()
  65.         if( playAgain == 'yes')# 재시작을 원할 경우 health를 초기화 시켜준다.
  66.             health = 50
  67.                
  68.     displayIntro()
  69.  
  70.     print('Your treasure = '+str(treasure)) # 현재 유저의 체력과 보물의 양을 나타낸다.
  71.     print('Your Health = '+str(health))
  72.    
  73.     caveNumber = chooseCave()
  74.  
  75.     isCheck = checkCave(caveNumber)
  76.    
  77.     if str(isCheck) == '1'# 반환 값이 1일 경우 보물의 양을 증가시킨다.
  78.         treasure = treasure + 10
  79.     else : # 0일 경우 체력을 감소시킨다.
  80.         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