Guest User

V's game/Python

a guest
Dec 6th, 2022
481
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.36 KB | Source Code | 0 0
  1. import os
  2. import time
  3.  
  4. #RESIZE TERMINAL
  5. #os.system('mode con: cols=100 lines=20')
  6.  
  7.  
  8. #clear screen function in console
  9. def cls():
  10.     os.system('cls' if os.name=='nt' else 'clear')
  11.    
  12. #Variables==============================================
  13. p_command = ""
  14. health = 100 #Hitpoints
  15. damage = 10 #player damage
  16. msg = "" #text box var
  17. ePosition = 5 #enemy position
  18. pPosition = 1 #player position
  19. level = ["[]","___","___","___","___","___","[]"] #5 movable posit, 2 walls
  20. max_forward_bounds = len(level)-2 #sets the player bounds of the level
  21. #=======================================================
  22.  
  23. #FUNCTIONS===================================================================
  24. #DEATH CHECK============================================
  25. def death(): #If health reaches 0 or below this kills the game
  26.     global health
  27.     if health <= 0:
  28.         cls()
  29.         input("DEATH.")
  30.         raise SystemExit(0)
  31. #=======================================================    
  32.  
  33. #Update the graphical portion of the screen=============
  34. def display():
  35.     cls()#clears the previous screen
  36.     global health,ePosition,pPosition,level#load global variables
  37.     print('HP:' + str(health)+'|'+ msg)#prints stats
  38.     print("ATK:"+str(damage))
  39.     if pPosition != ePosition:
  40.         level[pPosition] = '_V_'#places player graphic based off of pPosition
  41.     else:
  42.         level[pPosition] = '_X_' #battle graphic if pPosition == ePosition
  43.     print(''.join(level))#prints the level list without brackets
  44. #=======================================================
  45.            
  46. #User Input=============================================
  47. def command(): #accept user input, as str, turns lowercase
  48.     global p_command
  49.     p_command = str(input("Enter command:"))
  50.     p_command = p_command.lower()
  51. #=======================================================
  52.  
  53. #Forward command========================================
  54. def go_forward():
  55.     global p_command, pPosition, max_forward_bounds,level,ePosition,msg
  56.     if p_command == "go":
  57.         if pPosition < max_forward_bounds : #Limit the max allowable bounds of level
  58.             level[pPosition] = '___'#replaces player graphic with empty graphic
  59.             pPosition += 1 #moves the player by one space
  60.             msg = "-->" #points right just for flair
  61.         else:
  62.             msg = "Cannot proceed."#If you are at the edge of the level
  63. #=======================================================
  64.  
  65. #HEAL command===========================================
  66. def heal_player():
  67.     global health, p_command, msg
  68.     if p_command == 'heal':
  69.         if health <= 80:
  70.             health += 20
  71.             msg = "Healed 20HP"
  72.             display()
  73.             time.sleep(1)
  74.            
  75.         elif health >80 and health < 100:
  76.             remaining_hp = 100 - health
  77.             health = 100
  78.             msg = "Healed " + str(remaining_hp) + "HP"
  79.             display()
  80.             time.sleep(1)
  81.         elif health == 100:
  82.             msg = "HP already full"
  83.             display()
  84.             time.sleep(1)
  85.         msg = ''    
  86. #=======================================================
  87.            
  88. #BACKWARDS COMMAND======================================
  89. def go_backwards():
  90.     global p_command, pPosition,level,msg
  91.     if p_command == 'back':
  92.         if pPosition > 1: #prevents going out of starting bounds
  93.             level[pPosition] = '___'#replaces old position with tile graphic
  94.             pPosition -= 1 #moves the player back one space
  95.             msg = "<--" #Left arrow
  96.         else:
  97.             msg = "Cannot go back."
  98. #=======================================================
  99.            
  100.    
  101.    
  102. #ENEMY BATTLE===========================================
  103. def enemy():
  104.     global health, damage, msg, ePosition,pPosition,level
  105.     if pPosition == ePosition:
  106.         enemy_health = 100
  107.         enemy_attack = 5
  108.         msg = "Enemy:Troll:" + "HP:"+str(enemy_health)
  109.         while True:
  110.             death()
  111.             msg = "Enemy:Troll:" + "HP:"+str(enemy_health)
  112.             display()
  113.             command()
  114.             heal_player()
  115.             if p_command == 'attack':
  116.                 enemy_health = enemy_health - damage
  117.                 msg = "Attacked enemy for "+str(damage)
  118.                 display()
  119.                 time.sleep(1)
  120.             if enemy_health > 0:
  121.                 health = health - enemy_attack
  122.                 msg = "Attacked by enemy for "+str(enemy_attack)+"HP"
  123.                 display()
  124.                 time.sleep(1)
  125.             else:
  126.                 msg = "Killed enemy."
  127.                 damage = damage + 10
  128.                 ePosition = -1
  129.                 display()
  130.                 time.sleep(3)
  131.                 break
  132. #=======================================================
  133.        
  134.  
  135.        
  136.        
  137. #FUNCTIONS END===============================================================
  138.        
  139.        
  140.        
  141. #Main code loop, brings up display func, needs player input to run
  142. while True:
  143.     #Death check========================================
  144.     death()
  145.     #===================================================
  146.    
  147.     #Update Display=====================================
  148.     display()
  149.     #===================================================
  150.    
  151.     #Enemy encounter====================================
  152.     enemy()
  153.    
  154.     #User input=========================================
  155.     command()
  156.     #===================================================
  157.    
  158.     #Forward command. Prevents going out of bounds======
  159.     go_forward()
  160.     #===================================================
  161.    
  162.     #Restore hitpoints==================================
  163.     heal_player()
  164.     #===================================================
  165.    
  166.     #Backwards command==================================
  167.     go_backwards()
  168.            
  169.     #Game Exit Command==================================
  170.     if p_command == 'exit':
  171.         input("Game Over")
  172.         raise SystemExit(0)#exits the program
  173.     #===================================================        
  174.    
  175.     #Self Wound command=================================
  176.     if p_command == 'hurt':
  177.         health = health - damage
  178.         msg = "You lost "+ str(damage) + "HP"
  179.         damage = damage + 1
  180.         display()
  181.         time.sleep(1)
  182.         msg = ""
  183.     #===================================================
  184.        
  185.        
  186.  
  187.  
Advertisement
Add Comment
Please, Sign In to add comment