HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonModerate

Python Adventure game

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
gameadventurepython

Problem

I'm a beginner Python coder and this is the first program that I have ever programmed.
Please suggest improvements/bugs. Here's the link to the code.

``
global hp
hp = 20
global dice
import random
dice = random.randint(3,5)
global beardamage
import random
beardamage = random.randint(1,9)
global hunger
hunger = 5
global spiderhp
spiderhp = 5
global bearhp
bearhp = 10
global potion
potion = 0
global xp
xp = 0
# |Data storage for part 1|
#_______________________________________________________________________________
#
x = ("attack")
passwordcode = ("5665598")
road =("left")
key = ("key")
choicekey = ("yes")
hutchoice = ("gotoit")
menucheat = ("cheat menu")
menureadme = ("readme")
menustart = ("start game")
death = 1
menuhelp = ("help")
potionadd = 1
examinationyes = ("examine")
SmallHealing = random.randint(8,15)
cheat01 = ("cheat_01")
cheat02 = ("shutdown")
bearxp = 100
spiderxp = 350
bow = random.randint(5,10)
passwordcheat = ("password")
# |End of data storage for adventure part 1.|
#_______________________________________________________________________________
text = ("Loading files....")
import time
import sys
from random import randrange
for c in text:
sys.stdout.write(c)
sys.stdout.flush()
seconds = "0." + str(randrange(1, 4, 1))
seconds = float(0.5)
time.sleep(0.01)
text = ("-1%||||||||||||||||100%-")
print("")
import time
import sys
from random import randrange
for c in text:
sys.stdout.write(c)
sys.stdout.flush()
seconds = "0." + str(randrange(1, 4, 1))
seconds = float(0.01)
time.sleep(0.01)
print("")
import time
time.sleep(1)
import os
import sys
os.system("CLS")
print("")
print("")
print(" ")
print(" _ _ _ _ _ ")
print(" / \ __| |_ _____ _ __ | |_ _ _ _ __ ___ _ __ _ _| |_| |__ ___ _ __ ")
print(" / _ \ / _

Solution

global hp
hp = 20


Global only has an effect inside a function. It does absolutely nothing here. At any rate you should avoid using global.

global dice
import random
dice = random.randint(3,5)
global beardamage
import random
beardamage = random.randint(1,9)


You only need to import things once. Here you have imported random twice, the second time doesn't do anything. Instead, just import everything that you need at the beginning.

x = ("attack")
passwordcode = ("5665598")
road =("left")
key = ("key")


The parentheses do nothing here. Also, if these are global constants, they should be named in ALL_CAPS.

text = ("Loading files....")
import time
import sys
from random import randrange
for c in text:
    sys.stdout.write(c)
    sys.stdout.flush()
    seconds = "0." + str(randrange(1, 4, 1))
    seconds = float(0.5)
    time.sleep(0.01)
text = ("-1%||||||||||||||||100%-")
print("")
import time
import sys
from random import randrange
for c in text:
    sys.stdout.write(c)
    sys.stdout.flush()
    seconds = "0." + str(randrange(1, 4, 1))
    seconds = float(0.01)
    time.sleep(0.01)


See you've done the exact same thing twice. This means you should write a function like this:

def slow_print(text):
    for character in text:
        sys.stdout.write(character)
        time.sleep(0.01)

slow_print("Loading Files...")
slow_print("-1%||||||||||||||||100%-")


That way you only need one copy of the actual logic.

def clearscreen():
    if os.name == "posix":
       # Unix/Linux/MacOS/BSD/etc
       os.system('clear')
    elif os.name in ("nt", "dos", "ce"):
       # DOS/Windows
       os.system('CLS')


You define this, but don't actually use it above.

Code Snippets

global hp
hp = 20
global dice
import random
dice = random.randint(3,5)
global beardamage
import random
beardamage = random.randint(1,9)
x = ("attack")
passwordcode = ("5665598")
road =("left")
key = ("key")
text = ("Loading files....")
import time
import sys
from random import randrange
for c in text:
    sys.stdout.write(c)
    sys.stdout.flush()
    seconds = "0." + str(randrange(1, 4, 1))
    seconds = float(0.5)
    time.sleep(0.01)
text = ("-1%||||||||||||||||100%-")
print("")
import time
import sys
from random import randrange
for c in text:
    sys.stdout.write(c)
    sys.stdout.flush()
    seconds = "0." + str(randrange(1, 4, 1))
    seconds = float(0.01)
    time.sleep(0.01)
def slow_print(text):
    for character in text:
        sys.stdout.write(character)
        time.sleep(0.01)

slow_print("Loading Files...")
slow_print("-1%||||||||||||||||100%-")

Context

StackExchange Code Review Q#20601, answer score: 12

Revisions (0)

No revisions yet.