当前位置:Gxlcms > Python > python实现的简单文本类游戏实例

python实现的简单文本类游戏实例

时间:2021-07-01 10:21:17 帮助过:95人阅读

本文实例讲述了python实现的简单文本类游戏实现方法。分享给大家供大家参考。具体实现方法如下:

  1. ############################################################
  2. # - My version on the game "Dragon Realm".
  3. # - taken from the book "invent with python" by Al Sweigart.
  4. # - thanks for a great book Mr Sweigart.
  5. # - this code takes advantage of python 3.
  6. ############################################################
  7. #files.py
  8. import random
  9. import time
  10. print('\n\n[--system--] one file is bad the other is good ..guess the right one.\n')
  11. print('\n\nconnecting....')
  12. time.sleep(1)
  13. print('....')
  14. time.sleep(1)
  15. print('....')
  16. time.sleep(1)
  17. print('....')
  18. time.sleep(1)
  19. print('\nconnection established')
  20. def displayIntro():
  21. print('------------')
  22. print('SYSTEM FILES')
  23. print('------------\n')
  24. print('1.) file.')
  25. print('2.) file.\n')
  26. def chooseOption():
  27. option = ''
  28. while option != '1' and option != '2':
  29. print('which file to download? 1 or 2')
  30. option = input('user:> ')
  31. return option
  32. def checkOption(chosenOption):
  33. print('\nintialising download....')
  34. time.sleep(1)
  35. print('accessing file....')
  36. time.sleep(1)
  37. print('downloading....')
  38. time.sleep(1)
  39. print('....')
  40. time.sleep(1)
  41. print('....')
  42. time.sleep(1)
  43. goodfile = random.randint(1, 2)
  44. if chosenOption == str(goodfile):
  45. print('\ndownload complete.')
  46. print('\nGAME OVER')
  47. else:
  48. print('\nfile corrupt')
  49. print('system infected.')
  50. print('\nGAME OVER')
  51. playAgain = 'yes'
  52. while playAgain == 'yes':
  53. displayIntro()
  54. optionNumber = chooseOption()
  55. checkOption(optionNumber)
  56. print('\ndownload again? .... (yes or no)')
  57. playAgain = input('user:> ')

  1. ############################################################
  2. # - My version of the game "guess the number".
  3. # - taken from the book "invent with python" by Al Sweigart.
  4. # - thanks for a great book Mr Sweigart.
  5. # - this code takes advantage of python 3.
  6. ############################################################
  7. # -NOTE - this program will crash if a number is not typed.
  8. #digitcode.py
  9. import random
  10. import time
  11. guessesTaken = 0
  12. print('\n\n\n\n\n[--system--] enter code in 15 trys to avoid lockout\n')
  13. print('\nconnecting....')
  14. time.sleep(1)
  15. print('....')
  16. time.sleep(1)
  17. print('....')
  18. time.sleep(1)
  19. print('....')
  20. time.sleep(1)
  21. print('connection established\n')
  22. print('---------------------')
  23. print(' MAINFRAME - LOGIN ')
  24. print('---------------------')
  25. print('\nenter 3 digit access code..')
  26. number = random.randint(000, 999)
  27. while guessesTaken < 15:
  28. print()
  29. guess = input('user:> ')
  30. guess = int(guess)
  31. guessesTaken = guessesTaken + 1
  32. if guess < number:
  33. print('\nACCESS - DENIED -code to low')
  34. if guess > number:
  35. print('\nACCESS - DENIED -code to high')
  36. if guess == number:
  37. break
  38. if guess == number:
  39. guessesTaken = str(guessesTaken)
  40. print('\nverifying ....')
  41. time.sleep(1)
  42. print('\nauthenticating ....')
  43. time.sleep(1)
  44. print('....')
  45. time.sleep(1)
  46. print('....')
  47. time.sleep(1)
  48. print('\nACCESS - GRANTED')
  49. print('\nGAME OVER\n')
  50. exit(0)
  51. if guess != number:
  52. number = str(number)
  53. print('\n....')
  54. time.sleep(1)
  55. print('\n....')
  56. time.sleep(1)
  57. print('\nSYSTEM LOCKED -the code was ' + number)
  58. print()
  59. exit(0)

希望本文所述对大家的Python程序设计有所帮助。

人气教程排行