Skip to main content

L 4

This lesson i am working on functions in python.

below is a simple game with the two functions, walk(), and attack(). walking will trigger a 1/2 chance to spawn an enemy.

import random

hp = 5
atk = 1
enemy = False
enemyhp = 3
enemyatk = 1
encounter = random.choice([0,1])

def attack():
    global enemy
    global hp
    global atk
    global enemyhp
    global enemyatk
    if enemyhp <= 1:
        enemy == False
        print("You have destroyed the enemy.")
    else:
        if enemy == True:
            hp -= enemyatk
            enemyhp -= atk
            print("your health is {} and enemy health is {}".format(hp,enemyhp))
        else:
            print("you attack but there is nothing there...")
   
   
def walk():
    global enemy
    if enemy == False:
        global encounter
        encounter = random.choice([0,1])
        if encounter == 1:
            enemy = True
            print("You see an enemy!")
        else:
            print("You continue on your way.")
    elif enemy == True:
        print("You cant go now, you are under attack.")

Comments