Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import time
- #RESIZE TERMINAL
- #os.system('mode con: cols=100 lines=20')
- #clear screen function in console
- def cls():
- os.system('cls' if os.name=='nt' else 'clear')
- #Variables==============================================
- p_command = ""
- health = 100 #Hitpoints
- damage = 10 #player damage
- msg = "" #text box var
- ePosition = 5 #enemy position
- pPosition = 1 #player position
- level = ["[]","___","___","___","___","___","[]"] #5 movable posit, 2 walls
- max_forward_bounds = len(level)-2 #sets the player bounds of the level
- #=======================================================
- #FUNCTIONS===================================================================
- #DEATH CHECK============================================
- def death(): #If health reaches 0 or below this kills the game
- global health
- if health <= 0:
- cls()
- input("DEATH.")
- raise SystemExit(0)
- #=======================================================
- #Update the graphical portion of the screen=============
- def display():
- cls()#clears the previous screen
- global health,ePosition,pPosition,level#load global variables
- print('HP:' + str(health)+'|'+ msg)#prints stats
- print("ATK:"+str(damage))
- if pPosition != ePosition:
- level[pPosition] = '_V_'#places player graphic based off of pPosition
- else:
- level[pPosition] = '_X_' #battle graphic if pPosition == ePosition
- print(''.join(level))#prints the level list without brackets
- #=======================================================
- #User Input=============================================
- def command(): #accept user input, as str, turns lowercase
- global p_command
- p_command = str(input("Enter command:"))
- p_command = p_command.lower()
- #=======================================================
- #Forward command========================================
- def go_forward():
- global p_command, pPosition, max_forward_bounds,level,ePosition,msg
- if p_command == "go":
- if pPosition < max_forward_bounds : #Limit the max allowable bounds of level
- level[pPosition] = '___'#replaces player graphic with empty graphic
- pPosition += 1 #moves the player by one space
- msg = "-->" #points right just for flair
- else:
- msg = "Cannot proceed."#If you are at the edge of the level
- #=======================================================
- #HEAL command===========================================
- def heal_player():
- global health, p_command, msg
- if p_command == 'heal':
- if health <= 80:
- health += 20
- msg = "Healed 20HP"
- display()
- time.sleep(1)
- elif health >80 and health < 100:
- remaining_hp = 100 - health
- health = 100
- msg = "Healed " + str(remaining_hp) + "HP"
- display()
- time.sleep(1)
- elif health == 100:
- msg = "HP already full"
- display()
- time.sleep(1)
- msg = ''
- #=======================================================
- #BACKWARDS COMMAND======================================
- def go_backwards():
- global p_command, pPosition,level,msg
- if p_command == 'back':
- if pPosition > 1: #prevents going out of starting bounds
- level[pPosition] = '___'#replaces old position with tile graphic
- pPosition -= 1 #moves the player back one space
- msg = "<--" #Left arrow
- else:
- msg = "Cannot go back."
- #=======================================================
- #ENEMY BATTLE===========================================
- def enemy():
- global health, damage, msg, ePosition,pPosition,level
- if pPosition == ePosition:
- enemy_health = 100
- enemy_attack = 5
- msg = "Enemy:Troll:" + "HP:"+str(enemy_health)
- while True:
- death()
- msg = "Enemy:Troll:" + "HP:"+str(enemy_health)
- display()
- command()
- heal_player()
- if p_command == 'attack':
- enemy_health = enemy_health - damage
- msg = "Attacked enemy for "+str(damage)
- display()
- time.sleep(1)
- if enemy_health > 0:
- health = health - enemy_attack
- msg = "Attacked by enemy for "+str(enemy_attack)+"HP"
- display()
- time.sleep(1)
- else:
- msg = "Killed enemy."
- damage = damage + 10
- ePosition = -1
- display()
- time.sleep(3)
- break
- #=======================================================
- #FUNCTIONS END===============================================================
- #Main code loop, brings up display func, needs player input to run
- while True:
- #Death check========================================
- death()
- #===================================================
- #Update Display=====================================
- display()
- #===================================================
- #Enemy encounter====================================
- enemy()
- #User input=========================================
- command()
- #===================================================
- #Forward command. Prevents going out of bounds======
- go_forward()
- #===================================================
- #Restore hitpoints==================================
- heal_player()
- #===================================================
- #Backwards command==================================
- go_backwards()
- #Game Exit Command==================================
- if p_command == 'exit':
- input("Game Over")
- raise SystemExit(0)#exits the program
- #===================================================
- #Self Wound command=================================
- if p_command == 'hurt':
- health = health - damage
- msg = "You lost "+ str(damage) + "HP"
- damage = damage + 1
- display()
- time.sleep(1)
- msg = ""
- #===================================================
Advertisement
Add Comment
Please, Sign In to add comment