Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
code isnt working.
#1
so i was trying to work on my game but when i change the "inbattle" variable to true it doesnt put them in battle, how do i fix this?
heres my code for reference:
import random
import time
from threading import Timer

damage = 1
timeout = 0.25
level = 0
inbattle = "N/A"
gameplaying = True
health = 10
xp = 0
player = '{-}'
canheadattackchoice = 'undefined'
enemy_canhead = '[=]'
canheadattacks = ['headbash', 'defend', 'canslam']
canhead_health = 6
canhead_defense = 0
canhead_headbash_damage = 2
canhead_canslam_damage = 3
enemy_boulder = '+=\=+'
boulder_attacks = ['rolling_strike', 'defend']
boulder_attack_choice = 'undefined'
boulderhealth = 10
boulder_defense = 1
boulder_rs_damage = 4

enemychoice = [enemy_canhead, enemy_boulder]
enemyinfight = random.choice(enemychoice)
maplayer1 = [enemyinfight, '*', 'o']
maplayer2 = ['o', '*', 'o']
maplayer3 = ['o', player, 'o']
xpneededtolevel = level + 1 * 4 + 100
playerturn = False
canhead_defense_active = "no"
while True:
    if inbattle == True: # battle loop.
        if enemyinfight == "canhead":
            print(str(enemy_canhead) + "_______" + str(canhead_health) + " " + str(canhead_defense))
            print()
            print()
            print(str(player), "________" + str(health))
            playerturn = True
            action = input("what would you like to do?")
            if action == "attack":
                print(".....")
                print("-....")
                time.sleep(0.2)
                print("---..")
                time.sleep(0.2)
                print("attack now!")
                timeout = 0.7
                t = Timer(timeout, print, ["action command failed"])
                t.start()
                start_time = time.time()
                prompt = f"now!\n"
                answer = input(prompt)
                t.cancel()
                end_time = time.time()
                reaction_time = end_time - start_time
                if reaction_time > timeout:
                    print("action command missed")
                    damage = 1
                else:
                    damage += 1
                canhead_health -= (damage - canhead_defense)
                playerdamage = 1
                print("canhead is thinking")
                canheadattackchoice = random.choice(boulder_attacks)
                playerturn = False
                boulder_defense = 1
                print("canhead used " + canheadattackchoice)
                if canheadattackchoice == "defend":
                    canhead_defense += 1
                    canhead_defense_active = "yes"
                elif canheadattackchoice == "canslam":
                    timeout = 0.8
                    t = Timer(timeout, print, ["action command failed"])
                    t.start()
                    start_time = time.time()
                    prompt = f"now!\n"
                    answer = input(prompt)
                    t.cancel()
                    end_time = time.time()
                    reaction_time = end_time - start_time
                    if reaction_time > timeout:
                        print("action command missed")
                        health -= canhead_canslam_damage
                    else:
                        health = health
                elif canheadattackchoice == "headbash":
                    timeout = 0.5
                    t = Timer(timeout, print, ["action command failed"])
                    t.start()
                    start_time = time.time()
                    prompt = f"now!\n"
                    answer = input(prompt)
                    t.cancel()
                    end_time = time.time()
                    reaction_time = end_time - start_time
                    if reaction_time > timeout:
                        print("action command missed")
                        health -= canhead_headbash_damage
                    else:
                        health = health
                if canhead_health <= 0:
                    print("battle won!")
                    time.sleep(1)
                    inbattle = False
                    maplayer1[0] = 'o'
                else:
                    print("your turn monkey")
        if enemyinfight == "enemy_boulder":
            print(str(enemy_boulder) + "_______" + str(boulderhealth) + " " + str(boulder_defense))
            print()
            print()
            print(str(player), "________" + str(health))
            playerturn = True
            action = input("what would you like to do?")
            if action == "attack":
                print(".....")
                print("-....")
                time.sleep(0.2)
                print("---..")
                time.sleep(0.2)
                print("attack now!")
                timeout = 0.7
                t = Timer(timeout, print, ["action command failed"])
                t.start()
                start_time = time.time()
                prompt = f"now!\n"
                answer = input(prompt)
                t.cancel()
                end_time = time.time()
                reaction_time = end_time - start_time
                if reaction_time > timeout:
                    print("action command missed")
                    damage = 1
                else:
                    damage += 1
                boulderhealth -= (damage - boulder_defense)
                playerdamage = 1
                print("boulder is thinking")
                boulder_attack_choice = random.choice(boulder_attacks)
                playerturn = False
                print("boulder used " + boulder_attack_choice)
                if boulder_attack_choice == "defend":
                    boulder_defense += 1
                elif boulder_attack_choice == "rolling_strike":
                    timeout = 1.2
                    t = Timer(timeout, print, ["action command failed"])
                    t.start()
                    start_time = time.time()
                    prompt = f"now!\n"
                    answer = input(prompt)
                    t.cancel()
                    end_time = time.time()
                    reaction_time = end_time - start_time
                    if reaction_time > timeout:
                        print("action command missed")
                        health -= boulder_rs_damage
                    else:
                        health = health
                if boulderhealth <= 0:
                    print("battle won!")
                    time.sleep(1)
                    inbattle = False
                    maplayer1[0] = 'o'
                else:
                    print("your turn")
    else:
        pass

    xpneededtolevel = level + 1 * 4 + 100
    mapcolumn1 = [maplayer1[0], maplayer2[0], maplayer3[0]]
    mapcolumn2 = [maplayer1[1], maplayer2[1], maplayer3[1]]
    mapcolumn3 = [maplayer1[2], maplayer2[2], maplayer3[2]]
    enemysquare = [maplayer1[0], mapcolumn1[0], enemyinfight]

    movement = input("where would you like to move")
    if movement == "right" and player in maplayer3 and player in mapcolumn2:
        maplayer3 = ['o', '*', player]
    elif movement == "right" and player in maplayer3 and player in mapcolumn3:  # right movement
        print("you cannot move anymore to this direction!")
    elif movement == "right" and player in maplayer3 and mapcolumn1:  # right movement
        maplayer3 = ['o', player, 'o']
    elif movement == "left" and player in maplayer3 and player in mapcolumn2:  # left movement
        maplayer3 = [player, '*', 'o']
    elif movement == "left" and player in maplayer3 and player in mapcolumn1:  # left movement
        print("you cannot move anymore to this direction!")
    elif movement == "left" and player in maplayer3 and mapcolumn3:  # left movement
        maplayer3 = ['o', player, 'o']
    elif movement == "down" and player in maplayer3 and player in mapcolumn2:  # down movement
        print("you cannot move anymore in this direction!")
    elif movement == "down" and player in maplayer3 and player in mapcolumn3:  # down movement
        print("you cannot move anymore to this direction!")
    elif movement == "down" and player in maplayer3 and mapcolumn1:  # down movement
        print("you cannot move anymore in this direction!")
    elif movement == "up" and player in maplayer3 and player in mapcolumn2:  # up movement
        maplayer3 = ['o', '*', 'o']
        maplayer2 = ['o', player, 'o']
    elif movement == "up" and player in maplayer3 and player in mapcolumn3:  # up movement
        maplayer3 = ['o', '*', 'o']
        maplayer2 = ['o', '*', player]
    elif movement == "up" and player in maplayer3 and mapcolumn1:
        maplayer3 = ['o', '*', 'o']
        maplayer2 = [player, '*', 'o']
    elif movement == "down" and player in maplayer2 and player in mapcolumn1:
        maplayer2 = ['o', '*', 'o']
        maplayer3 = [player, '*', 'o']
    elif movement == "down" and player in maplayer2 and player in mapcolumn2:
        maplayer2 = ['o', '*', 'o']
        maplayer3 = ['o', player, 'o']
    elif movement == "down" and player in maplayer2 and player in mapcolumn3:
        maplayer2 = ['o', '*', 'o']
        maplayer3 = ['o', '*', player]
    elif movement == "right" and player in maplayer2 and player in mapcolumn3:
        maplayer2 = ['o', '*', player]
        print("you cannot move that way")
    elif movement == "right" and player in maplayer2 and player in mapcolumn2:
        maplayer2 = ['o', '*', player]
    elif movement == "right" and player in maplayer2 and player in mapcolumn1:
        maplayer2 = ['o', player, 'o']
    elif movement == "left" and player in maplayer2 and player in mapcolumn1:
        print("you cannot move that way!")
    elif movement == "left" and player in maplayer2 and player in mapcolumn2:
        maplayer2 = [player, '*', 'o']
    elif movement == "left" and player in maplayer2 and player in mapcolumn3:
        maplayer2 = ['o', player, 'o']
    elif movement == "up" and player in maplayer2 and player in mapcolumn3:
        maplayer1[2] = player
        maplayer2[2] = 'o'
    elif movement == "up" and player in maplayer2 and player in mapcolumn2:
        maplayer1[1] = player
        maplayer2[1] = '*'
    elif movement == "up" and player in maplayer2 and player in mapcolumn2:
        inbattle = True
    elif movement == "right" and player in maplayer1 and player in mapcolumn3:
        print("nuh uh cant move anymore that way")
    elif movement == "right" and player in maplayer1 and player in mapcolumn2:
        maplayer1[2] = player
        maplayer1[1] = '*'
    elif movement == "left" and player in maplayer1 and player in mapcolumn3:
        maplayer1[1] = player
        maplayer1[2] = 'o'
    elif movement == "left" and player in maplayer1 and player in mapcolumn2 and enemychoice in maplayer1:
        inbattle = True
        enemy = maplayer1[0]
    print(maplayer1)
    print(maplayer2)
    print(maplayer3)
Reply
#2
enemyinfight will be enemy_canhead '[=]' or enemy_boulder '+=\=+'. In the battle code your enemy selections are:
if enemyinfight == "canhead":
and
if enemyinfight == "enemy_boulder":
No matter which enemy is randomly selected, neither matches "canhead" or "enemy_boulder". So even if you enter battle, your code cannot find anyone for you to fight.
Calab likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Simple code not working properly tmv 2 476 Feb-28-2025, 09:27 PM
Last Post: deanhystad
  I'm new to Python - can someone help with this code as it is not working? lminc123 1 487 Feb-13-2025, 06:13 PM
Last Post: lminc123
  my code is not working erTurko 1 631 Nov-11-2024, 08:43 AM
Last Post: buran
  Excel isnt working properly after python function is started IchNar 2 1,219 May-01-2024, 06:43 PM
Last Post: IchNar
  New to Python - Not sure why this code isn't working - Any help appreciated TheGreatNinx 4 2,283 Jul-22-2023, 10:21 PM
Last Post: Pedroski55
  code not working when executed from flask app ThomasDC 1 3,059 Jul-18-2023, 07:16 AM
Last Post: ThomasDC
  I am new to python and Could someone please explain how this below code is working? kartheekdas 2 1,759 Dec-19-2022, 05:24 PM
Last Post: kartheekdas
Exclamation My code is not working as I expected and I don't know why! Marinho 4 2,161 Oct-13-2022, 08:09 PM
Last Post: deanhystad
  My Code isn't working... End3r 4 3,274 Mar-21-2022, 10:12 AM
Last Post: End3r
  I don't undestand why my code isn't working. RuyCab 2 2,592 Jun-17-2021, 03:06 PM
Last Post: RuyCab

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020